RequireClient returns a gin middleware which requires a client token or basic authentication header to be supplied with the request.
()
| 55 | // RequireClient returns a gin middleware which requires a client token or basic authentication header to be supplied |
| 56 | // with the request. |
| 57 | func (a *Auth) RequireClient() gin.HandlerFunc { |
| 58 | return a.requireToken(func(tokenID string, user *model.User) (bool, bool, uint, error) { |
| 59 | if user != nil { |
| 60 | return true, true, user.ID, nil |
| 61 | } |
| 62 | if client, err := a.DB.GetClientByToken(tokenID); err != nil { |
| 63 | return false, false, 0, err |
| 64 | } else if client != nil { |
| 65 | now := time.Now() |
| 66 | if client.LastUsed == nil || client.LastUsed.Add(5*time.Minute).Before(now) { |
| 67 | if err := a.DB.UpdateClientTokensLastUsed([]string{tokenID}, &now); err != nil { |
| 68 | return false, false, 0, err |
| 69 | } |
| 70 | } |
| 71 | return true, true, client.UserID, nil |
| 72 | } |
| 73 | return false, false, 0, nil |
| 74 | }) |
| 75 | } |
| 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 { |
no test coverage detected