Skip to content

Commit

Permalink
Support additional login ids
Browse files Browse the repository at this point in the history
  • Loading branch information
asafshen committed Nov 27, 2023
1 parent f30471c commit de9e47c
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 30 deletions.
25 changes: 13 additions & 12 deletions descope/internal/mgmt/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ type user struct {
managementBase
}

func (u *user) Create(loginID string, user *descope.UserRequest) (*descope.UserResponse, error) {
func (u *user) Create(loginID string, user *descope.CreateUserRequest) (*descope.UserResponse, error) {
if user == nil {
user = &descope.UserRequest{}
user = &descope.CreateUserRequest{}
}
return u.create(loginID, user.Email, user.Phone, user.Name, user.Picture, user.Roles, user.Tenants, false, false, user.CustomAttributes, user.VerifiedEmail, user.VerifiedPhone, nil)
return u.create(loginID, user.Email, user.Phone, user.Name, user.Picture, user.Roles, user.Tenants, false, false, user.CustomAttributes, user.VerifiedEmail, user.VerifiedPhone, user.AdditionalLoginIDs, nil)
}

func (u *user) CreateTestUser(loginID string, user *descope.UserRequest) (*descope.UserResponse, error) {
func (u *user) CreateTestUser(loginID string, user *descope.CreateUserRequest) (*descope.UserResponse, error) {
if user == nil {
user = &descope.UserRequest{}
user = &descope.CreateUserRequest{}
}
return u.create(loginID, user.Email, user.Phone, user.Name, user.Picture, user.Roles, user.Tenants, false, true, user.CustomAttributes, user.VerifiedEmail, user.VerifiedPhone, nil)
return u.create(loginID, user.Email, user.Phone, user.Name, user.Picture, user.Roles, user.Tenants, false, true, user.CustomAttributes, user.VerifiedEmail, user.VerifiedPhone, user.AdditionalLoginIDs, nil)
}

func (u *user) CreateBatch(users []*descope.BatchUser) (*descope.UsersBatchResponse, error) {
Expand All @@ -33,11 +33,11 @@ func (u *user) CreateBatch(users []*descope.BatchUser) (*descope.UsersBatchRespo
return u.createBatch(users, nil)
}

func (u *user) Invite(loginID string, user *descope.UserRequest, options *descope.InviteOptions) (*descope.UserResponse, error) {
func (u *user) Invite(loginID string, user *descope.CreateUserRequest, options *descope.InviteOptions) (*descope.UserResponse, error) {
if user == nil {
user = &descope.UserRequest{}
user = &descope.CreateUserRequest{}
}
return u.create(loginID, user.Email, user.Phone, user.Name, user.Picture, user.Roles, user.Tenants, true, false, user.CustomAttributes, user.VerifiedEmail, user.VerifiedPhone, options)
return u.create(loginID, user.Email, user.Phone, user.Name, user.Picture, user.Roles, user.Tenants, true, false, user.CustomAttributes, user.VerifiedEmail, user.VerifiedPhone, user.AdditionalLoginIDs, options)
}

func (u *user) InviteBatch(users []*descope.BatchUser, options *descope.InviteOptions) (*descope.UsersBatchResponse, error) {
Expand All @@ -47,11 +47,11 @@ func (u *user) InviteBatch(users []*descope.BatchUser, options *descope.InviteOp
return u.createBatch(users, options)
}

func (u *user) create(loginID, email, phone, displayName, picture string, roles []string, tenants []*descope.AssociatedTenant, invite, test bool, customAttributes map[string]any, verifiedEmail *bool, verifiedPhone *bool, options *descope.InviteOptions) (*descope.UserResponse, error) {
func (u *user) create(loginID, email, phone, displayName, picture string, roles []string, tenants []*descope.AssociatedTenant, invite, test bool, customAttributes map[string]any, verifiedEmail *bool, verifiedPhone *bool, additionalLoginIDs []string, options *descope.InviteOptions) (*descope.UserResponse, error) {
if loginID == "" {
return nil, utils.NewInvalidArgumentError("loginID")
}
req := makeCreateUserRequest(loginID, email, phone, displayName, picture, roles, tenants, invite, test, customAttributes, verifiedEmail, verifiedPhone, options)
req := makeCreateUserRequest(loginID, email, phone, displayName, picture, roles, tenants, invite, test, customAttributes, verifiedEmail, verifiedPhone, additionalLoginIDs, options)
res, err := u.client.DoPostRequest(api.Routes.ManagementUserCreate(), req, nil, u.conf.ManagementKey)
if err != nil {
return nil, err
Expand Down Expand Up @@ -455,9 +455,10 @@ func (u *user) GenerateEmbeddedLink(loginID string, customClaims map[string]any)
return tRes.Token, nil
}

func makeCreateUserRequest(loginID, email, phone, displayName, picture string, roles []string, tenants []*descope.AssociatedTenant, invite, test bool, customAttributes map[string]any, verifiedEmail *bool, verifiedPhone *bool, options *descope.InviteOptions) map[string]any {
func makeCreateUserRequest(loginID, email, phone, displayName, picture string, roles []string, tenants []*descope.AssociatedTenant, invite, test bool, customAttributes map[string]any, verifiedEmail *bool, verifiedPhone *bool, additionalLoginIDs []string, options *descope.InviteOptions) map[string]any {
req := makeUpdateUserRequest(loginID, email, phone, displayName, picture, roles, tenants, customAttributes, verifiedEmail, verifiedPhone)
req["invite"] = invite
req["additionalLoginIds"] = additionalLoginIDs
if test {
req["test"] = true
}
Expand Down
12 changes: 6 additions & 6 deletions descope/internal/mgmt/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestUserCreateSuccess(t *testing.T) {
}
i++
}, response))
user := &descope.UserRequest{}
user := &descope.CreateUserRequest{}
user.Email = "[email protected]"
user.Roles = []string{"foo"}
user.CustomAttributes = ca
Expand Down Expand Up @@ -81,7 +81,7 @@ func TestUserCreateSuccessWithOptions(t *testing.T) {
assert.Nil(t, req["sendMail"])
assert.Nil(t, req["sendSMS"])
}, response))
user := &descope.UserRequest{}
user := &descope.CreateUserRequest{}
user.Email = "[email protected]"
user.Roles = []string{"foo"}
user.CustomAttributes = ca
Expand Down Expand Up @@ -213,7 +213,7 @@ func TestUserCreateTestUserSuccess(t *testing.T) {
require.Equal(t, "foo", roleNames[0])
require.EqualValues(t, true, req["test"])
}, response))
user := &descope.UserRequest{}
user := &descope.CreateUserRequest{}
user.Email = "[email protected]"
user.Roles = []string{"foo"}
res, err := m.User().CreateTestUser("abc", user)
Expand All @@ -224,7 +224,7 @@ func TestUserCreateTestUserSuccess(t *testing.T) {

func TestUserCreateError(t *testing.T) {
m := newTestMgmt(nil, helpers.DoOk(nil))
user := &descope.UserRequest{}
user := &descope.CreateUserRequest{}
user.Email = "[email protected]"
_, err := m.User().Create("", user)
require.Error(t, err)
Expand Down Expand Up @@ -1318,7 +1318,7 @@ func TestUserCreateWithVerifiedEmailUserSuccess(t *testing.T) {
require.EqualValues(t, true, req["test"])
require.EqualValues(t, true, req["verifiedEmail"])
}, response))
user := &descope.UserRequest{}
user := &descope.CreateUserRequest{}
user.Email = "[email protected]"
tr := true
user.VerifiedEmail = &tr
Expand Down Expand Up @@ -1346,7 +1346,7 @@ func TestUserCreateWithVerifiedPhoneUserSuccess(t *testing.T) {
require.EqualValues(t, true, req["test"])
require.EqualValues(t, false, req["verifiedPhone"])
}, response))
user := &descope.UserRequest{}
user := &descope.CreateUserRequest{}
user.Email = "[email protected]"
tr := false
user.VerifiedPhone = &tr
Expand Down
6 changes: 3 additions & 3 deletions descope/sdk/mgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type User interface {
// aren't associated with a tenant, while the tenants parameter can be used
// to specify which tenants to associate the user with and what roles the
// user has in each one.
Create(loginID string, user *descope.UserRequest) (*descope.UserResponse, error)
Create(loginID string, user *descope.CreateUserRequest) (*descope.UserResponse, error)

// Create a new test user.
//
Expand All @@ -68,7 +68,7 @@ type User interface {
// You can later generate OTP, Magic link and enchanted link to use in the test without the need
// of 3rd party messaging services
// Those users are not counted as part of the monthly active users
CreateTestUser(loginID string, user *descope.UserRequest) (*descope.UserResponse, error)
CreateTestUser(loginID string, user *descope.CreateUserRequest) (*descope.UserResponse, error)

// Create users in batch.
//
Expand All @@ -85,7 +85,7 @@ type User interface {
// the email / phone is explicitly set, or the loginID itself is an email address / phone number.
// You must configure the invitation URL in the Descope console prior to
// calling the method.
Invite(loginID string, user *descope.UserRequest, options *descope.InviteOptions) (*descope.UserResponse, error)
Invite(loginID string, user *descope.CreateUserRequest, options *descope.InviteOptions) (*descope.UserResponse, error)

// Create users in batch and invite them via an email / text message.
//
Expand Down
12 changes: 6 additions & 6 deletions descope/tests/mocks/mgmt/managementmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,19 @@ func (m *MockSSO) ConfigureMapping(tenantID string, roleMappings []*descope.Role
// Mock User

type MockUser struct {
CreateAssert func(loginID string, user *descope.UserRequest)
CreateAssert func(loginID string, user *descope.CreateUserRequest)
CreateResponse *descope.UserResponse
CreateError error

CreateTestUserAssert func(loginID string, user *descope.UserRequest)
CreateTestUserAssert func(loginID string, user *descope.CreateUserRequest)
CreateTestUserResponse *descope.UserResponse
CreateTestUserError error

CreateBatchAssert func(users []*descope.BatchUser)
CreateBatchResponse *descope.UsersBatchResponse
CreateBatchError error

InviteAssert func(loginID string, user *descope.UserRequest, options *descope.InviteOptions)
InviteAssert func(loginID string, user *descope.CreateUserRequest, options *descope.InviteOptions)
InviteResponse *descope.UserResponse
InviteError error

Expand Down Expand Up @@ -270,14 +270,14 @@ type MockUser struct {
LogoutError error
}

func (m *MockUser) Create(loginID string, user *descope.UserRequest) (*descope.UserResponse, error) {
func (m *MockUser) Create(loginID string, user *descope.CreateUserRequest) (*descope.UserResponse, error) {
if m.CreateAssert != nil {
m.CreateAssert(loginID, user)
}
return m.CreateResponse, m.CreateError
}

func (m *MockUser) CreateTestUser(loginID string, user *descope.UserRequest) (*descope.UserResponse, error) {
func (m *MockUser) CreateTestUser(loginID string, user *descope.CreateUserRequest) (*descope.UserResponse, error) {
if m.CreateTestUserAssert != nil {
m.CreateTestUserAssert(loginID, user)
}
Expand All @@ -291,7 +291,7 @@ func (m *MockUser) CreateBatch(users []*descope.BatchUser) (*descope.UsersBatchR
return m.CreateBatchResponse, m.CreateBatchError
}

func (m *MockUser) Invite(loginID string, user *descope.UserRequest, options *descope.InviteOptions) (*descope.UserResponse, error) {
func (m *MockUser) Invite(loginID string, user *descope.CreateUserRequest, options *descope.InviteOptions) (*descope.UserResponse, error) {
if m.InviteAssert != nil {
m.InviteAssert(loginID, user, options)
}
Expand Down
4 changes: 2 additions & 2 deletions descope/tests/mocks/mgmt/managementmock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestMockManagement(t *testing.T) {
descopeClient := client.DescopeClient{
Management: &MockManagement{
MockUser: &MockUser{
CreateAssert: func(loginID string, user *descope.UserRequest) {
CreateAssert: func(loginID string, user *descope.CreateUserRequest) {
called = true
assert.EqualValues(t, expectedLoginID, loginID)
},
Expand All @@ -26,7 +26,7 @@ func TestMockManagement(t *testing.T) {
},
}
assert.NotNil(t, descopeClient.Management)
r, err := descopeClient.Management.User().Create(expectedLoginID, &descope.UserRequest{})
r, err := descopeClient.Management.User().Create(expectedLoginID, &descope.CreateUserRequest{})
require.NoError(t, err)
assert.EqualValues(t, expectedLoginID, r.UserID)
assert.True(t, called)
Expand Down
5 changes: 5 additions & 0 deletions descope/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ type UserRequest struct {
VerifiedPhone *bool `json:"verifiedPhone,omitempty"`
}

type CreateUserRequest struct {
UserRequest `json:",inline"`
AdditionalLoginIDs []string `json:"additionalLoginIds,omitempty"`
}

type BatchUser struct {
LoginID string `json:"loginId,omitempty"`
Password *BatchUserPassword `json:"password,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion examples/managementcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func userCreate(args []string) error {
for _, tenantID := range flags.Tenants {
tenants = append(tenants, &descope.AssociatedTenant{TenantID: tenantID})
}
user := &descope.UserRequest{}
user := &descope.CreateUserRequest{}
user.Email = "[email protected]"
user.Phone = flags.Phone
user.Name = flags.Name
Expand Down

0 comments on commit de9e47c

Please sign in to comment.