4
4
package auth
5
5
6
6
import (
7
- "errors"
8
7
"fmt"
9
8
"html"
10
9
"html/template"
11
10
"net/http"
12
11
"net/url"
13
12
"strconv"
14
- "strings"
15
13
16
14
"code.gitea.io/gitea/models/auth"
17
15
user_model "code.gitea.io/gitea/models/user"
18
- "code.gitea.io/gitea/modules/base "
16
+ "code.gitea.io/gitea/modules/auth/httpauth "
19
17
"code.gitea.io/gitea/modules/json"
20
18
"code.gitea.io/gitea/modules/log"
21
19
"code.gitea.io/gitea/modules/setting"
@@ -108,9 +106,8 @@ func InfoOAuth(ctx *context.Context) {
108
106
109
107
var accessTokenScope auth.AccessTokenScope
110
108
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 )
114
111
}
115
112
}
116
113
@@ -127,18 +124,11 @@ func InfoOAuth(ctx *context.Context) {
127
124
ctx .JSON (http .StatusOK , response )
128
125
}
129
126
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
-
138
127
// IntrospectOAuth introspects an oauth token
139
128
func IntrospectOAuth (ctx * context.Context ) {
140
129
clientIDValid := false
141
- if clientID , clientSecret , err := parseBasicAuth (ctx ); err == nil {
130
+ authHeader := ctx .Req .Header .Get ("Authorization" )
131
+ if clientID , clientSecret , ok := httpauth .ParseAuthorizationHeaderBasic (authHeader ); ok {
142
132
app , err := auth .GetOAuth2ApplicationByClientID (ctx , clientID )
143
133
if err != nil && ! auth .IsErrOauthClientIDInvalid (err ) {
144
134
// this is likely a database error; log it and respond without details
@@ -465,10 +455,9 @@ func AccessTokenOAuth(ctx *context.Context) {
465
455
form := * web .GetForm (ctx ).(* forms.AccessTokenForm )
466
456
// 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
467
457
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 {
458
+ if authHeader := ctx .Req .Header .Get ("Authorization" ); authHeader != "" {
459
+ clientID , clientSecret , ok := httpauth .ParseAuthorizationHeaderBasic (authHeader )
460
+ if ! ok {
472
461
handleAccessTokenError (ctx , oauth2_provider.AccessTokenError {
473
462
ErrorCode : oauth2_provider .AccessTokenErrorCodeInvalidRequest ,
474
463
ErrorDescription : "cannot parse basic auth header" ,
0 commit comments