fix(oauth2): enforce mandatory 2FA policy on OAuth2 authorize/grant endpoints (#38591)
Fixes #37407 --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
@@ -207,9 +207,24 @@ func IntrospectOAuth(ctx *context.Context) {
|
|||||||
ctx.JSON(http.StatusOK, response)
|
ctx.JSON(http.StatusOK, response)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func oauthDoerAuthorizePreCheck(ctx *context.Context, formState string) bool {
|
||||||
|
if ctx.DoerNeedTwoFactorAuth() {
|
||||||
|
handleAuthorizeError(ctx, AuthorizeError{
|
||||||
|
ErrorCode: ErrorCodeAccessDenied,
|
||||||
|
ErrorDescription: "two-factor authentication is required",
|
||||||
|
State: formState,
|
||||||
|
}, "")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// 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)
|
||||||
|
if !oauthDoerAuthorizePreCheck(ctx, form.State) {
|
||||||
|
return
|
||||||
|
}
|
||||||
errs := binding.Errors{}
|
errs := binding.Errors{}
|
||||||
errs = form.Validate(ctx.Req, errs)
|
errs = form.Validate(ctx.Req, errs)
|
||||||
if len(errs) > 0 {
|
if len(errs) > 0 {
|
||||||
@@ -385,6 +400,10 @@ func AuthorizeOAuth(ctx *context.Context) {
|
|||||||
// GrantApplicationOAuth manages the post request submitted when a user grants access to an application
|
// GrantApplicationOAuth manages the post request submitted when a user grants access to an application
|
||||||
func GrantApplicationOAuth(ctx *context.Context) {
|
func GrantApplicationOAuth(ctx *context.Context) {
|
||||||
form := web.GetForm(ctx).(*forms.GrantApplicationForm)
|
form := web.GetForm(ctx).(*forms.GrantApplicationForm)
|
||||||
|
if !oauthDoerAuthorizePreCheck(ctx, form.State) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if ctx.Session.Get("client_id") != form.ClientID || ctx.Session.Get("state") != form.State ||
|
if ctx.Session.Get("client_id") != form.ClientID || ctx.Session.Get("state") != form.State ||
|
||||||
ctx.Session.Get("redirect_uri") != form.RedirectURI {
|
ctx.Session.Get("redirect_uri") != form.RedirectURI {
|
||||||
ctx.HTTPError(http.StatusBadRequest)
|
ctx.HTTPError(http.StatusBadRequest)
|
||||||
|
|||||||
Reference in New Issue
Block a user