MCPcopy Create free account
hub / github.com/authorizerdev/authorizer / ValidateAccessToken

Method ValidateAccessToken

internal/token/auth_token.go:282–331  ·  view source on GitHub ↗

Function to validate access token for authorizer apis (profile, update_profile)

(gc *gin.Context, accessToken string)

Source from the content-addressed store, hash-verified

280
281// Function to validate access token for authorizer apis (profile, update_profile)
282func (p *provider) ValidateAccessToken(gc *gin.Context, accessToken string) (map[string]interface{}, error) {
283 res := make(map[string]interface{})
284
285 if accessToken == "" {
286 return res, fmt.Errorf(`unauthorized`)
287 }
288
289 res, err := p.ParseJWTToken(accessToken)
290 if err != nil {
291 return res, err
292 }
293
294 userID, ok := res["sub"].(string)
295 if !ok || userID == "" {
296 return res, fmt.Errorf(`unauthorized: missing sub claim`)
297 }
298 nonce, _ := res["nonce"].(string)
299
300 loginMethod, _ := res["login_method"].(string)
301 sessionKey := userID
302 if loginMethod != "" {
303 sessionKey = loginMethod + ":" + userID
304 }
305
306 token, err := p.dependencies.MemoryStoreProvider.GetUserSession(sessionKey, constants.TokenTypeAccessToken+"_"+nonce)
307 if nonce == "" || err != nil {
308 p.dependencies.Log.Debug().Err(err).Msgf("invalid access token: %v, key: %s", err, sessionKey+":"+constants.TokenTypeAccessToken+"_"+nonce)
309 return res, fmt.Errorf(`unauthorized`)
310 }
311
312 if subtle.ConstantTimeCompare([]byte(token), []byte(accessToken)) != 1 {
313 p.dependencies.Log.Debug().Msgf("invalid access token: %s, key: %s", err, sessionKey+":"+constants.TokenTypeAccessToken+"_"+nonce)
314 return res, fmt.Errorf(`unauthorized`)
315 }
316
317 hostname := parsers.GetHost(gc)
318 if ok, err := p.ValidateJWTClaims(res, &AuthTokenConfig{
319 HostName: hostname,
320 Nonce: nonce,
321 User: &schemas.User{ID: userID},
322 }); !ok || err != nil {
323 return res, err
324 }
325
326 if res["token_type"] != constants.TokenTypeAccessToken {
327 return res, fmt.Errorf(`unauthorized: invalid token type`)
328 }
329
330 return res, nil
331}
332
333// Function to validate refreshToken
334func (p *provider) ValidateRefreshToken(gc *gin.Context, refreshToken string) (map[string]interface{}, error) {

Callers 1

Calls 4

ParseJWTTokenMethod · 0.95
ValidateJWTClaimsMethod · 0.95
GetHostFunction · 0.92
GetUserSessionMethod · 0.65

Tested by

no test coverage detected