Skip to content

Commit 504fa2e

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

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
@@ -149,6 +149,8 @@ type OAuth2 struct {
149149
AlwaysShowLoginScreen bool `json:"alwaysShowLoginScreen"`
150150
// This is the connector that can be used for password grant
151151
PasswordConnector string `json:"passwordConnector"`
152+
// List of additional scope prefixes to allow
153+
AllowedScopePrefixes []string `json:"allowedScopePrefixes"`
152154
}
153155

154156
// 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
@@ -280,6 +280,9 @@ func runServe(options serveOptions) error {
280280
if len(c.Web.AllowedOrigins) > 0 {
281281
logger.Info("config allowed origins", "origins", c.Web.AllowedOrigins)
282282
}
283+
if len(c.OAuth2.AllowedScopePrefixes) > 0 {
284+
logger.Info("config allowed scope prefixes", "prefixes", strings.Join(c.OAuth2.AllowedScopePrefixes, ","))
285+
}
283286

284287
// explicitly convert to UTC.
285288
now := func() time.Time { return time.Now().UTC() }
@@ -295,6 +298,7 @@ func runServe(options serveOptions) error {
295298
Headers: c.Web.Headers.ToHTTPHeader(),
296299
AllowedOrigins: c.Web.AllowedOrigins,
297300
AllowedHeaders: c.Web.AllowedHeaders,
301+
AllowedScopePrefixes: c.OAuth2.AllowedScopePrefixes,
298302
Issuer: c.Issuer,
299303
Storage: s,
300304
Web: c.Frontend,

server/handlers.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,16 @@ func (s *Server) handlePasswordGrant(w http.ResponseWriter, r *http.Request, cli
11431143
default:
11441144
peerID, ok := parseCrossClientScope(scope)
11451145
if !ok {
1146-
unrecognized = append(unrecognized, scope)
1146+
var recognized bool
1147+
for _, prefix := range s.allowedScopePrefixes {
1148+
if strings.HasPrefix(scope, prefix) {
1149+
recognized = true
1150+
break
1151+
}
1152+
}
1153+
if !recognized {
1154+
unrecognized = append(unrecognized, scope)
1155+
}
11471156
continue
11481157
}
11491158

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"
@@ -533,7 +534,16 @@ func (s *Server) parseAuthorizationRequest(r *http.Request) (*storage.AuthReques
533534
default:
534535
peerID, ok := parseCrossClientScope(scope)
535536
if !ok {
536-
unrecognized = append(unrecognized, scope)
537+
var recognized bool
538+
for _, prefix := range s.allowedScopePrefixes {
539+
if strings.HasPrefix(scope, prefix) {
540+
recognized = true
541+
break
542+
}
543+
}
544+
if !recognized {
545+
unrecognized = append(unrecognized, scope)
546+
}
537547
continue
538548
}
539549

server/server.go

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

120120
PrometheusRegistry *prometheus.Registry
121121

122-
HealthChecker gosundheit.Health
122+
HealthChecker gosundheit.Health
123+
AllowedScopePrefixes []string
123124
}
124125

125126
// WebConfig holds the server's frontend templates and asset configuration.
@@ -188,6 +189,8 @@ type Server struct {
188189

189190
supportedGrantTypes []string
190191

192+
allowedScopePrefixes []string
193+
191194
now func() time.Time
192195

193196
idTokensValidFor time.Duration
@@ -303,6 +306,7 @@ func newServer(ctx context.Context, c Config, rotationStrategy rotationStrategy)
303306
storage: newKeyCacher(c.Storage, now),
304307
supportedResponseTypes: supportedRes,
305308
supportedGrantTypes: supportedGrants,
309+
allowedScopePrefixes: c.AllowedScopePrefixes,
306310
idTokensValidFor: value(c.IDTokensValidFor, 24*time.Hour),
307311
authRequestsValidFor: value(c.AuthRequestsValidFor, 24*time.Hour),
308312
deviceRequestsValidFor: value(c.DeviceRequestsValidFor, 5*time.Minute),

0 commit comments

Comments
 (0)