Skip to content
This repository was archived by the owner on Aug 28, 2025. It is now read-only.

Commit 2a00153

Browse files
authored
feat: make UserNameClaim configurable (#67)
Signed-off-by: aaronschweig <[email protected]>
1 parent 58a3678 commit 2a00153

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

gateway/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type Config struct {
1414
LogLevel string `envconfig:"default=INFO,optional"`
1515
LocalDevelopment bool `envconfig:"default=false,optional"`
1616
HandlerCfg HandlerConfig
17+
UserNameClaim string `envconfig:"default=email,optional"`
1718
}
1819

1920
type HandlerConfig struct {

gateway/manager/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func NewManager(log *logger.Logger, cfg *rest.Config, appCfg appConfig.Config) (
6565
cfg.Host = fmt.Sprintf("%s://%s", u.Scheme, u.Host)
6666

6767
cfg.Wrap(func(rt http.RoundTripper) http.RoundTripper {
68-
return NewRoundTripper(log, rt)
68+
return NewRoundTripper(log, rt, appCfg.UserNameClaim)
6969
})
7070

7171
runtimeClient, err := kcp.NewClusterAwareClientWithWatch(cfg, client.Options{})

gateway/manager/roundtripper.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ import (
1111
type TokenKey struct{}
1212

1313
type roundTripper struct {
14-
log *logger.Logger
15-
base http.RoundTripper // TODO change to awareBaseHttp
14+
userClaim string
15+
log *logger.Logger
16+
base http.RoundTripper // TODO change to awareBaseHttp
1617
}
1718

18-
func NewRoundTripper(log *logger.Logger, base http.RoundTripper) http.RoundTripper {
19+
func NewRoundTripper(log *logger.Logger, base http.RoundTripper, userNameClaim string) http.RoundTripper {
1920
return &roundTripper{
20-
log: log,
21-
base: base,
21+
log: log,
22+
base: base,
23+
userClaim: userNameClaim,
2224
}
2325
}
2426

@@ -36,8 +38,20 @@ func (rt *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
3638
return rt.base.RoundTrip(req)
3739
}
3840

41+
userNameRaw, ok := claims[rt.userClaim]
42+
if !ok {
43+
rt.log.Debug().Msg("No user claim found in token")
44+
return rt.base.RoundTrip(req)
45+
}
46+
47+
userName, ok := userNameRaw.(string)
48+
if !ok {
49+
rt.log.Debug().Msg("User claim is not a string")
50+
return rt.base.RoundTrip(req)
51+
}
52+
3953
t := transport.NewImpersonatingRoundTripper(transport.ImpersonationConfig{
40-
UserName: claims["email"].(string),
54+
UserName: userName,
4155
}, rt.base)
4256

4357
return t.RoundTrip(req)

0 commit comments

Comments
 (0)