Skip to content

Commit 3505983

Browse files
committed
Dynamic Scopes support #2960
Signed-off-by: Andy Lo-A-Foe <andy.loafoe@gmail.com>
1 parent a9d1fd3 commit 3505983

5 files changed

Lines changed: 32 additions & 3 deletions

File tree

cmd/dex/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ type OAuth2 struct {
141141
AlwaysShowLoginScreen bool `json:"alwaysShowLoginScreen"`
142142
// This is the connector that can be used for password grant
143143
PasswordConnector string `json:"passwordConnector"`
144+
// List of additional scope prefixes to allow
145+
AllowedScopePrefixes []string `json:"allowedScopePrefixes"`
144146
}
145147

146148
// Web is the config format for the HTTP server.

cmd/dex/serve.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ func runServe(options serveOptions) error {
252252
if len(c.Web.AllowedOrigins) > 0 {
253253
logger.Infof("config allowed origins: %s", c.Web.AllowedOrigins)
254254
}
255+
if len(c.OAuth2.AllowedScopePrefixes) > 0 {
256+
logger.Infof("config allowed scope prefixes: %s", strings.Join(c.OAuth2.AllowedScopePrefixes, ","))
257+
}
255258

256259
// explicitly convert to UTC.
257260
now := func() time.Time { return time.Now().UTC() }
@@ -265,6 +268,7 @@ func runServe(options serveOptions) error {
265268
AlwaysShowLoginScreen: c.OAuth2.AlwaysShowLoginScreen,
266269
PasswordConnector: c.OAuth2.PasswordConnector,
267270
AllowedOrigins: c.Web.AllowedOrigins,
271+
AllowedScopePrefixes: c.OAuth2.AllowedScopePrefixes,
268272
Issuer: c.Issuer,
269273
Storage: s,
270274
Web: c.Frontend,

server/handlers.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,16 @@ func (s *Server) handlePasswordGrant(w http.ResponseWriter, r *http.Request, cli
11221122
default:
11231123
peerID, ok := parseCrossClientScope(scope)
11241124
if !ok {
1125-
unrecognized = append(unrecognized, scope)
1125+
var recognized bool
1126+
for _, prefix := range s.allowedScopePrefixes {
1127+
if strings.HasPrefix(scope, prefix) {
1128+
recognized = true
1129+
break
1130+
}
1131+
}
1132+
if !recognized {
1133+
unrecognized = append(unrecognized, scope)
1134+
}
11261135
continue
11271136
}
11281137

server/oauth2.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ func tokenErr(w http.ResponseWriter, typ, description string, statusCode int) er
9393
return nil
9494
}
9595

96+
// nolint
9697
const (
9798
errInvalidRequest = "invalid_request"
9899
errUnauthorizedClient = "unauthorized_client"
@@ -504,7 +505,16 @@ func (s *Server) parseAuthorizationRequest(r *http.Request) (*storage.AuthReques
504505
default:
505506
peerID, ok := parseCrossClientScope(scope)
506507
if !ok {
507-
unrecognized = append(unrecognized, scope)
508+
var recognized bool
509+
for _, prefix := range s.allowedScopePrefixes {
510+
if strings.HasPrefix(scope, prefix) {
511+
recognized = true
512+
break
513+
}
514+
}
515+
if !recognized {
516+
unrecognized = append(unrecognized, scope)
517+
}
508518
continue
509519
}
510520

server/server.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ type Config struct {
106106

107107
PrometheusRegistry *prometheus.Registry
108108

109-
HealthChecker gosundheit.Health
109+
HealthChecker gosundheit.Health
110+
AllowedScopePrefixes []string
110111
}
111112

112113
// WebConfig holds the server's frontend templates and asset configuration.
@@ -175,6 +176,8 @@ type Server struct {
175176

176177
supportedGrantTypes []string
177178

179+
allowedScopePrefixes []string
180+
178181
now func() time.Time
179182

180183
idTokensValidFor time.Duration
@@ -287,6 +290,7 @@ func newServer(ctx context.Context, c Config, rotationStrategy rotationStrategy)
287290
storage: newKeyCacher(c.Storage, now),
288291
supportedResponseTypes: supportedRes,
289292
supportedGrantTypes: supportedGrants,
293+
allowedScopePrefixes: c.AllowedScopePrefixes,
290294
idTokensValidFor: value(c.IDTokensValidFor, 24*time.Hour),
291295
authRequestsValidFor: value(c.AuthRequestsValidFor, 24*time.Hour),
292296
deviceRequestsValidFor: value(c.DeviceRequestsValidFor, 5*time.Minute),

0 commit comments

Comments
 (0)