TokenHandler to handle /oauth/token requests grant type required
()
| 35 | // TokenHandler to handle /oauth/token requests |
| 36 | // grant type required |
| 37 | func (h *httpProvider) TokenHandler() gin.HandlerFunc { |
| 38 | log := h.Log.With().Str("func", "TokenHandler").Logger() |
| 39 | return func(gc *gin.Context) { |
| 40 | // RFC 6749 §5.1: token endpoint responses must not be cached. |
| 41 | gc.Writer.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate, private") |
| 42 | gc.Writer.Header().Set("Pragma", "no-cache") |
| 43 | |
| 44 | var reqBody RequestBody |
| 45 | if err := gc.Bind(&reqBody); err != nil { |
| 46 | log.Debug().Err(err).Msg("failed to bind request body") |
| 47 | gc.JSON(http.StatusBadRequest, gin.H{ |
| 48 | "error": "invalid_request", |
| 49 | "error_description": "Failed to parse request body", |
| 50 | }) |
| 51 | return |
| 52 | } |
| 53 | |
| 54 | codeVerifier := strings.TrimSpace(reqBody.CodeVerifier) |
| 55 | code := strings.TrimSpace(reqBody.Code) |
| 56 | clientID := strings.TrimSpace(reqBody.ClientID) |
| 57 | grantType := strings.TrimSpace(reqBody.GrantType) |
| 58 | refreshToken := strings.TrimSpace(reqBody.RefreshToken) |
| 59 | clientSecret := strings.TrimSpace(reqBody.ClientSecret) |
| 60 | |
| 61 | if grantType == "" { |
| 62 | grantType = "authorization_code" |
| 63 | } |
| 64 | |
| 65 | isRefreshTokenGrant := grantType == "refresh_token" |
| 66 | isAuthorizationCodeGrant := grantType == "authorization_code" |
| 67 | |
| 68 | if !isRefreshTokenGrant && !isAuthorizationCodeGrant { |
| 69 | log.Debug().Str("grant_type", grantType).Msg("Invalid grant type") |
| 70 | gc.JSON(http.StatusBadRequest, gin.H{ |
| 71 | "error": "unsupported_grant_type", |
| 72 | "error_description": "grant_type is not supported", |
| 73 | }) |
| 74 | return |
| 75 | } |
| 76 | |
| 77 | // check if clientID & clientSecret are present as part of |
| 78 | // authorization header with basic auth |
| 79 | if clientID == "" && clientSecret == "" { |
| 80 | clientID, clientSecret, _ = gc.Request.BasicAuth() |
| 81 | } |
| 82 | |
| 83 | if clientID == "" { |
| 84 | log.Debug().Msg("Client ID is missing") |
| 85 | gc.JSON(http.StatusBadRequest, gin.H{ |
| 86 | "error": "invalid_request", |
| 87 | "error_description": "The client_id parameter is required", |
| 88 | }) |
| 89 | return |
| 90 | } |
| 91 | |
| 92 | if h.Config.ClientID != clientID { |
| 93 | log.Debug().Msg("Client ID is invalid") |
| 94 | metrics.RecordSecurityEvent("invalid_client", "token_endpoint") |
nothing calls this directly
no test coverage detected