Skip to content

Commit

Permalink
feat: add DeleteCredentialsType to identity struct including tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Oct 19, 2021
1 parent e945336 commit b12bf52
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions identity/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
// make sure to add all of these values to the test that ensures they are created during migration
CredentialsTypePassword CredentialsType = "password"
CredentialsTypeOIDC CredentialsType = "oidc"
CredentialsTypeTOTP CredentialsType = "totp"
)

// Credentials represents a specific credential type
Expand Down
10 changes: 10 additions & 0 deletions identity/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,16 @@ func (i *Identity) SetCredentials(t CredentialsType, c Credentials) {
i.Credentials[t] = c
}

func (i *Identity) DeleteCredentialsType(t CredentialsType) {
i.lock().Lock()
defer i.lock().Unlock()
if i.Credentials == nil {
return
}

delete(i.Credentials, t)
}

func (i *Identity) GetCredentials(t CredentialsType) (*Credentials, bool) {
i.lock().RLock()
defer i.lock().RUnlock()
Expand Down
35 changes: 35 additions & 0 deletions identity/identity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,41 @@ func TestNewIdentity(t *testing.T) {
assert.True(t, i.IsActive())
}

func TestIdentityCredentials(t *testing.T) {
i := NewIdentity(config.DefaultIdentityTraitsSchemaID)
i.Credentials = nil

// Shouldn't error if map is nil
i.DeleteCredentialsType(CredentialsTypeTOTP)

expectedTOTP := Credentials{ID: x.NewUUID(), Type: CredentialsTypeTOTP}
i.SetCredentials(CredentialsTypeTOTP, expectedTOTP)
actual, found := i.GetCredentials(CredentialsTypeTOTP)
assert.True(t, found, "should set and find the credential if map was nil")
assert.Equal(t, &expectedTOTP, actual)

expectedPassword := Credentials{ID: x.NewUUID(), Type: CredentialsTypePassword}
i.SetCredentials(CredentialsTypePassword, expectedPassword)
actual, found = i.GetCredentials(CredentialsTypePassword)
assert.True(t, found, "should set and find the credential if map was not nil")
assert.Equal(t, &expectedPassword, actual)

expectedOIDC := Credentials{ID: x.NewUUID()}
i.SetCredentials(CredentialsTypeOIDC, expectedOIDC)
actual, found = i.GetCredentials(CredentialsTypeOIDC)
assert.True(t, found)
assert.Equal(t, expectedOIDC.ID, actual.ID)
assert.Equal(t, CredentialsTypeOIDC, actual.Type, "should set the type if we forgot to set it in the credentials struct")

i.DeleteCredentialsType(CredentialsTypePassword)
_, found = i.GetCredentials(CredentialsTypePassword)
assert.False(t, found, "should delete a credential properly")

actual, found = i.GetCredentials(CredentialsTypeTOTP)
assert.True(t, found, "but not alter other credentials")
assert.Equal(t, &expectedTOTP, actual)
}

func TestMarshalExcludesCredentials(t *testing.T) {
i := NewIdentity(config.DefaultIdentityTraitsSchemaID)
i.Credentials = map[CredentialsType]Credentials{
Expand Down

0 comments on commit b12bf52

Please sign in to comment.