Skip to content

Commit

Permalink
fix: make user code creation configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
nsklikas committed Nov 6, 2024
1 parent 5cabb83 commit 21fed42
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
6 changes: 6 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ type DeviceAndUserCodeLifespanProvider interface {
GetDeviceAndUserCodeLifespan(ctx context.Context) time.Duration
}

// DeviceAndUserCodeLifespanProvider returns the provider for configuring the device and user code lifespan
type UserCodeProvider interface {
GetUserCodeLength(ctx context.Context) int
GetUserCodeSymbols(ctx context.Context) []rune
}

// ScopeStrategyProvider returns the provider for configuring the scope strategy.
type ScopeStrategyProvider interface {
// GetScopeStrategy returns the scope strategy.
Expand Down
23 changes: 23 additions & 0 deletions config_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/hashicorp/go-retryablehttp"

"github.com/ory/fosite/token/jwt"
"github.com/ory/x/randx"

"github.com/ory/fosite/i18n"
)
Expand Down Expand Up @@ -229,6 +230,12 @@ type Config struct {

// IsPushedAuthorizeEnforced enforces pushed authorization request for /authorize
IsPushedAuthorizeEnforced bool

// UserCodeLength defines the length of the user_code
UserCodeLength int

// UserCodeSymbols defines the symbols that will be used to construct the user_code
UserCodeSymbols []rune
}

func (c *Config) GetGlobalSecret(ctx context.Context) ([]byte, error) {
Expand Down Expand Up @@ -540,3 +547,19 @@ func (c *Config) GetDeviceAuthTokenPollingInterval(ctx context.Context) time.Dur
}
return c.DeviceAuthTokenPollingInterval
}

// GetUserCodeLength returns configured user_code length
func (c *Config) GetUserCodeLength(ctx context.Context) int {
if c.UserCodeLength == 0 {
return 8
}
return c.UserCodeLength
}

// GetDeviceAuthTokenPollingInterval returns configured user_code allowed symbols
func (c *Config) GetUserCodeSymbols(ctx context.Context) []rune {
if c.UserCodeSymbols == nil {
return []rune(randx.AlphaUpper)
}
return c.UserCodeSymbols
}
1 change: 1 addition & 0 deletions fosite.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ type Configurator interface {
RevocationHandlersProvider
UseLegacyErrorFormatProvider
DeviceEndpointHandlersProvider
UserCodeProvider
DeviceProvider
}

Expand Down
3 changes: 2 additions & 1 deletion handler/rfc8628/strategy_hmacsha.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,15 @@ type DefaultDeviceStrategy struct {
Config interface {
fosite.DeviceProvider
fosite.DeviceAndUserCodeLifespanProvider
fosite.UserCodeProvider
}
}

var _ RFC8628CodeStrategy = (*DefaultDeviceStrategy)(nil)

// GenerateUserCode generates a user_code
func (h *DefaultDeviceStrategy) GenerateUserCode(ctx context.Context) (string, string, error) {
seq, err := randx.RuneSequence(8, []rune(randx.AlphaUpper))
seq, err := randx.RuneSequence(h.Config.GetUserCodeLength(ctx), h.Config.GetUserCodeSymbols(ctx))
if err != nil {
return "", "", err
}
Expand Down

0 comments on commit 21fed42

Please sign in to comment.