Skip to content

Commit

Permalink
refactor(identity): move credentials counter
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Mar 7, 2022
1 parent b7be327 commit c9875a7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
13 changes: 13 additions & 0 deletions identity/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type (
PoolProvider
courier.Provider
ValidationProvider
ActiveCredentialsCounterStrategyProvider
}
ManagementProvider interface {
IdentityManager() *Manager
Expand Down Expand Up @@ -165,3 +166,15 @@ func (m *Manager) validate(ctx context.Context, i *Identity, o *managerOptions)

return nil
}

func (m *Manager) CountActiveFirstFactorCredentials(ctx context.Context, i *Identity) (count int, err error) {
for _, strategy := range m.r.ActiveCredentialsCounterStrategies(ctx) {
current, err := strategy.CountActiveFirstFactorCredentials(i.Credentials)
if err != nil {
return 0, err
}

count += current
}
return count, nil
}
17 changes: 17 additions & 0 deletions identity/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,23 @@ func TestManager(t *testing.T) {
})
})

t.Run("method=CountActiveFirstFactorCredentials", func(t *testing.T) {
id := identity.NewIdentity(config.DefaultIdentityTraitsSchemaID)
count, err := reg.IdentityManager().CountActiveFirstFactorCredentials(ctx, id)
require.NoError(t, err)
assert.Equal(t, 0, count)

id.Credentials[identity.CredentialsTypePassword] = identity.Credentials{
Type: identity.CredentialsTypePassword,
Identifiers: []string{"foo"},
Config: []byte(`{"hashed_password":"$argon2id$v=19$m=32,t=2,p=4$cm94YnRVOW5jZzFzcVE4bQ$MNzk5BtR2vUhrp6qQEjRNw"}`),
}

count, err = reg.IdentityManager().CountActiveFirstFactorCredentials(ctx, id)
require.NoError(t, err)
assert.Equal(t, 1, count)
})

t.Run("method=UpdateTraits", func(t *testing.T) {
t.Run("case=should update protected traits with option", func(t *testing.T) {
original := identity.NewIdentity(config.DefaultIdentityTraitsSchemaID)
Expand Down

0 comments on commit c9875a7

Please sign in to comment.