Skip to content

fix: accept api://botid-{app_id} audience format in token validation#314

Open
heyitsaamir wants to merge 2 commits intomainfrom
aamirj/fix-audience-validation
Open

fix: accept api://botid-{app_id} audience format in token validation#314
heyitsaamir wants to merge 2 commits intomainfrom
aamirj/fix-audience-validation

Conversation

@heyitsaamir
Copy link
Collaborator

@heyitsaamir heyitsaamir commented Mar 15, 2026

Summary

  • Add api://botid-{app_id} to the valid audiences list in TokenValidator.for_service() and TokenValidator.for_entra(), matching the TypeScript SDK behavior (teams.ts#469)
  • Bot Framework tokens issued for bots registered with Entra ID use this audience format and were being rejected with a 401
  • Add application_id_uri option to AppOptions — matches webApplicationInfo.resource in the Teams app manifest — for custom audience values in Entra token validation
  • Add parametrized test covering all three default audience formats and tests for application_id_uri

Test plan

  • All 24 token validator tests pass
  • Verify with a bot registered via Entra ID that tokens with aud=api://botid-{app_id} are accepted
  • Verify custom application_id_uri audiences are accepted

🤖 Generated with Claude Code

Bot Framework tokens issued for bots registered with Entra ID can carry
aud=api://botid-{app_id}, which was being rejected with a 401. Add this
audience format to both for_service() and for_entra() factory methods,
matching the TypeScript SDK behavior.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings March 15, 2026 16:55
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the Python Teams Apps SDK token validation to accept the aud=api://botid-{app_id} audience format (in addition to the existing app_id and api://{app_id} formats), aligning behavior with Bot Framework/Entra-issued tokens and the TypeScript SDK.

Changes:

  • Extend TokenValidator.for_service() and TokenValidator.for_entra() to include api://botid-{app_id} in valid_audiences.
  • Update initialization tests to expect the additional audience.
  • Add a parametrized test intended to cover acceptance of all three audience formats.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
packages/apps/src/microsoft_teams/apps/auth/token_validator.py Adds api://botid-{app_id} to the accepted audiences passed to jwt.decode for both service and Entra validators.
packages/apps/tests/test_token_validator.py Updates expected audience lists and adds a parametrized test for audience-format acceptance.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +173 to +175
with (
patch("jwt.PyJWKClient", return_value=mock_signing_key),
patch("jwt.decode", return_value=payload),
Port applicationIdUri from teams.ts PR #469: adds application_id_uri
to AppOptions, wires it through to TokenValidator.for_entra() so custom
audience values (matching webApplicationInfo.resource in the app manifest)
are accepted during Entra token validation.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

3 participants