Skip to content

Commit 5fc1c8d

Browse files
authored
fix: possible redirect url inconsistency (#1003)
1 parent 41a0a7e commit 5fc1c8d

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

cmd/dashboard/controller/oauth2.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,19 @@ func oauth2redirect(c *gin.Context) (*model.Oauth2LoginResponse, error) {
5151
if !has {
5252
return nil, singleton.Localizer.ErrorT("provider not found")
5353
}
54-
o2conf := o2confRaw.Setup(getRedirectURL(c))
54+
redirectURL := getRedirectURL(c)
55+
o2conf := o2confRaw.Setup(redirectURL)
5556

5657
randomString, err := utils.GenerateRandomString(32)
5758
if err != nil {
5859
return nil, err
5960
}
6061
state, stateKey := randomString[:16], randomString[16:]
6162
singleton.Cache.Set(fmt.Sprintf("%s%s", model.CacheKeyOauth2State, stateKey), &model.Oauth2State{
62-
Action: model.Oauth2LoginType(rTypeInt),
63-
Provider: provider,
64-
State: state,
63+
Action: model.Oauth2LoginType(rTypeInt),
64+
Provider: provider,
65+
State: state,
66+
RedirectURL: redirectURL,
6567
}, cache.DefaultExpiration)
6668

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

141-
openId, err := exchangeOpenId(c, o2confRaw, callbackData)
143+
openId, err := exchangeOpenId(c, o2confRaw, callbackData, state.RedirectURL)
142144
if err != nil {
143145
model.BlockIP(singleton.DB, realip, model.WAFBlockReasonTypeBruteForceOauth2, model.BlockIDToken)
144146
return nil, err
@@ -188,8 +190,9 @@ func oauth2callback(jwtConfig *jwt.GinJWTMiddleware) func(c *gin.Context) (any,
188190
}
189191
}
190192

191-
func exchangeOpenId(c *gin.Context, o2confRaw *model.Oauth2Config, callbackData *model.Oauth2Callback) (string, error) {
192-
o2conf := o2confRaw.Setup(getRedirectURL(c))
193+
func exchangeOpenId(c *gin.Context, o2confRaw *model.Oauth2Config,
194+
callbackData *model.Oauth2Callback, redirectURL string) (string, error) {
195+
o2conf := o2confRaw.Setup(redirectURL)
193196
ctx := context.Background()
194197

195198
otk, err := o2conf.Exchange(ctx, callbackData.Code)

model/oauth2bind.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ const (
1717
)
1818

1919
type Oauth2State struct {
20-
Action Oauth2LoginType
21-
Provider string
22-
State string
20+
Action Oauth2LoginType
21+
Provider string
22+
State string
23+
RedirectURL string
2324
}

0 commit comments

Comments
 (0)