-
Notifications
You must be signed in to change notification settings - Fork 6
Add auth files #38
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
Add auth files #38
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| """ | ||
| Copyright (c) Microsoft Corporation. All rights reserved. | ||
| Licensed under the MIT License. | ||
| """ | ||
|
|
||
| from .caller import CallerIds, CallerType | ||
| from .credentials import ClientCredentials, Credentials, TokenCredentials | ||
| from .json_web_token import JsonWebToken, JsonWebTokenPayload | ||
| from .token import TokenProtocol | ||
|
|
||
| __all__ = [ | ||
| "CallerIds", | ||
| "CallerType", | ||
| "ClientCredentials", | ||
| "Credentials", | ||
| "TokenCredentials", | ||
| "TokenProtocol", | ||
| "JsonWebToken", | ||
| "JsonWebTokenPayload", | ||
| ] |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| """ | ||
| Copyright (c) Microsoft Corporation. All rights reserved. | ||
| Licensed under the MIT License. | ||
| """ | ||
|
|
||
| from enum import Enum | ||
| from typing import Literal | ||
|
|
||
|
|
||
| class CallerIds(str, Enum): | ||
| """Enum for caller ID types.""" | ||
|
|
||
| AZURE = "azure" | ||
| GOV = "gov" | ||
| BOT = "bot" | ||
|
|
||
|
|
||
| CallerType = Literal["azure", "gov", "bot"] |
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
129 changes: 129 additions & 0 deletions
129
packages/api/src/microsoft/teams/api/auth/json_web_token.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| """ | ||
| Copyright (c) Microsoft Corporation. All rights reserved. | ||
| Licensed under the MIT License. | ||
| """ | ||
|
|
||
| import time | ||
| from typing import Optional, Union | ||
|
|
||
| import jwt | ||
| from pydantic import BaseModel, ConfigDict | ||
|
|
||
| from .caller import CallerIds, CallerType | ||
| from .token import TokenProtocol | ||
|
|
||
|
|
||
| class JsonWebTokenPayload(BaseModel): | ||
| """JWT payload with additional Teams-specific fields.""" | ||
|
|
||
| model_config = ConfigDict(extra="allow") | ||
|
|
||
| aud: Optional[Union[str, list[str]]] = None | ||
| iss: Optional[str] = None | ||
| exp: Optional[int] = None | ||
| kid: Optional[str] = None | ||
| appid: Optional[str] = None | ||
| app_displayname: Optional[str] = None | ||
| tid: Optional[str] = None | ||
| version: Optional[str] = None | ||
| serviceurl: Optional[str] = None | ||
heyitsaamir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| class JsonWebToken(TokenProtocol): | ||
| """JSON Web Token implementation for Teams authentication.""" | ||
|
|
||
| def __init__(self, value: str): | ||
| """ | ||
| Initialize JWT from token string. | ||
|
|
||
| Args: | ||
| value: The JWT token string. | ||
| """ | ||
| self._value = value | ||
| # Decode without verification for payload extraction | ||
| jwt_instance = jwt.JWT() | ||
| self._payload = JsonWebTokenPayload(**jwt_instance.decode(value, do_verify=False, do_time_check=False)) | ||
|
|
||
| @property | ||
| def audience(self) -> Optional[Union[str, list[str]]]: | ||
| """The token audience.""" | ||
| return self._payload.aud | ||
|
|
||
| @property | ||
| def issuer(self) -> Optional[str]: | ||
| """The token issuer.""" | ||
| return self._payload.iss | ||
|
|
||
| @property | ||
| def key_id(self) -> Optional[str]: | ||
| """The key ID.""" | ||
| return self._payload.kid | ||
|
|
||
| @property | ||
| def app_id(self) -> str: | ||
| """The app ID.""" | ||
| return self._payload.appid or "" | ||
|
|
||
| @property | ||
| def app_display_name(self) -> Optional[str]: | ||
| """The app display name.""" | ||
| return self._payload.app_displayname | ||
|
|
||
| @property | ||
| def tenant_id(self) -> Optional[str]: | ||
| """The tenant ID.""" | ||
| return self._payload.tid | ||
|
|
||
| @property | ||
| def version(self) -> Optional[str]: | ||
| """The token version.""" | ||
| return self._payload.version | ||
|
|
||
| @property | ||
| def service_url(self) -> str: | ||
| """The service URL to send responses to.""" | ||
| url = self._payload.serviceurl or "https://smba.trafficmanager.net/teams" | ||
|
|
||
| if url.endswith("/"): | ||
| url = url[:-1] | ||
|
|
||
| return url | ||
|
|
||
| @property | ||
| def from_(self) -> CallerType: | ||
| """Where the activity originated from.""" | ||
| if self.app_id: | ||
| return "bot" | ||
| return "azure" | ||
|
|
||
| @property | ||
| def from_id(self) -> str: | ||
| """The id of the activity sender.""" | ||
| if self.from_ == "bot": | ||
| return f"{CallerIds.BOT}:{self.app_id}" | ||
| return CallerIds.AZURE | ||
|
|
||
| @property | ||
| def expiration(self) -> Optional[int]: | ||
| """The expiration of the token since epoch in milliseconds.""" | ||
| if self._payload.exp: | ||
| return self._payload.exp * 1000 | ||
| return None | ||
|
|
||
| def is_expired(self, buffer_ms: int = 5 * 60 * 1000) -> bool: | ||
| """ | ||
| Check if the token is expired. | ||
|
|
||
| Args: | ||
| buffer_ms: Buffer time in milliseconds (default 5 minutes). | ||
|
|
||
| Returns: | ||
| True if the token is expired, False otherwise. | ||
| """ | ||
| if not self.expiration: | ||
| return False | ||
| return self.expiration < (time.time() * 1000) + buffer_ms | ||
|
|
||
| def __str__(self) -> str: | ||
| """String form of the token.""" | ||
| return self._value | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| """ | ||
| Copyright (c) Microsoft Corporation. All rights reserved. | ||
| Licensed under the MIT License. | ||
| """ | ||
|
|
||
| from typing import Optional, Protocol | ||
|
|
||
| from .caller import CallerType | ||
|
|
||
|
|
||
| class TokenProtocol(Protocol): | ||
| """Any authorized token.""" | ||
|
|
||
| @property | ||
| def app_id(self) -> str: | ||
| """The app id.""" | ||
| ... | ||
|
|
||
| @property | ||
| def app_display_name(self) -> Optional[str]: | ||
| """The app display name.""" | ||
| ... | ||
|
|
||
| @property | ||
| def tenant_id(self) -> Optional[str]: | ||
| """The tenant id.""" | ||
| ... | ||
|
|
||
| @property | ||
| def service_url(self) -> str: | ||
| """The service url to send responses to.""" | ||
| ... | ||
|
|
||
| @property | ||
| def from_(self) -> CallerType: | ||
| """Where the activity originated from.""" | ||
| ... | ||
|
|
||
| @property | ||
| def from_id(self) -> str: | ||
| """The id of the activity sender.""" | ||
| ... | ||
|
|
||
| @property | ||
| def expiration(self) -> Optional[int]: | ||
| """The expiration of the token since epoch in milliseconds.""" | ||
| ... | ||
|
|
||
| def is_expired(self, buffer_ms: int = 5 * 60 * 1000) -> bool: | ||
| """ | ||
| Check if the token is expired. | ||
|
|
||
| Args: | ||
| buffer_ms: Buffer time in milliseconds (default 5 minutes). | ||
|
|
||
| Returns: | ||
| True if the token is expired, False otherwise. | ||
| """ | ||
| ... | ||
|
|
||
| def __str__(self) -> str: | ||
| """String form of the token.""" | ||
| ... |
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
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
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.