-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Add bearer token provider #32655
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 bearer token provider #32655
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
13c5a3f
Add bearer token provider
lmazuel 9cf8430
Only creates the policy once
lmazuel a092c05
Bump azure-core for typing
lmazuel 6454f84
black
xiangyan99 6809234
Revert "black"
xiangyan99 253e748
black
xiangyan99 37584ee
Feedback
lmazuel 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
46 changes: 46 additions & 0 deletions
46
sdk/identity/azure-identity/azure/identity/_bearer_token_provider.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,46 @@ | ||
| # ------------------------------------ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
| # ------------------------------------ | ||
| from typing import Callable | ||
|
|
||
| from azure.core.credentials import TokenCredential | ||
| from azure.core.pipeline.policies import BearerTokenCredentialPolicy | ||
| from azure.core.pipeline import PipelineRequest, PipelineContext | ||
| from azure.core.rest import HttpRequest | ||
|
|
||
|
|
||
| def _make_request() -> PipelineRequest[HttpRequest]: | ||
| return PipelineRequest(HttpRequest("CredentialWrapper", "https://fakeurl"), PipelineContext(None)) | ||
|
|
||
|
|
||
| def get_bearer_token_provider(credential: TokenCredential, *scopes: str) -> Callable[[], str]: | ||
| """Returns a callable that provides a bearer token. | ||
|
|
||
| It can be used for instance to write code like: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| from azure.identity import DefaultAzureCredential, get_bearer_token_provider | ||
|
|
||
| credential = DefaultAzureCredential() | ||
| bearer_token_provider = get_bearer_token_provider(credential, "https://storage.azure.com/.default") | ||
|
|
||
| # Usage | ||
| request.headers["Authorization"] = "Bearer " + bearer_token_provider() | ||
|
|
||
| :param credential: The credential used to authenticate the request. | ||
| :type credential: ~azure.core.credentials.TokenCredential | ||
| :param str scopes: The scopes required for the bearer token. | ||
| :rtype: callable | ||
| :return: A callable that returns a bearer token. | ||
| """ | ||
|
|
||
| policy = BearerTokenCredentialPolicy(credential, *scopes) | ||
|
|
||
| def wrapper() -> str: | ||
| request = _make_request() | ||
| policy.on_request(request) | ||
| return request.http_request.headers["Authorization"][len("Bearer ") :] | ||
|
|
||
| return wrapper | ||
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
47 changes: 47 additions & 0 deletions
47
sdk/identity/azure-identity/azure/identity/aio/_bearer_token_provider.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,47 @@ | ||
| # ------------------------------------ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
| # ------------------------------------ | ||
| from typing import Callable, Coroutine, Any | ||
|
|
||
| from azure.core.credentials_async import AsyncTokenCredential | ||
| from azure.core.pipeline.policies import AsyncBearerTokenCredentialPolicy | ||
| from azure.core.pipeline import PipelineRequest, PipelineContext | ||
| from azure.core.rest import HttpRequest | ||
|
|
||
|
|
||
| def _make_request() -> PipelineRequest[HttpRequest]: | ||
| return PipelineRequest(HttpRequest("CredentialWrapper", "https://fakeurl"), PipelineContext(None)) | ||
|
|
||
|
|
||
| def get_bearer_token_provider(credential: AsyncTokenCredential, *scopes: str) -> Callable[[], Coroutine[Any, Any, str]]: | ||
| """Returns a callable that provides a bearer token. | ||
|
|
||
| It can be used for instance to write code like: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| from azure.identity.aio import DefaultAzureCredential, get_bearer_token_provider | ||
|
|
||
| credential = DefaultAzureCredential() | ||
| bearer_token_provider = get_bearer_token_provider(credential, "https://storage.azure.com/.default") | ||
|
|
||
|
|
||
| # Usage | ||
| request.headers["Authorization"] = "Bearer " + await bearer_token_provider() | ||
|
|
||
| :param credential: The credential used to authenticate the request. | ||
| :type credential: ~azure.core.credentials.TokenCredential | ||
|
lmazuel marked this conversation as resolved.
Outdated
|
||
| :param str scopes: The scopes required for the bearer token. | ||
| :rtype: coroutine | ||
| :return: A coroutine that returns a bearer token. | ||
| """ | ||
|
|
||
| policy = AsyncBearerTokenCredentialPolicy(credential, *scopes) | ||
|
|
||
| async def wrapper() -> str: | ||
| request = _make_request() | ||
| await policy.on_request(request) | ||
| return request.http_request.headers["Authorization"][len("Bearer ") :] | ||
|
|
||
| return wrapper | ||
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
20 changes: 20 additions & 0 deletions
20
sdk/identity/azure-identity/tests/test_bearer_token_provider.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,20 @@ | ||
| # ------------------------------------ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
| # ------------------------------------ | ||
|
|
||
| from azure.core.credentials import AccessToken | ||
| from azure.identity import get_bearer_token_provider | ||
|
|
||
|
|
||
| class MockCredential: | ||
| def get_token(self, *scopes, **kwargs): | ||
| assert len(scopes) == 1 | ||
| assert scopes[0] == "scope" | ||
| return AccessToken("mock_token", 42) | ||
|
|
||
|
|
||
| def test_get_bearer_token_provider(): | ||
|
|
||
| func = get_bearer_token_provider(MockCredential(), "scope") | ||
| assert func() == "mock_token" |
23 changes: 23 additions & 0 deletions
23
sdk/identity/azure-identity/tests/test_bearer_token_provider_async.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,23 @@ | ||
| # ------------------------------------ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
| # ------------------------------------ | ||
|
|
||
| from azure.core.credentials import AccessToken | ||
| from azure.identity.aio import get_bearer_token_provider | ||
|
|
||
| import pytest | ||
|
|
||
|
|
||
| class MockCredential: | ||
| async def get_token(self, *scopes, **kwargs): | ||
| assert len(scopes) == 1 | ||
| assert scopes[0] == "scope" | ||
| return AccessToken("mock_token", 42) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_get_bearer_token_provider(): | ||
|
|
||
| func = get_bearer_token_provider(MockCredential(), "scope") | ||
| assert await func() == "mock_token" |
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.