Add list sessions and revoke session methods and tests#483
Merged
Conversation
Contributor
There was a problem hiding this comment.
Greptile Overview
Summary
This PR adds User Management Sessions APIs to the WorkOS Python SDK with two new methods: list_sessions (user-scoped session listing) and revoke_session (session revocation).
Key Changes:
- New
SessionPydantic model with proper typing for all session attributes (id,user_id,organization_id,status,auth_method,ip_address,user_agent,expires_at,ended_at, timestamps) list_sessionsmethod with full pagination support (limit, before, after, order) and auto-pagination viaWorkOSListResourcerevoke_sessionmethod using POST with JSON body{"session_id": "..."}- Both methods implemented for sync (
UserManagement) and async (AsyncUserManagement) clients - Comprehensive test coverage using
@pytest.mark.sync_and_asyncdecorator - Session model exported in public API (
workos/__init__.py)
Implementation Quality:
- Follows existing SDK patterns consistently (matches
list_auth_factorspattern exactly) - Proper type annotations throughout with Pydantic validation
- Includes
user_idinlist_argsfor correct auto-pagination behavior - Tests verify request path, query params, JSON body, and response parsing
- No breaking changes to existing APIs
Confidence Score: 5/5
- This PR is safe to merge with no identified issues
- The implementation precisely follows established SDK patterns, includes comprehensive test coverage, maintains type safety throughout, and introduces no breaking changes. All code mirrors existing similar features (especially
list_auth_factors), demonstrating consistency with the codebase architecture. - No files require special attention
Important Files Changed
File Analysis
| Filename | Score | Overview |
|---|---|---|
| workos/types/user_management/session.py | 5/5 | Added new Session model with proper Pydantic typing, following existing SDK patterns |
| workos/user_management.py | 5/5 | Implemented list_sessions and revoke_session methods for both sync and async clients, following existing patterns |
| tests/test_user_management_list_sessions.py | 5/5 | Comprehensive tests for list_sessions including query params, parsing, and auto-pagination |
| tests/test_user_management_revoke_session.py | 5/5 | Complete test coverage for revoke_session verifying POST request, JSON body, and response parsing |
Sequence Diagram
sequenceDiagram
participant Client
participant UserManagement
participant HTTPClient
participant WorkOSAPI
Note over Client,WorkOSAPI: List Sessions Flow
Client->>UserManagement: list_sessions(user_id="user_123", limit=10)
UserManagement->>UserManagement: Build params with limit, before, after, order
UserManagement->>HTTPClient: request(GET, /user_management/users/{user_id}/sessions, params)
HTTPClient->>WorkOSAPI: GET /user_management/users/user_123/sessions?limit=10
WorkOSAPI-->>HTTPClient: Response with session list + metadata
HTTPClient-->>UserManagement: Parse response
UserManagement->>UserManagement: Create SessionsListResource with list_args (includes user_id)
UserManagement-->>Client: Return SessionsListResource (supports auto-pagination)
Note over Client,WorkOSAPI: Revoke Session Flow
Client->>UserManagement: revoke_session(session_id="session_abc")
UserManagement->>UserManagement: Build JSON payload {"session_id": "session_abc"}
UserManagement->>HTTPClient: request(POST, /user_management/sessions/revoke, json)
HTTPClient->>WorkOSAPI: POST /user_management/sessions/revoke
WorkOSAPI-->>HTTPClient: Response with revoked Session object
HTTPClient-->>UserManagement: Parse response
UserManagement->>UserManagement: Validate as Session model
UserManagement-->>Client: Return Session with status="revoked"
8 files reviewed, no comments
mattgd
reviewed
Oct 14, 2025
…thods like list_users, list_invitations, and list_auth_factors
mattgd
approved these changes
Oct 14, 2025
Contributor
mattgd
left a comment
There was a problem hiding this comment.
Looks good - thanks for adding!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds User Management Sessions APIs to the WorkOS Python SDK, following existing patterns (typed models, RequestHelper, WorkOSListResource pagination, sync/async clients), without breaking changes.
What’s included
Pagination behavior
Tests
Documentation
Does this require changes to the WorkOS Docs? E.g. the API Reference or code snippets need updates.
If yes, link a related docs PR and add a docs maintainer as a reviewer. Their approval is required.