From 83d5f2cfc9cf00d57ff8dedd0df05e59e53364ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frederik=20Sch=C3=B6ll?= Date: Tue, 15 Mar 2022 17:43:48 +0100 Subject: [PATCH 1/3] add method to unlink a users identity --- management/user.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/management/user.go b/management/user.go index 954f5b39..2bdc8d19 100644 --- a/management/user.go +++ b/management/user.go @@ -548,6 +548,14 @@ func (m *UserManager) Link(id string, il *UserIdentityLink, opts ...RequestOptio return uIDs, nil } +// Unlink unlinks an identity from a user making it a separate account again. +// +// See: https://auth0.com/docs/api/management/v2#!/Users/delete_user_identity_by_user_id +func (m *UserManager) Unlink(id, provider, userId string, opts ...RequestOption) (uIDs []UserIdentity, err error) { + err = m.Request("DELETE", m.URI("users", id, "identities", provider, userId), &uIDs, opts...) + return +} + // Organizations lists user's organizations. // // See: https://auth0.com/docs/api/management/v2#!/Users/get_organizations From ed5cf92d60cdaf20e12d613e777cc9c76cb3c3f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frederik=20Sch=C3=B6ll?= Date: Wed, 16 Mar 2022 11:51:24 +0100 Subject: [PATCH 2/3] add test for unlinking users --- management/user_test.go | 52 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/management/user_test.go b/management/user_test.go index 95ee09c5..5fa6354c 100644 --- a/management/user_test.go +++ b/management/user_test.go @@ -2,6 +2,7 @@ package management import ( "encoding/json" + "strings" "testing" "time" @@ -369,6 +370,57 @@ func TestUser(t *testing.T) { m.User.Delete(batman.GetID()) }) }) + + t.Run("Unlink", func(t *testing.T) { + + cs, err := m.Connection.ReadByName("Username-Password-Authentication") + if err != nil { + t.Error(err) + } + + mainUser := &User{ + Email: auth0.String("main@user.example"), + Username: auth0.String("main_user"), + Password: auth0.String("NF2QZxci3Z5NikLRoHcAu3H5"), + Connection: cs.Name, + } + if err := m.User.Create(mainUser); err != nil { + t.Error(err) + } + + secondaryUser := &User{ + Email: auth0.String("secondary@user.example"), + Username: auth0.String("secondary_user"), + Password: auth0.String("Ta9Y95PNbiCummJ3zpzCtEYy"), + Connection: cs.Name, + } + if err := m.User.Create(secondaryUser); err != nil { + t.Error(err) + } + + linkedIdentities, err := m.User.Link(mainUser.GetID(), &UserIdentityLink{ + Provider: auth0.String("auth0"), + UserID: secondaryUser.ID, + ConnectionID: cs.ID, + }) + if err != nil { + t.Error(err) + } + jsonLinkedIdentities, _ := json.Marshal(linkedIdentities) + t.Logf("%v\n", string(jsonLinkedIdentities)) + + unlinkedIdentities, err := m.User.Unlink(mainUser.GetID(), "auth0", strings.TrimPrefix(secondaryUser.GetID(), "auth0|")) + if err != nil { + t.Error(err) + } + jsonUnlinkedIdentities, _ := json.Marshal(unlinkedIdentities) + t.Logf("%v\n", string(jsonUnlinkedIdentities)) + + t.Cleanup(func() { + m.User.Delete(mainUser.GetID()) + m.User.Delete(secondaryUser.GetID()) + }) + }) } func TestUserIdentity(t *testing.T) { From 8bf23c70ffe0d3750b74c8e5909283bd6a0692fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frederik=20Sch=C3=B6ll?= Date: Thu, 7 Apr 2022 11:58:44 +0200 Subject: [PATCH 3/3] fix linting and error handling --- management/user.go | 4 ++-- management/user_test.go | 11 ++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/management/user.go b/management/user.go index 2bdc8d19..9f96ae36 100644 --- a/management/user.go +++ b/management/user.go @@ -551,8 +551,8 @@ func (m *UserManager) Link(id string, il *UserIdentityLink, opts ...RequestOptio // Unlink unlinks an identity from a user making it a separate account again. // // See: https://auth0.com/docs/api/management/v2#!/Users/delete_user_identity_by_user_id -func (m *UserManager) Unlink(id, provider, userId string, opts ...RequestOption) (uIDs []UserIdentity, err error) { - err = m.Request("DELETE", m.URI("users", id, "identities", provider, userId), &uIDs, opts...) +func (m *UserManager) Unlink(id, provider, userID string, opts ...RequestOption) (uIDs []UserIdentity, err error) { + err = m.Request("DELETE", m.URI("users", id, "identities", provider, userID), &uIDs, opts...) return } diff --git a/management/user_test.go b/management/user_test.go index 5fa6354c..56485899 100644 --- a/management/user_test.go +++ b/management/user_test.go @@ -372,7 +372,6 @@ func TestUser(t *testing.T) { }) t.Run("Unlink", func(t *testing.T) { - cs, err := m.Connection.ReadByName("Username-Password-Authentication") if err != nil { t.Error(err) @@ -406,14 +405,20 @@ func TestUser(t *testing.T) { if err != nil { t.Error(err) } - jsonLinkedIdentities, _ := json.Marshal(linkedIdentities) + jsonLinkedIdentities, err := json.Marshal(linkedIdentities) + if err != nil { + t.Error(err) + } t.Logf("%v\n", string(jsonLinkedIdentities)) unlinkedIdentities, err := m.User.Unlink(mainUser.GetID(), "auth0", strings.TrimPrefix(secondaryUser.GetID(), "auth0|")) if err != nil { t.Error(err) } - jsonUnlinkedIdentities, _ := json.Marshal(unlinkedIdentities) + jsonUnlinkedIdentities, err := json.Marshal(unlinkedIdentities) + if err != nil { + t.Error(err) + } t.Logf("%v\n", string(jsonUnlinkedIdentities)) t.Cleanup(func() {