Skip to content

Conversation

@dsinghvi
Copy link
Contributor

Description

Solves #309 and correctly parses email_verification_id.

Documentation

Does this require changes to the WorkOS Docs? E.g. the API Reference or code snippets need updates.

[ ] Yes

If yes, link a related docs PR and add a docs maintainer as a reviewer. Their approval is required.

devin-ai-integration bot and others added 2 commits October 15, 2025 15:54
Fixes workos#309

When authenticate_with_password() raises an AuthorizationException for an
unverified email, the API response includes an email_verification_id field.
This field is now properly extracted and accessible on the exception object,
eliminating the need to manually parse the response JSON.

Co-Authored-By: Deep Singhvi <deep@buildwithfern.com>
…xception

- Created EmailVerificationRequiredException subclass of AuthorizationException
- Added email_verification_id field specific to this exception type
- Updated HTTP client to detect email_verification_required error code and raise specific exception
- Added tests for both the new exception and to verify regular AuthorizationException still works
- All 370 tests pass

Co-Authored-By: Deep Singhvi <deep@buildwithfern.com>
@dsinghvi dsinghvi requested a review from a team as a code owner October 15, 2025 16:24
@dsinghvi dsinghvi requested a review from mthadley October 15, 2025 16:24
@dsinghvi dsinghvi changed the title Devin/1760543656 add email verification id to exceptions fix: email verification id to exceptions Oct 15, 2025
@dsinghvi dsinghvi changed the title fix: email verification id to exceptions fix: parse email verification id in exception Oct 15, 2025
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Greptile Overview

Summary

Adds proper handling for the email_verification_id field returned by WorkOS API when email verification is required during password authentication.

Key Changes:

  • Created new EmailVerificationRequiredException subclass of AuthorizationException in workos/exceptions.py:48-63 that extracts and stores the email_verification_id field
  • Modified _maybe_raise_error_by_status_code() in workos/utils/_base_http_client.py:102-108 to detect 403 responses with code email_verification_required and raise the specific exception
  • Added comprehensive tests in tests/test_sync_http_client.py:267-316 to verify both the new exception and that regular authorization exceptions still work correctly

Issue Found:

  • Missing equivalent tests in tests/test_async_http_client.py - the async HTTP client uses the same BaseHTTPClient._maybe_raise_error_by_status_code() method, so it will work correctly, but test coverage is incomplete

Confidence Score: 4/5

  • This PR is safe to merge with one test coverage gap
  • The implementation correctly solves the issue by adding a specific exception type that inherits from AuthorizationException and extracts the email_verification_id field. The logic properly checks for the specific error code before raising the specialized exception, ensuring backward compatibility. Comprehensive sync tests verify the behavior. Score reduced by 1 point due to missing async test coverage, though the functionality will work correctly since both sync and async clients share the same exception handling logic
  • tests/test_async_http_client.py requires equivalent test coverage for EmailVerificationRequiredException

Important Files Changed

File Analysis

Filename Score Overview
workos/exceptions.py 5/5 Added EmailVerificationRequiredException class with email_verification_id field - clean implementation following existing patterns
workos/utils/_base_http_client.py 5/5 Added conditional logic to raise EmailVerificationRequiredException for 403 responses with code email_verification_required
tests/test_sync_http_client.py 4/5 Added comprehensive tests for new exception, but missing equivalent tests in async test file

Sequence Diagram

sequenceDiagram
    participant Client as User Code
    participant UM as UserManagement
    participant HTTP as BaseHTTPClient
    participant API as WorkOS API
    
    Client->>UM: authenticate_with_password(email, password)
    UM->>HTTP: request(path="/user_management/authenticate", json={...})
    HTTP->>API: POST /user_management/authenticate
    
    alt Email Verification Required
        API-->>HTTP: 403 {code: "email_verification_required", email_verification_id: "..."}
        HTTP->>HTTP: Check if code == "email_verification_required"
        HTTP-->>UM: Raise EmailVerificationRequiredException(email_verification_id)
        UM-->>Client: EmailVerificationRequiredException
        Note over Client: Can access ex.email_verification_id
    else Other 403 Error
        API-->>HTTP: 403 {code: "forbidden", message: "..."}
        HTTP-->>UM: Raise AuthorizationException
        UM-->>Client: AuthorizationException
    else Success
        API-->>HTTP: 200 {user: {...}, access_token: "..."}
        HTTP-->>UM: Return response_json
        UM-->>Client: AuthenticationResponse
    end
Loading

Additional Comments (1)

  1. tests/test_async_http_client.py, line 8-13 (link)

    logic: missing import and tests for EmailVerificationRequiredException in async tests

3 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

@nicknisi nicknisi merged commit b9a93d4 into workos:main Oct 15, 2025
6 checks passed
@nicknisi nicknisi mentioned this pull request Oct 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants