Skip to content

Commit b0022b9

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

5 files changed

Lines changed: 31 additions & 2 deletions

File tree

cmd/dex/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ type OAuth2 struct {
245245
PasswordConnector string `json:"passwordConnector"`
246246
// PKCE configuration
247247
PKCE PKCE `json:"pkce"`
248+
// List of additional scope prefixes to allow
249+
AllowedScopePrefixes []string `json:"allowedScopePrefixes"`
248250
}
249251

250252
// PKCE holds the PKCE (Proof Key for Code Exchange) configuration.

cmd/dex/serve.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,9 @@ func runServe(options serveOptions) error {
297297
if featureflags.ContinueOnConnectorFailure.Enabled() {
298298
logger.Info("continue on connector failure feature flag enabled")
299299
}
300+
if len(c.OAuth2.AllowedScopePrefixes) > 0 {
301+
logger.Info("config allowed scope prefixes", "prefixes", strings.Join(c.OAuth2.AllowedScopePrefixes, ","))
302+
}
300303

301304
// explicitly convert to UTC.
302305
now := func() time.Time { return time.Now().UTC() }

server/handlers.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1548,7 +1548,16 @@ func (s *Server) handlePasswordGrant(w http.ResponseWriter, r *http.Request, cli
15481548
default:
15491549
peerID, ok := parseCrossClientScope(scope)
15501550
if !ok {
1551-
unrecognized = append(unrecognized, scope)
1551+
var recognized bool
1552+
for _, prefix := range s.allowedScopePrefixes {
1553+
if strings.HasPrefix(scope, prefix) {
1554+
recognized = true
1555+
break
1556+
}
1557+
}
1558+
if !recognized {
1559+
unrecognized = append(unrecognized, scope)
1560+
}
15521561
continue
15531562
}
15541563

server/oauth2.go

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

121+
//nolint
121122
const (
122123
errInvalidRequest = "invalid_request"
123124
errUnauthorizedClient = "unauthorized_client"
@@ -566,7 +567,16 @@ func (s *Server) parseAuthorizationRequest(r *http.Request) (*storage.AuthReques
566567
default:
567568
peerID, ok := parseCrossClientScope(scope)
568569
if !ok {
569-
unrecognized = append(unrecognized, scope)
570+
var recognized bool
571+
for _, prefix := range s.allowedScopePrefixes {
572+
if strings.HasPrefix(scope, prefix) {
573+
recognized = true
574+
break
575+
}
576+
}
577+
if !recognized {
578+
unrecognized = append(unrecognized, scope)
579+
}
570580
continue
571581
}
572582

server/server.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ type Config struct {
147147

148148
// DefaultMFAChain is applied to clients that don't specify their own mfaChain.
149149
DefaultMFAChain []string
150+
151+
AllowedScopePrefixes []string
150152
}
151153

152154
// SessionConfig holds resolved session configuration.
@@ -237,6 +239,8 @@ type Server struct {
237239

238240
pkce PKCEConfig
239241

242+
allowedScopePrefixes []string
243+
240244
now func() time.Time
241245

242246
idTokensValidFor time.Duration
@@ -365,6 +369,7 @@ func newServer(ctx context.Context, c Config) (*Server, error) {
365369
supportedResponseTypes: supportedRes,
366370
supportedGrantTypes: supportedGrants,
367371
pkce: c.PKCE,
372+
allowedScopePrefixes: c.AllowedScopePrefixes,
368373
idTokensValidFor: value(c.IDTokensValidFor, 24*time.Hour),
369374
authRequestsValidFor: value(c.AuthRequestsValidFor, 24*time.Hour),
370375
deviceRequestsValidFor: value(c.DeviceRequestsValidFor, 5*time.Minute),

0 commit comments

Comments
 (0)