Skip to content

Commit

Permalink
chore: re-add WithIdentifier (#3992)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-jonas authored Jul 10, 2024
1 parent e070b27 commit 0da0c9b
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 3 deletions.
7 changes: 7 additions & 0 deletions selfservice/flow/login/strategy_form_hydrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var ErrBreakLoginPopulate = errors.New("skip rest of login form population")

type FormHydratorOptions struct {
IdentityHint *identity.Identity
Identifier string
}

type FormHydratorModifier func(o *FormHydratorOptions)
Expand All @@ -51,6 +52,12 @@ func WithIdentityHint(i *identity.Identity) FormHydratorModifier {
}
}

func WithIdentifier(i string) FormHydratorModifier {
return func(o *FormHydratorOptions) {
o.Identifier = i
}
}

func NewFormHydratorOptions(modifiers []FormHydratorModifier) *FormHydratorOptions {
o := new(FormHydratorOptions)
for _, m := range modifiers {
Expand Down
6 changes: 6 additions & 0 deletions selfservice/flow/login/strategy_form_hydrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ func TestWithIdentityHint(t *testing.T) {
opts := NewFormHydratorOptions([]FormHydratorModifier{WithIdentityHint(expected)})
assert.Equal(t, expected, opts.IdentityHint)
}

func TestWithIdentifier(t *testing.T) {
expected := "identifier"
opts := NewFormHydratorOptions([]FormHydratorModifier{WithIdentifier(expected)})
assert.Equal(t, expected, opts.Identifier)
}
4 changes: 2 additions & 2 deletions selfservice/strategy/code/strategy_login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ func TestFormHydration(t *testing.T) {
configtesthelpers.WithConfigValue(mfaEnabled, config.ViperKeySecurityAccountEnumerationMitigate, true),
t,
)
require.ErrorIs(t, fh.PopulateLoginMethodIdentifierFirstCredentials(r, f), idfirst.ErrNoCredentialsFound)
require.ErrorIs(t, fh.PopulateLoginMethodIdentifierFirstCredentials(r, f, login.WithIdentifier("foo@bar.com")), idfirst.ErrNoCredentialsFound)
toSnapshot(t, f)
})

