Skip to content
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

ManagedIdentityClient(..., client_capabilities=["cp1"]).acquire_token_for_client(..., claims_challenge="...") #791

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

rayluo
Copy link
Collaborator

@rayluo rayluo commented Feb 12, 2025

This pull request adds the access_token_sha256_to_refresh parameter to the acquire_token_for_client method in the msal.ConfidentialClientApplication class.

This PR meets feature requirements and acceptance criteria.

This PR supports ManagedIdentityClient(..., client_capabilities=["cp1"]).acquire_token_for_client(..., claims_challenge="...").

Currently, the new behavior will be available for Service Fabric. Other Managed Identity v1 providers may pick up those new behaviors soon. After that happens, we will enable this behavior for more Managed Identity code paths.

@rayluo rayluo force-pushed the at-to-refresh branch 3 times, most recently from 2e6dc04 to f05ae3c Compare March 2, 2025 00:13
@rayluo rayluo force-pushed the at-to-refresh branch 2 times, most recently from 92ae1e3 to 05b1424 Compare March 19, 2025 23:14
@rayluo rayluo changed the title ConfidentialClientApplication(...).acquire_token_for_client(..., access_token_sha256_to_refresh=...) ManagedIdentityClient(..., client_capabilities=["cp1"]).acquire_token_for_client(..., claims_challenge="...") Mar 19, 2025
@rayluo rayluo marked this pull request as ready for review March 19, 2025 23:25
@rayluo rayluo requested a review from a team as a code owner March 19, 2025 23:25
@@ -402,6 +432,8 @@ def _obtain_token(http_client, managed_identity, resource):
os.environ["IDENTITY_HEADER"],
os.environ["IDENTITY_SERVER_THUMBPRINT"],
resource,
access_token_sha256_to_refresh=access_token_sha256_to_refresh,

Choose a reason for hiding this comment

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

Suggested change
access_token_sha256_to_refresh=access_token_sha256_to_refresh,
token_sha256_to_refresh=access_token_sha256_to_refresh,

@rayluo according to the spec it is token_sha256_to_refresh

Copy link
Collaborator Author

@rayluo rayluo Mar 21, 2025

Choose a reason for hiding this comment

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

TL;DR: This is not really a problem here, but still thanks for pointing this out.

Long explanation. There are (inevitably?) several internal layers of helpers. Currently, most of the helpers use a name "access_token_sha256_to_refresh", which "access" prefix was influenced by MSAL C++'s official parameter name "AccessTokenToRefresh". But we did use the right name "token_sha256_to_refresh" on the wire when sending out the request. You can confirm that by both the implementation inside _obtain_token_on_service_fabric() and by the test cases.

@@ -563,7 +598,12 @@ def _obtain_token_on_service_fabric(
logger.debug("Obtaining token via managed identity on Azure Service Fabric")
resp = http_client.get(
endpoint,
params={"api-version": "2019-07-01-preview", "resource": resource},
params={k: v for k, v in {
"api-version": "2019-07-01-preview",

Choose a reason for hiding this comment

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

Suggested change
"api-version": "2019-07-01-preview",
"api-version": "2019-07-01-preview",

We just need a final confirmation on this one @rayluo. App Service is setting a new api-version, we want a sign off from SF

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

What kind of sign-off do we need? A confirmation written in email? Or as a PR? (This is my genuine question, because I want to be more effective on this kind of things.)

In this case, I chatted with SF via Teams channel. I can add you into that group chat upon request.

Choose a reason for hiding this comment

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

I do it this way, so we have tracking items closed and partner's agreement captured

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I do it this way, so we have tracking items closed and partner's agreement captured

That work item is now in Done status, but where can we find the partner's agreement captured? The api version topic seems not mentioned, neither in that work item, nor in the spec that work item referenced.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@gladjohn , now that we have that final confirmation, can we resolve this and re-review this PR?

'api-version': '2019-07-01-preview',
'resource': 'R',
'token_sha256_to_refresh': hashlib.sha256(b"AT").hexdigest(),
},

Choose a reason for hiding this comment

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

do we need a test of claims + forceRefresh?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not currently applicable, as MSAL Python does not support forceRefresh. We may revisit this when the circumstances changes in the future.

'token_sha256_to_refresh': hashlib.sha256(b"AT").hexdigest(),
},
headers={'Secret': 'foo'},
)

Choose a reason for hiding this comment

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

can we also add a test case specifically injecting a mismatched hash to verify how the code handles that scenario?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not applicable, unless we would also want to implement CCA.AcquireTokenForClient(...).WithAccessTokenSha256ToRefresh(...) in non-dotnet MSALs.


def test_happy_path(self):
self._test_happy_path(self.app)
def test_happy_path_with_client_capabilities_should_relay_capabilities(self):

Choose a reason for hiding this comment

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

can you also pass an empty list client_capabilities=[], and confirm that xms_cc is omitted? did not see one.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fair point. Added it into the next test case.

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.

2 participants