Skip to content

Commit

Permalink
Add return_to parameter to get_logout_url (#400)
Browse files Browse the repository at this point in the history
* Add `return_to` parameter to `get_logout_url`

* Also update `session.get_logout_url`
  • Loading branch information
mthadley authored Jan 14, 2025
1 parent 980b769 commit c826292
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
8 changes: 8 additions & 0 deletions tests/test_user_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,14 @@ def test_get_logout_url(self):

assert expected == result

def test_get_logout_url_with_return_to(self):
expected = "https://api.workos.test/user_management/sessions/logout?session_id=session_123&return_to=https%3A%2F%2Fexample.com%2Fsigned-out"
result = self.user_management.get_logout_url(
"session_123", return_to="https://example.com/signed-out"
)

assert expected == result


@pytest.mark.sync_and_async(UserManagement, AsyncUserManagement)
class TestUserManagement(UserManagementFixtures):
Expand Down
5 changes: 3 additions & 2 deletions workos/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def refresh(
authenticated=False, reason=str(e)
)

def get_logout_url(self) -> str:
def get_logout_url(self, return_to: Optional[str] = None) -> str:
auth_response = self.authenticate()

if isinstance(auth_response, AuthenticateWithSessionCookieErrorResponse):
Expand All @@ -168,7 +168,8 @@ def get_logout_url(self) -> str:
)

result = self.user_management.get_logout_url(
session_id=auth_response.session_id
session_id=auth_response.session_id,
return_to=return_to,
)
return str(result)

Expand Down
11 changes: 9 additions & 2 deletions workos/user_management.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Optional, Protocol, Sequence, Type, cast
from urllib.parse import urlencode
from workos._client_configuration import ClientConfiguration
from workos.session import Session
from workos.types.list_resource import (
Expand Down Expand Up @@ -588,19 +589,25 @@ def get_jwks_url(self) -> str:

return f"{self._client_configuration.base_url}sso/jwks/{self._client_configuration.client_id}"

def get_logout_url(self, session_id: str) -> str:
def get_logout_url(self, session_id: str, return_to: Optional[str] = None) -> str:
"""Get the URL for ending the session and redirecting the user
This method is purposefully designed as synchronous as it does not make any HTTP requests.
Args:
session_id (str): The ID of the user's session
return_to (str): The URL to redirect the user to after the session is ended. (Optional)
Returns:
(str): URL to redirect the user to to end the session.
"""

return f"{self._client_configuration.base_url}user_management/sessions/logout?session_id={session_id}"
params = {"session_id": session_id}

if return_to:
params["return_to"] = return_to

return f"{self._client_configuration.base_url}user_management/sessions/logout?{urlencode(params)}"

def get_password_reset(self, password_reset_id: str) -> SyncOrAsync[PasswordReset]:
"""Get the details of a password reset object.
Expand Down

0 comments on commit c826292

Please sign in to comment.