-
Notifications
You must be signed in to change notification settings - Fork 400
Add agentic ID support #5883
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
Draft
Avery-Dunn
wants to merge
8
commits into
main
Choose a base branch
from
avdunn/agent-identity-apis
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add agentic ID support #5883
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
588c935
Add agent ID support
Avery-Dunn 0950e3f
Merge branch 'main' into avdunn/agent-identity-apis
Avery-Dunn 9f5ea5c
Improve caching behavior for agent scenarios
Avery-Dunn 5f86f9a
Merge branch 'avdunn/agent-identity-apis' of https://github.com/Azure…
Avery-Dunn 9462880
Improve unit test coverage of caching behavior
Avery-Dunn a1c2246
Simplify internal CCA behavior and improve readability
Avery-Dunn d67bd28
Propagate app-level and request-level parameters
Avery-Dunn ed0ceb7
Various PR feedback items
Avery-Dunn 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
|
|
||
| namespace Microsoft.Identity.Client | ||
| { | ||
| /// <summary> | ||
| /// Represents the identity of an agent application and the user it acts on behalf of. | ||
| /// Used with <see cref="IConfidentialClientApplication.AcquireTokenForAgent(System.Collections.Generic.IEnumerable{string}, AgentIdentity)"/> | ||
| /// to acquire tokens for agent scenarios using Federated Managed Identity (FMI) and User Federated Identity Credentials (UserFIC). | ||
| /// </summary> | ||
| public sealed class AgentIdentity | ||
| { | ||
| private AgentIdentity(string agentApplicationId) | ||
| { | ||
| if (string.IsNullOrEmpty(agentApplicationId)) | ||
| { | ||
| throw new ArgumentNullException(nameof(agentApplicationId)); | ||
| } | ||
|
|
||
| AgentApplicationId = agentApplicationId; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates an <see cref="AgentIdentity"/> that identifies the user by their object ID (OID). | ||
| /// This is the recommended approach for identifying users in agent scenarios. | ||
| /// </summary> | ||
| /// <param name="agentApplicationId">The client ID of the agent application.</param> | ||
| /// <param name="userObjectId">The object ID (OID) of the user the agent acts on behalf of.</param> | ||
| /// <returns>An <see cref="AgentIdentity"/> configured with the user's OID.</returns> | ||
| public AgentIdentity(string agentApplicationId, Guid userObjectId) | ||
| : this(agentApplicationId) | ||
| { | ||
| if (userObjectId == Guid.Empty) | ||
| { | ||
| throw new ArgumentException("userObjectId must not be empty.", nameof(userObjectId)); | ||
| } | ||
|
|
||
| UserObjectId = userObjectId; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates an <see cref="AgentIdentity"/> that identifies the user by their UPN (User Principal Name). | ||
| /// </summary> | ||
| /// <param name="agentApplicationId">The client ID of the agent application.</param> | ||
| /// <param name="username">The UPN of the user the agent acts on behalf of.</param> | ||
| /// <returns>An <see cref="AgentIdentity"/> configured with the user's UPN.</returns> | ||
| public static AgentIdentity WithUsername(string agentApplicationId, string username) | ||
| { | ||
| if (string.IsNullOrEmpty(username)) | ||
| { | ||
| throw new ArgumentNullException(nameof(username)); | ||
| } | ||
|
|
||
| return new AgentIdentity(agentApplicationId) | ||
| { | ||
| Username = username | ||
| }; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates an <see cref="AgentIdentity"/> for app-only (no user) scenarios, where only Legs 1-2 of the | ||
| /// agent token acquisition are performed. | ||
| /// </summary> | ||
| /// <param name="agentApplicationId">The client ID of the agent application.</param> | ||
| /// <returns>An <see cref="AgentIdentity"/> configured for app-only access.</returns> | ||
| public static AgentIdentity AppOnly(string agentApplicationId) | ||
| { | ||
| return new AgentIdentity(agentApplicationId); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the client ID of the agent application. | ||
| /// </summary> | ||
| public string AgentApplicationId { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the object ID (OID) of the user, if specified. | ||
| /// </summary> | ||
| public Guid? UserObjectId { get; private set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the UPN of the user, if specified. | ||
| /// </summary> | ||
| public string Username { get; private set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets a value indicating whether this identity includes a user identifier (OID or UPN). | ||
| /// </summary> | ||
| internal bool HasUserIdentifier => UserObjectId.HasValue || !string.IsNullOrEmpty(Username); | ||
| } | ||
| } |
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
104 changes: 104 additions & 0 deletions
104
src/client/Microsoft.Identity.Client/ApiConfig/AcquireTokenForAgentParameterBuilder.cs
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,104 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Identity.Client.ApiConfig.Executors; | ||
| using Microsoft.Identity.Client.ApiConfig.Parameters; | ||
| using Microsoft.Identity.Client.TelemetryCore.Internal.Events; | ||
|
|
||
| namespace Microsoft.Identity.Client | ||
| { | ||
| /// <summary> | ||
| /// Builder for AcquireTokenForAgent, used to acquire tokens for agent scenarios involving | ||
| /// Federated Managed Identity (FMI) and User Federated Identity Credentials (UserFIC). | ||
| /// This orchestrates the multi-leg token acquisition automatically. | ||
| /// </summary> | ||
| #if !SUPPORTS_CONFIDENTIAL_CLIENT | ||
| [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] // hide confidential client on mobile | ||
| #endif | ||
| public sealed class AcquireTokenForAgentParameterBuilder : | ||
| AbstractConfidentialClientAcquireTokenParameterBuilder<AcquireTokenForAgentParameterBuilder> | ||
| { | ||
| internal AcquireTokenForAgentParameters Parameters { get; } = new AcquireTokenForAgentParameters(); | ||
|
|
||
| /// <inheritdoc/> | ||
| internal AcquireTokenForAgentParameterBuilder( | ||
| IConfidentialClientApplicationExecutor confidentialClientApplicationExecutor, | ||
| AgentIdentity agentIdentity) | ||
| : base(confidentialClientApplicationExecutor) | ||
| { | ||
| Parameters.AgentIdentity = agentIdentity; | ||
| } | ||
|
|
||
| internal static AcquireTokenForAgentParameterBuilder Create( | ||
| IConfidentialClientApplicationExecutor confidentialClientApplicationExecutor, | ||
| IEnumerable<string> scopes, | ||
| AgentIdentity agentIdentity) | ||
| { | ||
| if (agentIdentity == null) | ||
| { | ||
| throw new ArgumentNullException(nameof(agentIdentity)); | ||
| } | ||
|
|
||
| return new AcquireTokenForAgentParameterBuilder( | ||
| confidentialClientApplicationExecutor, | ||
| agentIdentity) | ||
| .WithScopes(scopes); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Specifies if the client application should ignore access tokens when reading the token cache. | ||
| /// New tokens will still be written to the token cache. | ||
| /// By default the token is taken from the cache (forceRefresh=false). | ||
| /// </summary> | ||
| /// <param name="forceRefresh"> | ||
| /// If <c>true</c>, the request will ignore cached access tokens on read, but will still write them to the cache once obtained from the identity provider. The default is <c>false</c>. | ||
| /// </param> | ||
| /// <returns>The builder to chain the .With methods.</returns> | ||
| public AcquireTokenForAgentParameterBuilder WithForceRefresh(bool forceRefresh) | ||
| { | ||
| Parameters.ForceRefresh = forceRefresh; | ||
| return this; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Specifies if the x5c claim (public key of the certificate) should be sent to the identity provider, | ||
| /// which enables subject name/issuer based authentication for the client credential. | ||
| /// This is useful for certificate rollover scenarios. See https://aka.ms/msal-net-sni. | ||
| /// </summary> | ||
| /// <param name="withSendX5C"><c>true</c> if the x5c should be sent. Otherwise <c>false</c>. | ||
| /// The default is <c>false</c>.</param> | ||
| /// <returns>The builder to chain the .With methods.</returns> | ||
| public AcquireTokenForAgentParameterBuilder WithSendX5C(bool withSendX5C) | ||
| { | ||
| Parameters.SendX5C = withSendX5C; | ||
| return this; | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| internal override Task<AuthenticationResult> ExecuteInternalAsync(CancellationToken cancellationToken) | ||
| { | ||
| return ConfidentialClientApplicationExecutor.ExecuteAsync(CommonParameters, Parameters, cancellationToken); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| protected override void Validate() | ||
| { | ||
| base.Validate(); | ||
|
|
||
| if (Parameters.SendX5C == null) | ||
| { | ||
| Parameters.SendX5C = this.ServiceBundle.Config.SendX5C; | ||
| } | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| internal override ApiEvent.ApiIds CalculateApiEventId() | ||
| { | ||
| return ApiEvent.ApiIds.AcquireTokenForAgent; | ||
| } | ||
| } | ||
| } |
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
13 changes: 13 additions & 0 deletions
13
...ty.Client/ApiConfig/Parameters/AcquireTokenByUserFederatedIdentityCredentialParameters.cs
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 |
|---|---|---|
| @@ -1,20 +1,33 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Text; | ||
| using Microsoft.Identity.Client.Core; | ||
|
|
||
| namespace Microsoft.Identity.Client.ApiConfig.Parameters | ||
| { | ||
| internal class AcquireTokenByUserFederatedIdentityCredentialParameters : IAcquireTokenParameters | ||
| { | ||
| public string Username { get; set; } | ||
| public Guid? UserObjectId { get; set; } | ||
| public string Assertion { get; set; } | ||
| public bool? SendX5C { get; set; } | ||
| public bool ForceRefresh { get; set; } | ||
|
|
||
| /// <inheritdoc/> | ||
| public void LogParameters(ILoggerAdapter logger) | ||
| { | ||
| if (logger.IsLoggingEnabled(LogLevel.Info)) | ||
| { | ||
| var builder = new StringBuilder(); | ||
| builder.AppendLine("=== AcquireTokenByUserFederatedIdentityCredentialParameters ==="); | ||
| builder.AppendLine("SendX5C: " + SendX5C); | ||
| builder.AppendLine("ForceRefresh: " + ForceRefresh); | ||
| builder.AppendLine("UserIdentifiedByOid: " + UserObjectId.HasValue); | ||
| builder.AppendLine("Assertion set: " + !string.IsNullOrEmpty(Assertion)); | ||
| logger.Info(builder.ToString()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
30 changes: 30 additions & 0 deletions
30
src/client/Microsoft.Identity.Client/ApiConfig/Parameters/AcquireTokenForAgentParameters.cs
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,30 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Text; | ||
| using Microsoft.Identity.Client.Core; | ||
|
|
||
| namespace Microsoft.Identity.Client.ApiConfig.Parameters | ||
| { | ||
| internal class AcquireTokenForAgentParameters : AbstractAcquireTokenConfidentialClientParameters, IAcquireTokenParameters | ||
| { | ||
| public AgentIdentity AgentIdentity { get; set; } | ||
|
|
||
| public bool ForceRefresh { get; set; } | ||
|
|
||
| /// <inheritdoc/> | ||
| public void LogParameters(ILoggerAdapter logger) | ||
| { | ||
| if (logger.IsLoggingEnabled(LogLevel.Info)) | ||
| { | ||
| var builder = new StringBuilder(); | ||
| builder.AppendLine("=== AcquireTokenForAgentParameters ==="); | ||
| builder.AppendLine("SendX5C: " + SendX5C); | ||
| builder.AppendLine("ForceRefresh: " + ForceRefresh); | ||
| builder.AppendLine("AgentApplicationId: " + AgentIdentity?.AgentApplicationId); | ||
| builder.AppendLine("HasUserIdentifier: " + (AgentIdentity?.HasUserIdentifier ?? false)); | ||
| logger.Info(builder.ToString()); | ||
Avery-Dunn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Log username as well