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 headerAuthToken , ok := httpauth .ParseAuthorizationHeaderBearerToken (auHead ); ok {
110+ accessTokenScope , _ = auth_service .GetOAuthAccessTokenScopeAndUserID (ctx , headerAuthToken )
114111 }
115112 }
116113
@@ -127,18 +124,15 @@ func InfoOAuth(ctx *context.Context) {
127124 ctx .JSON (http .StatusOK , response )
128125}
129126
130- func parseBasicAuth (ctx * context.Context ) (username , password string , err error ) {
127+ func parseBasicAuth (ctx * context.Context ) (username , password string , ok bool ) {
131128 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" )
129+ return httpauth .ParseAuthorizationHeaderBasic (authHeader )
136130}
137131
138132// IntrospectOAuth introspects an oauth token
139133func IntrospectOAuth (ctx * context.Context ) {
140134 clientIDValid := false
141- if clientID , clientSecret , err := parseBasicAuth (ctx ); err == nil {
135+ if clientID , clientSecret , ok := parseBasicAuth (ctx ); ok {
142136 app , err := auth .GetOAuth2ApplicationByClientID (ctx , clientID )
143137 if err != nil && ! auth .IsErrOauthClientIDInvalid (err ) {
144138 // this is likely a database error; log it and respond without details
@@ -465,10 +459,9 @@ func AccessTokenOAuth(ctx *context.Context) {
465459 form := * web .GetForm (ctx ).(* forms.AccessTokenForm )
466460 // 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
467461 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 {
462+ if authHeader := ctx .Req .Header .Get ("Authorization" ); authHeader != "" {
463+ clientID , clientSecret , ok := httpauth .ParseAuthorizationHeaderBasic (authHeader )
464+ if ! ok {
472465 handleAccessTokenError (ctx , oauth2_provider.AccessTokenError {
473466 ErrorCode : oauth2_provider .AccessTokenErrorCodeInvalidRequest ,
474467 ErrorDescription : "cannot parse basic auth header" ,
0 commit comments