Skip to content

Commit dcaecc5

Browse files
committed
fix: make user code creation configurable
1 parent 6e7398f commit dcaecc5

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ type DeviceAndUserCodeLifespanProvider interface {
5151
GetDeviceAndUserCodeLifespan(ctx context.Context) time.Duration
5252
}
5353

54+
// DeviceAndUserCodeLifespanProvider returns the provider for configuring the device and user code lifespan
55+
type UserCodeProvider interface {
56+
GetUserCodeLength(ctx context.Context) int
57+
GetUserCodeSymbols(ctx context.Context) []rune
58+
}
59+
5460
// ScopeStrategyProvider returns the provider for configuring the scope strategy.
5561
type ScopeStrategyProvider interface {
5662
// GetScopeStrategy returns the scope strategy.

config_default.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/hashicorp/go-retryablehttp"
1414

1515
"github.com/ory/fosite/token/jwt"
16+
"github.com/ory/x/randx"
1617

1718
"github.com/ory/fosite/i18n"
1819
)
@@ -229,6 +230,12 @@ type Config struct {
229230

230231
// IsPushedAuthorizeEnforced enforces pushed authorization request for /authorize
231232
IsPushedAuthorizeEnforced bool
233+
234+
// UserCodeLength defines the length of the user_code
235+
UserCodeLength int
236+
237+
// UserCodeSymbols defines the symbols that will be used to construct the user_code
238+
UserCodeSymbols []rune
232239
}
233240

234241
func (c *Config) GetGlobalSecret(ctx context.Context) ([]byte, error) {
@@ -540,3 +547,19 @@ func (c *Config) GetDeviceAuthTokenPollingInterval(ctx context.Context) time.Dur
540547
}
541548
return c.DeviceAuthTokenPollingInterval
542549
}
550+
551+
// GetUserCodeLength returns configured user_code length
552+
func (c *Config) GetUserCodeLength(ctx context.Context) int {
553+
if c.UserCodeLength == 0 {
554+
return 8
555+
}
556+
return c.UserCodeLength
557+
}
558+
559+
// GetDeviceAuthTokenPollingInterval returns configured user_code allowed symbols
560+
func (c *Config) GetUserCodeSymbols(ctx context.Context) []rune {
561+
if c.UserCodeSymbols == nil {
562+
return []rune(randx.AlphaUpper)
563+
}
564+
return c.UserCodeSymbols
565+
}

fosite.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ type Configurator interface {
148148
RevocationHandlersProvider
149149
UseLegacyErrorFormatProvider
150150
DeviceEndpointHandlersProvider
151+
UserCodeProvider
151152
DeviceProvider
152153
}
153154

handler/rfc8628/strategy_hmacsha.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,15 @@ type DefaultDeviceStrategy struct {
103103
Config interface {
104104
fosite.DeviceProvider
105105
fosite.DeviceAndUserCodeLifespanProvider
106+
fosite.UserCodeProvider
106107
}
107108
}
108109

109110
var _ RFC8628CodeStrategy = (*DefaultDeviceStrategy)(nil)
110111

111112
// GenerateUserCode generates a user_code
112113
func (h *DefaultDeviceStrategy) GenerateUserCode(ctx context.Context) (string, string, error) {
113-
seq, err := randx.RuneSequence(8, []rune(randx.AlphaUpper))
114+
seq, err := randx.RuneSequence(h.Config.GetUserCodeLength(ctx), h.Config.GetUserCodeSymbols(ctx))
114115
if err != nil {
115116
return "", "", err
116117
}

0 commit comments

Comments
 (0)