Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

Commit

Permalink
Merge pull request #415 from microsoft/shem/enable_cae_by_default
Browse files Browse the repository at this point in the history
Enable CAE by Default
  • Loading branch information
baywet authored Aug 21, 2024
2 parents 7489b6f + 25bdc12 commit 84c3d9e
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ coverage.xml
*.py,cover
.hypothesis/
.pytest_cache/
.idea/

# Translations
*.mo
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## [1.1.0] - 2023-10-31

### Added
Enabled CAE by default

## [1.0.0] - 2023-10-31

### Added
Expand Down
2 changes: 1 addition & 1 deletion kiota_authentication_azure/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION: str = '1.0.0'
VERSION: str = '1.1.0'
15 changes: 12 additions & 3 deletions kiota_authentication_azure/azure_identity_access_token_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def __init__(
options: Optional[Dict],
scopes: List[str] = [],
allowed_hosts: List[str] = [],
is_cae_enabled: bool = True,
) -> None:
if not credentials:
raise ValueError("Parameter credentials cannot be null")
Expand All @@ -45,10 +46,13 @@ def __init__(
self._credentials = credentials
self._scopes = scopes
self._options = options
self._is_cae_enabled = is_cae_enabled
self._allowed_hosts_validator = AllowedHostsValidator(allowed_hosts)

async def get_authorization_token(
self, uri: str, additional_authentication_context: Dict[str, Any] = {}
self,
uri: str,
additional_authentication_context: Dict[str, Any] = {},
) -> str:
"""This method is called by the BaseBearerTokenAuthenticationProvider class to get the
access token.
Expand Down Expand Up @@ -97,10 +101,15 @@ async def get_authorization_token(

if self._options:
result = self._credentials.get_token(
*self._scopes, claims=decoded_claim, **self._options
*self._scopes,
claims=decoded_claim,
is_cae_enabled=self._is_cae_enabled,
**self._options
)
else:
result = self._credentials.get_token(*self._scopes, claims=decoded_claim)
result = self._credentials.get_token(
*self._scopes, claims=decoded_claim, is_cae_enabled=self._is_cae_enabled
)

if inspect.isawaitable(result):
result = await result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def __init__(
options: Optional[Dict] = None,
scopes: List[str] = [],
allowed_hosts: List[str] = [],
is_cae_enabled: bool = True,
) -> None:
"""[summary]
Expand All @@ -30,5 +31,7 @@ def __init__(
authentication.
"""
super().__init__(
AzureIdentityAccessTokenProvider(credentials, options, scopes, allowed_hosts)
AzureIdentityAccessTokenProvider(
credentials, options, scopes, allowed_hosts, is_cae_enabled=is_cae_enabled
)
)
17 changes: 10 additions & 7 deletions tests/test_azure_identity_authentication_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
)
from unittest.mock import MagicMock


from .helpers import DummyAsyncAzureTokenCredential, DummySyncAzureTokenCredential


Expand All @@ -24,20 +23,24 @@ async def test_valid_instantiation_without_options():
assert isinstance(auth_provider, AzureIdentityAuthenticationProvider)
assert 'authorization' in request_info.request_headers


@pytest.mark.asyncio
async def test_adds_claim_to_the_token_context(mocker):
credential = DummyAsyncAzureTokenCredential()
mocker.patch.object(credential, 'get_token', autospec=True)
auth_provider = AzureIdentityAuthenticationProvider(credential)

request_info = RequestInformation()
request_info.url = "https://graph.microsoft.com"
await auth_provider.authenticate_request(
request_info,
{"claims": "eyJhY2Nlc3NfdG9rZW4iOnsibmJmIjp7ImVzc2VudGlhbCI6dHJ1ZSwgInZhbHVlIjoiMTY1MjgxMzUwOCJ9fX0="}
)
request_info, {
"claims":
"eyJhY2Nlc3NfdG9rZW4iOnsibmJmIjp7ImVzc2VudGlhbCI6dHJ1ZSwgInZhbHVlIjoiMTY1MjgxMzUwOCJ9fX0="
}
)
assert isinstance(auth_provider, AzureIdentityAuthenticationProvider)
credential.get_token.assert_called_once_with(
'https://graph.microsoft.com/.default',
claims = """{"access_token":{"nbf":{"essential":true, "value":"1652813508"}}}"""
)
claims="""{"access_token":{"nbf":{"essential":true, "value":"1652813508"}}}""",
is_cae_enabled=True
)

0 comments on commit 84c3d9e

Please sign in to comment.