Skip to content

feat(microsoft-teams): microsoft: Obtain tenantId by decoding the access token instead of calling the /organizations endpoint #4024

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 12, 2025

Conversation

lordsarcastic
Copy link
Contributor

@lordsarcastic lordsarcastic commented May 9, 2025

This PR updates the Microsoft Teams post-connection logic to decode the access token using JWT. It introduces a new interface for the decoded token structure and retrieves the tenant ID directly from the decoded token instead of making an API call. This change improves efficiency by reducing unnecessary network requests and streamlining the connection configuration process.


Microsoft Teams Authentication Optimization: JWT Token Decoding for TenantID

This PR optimizes the Microsoft Teams authentication flow by replacing an API call with direct JWT token decoding. Instead of making a network request to the '/v1.0/organization' endpoint, the code now extracts the tenant ID directly from the access token. This approach reduces latency, eliminates a potential failure point, and improves overall connection efficiency by removing an unnecessary network dependency.

Key Changes:
• Replaced API call to /v1.0/organization with JWT token decoding
• Created comprehensive MicrosoftDecodedToken interface for token structure
• Extracted tenant ID (tid) directly from the decoded token
• Removed dependency on axios for this operation

Affected Areas:
• Microsoft Teams post-connection handler
• Authentication flow for Microsoft Teams integration

Potential Impact:

Functionality: More reliable tenant ID retrieval with fewer dependencies on external API calls

Performance: Reduced latency by eliminating an HTTP request during connection setup

Security: Potential security concern due to using jwt.decode() without proper token verification

Scalability: Improved scalability by reducing external API dependencies and network traffic

Review Focus:
• Security concern: Using jwt.decode() without verification
• Completeness of the MicrosoftDecodedToken interface
• Error handling when token is not decodable
• Type assertion safety when handling the decoded JWT

Testing Needed

• Verify tenant ID extraction works for tokens from different Microsoft tenants
• Test with various token structures to ensure the interface is complete
• Verify error handling when token cannot be decoded properly
• Test with expired/invalid tokens to ensure graceful failure

Code Quality Assessment

packages/server/lib/hooks/connection/providers/microsoft-teams/post-connection.ts: Well-structured with clear type definitions. The interface is comprehensive but could potentially be simplified if not all properties are needed.

Best Practices

Code Organization:
• Well-defined interfaces
• Type safety

Error Handling:
• Defensive coding with type and existence checks

Performance:
• Reducing API calls
• Local processing instead of network requests

Possible Issues

• Security vulnerability from using jwt.decode() without verification
• Token structure might change in Microsoft API updates
• Interface may need updates if Microsoft adds/removes fields from their tokens


This summary was automatically generated by @propel-code-bot

This commit updates the Microsoft Teams post-connection logic to decode the access token using JWT. It introduces a new interface for the decoded token structure and retrieves the tenant ID directly from the decoded token instead of making an API call. This change improves efficiency by reducing unnecessary network requests and streamlining the connection configuration process.

**Key Changes:**
- Added JWT decoding for Microsoft Teams access tokens.
- Introduced `MicrosoftDecodedToken` interface to define the structure of the decoded token.
- Updated connection handling to extract tenant ID from the decoded token.

**Affected Areas:**
- Microsoft Teams connection logic in `post-connection.ts`.
Copy link

linear bot commented May 9, 2025

Copy link

cubic-dev-ai bot commented May 9, 2025

Your mrge subscription is currently inactive. Please reactivate your subscription to receive AI reviews and use mrge.

providerConfigKey: connection.provider_config_key
});
const accessToken = (connection.credentials as OAuth2Credentials).access_token;
const decoded = jwt.decode(accessToken) as MicrosoftDecodedToken;

Choose a reason for hiding this comment

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

[Security]

The JWT is being decoded without verification, which is a potential security risk. Since you're using the JWT's claims for retrieving the tenant ID, you should verify the token's authenticity using jwt.verify() instead of jwt.decode(). This helps prevent accepting manipulated or forged tokens.

@khaliqgant khaliqgant requested a review from a team May 12, 2025 08:22
@khaliqgant khaliqgant merged commit 6301fb0 into master May 12, 2025
17 of 18 checks passed
@khaliqgant khaliqgant deleted the ext-699 branch May 12, 2025 10:40
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