44package auth
55
66import (
7- "errors"
87 "fmt"
98 "html"
109 "html/template"
1110 "net/http"
1211 "net/url"
1312 "strconv"
14- "strings"
1513
1614 "code.gitea.io/gitea/models/auth"
1715 user_model "code.gitea.io/gitea/models/user"
18- "code.gitea.io/gitea/modules/base "
16+ "code.gitea.io/gitea/modules/auth/httpauth "
1917 "code.gitea.io/gitea/modules/json"
2018 "code.gitea.io/gitea/modules/log"
2119 "code.gitea.io/gitea/modules/setting"
@@ -108,9 +106,8 @@ func InfoOAuth(ctx *context.Context) {
108106
109107 var accessTokenScope auth.AccessTokenScope
110108 if auHead := ctx .Req .Header .Get ("Authorization" ); auHead != "" {
111- auths := strings .Fields (auHead )
112- if len (auths ) == 2 && (auths [0 ] == "token" || strings .ToLower (auths [0 ]) == "bearer" ) {
113- accessTokenScope , _ = auth_service .GetOAuthAccessTokenScopeAndUserID (ctx , auths [1 ])
109+ if parsed , ok := httpauth .ParseAuthorizationHeader (auHead ); ok && parsed .BearerToken != nil {
110+ accessTokenScope , _ = auth_service .GetOAuthAccessTokenScopeAndUserID (ctx , parsed .BearerToken .Token )
114111 }
115112 }
116113
@@ -127,18 +124,12 @@ func InfoOAuth(ctx *context.Context) {
127124 ctx .JSON (http .StatusOK , response )
128125}
129126
130- func parseBasicAuth (ctx * context.Context ) (username , password string , err error ) {
131- authHeader := ctx .Req .Header .Get ("Authorization" )
132- if authType , authData , ok := strings .Cut (authHeader , " " ); ok && strings .EqualFold (authType , "Basic" ) {
133- return base .BasicAuthDecode (authData )
134- }
135- return "" , "" , errors .New ("invalid basic authentication" )
136- }
137-
138127// IntrospectOAuth introspects an oauth token
139128func IntrospectOAuth (ctx * context.Context ) {
140129 clientIDValid := false
141- if clientID , clientSecret , err := parseBasicAuth (ctx ); err == nil {
130+ authHeader := ctx .Req .Header .Get ("Authorization" )
131+ if parsed , ok := httpauth .ParseAuthorizationHeader (authHeader ); ok && parsed .BasicAuth != nil {
132+ clientID , clientSecret := parsed .BasicAuth .Username , parsed .BasicAuth .Password
142133 app , err := auth .GetOAuth2ApplicationByClientID (ctx , clientID )
143134 if err != nil && ! auth .IsErrOauthClientIDInvalid (err ) {
144135 // this is likely a database error; log it and respond without details
@@ -465,16 +456,16 @@ func AccessTokenOAuth(ctx *context.Context) {
465456 form := * web .GetForm (ctx ).(* forms.AccessTokenForm )
466457 // if there is no ClientID or ClientSecret in the request body, fill these fields by the Authorization header and ensure the provided field matches the Authorization header
467458 if form .ClientID == "" || form .ClientSecret == "" {
468- authHeader := ctx .Req .Header .Get ("Authorization" )
469- if authType , authData , ok := strings .Cut (authHeader , " " ); ok && strings .EqualFold (authType , "Basic" ) {
470- clientID , clientSecret , err := base .BasicAuthDecode (authData )
471- if err != nil {
459+ if authHeader := ctx .Req .Header .Get ("Authorization" ); authHeader != "" {
460+ parsed , ok := httpauth .ParseAuthorizationHeader (authHeader )
461+ if ! ok || parsed .BasicAuth == nil {
472462 handleAccessTokenError (ctx , oauth2_provider.AccessTokenError {
473463 ErrorCode : oauth2_provider .AccessTokenErrorCodeInvalidRequest ,
474464 ErrorDescription : "cannot parse basic auth header" ,
475465 })
476466 return
477467 }
468+ clientID , clientSecret := parsed .BasicAuth .Username , parsed .BasicAuth .Password
478469 // validate that any fields present in the form match the Basic auth header
479470 if form .ClientID != "" && form .ClientID != clientID {
480471 handleAccessTokenError (ctx , oauth2_provider.AccessTokenError {
0 commit comments