Skip to content

fix(auth): report a missing Azure AD access token as a 400 - #3048

Open
lihongyuan99 wants to merge 1 commit into
Chainlit:mainfrom
lihongyuan99:fix/azure-ad-missing-access-token
Open

lihongyuan99 wants to merge 1 commit into
Chainlit:mainfrom
lihongyuan99:fix/azure-ad-missing-access-token

Conversation

@lihongyuan99

@lihongyuan99 lihongyuan99 commented Sep 18, 2026

Copy link
Copy Markdown

Description

The two Azure AD providers (AzureADOAuthProvider.get_token and AzureADHybridOAuthProvider.get_token) read the token endpoint response with json["access_token"]. When the token endpoint replies with an OAuth error body — for example {"error": "invalid_grant"} — this raises an unhandled KeyError, which surfaces to the user as a 500.

The if not token: raise HTTPException(400, ACCESS_TOKEN_MISSING) check immediately below never runs, because the subscript raises before it is reached.

Every other provider in this file reads the field with .get() and raises HTTPException(400, ACCESS_TOKEN_MISSING). There are 12 access_token reads in total; the other 10 all use .get(...), and ACCESS_TOKEN_MISSING is referenced 11 times in the file. This change brings the two Azure AD providers in line with that convention.

Changes

  • backend/chainlit/oauth_providers.py: use json.get("access_token") in both Azure AD providers.
  • backend/tests/test_oauth_providers.py: add two regression tests (plain top-level test_* functions, per AGENTS.md).

Tests

cd backend
python -m pytest tests/test_oauth_providers.py -q
  • Before: the two new tests fail with KeyError: 'access_token'; the pre-existing TestGithubOAuthProvider::test_github_get_token_missing_access_token control passes.
  • After: 57 passed (55 pre-existing + 2 new).

Checklist

  • Conventional Commits format used.
  • Change is backward-compatible; the success path is unchanged.
  • Tests pass locally, and ruff check / ruff format --check pass.

Summary by cubic

Reports a missing Azure AD access token as a 400 instead of an unhandled 500 when the token endpoint returns an OAuth error body. Both Azure AD providers now read access_token with .get() to match the other providers, and regression tests cover the two providers.

Written for commit eab2482. Summary will update on new commits.

Review in cubic

The two Azure AD providers read the token endpoint response with
`json["access_token"]`. An OAuth error body such as
`{"error": "invalid_grant"}` therefore raised an unhandled `KeyError` and
surfaced as a 500 instead of the 400 the `if not token` check right below
already intends.

The other ten `get_token` implementations read the field with `.get()` and
raise `HTTPException(400, ACCESS_TOKEN_MISSING)`. Match that.

Co-Authored-By: WorkBuddy AI <noreply@workbuddy.ai>

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No issues found across 2 files

Re-trigger cubic

@dokterbob
dokterbob enabled auto-merge September 18, 2026 15:31
@dokterbob
dokterbob added this pull request to the merge queue Sep 18, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Sep 18, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants