@@ -94,6 +94,24 @@ func (err AccessTokenError) Error() string {
94
94
return fmt .Sprintf ("%s: %s" , err .ErrorCode , err .ErrorDescription )
95
95
}
96
96
97
+ // BearerTokenErrorCode represents an error code specified in RFC 6750
98
+ type BearerTokenErrorCode string
99
+
100
+ const (
101
+ // BearerTokenErrorCodeInvalidRequest represents an error code specified in RFC 6750
102
+ BearerTokenErrorCodeInvalidRequest BearerTokenErrorCode = "invalid_request"
103
+ // BearerTokenErrorCodeInvalidToken represents an error code specified in RFC 6750
104
+ BearerTokenErrorCodeInvalidToken BearerTokenErrorCode = "invalid_token"
105
+ // BearerTokenErrorCodeInsufficientScope represents an error code specified in RFC 6750
106
+ BearerTokenErrorCodeInsufficientScope BearerTokenErrorCode = "insufficient_scope"
107
+ )
108
+
109
+ // BearerTokenError represents an error response specified in RFC 6750
110
+ type BearerTokenError struct {
111
+ ErrorCode BearerTokenErrorCode
112
+ ErrorDescription string
113
+ }
114
+
97
115
// TokenType specifies the kind of token
98
116
type TokenType string
99
117
@@ -211,6 +229,13 @@ func InfoOAuth(ctx *context.Context) {
211
229
return
212
230
}
213
231
uid := sso .CheckOAuthAccessToken (auths [1 ])
232
+ if uid == 0 {
233
+ handleBearerTokenError (ctx , BearerTokenError {
234
+ ErrorCode : BearerTokenErrorCodeInvalidToken ,
235
+ ErrorDescription : "Access token not assigned to any user" ,
236
+ })
237
+ return
238
+ }
214
239
if uid != 0 {
215
240
authUser , err := models .GetUserByID (uid )
216
241
if err != nil {
@@ -225,8 +250,6 @@ func InfoOAuth(ctx *context.Context) {
225
250
Picture : authUser .AvatarLink (),
226
251
}
227
252
ctx .JSON (http .StatusOK , response )
228
- } else {
229
- ctx .ServerError ("InfoOAuth:" , fmt .Errorf ("UserID not valid" ))
230
253
}
231
254
}
232
255
@@ -608,3 +631,16 @@ func handleAuthorizeError(ctx *context.Context, authErr AuthorizeError, redirect
608
631
redirect .RawQuery = q .Encode ()
609
632
ctx .Redirect (redirect .String (), 302 )
610
633
}
634
+
635
+ func handleBearerTokenError (ctx * context.Context , beErr BearerTokenError ) {
636
+ ctx .Resp .Header ().Set ("WWW-Authenticate" , fmt .Sprintf ("Bearer realm=\" \" , error=\" %s\" , error_description=\" %s\" " , beErr .ErrorCode , beErr .ErrorDescription ))
637
+ if beErr .ErrorCode == BearerTokenErrorCodeInvalidRequest {
638
+ ctx .Error (http .StatusBadRequest )
639
+ }
640
+ if beErr .ErrorCode == BearerTokenErrorCodeInvalidToken {
641
+ ctx .Error (http .StatusUnauthorized )
642
+ }
643
+ if beErr .ErrorCode == BearerTokenErrorCodeInsufficientScope {
644
+ ctx .Error (http .StatusForbidden )
645
+ }
646
+ }
0 commit comments