@@ -96,24 +96,6 @@ func (err AccessTokenError) Error() string {
9696 return fmt .Sprintf ("%s: %s" , err .ErrorCode , err .ErrorDescription )
9797}
9898
99- // BearerTokenErrorCode represents an error code specified in RFC 6750
100- type BearerTokenErrorCode string
101-
102- const (
103- // BearerTokenErrorCodeInvalidRequest represents an error code specified in RFC 6750
104- BearerTokenErrorCodeInvalidRequest BearerTokenErrorCode = "invalid_request"
105- // BearerTokenErrorCodeInvalidToken represents an error code specified in RFC 6750
106- BearerTokenErrorCodeInvalidToken BearerTokenErrorCode = "invalid_token"
107- // BearerTokenErrorCodeInsufficientScope represents an error code specified in RFC 6750
108- BearerTokenErrorCodeInsufficientScope BearerTokenErrorCode = "insufficient_scope"
109- )
110-
111- // BearerTokenError represents an error response specified in RFC 6750
112- type BearerTokenError struct {
113- ErrorCode BearerTokenErrorCode `json:"error" form:"error"`
114- ErrorDescription string `json:"error_description"`
115- }
116-
11799// TokenType specifies the kind of token
118100type TokenType string
119101
@@ -253,35 +235,56 @@ type userInfoResponse struct {
253235
254236// InfoOAuth manages request for userinfo endpoint
255237func InfoOAuth (ctx * context.Context ) {
256- header := ctx .Req .Header .Get ("Authorization" )
257- auths := strings .Fields (header )
258- if len (auths ) != 2 || auths [0 ] != "Bearer" {
259- ctx .HandleText (http .StatusUnauthorized , "no valid auth token authorization" )
260- return
261- }
262- uid := auth .CheckOAuthAccessToken (auths [1 ])
263- if uid == 0 {
264- handleBearerTokenError (ctx , BearerTokenError {
265- ErrorCode : BearerTokenErrorCodeInvalidToken ,
266- ErrorDescription : "Access token not assigned to any user" ,
267- })
268- return
269- }
270- authUser , err := models .GetUserByID (uid )
271- if err != nil {
272- ctx .ServerError ("GetUserByID" , err )
238+ if ctx .User == nil || ctx .Data ["AuthedMethod" ] != (& auth.OAuth2 {}).Name () {
239+ ctx .Resp .Header ().Set ("WWW-Authenticate" , `Bearer realm=""` )
240+ ctx .HandleText (http .StatusUnauthorized , "no valid authorization" )
273241 return
274242 }
275243 response := & userInfoResponse {
276- Sub : fmt .Sprint (authUser .ID ),
277- Name : authUser .FullName ,
278- Username : authUser .Name ,
279- Email : authUser .Email ,
280- Picture : authUser .AvatarLink (),
244+ Sub : fmt .Sprint (ctx . User .ID ),
245+ Name : ctx . User .FullName ,
246+ Username : ctx . User .Name ,
247+ Email : ctx . User .Email ,
248+ Picture : ctx . User .AvatarLink (),
281249 }
282250 ctx .JSON (http .StatusOK , response )
283251}
284252
253+ // IntrospectOAuth introspects an oauth token
254+ func IntrospectOAuth (ctx * context.Context ) {
255+ if ctx .User == nil {
256+ ctx .Resp .Header ().Set ("WWW-Authenticate" , `Bearer realm=""` )
257+ ctx .HandleText (http .StatusUnauthorized , "no valid authorization" )
258+ return
259+ }
260+
261+ var response struct {
262+ Active bool `json:"active"`
263+ Scope string `json:"scope,omitempty"`
264+ jwt.StandardClaims
265+ }
266+
267+ form := web .GetForm (ctx ).(* forms.IntrospectTokenForm )
268+ token , err := oauth2 .ParseToken (form .Token )
269+ if err == nil {
270+ if token .Valid () == nil {
271+ grant , err := models .GetOAuth2GrantByID (token .GrantID )
272+ if err == nil && grant != nil {
273+ app , err := models .GetOAuth2ApplicationByID (grant .ApplicationID )
274+ if err == nil && app != nil {
275+ response .Active = true
276+ response .Scope = grant .Scope
277+ response .Issuer = setting .AppURL
278+ response .Audience = app .ClientID
279+ response .Subject = fmt .Sprint (grant .UserID )
280+ }
281+ }
282+ }
283+ }
284+
285+ ctx .JSON (http .StatusOK , response )
286+ }
287+
285288// AuthorizeOAuth manages authorize requests
286289func AuthorizeOAuth (ctx * context.Context ) {
287290 form := web .GetForm (ctx ).(* forms.AuthorizationForm )
@@ -697,18 +700,3 @@ func handleAuthorizeError(ctx *context.Context, authErr AuthorizeError, redirect
697700 redirect .RawQuery = q .Encode ()
698701 ctx .Redirect (redirect .String (), 302 )
699702}
700-
701- func handleBearerTokenError (ctx * context.Context , beErr BearerTokenError ) {
702- ctx .Resp .Header ().Set ("WWW-Authenticate" , fmt .Sprintf ("Bearer realm=\" \" , error=\" %s\" , error_description=\" %s\" " , beErr .ErrorCode , beErr .ErrorDescription ))
703- switch beErr .ErrorCode {
704- case BearerTokenErrorCodeInvalidRequest :
705- ctx .JSON (http .StatusBadRequest , beErr )
706- case BearerTokenErrorCodeInvalidToken :
707- ctx .JSON (http .StatusUnauthorized , beErr )
708- case BearerTokenErrorCodeInsufficientScope :
709- ctx .JSON (http .StatusForbidden , beErr )
710- default :
711- log .Error ("Invalid BearerTokenErrorCode: %v" , beErr .ErrorCode )
712- ctx .ServerError ("Unhandled BearerTokenError" , fmt .Errorf ("BearerTokenError: error=\" %v\" , error_description=\" %v\" " , beErr .ErrorCode , beErr .ErrorDescription ))
713- }
714- }
0 commit comments