Skip to content

Commit da247f8

Browse files
committed
fix: handle oauth provider id mismatch correctly
1 parent ce581a7 commit da247f8

4 files changed

Lines changed: 18 additions & 5 deletions

File tree

internal/controller/oauth_controller.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,17 @@ func (controller *OAuthController) oauthCallbackHandler(c *gin.Context) {
206206
return
207207
}
208208

209+
if service.ID() != req.Provider {
210+
tlog.App.Error().Msgf("OAuth service ID mismatch: expected %s, got %s", service.ID(), req.Provider)
211+
c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("%s/error", controller.config.AppURL))
212+
return
213+
}
214+
209215
sessionCookie := repository.Session{
210216
Username: username,
211217
Name: name,
212218
Email: user.Email,
213-
Provider: req.Provider,
219+
Provider: service.ID(),
214220
OAuthGroups: utils.CoalesceToString(user.Groups),
215221
OAuthName: service.Name(),
216222
OAuthSub: user.Sub,

internal/service/oauth_broker_service.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
type OAuthServiceImpl interface {
1212
Name() string
13+
ID() string
1314
NewRandom() string
1415
GetAuthURL(state string, verifier string) string
1516
GetToken(code string, verifier string) (*oauth2.Token, error)
@@ -39,7 +40,7 @@ func (broker *OAuthBrokerService) Init() error {
3940
broker.services[name] = presetFunc(cfg)
4041
tlog.App.Debug().Str("service", name).Msg("Loaded OAuth service from preset")
4142
} else {
42-
broker.services[name] = NewOAuthService(cfg)
43+
broker.services[name] = NewOAuthService(cfg, name)
4344
tlog.App.Debug().Str("service", name).Msg("Loaded OAuth service from config")
4445
}
4546
}

internal/service/oauth_presets.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ func newGoogleOAuthService(config config.OAuthServiceConfig) *OAuthService {
1111
config.AuthURL = endpoints.Google.AuthURL
1212
config.TokenURL = endpoints.Google.TokenURL
1313
config.UserinfoURL = "https://openidconnect.googleapis.com/v1/userinfo"
14-
return NewOAuthService(config)
14+
return NewOAuthService(config, "google")
1515
}
1616

1717
func newGitHubOAuthService(config config.OAuthServiceConfig) *OAuthService {
1818
scopes := []string{"read:user", "user:email"}
1919
config.Scopes = scopes
2020
config.AuthURL = endpoints.GitHub.AuthURL
2121
config.TokenURL = endpoints.GitHub.TokenURL
22-
return NewOAuthService(config).WithUserinfoExtractor(githubExtractor)
22+
return NewOAuthService(config, "github").WithUserinfoExtractor(githubExtractor)
2323
}

internal/service/oauth_service.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ type OAuthService struct {
1717
config *oauth2.Config
1818
ctx context.Context
1919
userinfoExtractor UserinfoExtractor
20+
id string
2021
}
2122

22-
func NewOAuthService(config config.OAuthServiceConfig) *OAuthService {
23+
func NewOAuthService(config config.OAuthServiceConfig, id string) *OAuthService {
2324
httpClient := &http.Client{
2425
Timeout: 30 * time.Second,
2526
Transport: &http.Transport{
@@ -45,6 +46,7 @@ func NewOAuthService(config config.OAuthServiceConfig) *OAuthService {
4546
},
4647
ctx: ctx,
4748
userinfoExtractor: defaultExtractor,
49+
id: id,
4850
}
4951
}
5052

@@ -57,6 +59,10 @@ func (s *OAuthService) Name() string {
5759
return s.serviceCfg.Name
5860
}
5961

62+
func (s *OAuthService) ID() string {
63+
return s.id
64+
}
65+
6066
func (s *OAuthService) NewRandom() string {
6167
// The generate verifier function just creates a random string,
6268
// so we can use it to generate a random state as well

0 commit comments

Comments
 (0)