Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logout all devices call #322

Merged
merged 2 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,13 @@ if err == nil {
// Do something
}
}

// Logout given user from all its devices, by login ID
err := descopeClient.Management.User().LogoutUser("<login id>")

// Logout given user from all its devices, by user ID
err := descopeClient.Management.User()LogoutUserByUserID("<user id>")

```

#### Set or Expire User Password
Expand Down
6 changes: 6 additions & 0 deletions descope/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ var (
userSetPassword: "mgmt/user/password/set",
userExpirePassword: "mgmt/user/password/expire",
userGetProviderToken: "mgmt/user/provider/token",
userLogoutAllDevices: "mgmt/user/logout",
userGenerateOTPForTest: "mgmt/tests/generate/otp",
userGenerateMagicLinkForTest: "mgmt/tests/generate/magiclink",
userGenerateEnchantedLinkForTest: "mgmt/tests/generate/enchantedlink",
Expand Down Expand Up @@ -223,6 +224,7 @@ type mgmtEndpoints struct {
userSetPassword string
userExpirePassword string
userGetProviderToken string
userLogoutAllDevices string

userGenerateOTPForTest string
userGenerateMagicLinkForTest string
Expand Down Expand Up @@ -511,6 +513,10 @@ func (e *endpoints) ManagementUserGetProviderToken() string {
return path.Join(e.version, e.mgmt.userGetProviderToken)
}

func (e *endpoints) ManagementUserLogoutAllDevices() string {
return path.Join(e.version, e.mgmt.userLogoutAllDevices)
}

func (e *endpoints) ManagementUserGenerateOTPForTest() string {
return path.Join(e.version, e.mgmt.userGenerateOTPForTest)
}
Expand Down
19 changes: 19 additions & 0 deletions descope/internal/mgmt/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,25 @@ func (u *user) GetProviderToken(loginID, provider string) (*descope.ProviderToke
return unmarshalProviderTokenResponse(res)
}

func (u *user) LogoutUser(loginID string) error {
if len(loginID) == 0 {
return utils.NewInvalidArgumentError("loginID")
}
return u.logoutUser("", loginID)
}

func (u *user) LogoutUserByUserID(userID string) error {
if len(userID) == 0 {
return utils.NewInvalidArgumentError("userID")
}
return u.logoutUser(userID, "")
}

func (u *user) logoutUser(userID string, loginID string) error {
_, err := u.client.DoPostRequest(api.Routes.ManagementUserLogoutAllDevices(), map[string]any{"userId": userID, "loginId": loginID}, nil, u.conf.ManagementKey)
return err
}

func (u *user) GenerateOTPForTestUser(method descope.DeliveryMethod, loginID string) (code string, err error) {
if loginID == "" {
return "", utils.NewInvalidArgumentError("loginID")
Expand Down
40 changes: 40 additions & 0 deletions descope/internal/mgmt/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,46 @@ func TestUserLoadByUserIDError(t *testing.T) {
require.Nil(t, res)
}

func TestUserLogoutUserByUserIDSuccess(t *testing.T) {
m := newTestMgmt(nil, helpers.DoOkWithBody(func(r *http.Request) {
require.Equal(t, r.Header.Get("Authorization"), "Bearer a:key")
req := map[string]any{}
require.NoError(t, helpers.ReadBody(r, &req))
assert.EqualValues(t, "abc", req["userId"])
assert.EqualValues(t, "", req["loginId"])
}, nil))
err := m.User().LogoutUserByUserID("abc")
require.NoError(t, err)
}

func TestUserLogoutUserByLoginIDSuccess(t *testing.T) {
m := newTestMgmt(nil, helpers.DoOkWithBody(func(r *http.Request) {
require.Equal(t, r.Header.Get("Authorization"), "Bearer a:key")
req := map[string]any{}
require.NoError(t, helpers.ReadBody(r, &req))
assert.EqualValues(t, "", req["userId"])
assert.EqualValues(t, "abc", req["loginId"])
}, nil))
err := m.User().LogoutUser("abc")
require.NoError(t, err)
}

func TestUserLogoutUserByUserIdErr(t *testing.T) {
m := newTestMgmt(nil, helpers.DoOkWithBody(func(r *http.Request) {
assert.Fail(t, "shouldn't get here")
}, nil))
err := m.User().LogoutUserByUserID("")
require.Error(t, err)
}

func TestUserLogoutUserByLoginIdErr(t *testing.T) {
m := newTestMgmt(nil, helpers.DoOkWithBody(func(r *http.Request) {
assert.Fail(t, "shouldn't get here")
}, nil))
err := m.User().LogoutUser("")
require.Error(t, err)
}

func TestSearchAllUsersSuccess(t *testing.T) {
response := map[string]any{
"users": []map[string]any{{
Expand Down
6 changes: 6 additions & 0 deletions descope/sdk/mgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ type User interface {
// Deactivate an existing user.
Deactivate(loginID string) (*descope.UserResponse, error)

// Logout given user from all their devices, by login ID
LogoutUser(loginID string) error

// Logout given user from all their devices, by user ID
LogoutUserByUserID(userID string) error

// Change current loginID to new one
// Leave empty to remove the current login ID
// Pay attention that if this is the only login ID, it cannot be removed
Expand Down
17 changes: 17 additions & 0 deletions descope/tests/mocks/mgmt/managementmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ type MockUser struct {
GenerateEmbeddedLinkAssert func(loginID string, customClaims map[string]any)
GenerateEmbeddedLinkResponse string
GenerateEmbeddedLinkError error

LogoutAssert func(id string)
LogoutError error
}

func (m *MockUser) Create(loginID string, user *descope.UserRequest) (*descope.UserResponse, error) {
Expand Down Expand Up @@ -306,6 +309,20 @@ func (m *MockUser) LoadByUserID(userID string) (*descope.UserResponse, error) {
return m.LoadResponse, m.LoadError
}

func (m *MockUser) LogoutUser(loginID string) error {
if m.LogoutAssert != nil {
m.LogoutAssert(loginID)
}
return m.LogoutError
}

func (m *MockUser) LogoutUserByUserID(userID string) error {
if m.LogoutAssert != nil {
m.LogoutAssert(userID)
}
return m.LogoutError
}

func (m *MockUser) SearchAll(options *descope.UserSearchOptions) ([]*descope.UserResponse, error) {
if m.SearchAllAssert != nil {
m.SearchAllAssert(options)
Expand Down