Expand All @@ -931,7 +931,7 @@ func TestFormHydration(t *testing.T) {
configtesthelpers.WithConfigValue(passwordlessEnabled, config.ViperKeySecurityAccountEnumerationMitigate, true),
t,
)
require.NoError(t, fh.PopulateLoginMethodIdentifierFirstCredentials(r, f))
require.NoError(t, fh.PopulateLoginMethodIdentifierFirstCredentials(r, f, login.WithIdentifier("foo@bar.com")))
toSnapshot(t, f)
})
})
Expand Down
1 change: 1 addition & 0 deletions selfservice/strategy/idfirst/strategy_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func (s *Strategy) Login(w http.ResponseWriter, r *http.Request, f *login.Flow,

// Add identity hint
opts = append(opts, login.WithIdentityHint(identityHint))
opts = append(opts, login.WithIdentifier(p.Identifier))

didPopulate := false
for _, ls := range s.d.LoginStrategies(r.Context()) {
Expand Down
6 changes: 6 additions & 0 deletions selfservice/strategy/idfirst/strategy_login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,12 @@ func TestFormHydration(t *testing.T) {
toSnapshot(t, f)
})

t.Run("case=WithIdentifier", func(t *testing.T) {
r, f := newFlow(ctx, t)
require.ErrorIs(t, fh.PopulateLoginMethodIdentifierFirstCredentials(r, f, login.WithIdentifier("foo@bar.com")), idfirst.ErrNoCredentialsFound)
toSnapshot(t, f)
})

t.Run("case=WithIdentityHint", func(t *testing.T) {
t.Run("case=account enumeration mitigation enabled", func(t *testing.T) {
ctx := configtesthelpers.WithConfigValue(ctx, config.ViperKeySecurityAccountEnumerationMitigate, true)
Expand Down
6 changes: 6 additions & 0 deletions selfservice/strategy/oidc/strategy_login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ func TestFormHydration(t *testing.T) {
toSnapshot(t, f)
})

t.Run("case=WithIdentifier", func(t *testing.T) {
r, f := newFlow(ctx, t)
require.ErrorIs(t, fh.PopulateLoginMethodIdentifierFirstCredentials(r, f, login.WithIdentifier("foo@bar.com")), idfirst.ErrNoCredentialsFound)
toSnapshot(t, f)
})

t.Run("case=WithIdentityHint", func(t *testing.T) {
t.Run("case=account enumeration mitigation enabled", func(t *testing.T) {
ctx := configtesthelpers.WithConfigValue(ctx, config.ViperKeySecurityAccountEnumerationMitigate, true)
Expand Down
16 changes: 16 additions & 0 deletions selfservice/strategy/passkey/passkey_login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,22 @@ func TestFormHydration(t *testing.T) {
})
})

t.Run("case=WithIdentifier", func(t *testing.T) {
t.Run("case=account enumeration mitigation disabled", func(t *testing.T) {
ctx := configtesthelpers.WithConfigValue(ctx, config.ViperKeySecurityAccountEnumerationMitigate, false)
r, f := newFlow(ctx, t)
require.ErrorIs(t, fh.PopulateLoginMethodIdentifierFirstCredentials(r, f, login.WithIdentifier("foo@bar.com")), idfirst.ErrNoCredentialsFound)
toSnapshot(t, f)
})

t.Run("case=account enumeration mitigation enabled", func(t *testing.T) {
ctx := configtesthelpers.WithConfigValue(ctx, config.ViperKeySecurityAccountEnumerationMitigate, true)
r, f := newFlow(ctx, t)
require.ErrorIs(t, fh.PopulateLoginMethodIdentifierFirstCredentials(r, f, login.WithIdentifier("foo@bar.com")), idfirst.ErrNoCredentialsFound)
toSnapshot(t, f)
})
})

t.Run("case=WithIdentityHint", func(t *testing.T) {
t.Run("case=account enumeration mitigation enabled", func(t *testing.T) {
ctx := configtesthelpers.WithConfigValue(ctx, config.ViperKeySecurityAccountEnumerationMitigate, true)
Expand Down
17 changes: 16 additions & 1 deletion selfservice/strategy/password/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,6 @@ func TestCompleteLogin(t *testing.T) {
},
expectSuccess: false,
}} {

t.Run("case="+tc.name, func(t *testing.T) {
if tc.setupFn != nil {
cleanup := tc.setupFn()
Expand Down Expand Up @@ -1172,6 +1171,22 @@ func TestFormHydration(t *testing.T) {
})
})

t.Run("case=WithIdentifier", func(t *testing.T) {
t.Run("case=account enumeration mitigation disabled", func(t *testing.T) {
ctx := configtesthelpers.WithConfigValue(ctx, config.ViperKeySecurityAccountEnumerationMitigate, false)
r, f := newFlow(ctx, t)
require.ErrorIs(t, fh.PopulateLoginMethodIdentifierFirstCredentials(r, f, login.WithIdentifier("foo@bar.com")), idfirst.ErrNoCredentialsFound)
toSnapshot(t, f)
})

t.Run("case=account enumeration mitigation enabled", func(t *testing.T) {
ctx := configtesthelpers.WithConfigValue(ctx, config.ViperKeySecurityAccountEnumerationMitigate, true)
r, f := newFlow(ctx, t)
require.ErrorIs(t, fh.PopulateLoginMethodIdentifierFirstCredentials(r, f, login.WithIdentifier("foo@bar.com")), idfirst.ErrNoCredentialsFound)
toSnapshot(t, f)
})
})

t.Run("case=WithIdentityHint", func(t *testing.T) {
t.Run("case=account enumeration mitigation enabled and identity has no password", func(t *testing.T) {
ctx := configtesthelpers.WithConfigValue(ctx, config.ViperKeySecurityAccountEnumerationMitigate, true)
Expand Down
42 changes: 42 additions & 0 deletions selfservice/strategy/webauthn/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,48 @@ func TestFormHydration(t *testing.T) {
})
})

t.Run("case=WithIdentifier", func(t *testing.T) {
t.Run("case=passwordless enabled", func(t *testing.T) {
t.Run("case=account enumeration mitigation disabled", func(t *testing.T) {
r, f := newFlow(
configtesthelpers.WithConfigValue(passwordlessEnabled, config.ViperKeySecurityAccountEnumerationMitigate, false),
t,
)
require.ErrorIs(t, fh.PopulateLoginMethodIdentifierFirstCredentials(r, f, login.WithIdentifier("foo@bar.com")), idfirst.ErrNoCredentialsFound)
toSnapshot(t, f)
})

t.Run("case=account enumeration mitigation enabled", func(t *testing.T) {
r, f := newFlow(
configtesthelpers.WithConfigValue(passwordlessEnabled, config.ViperKeySecurityAccountEnumerationMitigate, true),
t,
)
require.ErrorIs(t, fh.PopulateLoginMethodIdentifierFirstCredentials(r, f, login.WithIdentifier("foo@bar.com")), idfirst.ErrNoCredentialsFound)
toSnapshot(t, f)
})
})

t.Run("case=mfa enabled", func(t *testing.T) {
t.Run("case=account enumeration mitigation disabled", func(t *testing.T) {
r, f := newFlow(
configtesthelpers.WithConfigValue(mfaEnabled, config.ViperKeySecurityAccountEnumerationMitigate, false),
t,
)
require.ErrorIs(t, fh.PopulateLoginMethodIdentifierFirstCredentials(r, f, login.WithIdentifier("foo@bar.com")), idfirst.ErrNoCredentialsFound)
toSnapshot(t, f)
})

t.Run("case=account enumeration mitigation enabled", func(t *testing.T) {
r, f := newFlow(
configtesthelpers.WithConfigValue(mfaEnabled, config.ViperKeySecurityAccountEnumerationMitigate, true),
t,
)
require.ErrorIs(t, fh.PopulateLoginMethodIdentifierFirstCredentials(r, f, login.WithIdentifier("foo@bar.com")), idfirst.ErrNoCredentialsFound)
toSnapshot(t, f)
})
})
})

t.Run("case=WithIdentityHint", func(t *testing.T) {
t.Run("case=account enumeration mitigation enabled", func(t *testing.T) {
mfaEnabled := configtesthelpers.WithConfigValue(mfaEnabled, config.ViperKeySecurityAccountEnumerationMitigate, true)
Expand Down

0 comments on commit 0da0c9b

Please sign in to comment.