示例#1
0
func (c *apiController) Preprocess(p parameters.RequestParameter) bool {
	// Validate Request Method
	if !c.IsAllowedMethod() {
		c.MethodNotAllowed(map[string]interface{}{
			"status":  "NG",
			"message": "Method Not Allowed",
		})
		return false
	}

	// Parse Requesr Header
	if err := c.ParseRequestHeader(p); err != nil {
		c.BadRequest(map[string]interface{}{
			"status":  "NG",
			"func":    "ParseRequestHeader",
			"message": err.Error(),
		})
		return false
	}

	// Parse Request Parameter
	if err := c.ParseRequestParameter(p); err != nil {
		c.BadRequest(map[string]interface{}{
			"status":  "NG",
			"func":    "ParseRequestParameter",
			"message": err,
		})
		return false
	}

	// Validate Request Parameter
	if err := p.Validate(); err != nil {
		c.BadRequest(map[string]interface{}{
			"status":  "NG",
			"func":    "Validate",
			"message": err,
		})
		return false
	}

	// TODO:

	return true
}
示例#2
0
// ParseRequestHeader check if need access token.
func (c RootController) ParseRequestHeader(p parameters.RequestParameter) error {
	req := c.GetContext().Request()

	// API
	apiToken := req.Header("Token")
	if len(apiToken) <= 0 {
		return fmt.Errorf("Not found the ApiToken.")
	}
	if apiToken != config.GetAPI().Token {
		return fmt.Errorf("Not matched the ApiToken.")
	}

	// AccessToken
	if !p.NeedAccessToken() {
		return nil
	}
	auth := req.Header("Authorization")
	if len(auth) <= 0 || !strings.HasPrefix(auth, "Bearer ") {
		return fmt.Errorf("Not found the Authorization.")
	}
	p.SetAccessToken(strings.TrimPrefix(auth, "Bearer "))
	return nil
}