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,11 @@ 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  clientID , clientSecret , ok  :=  httpauth .ParseAuthorizationHeaderBasic (authHeader ); ok  {
142132		app , err  :=  auth .GetOAuth2ApplicationByClientID (ctx , clientID )
143133		if  err  !=  nil  &&  ! auth .IsErrOauthClientIDInvalid (err ) {
144134			// this is likely a database error; log it and respond without details 
@@ -465,10 +455,9 @@ func AccessTokenOAuth(ctx *context.Context) {
465455	form  :=  * web .GetForm (ctx ).(* forms.AccessTokenForm )
466456	// 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 
467457	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  {
472461				handleAccessTokenError (ctx , oauth2_provider.AccessTokenError {
473462					ErrorCode :        oauth2_provider .AccessTokenErrorCodeInvalidRequest ,
474463					ErrorDescription : "cannot parse basic auth header" ,
0 commit comments