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

add token acquisition options #596

Merged
merged 10 commits into from
Sep 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ public async Task<HttpResponseMessage> CallWebApiForUserAsync(
string accessToken = await _tokenAcquisition.GetAccessTokenForUserAsync(
effectiveOptions.GetScopes(),
effectiveOptions.Tenant,
userflow)
userflow,
user,
Copy link
Collaborator

@jmprieur jmprieur Sep 21, 2020

Choose a reason for hiding this comment

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

good catch for missing user #Resolved

effectiveOptions.TokenAcquisitionOptions)
.ConfigureAwait(false);

HttpResponseMessage response;
Expand Down Expand Up @@ -175,7 +177,8 @@ public async Task<HttpResponseMessage> CallWebApiForAppAsync(

string accessToken = await _tokenAcquisition.GetAccessTokenForAppAsync(
effectiveOptions.Scopes,
effectiveOptions.Tenant)
effectiveOptions.Tenant,
effectiveOptions.TokenAcquisitionOptions)
.ConfigureAwait(false);

HttpResponseMessage response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public class DownstreamWebApiOptions
/// </summary>
public HttpMethod HttpMethod { get; set; } = HttpMethod.Get;

/// <summary>
/// Options passed-in to create the token acquisition object which calls into MSAL .NET.
Copy link
Collaborator

@jmprieur jmprieur Sep 21, 2020

Choose a reason for hiding this comment

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

token acquisition options object ? #Resolved

/// </summary>
public TokenAcquisitionOptions TokenAcquisitionOptions { get; set; } = new TokenAcquisitionOptions();

/// <summary>
/// Clone the options (to be able to override them).
/// </summary>
Expand All @@ -60,6 +65,7 @@ public DownstreamWebApiOptions Clone()
Tenant = Tenant,
UserFlow = UserFlow,
HttpMethod = HttpMethod,
TokenAcquisitionOptions = TokenAcquisitionOptions.Clone(),
};
}

Expand Down
14 changes: 11 additions & 3 deletions src/Microsoft.Identity.Web/ITokenAcquisition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ public interface ITokenAcquisition
/// <param name="user">Optional claims principal representing the user. If not provided, will use the signed-in
/// user (in a web app), or the user for which the token was received (in a web API)
/// cases where a given account is guest in other tenants, and you want to acquire tokens for a specific tenant, like where the user is a guest in.</param>
/// <param name="tokenAcquisitionOptions">Options passed-in to create the token acquisition object which calls into MSAL .NET.</param>
/// <returns>An access token to call on behalf of the user, the downstream API characterized by its scopes.</returns>
Task<string> GetAccessTokenForUserAsync(
IEnumerable<string> scopes,
string? tenantId = null,
string? userFlow = null,
ClaimsPrincipal? user = null);
ClaimsPrincipal? user = null,
TokenAcquisitionOptions? tokenAcquisitionOptions = null);

/// <summary>
/// Typically used from an ASP.NET Core web app or web API controller, this method gets an access token
Expand All @@ -45,12 +47,14 @@ Task<string> GetAccessTokenForUserAsync(
/// <param name="user">Optional claims principal representing the user. If not provided, will use the signed-in
/// user (in a web app), or the user for which the token was received (in a web API)
/// cases where a given account is a guest in other tenants, and you want to acquire tokens for a specific tenant, like where the user is a guest in.</param>
/// <param name="tokenAcquisitionOptions">Options passed-in to create the token acquisition object which calls into MSAL .NET.</param>
/// <returns>An <see cref="AuthenticationResult"/> to call on behalf of the user, the downstream API characterized by its scopes.</returns>
Task<AuthenticationResult> GetAuthenticationResultForUserAsync(
IEnumerable<string> scopes,
string? tenantId = null,
string? userFlow = null,
ClaimsPrincipal? user = null);
ClaimsPrincipal? user = null,
TokenAcquisitionOptions? tokenAcquisitionOptions = null);

/// <summary>
/// Acquires a token from the authority configured in the app, for the confidential client itself (not on behalf of a user)
Expand All @@ -63,8 +67,12 @@ Task<AuthenticationResult> GetAuthenticationResultForUserAsync(
/// several calls to get tokens for other resources).</param>
/// <param name="tenant">Enables overriding of the tenant/account for the same identity. This is useful in the
/// cases where a given account is a guest in other tenants, and you want to acquire tokens for a specific tenant.</param>
/// <param name="tokenAcquisitionOptions">Options passed-in to create the token acquisition object which calls into MSAL .NET.</param>
/// <returns>An access token for the app itself, based on its scopes.</returns>
Task<string> GetAccessTokenForAppAsync(string scope, string? tenant = null);
Task<string> GetAccessTokenForAppAsync(
string scope,
string? tenant = null,
TokenAcquisitionOptions? tokenAcquisitionOptions = null);

/// <summary>
/// Used in web APIs (which therefore cannot have an interaction with the user).
Expand Down
58 changes: 50 additions & 8 deletions src/Microsoft.Identity.Web/Microsoft.Identity.Web.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading