RequireApplicationToken returns a gin middleware which requires an application token to be supplied with the request.
()
| 76 | |
| 77 | // RequireApplicationToken returns a gin middleware which requires an application token to be supplied with the request. |
| 78 | func (a *Auth) RequireApplicationToken() gin.HandlerFunc { |
| 79 | return a.requireToken(func(tokenID string, user *model.User) (bool, bool, uint, error) { |
| 80 | if user != nil { |
| 81 | return true, false, 0, nil |
| 82 | } |
| 83 | if app, err := a.DB.GetApplicationByToken(tokenID); err != nil { |
| 84 | return false, false, 0, err |
| 85 | } else if app != nil { |
| 86 | now := time.Now() |
| 87 | if app.LastUsed == nil || app.LastUsed.Add(5*time.Minute).Before(now) { |
| 88 | if err := a.DB.UpdateApplicationTokenLastUsed(tokenID, &now); err != nil { |
| 89 | return false, false, 0, err |
| 90 | } |
| 91 | } |
| 92 | return true, true, app.UserID, nil |
| 93 | } |
| 94 | return false, false, 0, nil |
| 95 | }) |
| 96 | } |
| 97 | |
| 98 | func (a *Auth) tokenFromQueryOrHeader(ctx *gin.Context) string { |
| 99 | if token := a.tokenFromQuery(ctx); token != "" { |