Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: possible redirect url inconsistency #1003

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions cmd/dashboard/controller/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,19 @@ func oauth2redirect(c *gin.Context) (*model.Oauth2LoginResponse, error) {
if !has {
return nil, singleton.Localizer.ErrorT("provider not found")
}
o2conf := o2confRaw.Setup(getRedirectURL(c))
redirectURL := getRedirectURL(c)
o2conf := o2confRaw.Setup(redirectURL)

randomString, err := utils.GenerateRandomString(32)
if err != nil {
return nil, err
}
state, stateKey := randomString[:16], randomString[16:]
singleton.Cache.Set(fmt.Sprintf("%s%s", model.CacheKeyOauth2State, stateKey), &model.Oauth2State{
Action: model.Oauth2LoginType(rTypeInt),
Provider: provider,
State: state,
Action: model.Oauth2LoginType(rTypeInt),
Provider: provider,
State: state,
RedirectURL: redirectURL,
}, cache.DefaultExpiration)

url := o2conf.AuthCodeURL(state, oauth2.AccessTypeOnline)
Expand Down Expand Up @@ -138,7 +140,7 @@ func oauth2callback(jwtConfig *jwt.GinJWTMiddleware) func(c *gin.Context) (any,
return nil, singleton.Localizer.ErrorT("code is required")
}

openId, err := exchangeOpenId(c, o2confRaw, callbackData)
openId, err := exchangeOpenId(c, o2confRaw, callbackData, state.RedirectURL)
if err != nil {
model.BlockIP(singleton.DB, realip, model.WAFBlockReasonTypeBruteForceOauth2, model.BlockIDToken)
return nil, err
Expand Down Expand Up @@ -188,8 +190,9 @@ func oauth2callback(jwtConfig *jwt.GinJWTMiddleware) func(c *gin.Context) (any,
}
}

func exchangeOpenId(c *gin.Context, o2confRaw *model.Oauth2Config, callbackData *model.Oauth2Callback) (string, error) {
o2conf := o2confRaw.Setup(getRedirectURL(c))
func exchangeOpenId(c *gin.Context, o2confRaw *model.Oauth2Config,
callbackData *model.Oauth2Callback, redirectURL string) (string, error) {
o2conf := o2confRaw.Setup(redirectURL)
ctx := context.Background()

otk, err := o2conf.Exchange(ctx, callbackData.Code)
Expand Down
7 changes: 4 additions & 3 deletions model/oauth2bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const (
)

type Oauth2State struct {
Action Oauth2LoginType
Provider string
State string
Action Oauth2LoginType
Provider string
State string
RedirectURL string
}
Loading