Skip to content

Commit

Permalink
Add pop token support (#37482)
Browse files Browse the repository at this point in the history
* Add pop token support

* Update sdk/identity/azure-identity-broker/CHANGELOG.md

Co-authored-by: Paul Van Eck <paulvaneck@microsoft.com>

* rename sample

* update changelog

* Update sdk/identity/azure-identity-broker/CHANGELOG.md

Co-authored-by: Paul Van Eck <paulvaneck@microsoft.com>

* unblock this PR. fix incoming in 37450

* Update version

Signed-off-by: Paul Van Eck <paulvaneck@microsoft.com>

---------

Signed-off-by: Paul Van Eck <paulvaneck@microsoft.com>
Co-authored-by: Paul Van Eck <paulvaneck@microsoft.com>
Co-authored-by: Scott Beddall <scbedd@microsoft.com>
  • Loading branch information
3 people authored Sep 20, 2024
1 parent 22c2d80 commit b551e02
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 12 deletions.
9 changes: 3 additions & 6 deletions sdk/identity/azure-identity-broker/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
# Release History

## 1.1.1 (Unreleased)
## 1.2.0b1 (2024-09-20)

### Features Added

### Breaking Changes

### Bugs Fixed

### Other Changes
- `InteractiveBrowserBrokerCredential` now implements the `SupportsTokenInfo` protocol. It now has a `get_token_info` method which returns an `AccessTokenInfo` object. The `get_token_info` method is an alternative method to `get_token` that improves support for more complex authentication scenarios.
- Added Proof-of-Possession (PoP) token support to `InteractiveBrowserBrokerCredential`.

## 1.1.0 (2024-04-09)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
from ._browser import InteractiveBrowserBrokerCredential
from ._browser import InteractiveBrowserBrokerCredential, PopTokenRequestOptions


__all__ = [
"InteractiveBrowserBrokerCredential",
"PopTokenRequestOptions",
]
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
# Licensed under the MIT License.
# ------------------------------------
import socket
from typing import Dict, Any
from typing import Dict, Any, Mapping, Union
import msal

from azure.core.exceptions import ClientAuthenticationError
from azure.core.credentials import TokenRequestOptions
from azure.identity._credentials import (
InteractiveBrowserCredential as _InteractiveBrowserCredential,
) # pylint:disable=protected-access
Expand All @@ -15,6 +16,12 @@
from ._utils import wrap_exceptions, resolve_tenant


class PopTokenRequestOptions(TokenRequestOptions):
"""Options to use for pop token requests."""

pop: Union[bool, Mapping[str, str]]


class InteractiveBrowserBrokerCredential(_InteractiveBrowserCredential):
"""Uses an authentication broker to interactively sign in a user.
Expand Down Expand Up @@ -64,8 +71,14 @@ def __init__(self, **kwargs: Any) -> None:
def _request_token(self, *scopes: str, **kwargs: Any) -> Dict:
scopes = list(scopes) # type: ignore
claims = kwargs.get("claims")
pop = kwargs.get("pop")
app = self._get_app(**kwargs)
port = self._parsed_url.port if self._parsed_url else None
auth_scheme = None
if pop:
auth_scheme = msal.PopAuthScheme(
http_method=pop["resource_request_method"], url=pop["resource_request_url"], nonce=pop["nonce"]
)

if self._use_default_broker_account:
try:
Expand All @@ -78,6 +91,7 @@ def _request_token(self, *scopes: str, **kwargs: Any) -> Dict:
port=port,
parent_window_handle=self._parent_window_handle,
enable_msa_passthrough=self._enable_msa_passthrough,
auth_scheme=auth_scheme,
)
if "access_token" in result:
return result
Expand All @@ -93,6 +107,7 @@ def _request_token(self, *scopes: str, **kwargs: Any) -> Dict:
port=port,
parent_window_handle=self._parent_window_handle,
enable_msa_passthrough=self._enable_msa_passthrough,
auth_scheme=auth_scheme,
)
except socket.error as ex:
raise CredentialUnavailableError(message="Couldn't start an HTTP server.") from ex
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
VERSION = "1.1.1"
VERSION = "1.2.0b1"
1 change: 1 addition & 0 deletions sdk/identity/azure-identity-broker/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[tool.azure-sdk-build]
type_check_samples = false
pyright = false
mindependency = false
24 changes: 24 additions & 0 deletions sdk/identity/azure-identity-broker/samples/pop_sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
"""
This sample is intended to show how to get a Proof-of-Possession (PoP) token.
"""

from azure.identity.broker import PopTokenRequestOptions, InteractiveBrowserBrokerCredential

nonce = "nonce" # needs to be a valid nonce
resource_request_url = "url" # needs to be a valid URL
resource_request_method = "GET" # needs to be a valid HTTP method
request_options = PopTokenRequestOptions(
{
"pop": {
"nonce": nonce,
"resource_request_url": resource_request_url,
"resource_request_method": resource_request_method,
}
}
)
cred = InteractiveBrowserBrokerCredential(parent_window_handle="window_handle")
pop_token = cred.get_token_info("scope", options=request_options)
6 changes: 3 additions & 3 deletions sdk/identity/azure-identity-broker/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
url="https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity-broker",
keywords="azure, azure sdk",
classifiers=[
"Development Status :: 5 - Production/Stable",
"Development Status :: 4 - Beta",
"Programming Language :: Python",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3",
Expand All @@ -62,7 +62,7 @@
},
python_requires=">=3.8",
install_requires=[
"azure-identity<2.0.0,>=1.15.0",
"msal[broker]>=1.25,<2",
"azure-identity<2.0.0,>=1.18.0",
"msal[broker]>=1.31,<2",
],
)

0 comments on commit b551e02

Please sign in to comment.