Ejemplo n.º 1
0
func (d *ApiTokenDriver) ApiUser(tokenString string) (wcg.User, error) {
	ent := &ApiToken{}
	key := d.NewKey(tokenString, 0, nil)
	err := d.Get(key, ent)
	if err == nil {
		// Found and Update LastAccess
		ent.LastAccess = time.Now()
		_, err = d.Put(key, ent)
	} else {
		// Not Found.
		if !util.IsOnGAE() && !util.IsProduction() && tokenString != "" {
			// local dev env allow any token string.
			err = nil
			ent = &ApiToken{
				Token:       tokenString,
				Description: "Dummy API Token",
				CreatedAt:   time.Now(),
				AlertOn:     time.Duration(0),
				LastAccess:  time.Now(),
			}
		}
	}
	if err != nil {
		return nil, err
	}
	// Update lastAccess
	return &ApiUser{
		token:     ent,
		lastLogin: ent.LastAccess,
	}, nil
}
Ejemplo n.º 2
0
// HTTP 500 Internal Error
func (api *ApiHelper) InternalError(res *wcg.Response, req *wcg.Request, e error) {
	var msg string
	if util.IsProduction() {
		msg = "Sorry for inconvinience. An internal error occurred. Please try again later."
	} else {
		msg = fmt.Sprintf("An error occurred: %v", e)
	}
	req.Logger.Error("Serve 500: %v", e)
	res.WriteJsonWithStatus(500, nil, map[string]interface{}{
		"error":   "internal_server_error",
		"message": msg,
	})
}