|
|
@ -96,24 +96,6 @@ func (err AccessTokenError) Error() string { |
|
|
|
return fmt.Sprintf("%s: %s", err.ErrorCode, err.ErrorDescription) |
|
|
|
return fmt.Sprintf("%s: %s", err.ErrorCode, err.ErrorDescription) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// BearerTokenErrorCode represents an error code specified in RFC 6750
|
|
|
|
|
|
|
|
type BearerTokenErrorCode string |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const ( |
|
|
|
|
|
|
|
// BearerTokenErrorCodeInvalidRequest represents an error code specified in RFC 6750
|
|
|
|
|
|
|
|
BearerTokenErrorCodeInvalidRequest BearerTokenErrorCode = "invalid_request" |
|
|
|
|
|
|
|
// BearerTokenErrorCodeInvalidToken represents an error code specified in RFC 6750
|
|
|
|
|
|
|
|
BearerTokenErrorCodeInvalidToken BearerTokenErrorCode = "invalid_token" |
|
|
|
|
|
|
|
// BearerTokenErrorCodeInsufficientScope represents an error code specified in RFC 6750
|
|
|
|
|
|
|
|
BearerTokenErrorCodeInsufficientScope BearerTokenErrorCode = "insufficient_scope" |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// BearerTokenError represents an error response specified in RFC 6750
|
|
|
|
|
|
|
|
type BearerTokenError struct { |
|
|
|
|
|
|
|
ErrorCode BearerTokenErrorCode `json:"error" form:"error"` |
|
|
|
|
|
|
|
ErrorDescription string `json:"error_description"` |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TokenType specifies the kind of token
|
|
|
|
// TokenType specifies the kind of token
|
|
|
|
type TokenType string |
|
|
|
type TokenType string |
|
|
|
|
|
|
|
|
|
|
@ -253,35 +235,56 @@ type userInfoResponse struct { |
|
|
|
|
|
|
|
|
|
|
|
// InfoOAuth manages request for userinfo endpoint
|
|
|
|
// InfoOAuth manages request for userinfo endpoint
|
|
|
|
func InfoOAuth(ctx *context.Context) { |
|
|
|
func InfoOAuth(ctx *context.Context) { |
|
|
|
header := ctx.Req.Header.Get("Authorization") |
|
|
|
if ctx.User == nil || ctx.Data["AuthedMethod"] != (&auth.OAuth2{}).Name() { |
|
|
|
auths := strings.Fields(header) |
|
|
|
ctx.Resp.Header().Set("WWW-Authenticate", `Bearer realm=""`) |
|
|
|
if len(auths) != 2 || auths[0] != "Bearer" { |
|
|
|
ctx.HandleText(http.StatusUnauthorized, "no valid authorization") |
|
|
|
ctx.HandleText(http.StatusUnauthorized, "no valid auth token authorization") |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
uid := auth.CheckOAuthAccessToken(auths[1]) |
|
|
|
|
|
|
|
if uid == 0 { |
|
|
|
|
|
|
|
handleBearerTokenError(ctx, BearerTokenError{ |
|
|
|
|
|
|
|
ErrorCode: BearerTokenErrorCodeInvalidToken, |
|
|
|
|
|
|
|
ErrorDescription: "Access token not assigned to any user", |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
authUser, err := models.GetUserByID(uid) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
ctx.ServerError("GetUserByID", err) |
|
|
|
|
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
response := &userInfoResponse{ |
|
|
|
response := &userInfoResponse{ |
|
|
|
Sub: fmt.Sprint(authUser.ID), |
|
|
|
Sub: fmt.Sprint(ctx.User.ID), |
|
|
|
Name: authUser.FullName, |
|
|
|
Name: ctx.User.FullName, |
|
|
|
Username: authUser.Name, |
|
|
|
Username: ctx.User.Name, |
|
|
|
Email: authUser.Email, |
|
|
|
Email: ctx.User.Email, |
|
|
|
Picture: authUser.AvatarLink(), |
|
|
|
Picture: ctx.User.AvatarLink(), |
|
|
|
} |
|
|
|
} |
|
|
|
ctx.JSON(http.StatusOK, response) |
|
|
|
ctx.JSON(http.StatusOK, response) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// IntrospectOAuth introspects an oauth token
|
|
|
|
|
|
|
|
func IntrospectOAuth(ctx *context.Context) { |
|
|
|
|
|
|
|
if ctx.User == nil { |
|
|
|
|
|
|
|
ctx.Resp.Header().Set("WWW-Authenticate", `Bearer realm=""`) |
|
|
|
|
|
|
|
ctx.HandleText(http.StatusUnauthorized, "no valid authorization") |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var response struct { |
|
|
|
|
|
|
|
Active bool `json:"active"` |
|
|
|
|
|
|
|
Scope string `json:"scope,omitempty"` |
|
|
|
|
|
|
|
jwt.StandardClaims |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
form := web.GetForm(ctx).(*forms.IntrospectTokenForm) |
|
|
|
|
|
|
|
token, err := oauth2.ParseToken(form.Token) |
|
|
|
|
|
|
|
if err == nil { |
|
|
|
|
|
|
|
if token.Valid() == nil { |
|
|
|
|
|
|
|
grant, err := models.GetOAuth2GrantByID(token.GrantID) |
|
|
|
|
|
|
|
if err == nil && grant != nil { |
|
|
|
|
|
|
|
app, err := models.GetOAuth2ApplicationByID(grant.ApplicationID) |
|
|
|
|
|
|
|
if err == nil && app != nil { |
|
|
|
|
|
|
|
response.Active = true |
|
|
|
|
|
|
|
response.Scope = grant.Scope |
|
|
|
|
|
|
|
response.Issuer = setting.AppURL |
|
|
|
|
|
|
|
response.Audience = app.ClientID |
|
|
|
|
|
|
|
response.Subject = fmt.Sprint(grant.UserID) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, response) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// AuthorizeOAuth manages authorize requests
|
|
|
|
// AuthorizeOAuth manages authorize requests
|
|
|
|
func AuthorizeOAuth(ctx *context.Context) { |
|
|
|
func AuthorizeOAuth(ctx *context.Context) { |
|
|
|
form := web.GetForm(ctx).(*forms.AuthorizationForm) |
|
|
|
form := web.GetForm(ctx).(*forms.AuthorizationForm) |
|
|
@ -697,18 +700,3 @@ func handleAuthorizeError(ctx *context.Context, authErr AuthorizeError, redirect |
|
|
|
redirect.RawQuery = q.Encode() |
|
|
|
redirect.RawQuery = q.Encode() |
|
|
|
ctx.Redirect(redirect.String(), 302) |
|
|
|
ctx.Redirect(redirect.String(), 302) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func handleBearerTokenError(ctx *context.Context, beErr BearerTokenError) { |
|
|
|
|
|
|
|
ctx.Resp.Header().Set("WWW-Authenticate", fmt.Sprintf("Bearer realm=\"\", error=\"%s\", error_description=\"%s\"", beErr.ErrorCode, beErr.ErrorDescription)) |
|
|
|
|
|
|
|
switch beErr.ErrorCode { |
|
|
|
|
|
|
|
case BearerTokenErrorCodeInvalidRequest: |
|
|
|
|
|
|
|
ctx.JSON(http.StatusBadRequest, beErr) |
|
|
|
|
|
|
|
case BearerTokenErrorCodeInvalidToken: |
|
|
|
|
|
|
|
ctx.JSON(http.StatusUnauthorized, beErr) |
|
|
|
|
|
|
|
case BearerTokenErrorCodeInsufficientScope: |
|
|
|
|
|
|
|
ctx.JSON(http.StatusForbidden, beErr) |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
log.Error("Invalid BearerTokenErrorCode: %v", beErr.ErrorCode) |
|
|
|
|
|
|
|
ctx.ServerError("Unhandled BearerTokenError", fmt.Errorf("BearerTokenError: error=\"%v\", error_description=\"%v\"", beErr.ErrorCode, beErr.ErrorDescription)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|