Skip to content

Commit

Permalink
make CountActiveMultiFactorCredentials retro-compatible with v0 code …
Browse files Browse the repository at this point in the history
…credentials
  • Loading branch information
wewelll authored and aeneasr committed Feb 27, 2025
1 parent 4a9c164 commit d02a07a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
13 changes: 11 additions & 2 deletions selfservice/strategy/code/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,17 @@ func (s *Strategy) CountActiveMultiFactorCredentials(ctx context.Context, cc map
return 0, nil
}

// If config is empty, return 0
if len(creds.Config) == 0 {
// Check if the credentials config is valid JSON
if !gjson.Valid(string(creds.Config)) {
return 0, nil
}

// Check for v0 format with address_type field
if gjson.GetBytes(creds.Config, "address_type").Exists() {
addressType := gjson.GetBytes(creds.Config, "address_type").String()
if addressType != "" {
return 1, nil
}
return 0, nil
}

Expand Down
9 changes: 9 additions & 0 deletions selfservice/strategy/code/strategy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,15 @@ func TestCountActiveCredentials(t *testing.T) {
enabled: true,
expected: 0,
},
{
in: map[identity.CredentialsType]identity.Credentials{strategy.ID(): {
Type: strategy.ID(),
Config: []byte(`{"address_type":"email","used_at":{"Time":"0001-01-01T00:00:00Z","Valid":false}}`),
}},
mfaEnabled: true,
enabled: true,
expected: 1,
},
{
in: map[identity.CredentialsType]identity.Credentials{strategy.ID(): {
Type: strategy.ID(),
Expand Down
2 changes: 1 addition & 1 deletion session/manager_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ func TestDoesSessionSatisfy(t *testing.T) {
{
desc: "with highest_available a recovery link user requires aal2 if they have 2fa code configured",
matcher: config.HighestAvailableAAL,
creds: []identity.Credentials{},
creds: []identity.Credentials{code},
withAMR: session.AuthenticationMethods{amrs[identity.CredentialsTypeRecoveryLink]},
withContext: func(t *testing.T, ctx context.Context) context.Context {
return confighelpers.WithConfigValues(ctx, map[string]any{
Expand Down

0 comments on commit d02a07a

Please sign in to comment.