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 support for tenant selection when using AppOnly Microsoft Graph #790

Merged
merged 1 commit into from
Nov 30, 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 @@ -30,10 +30,15 @@ public static T WithScopes<T>(this T baseRequest, params string[] scopes) where
/// <typeparam name="T">Type of the request.</typeparam>
/// <param name="baseRequest">Request.</param>
/// <param name="appOnly">Should the permissions be app only or not.</param>
/// <param name="tenant">Tenant ID or domain for which we want to make the call..</param>
/// <returns></returns>
public static T WithAppOnly<T>(this T baseRequest, bool appOnly = true) where T : IBaseRequest
public static T WithAppOnly<T>(this T baseRequest, bool appOnly = true, string? tenant = null) where T : IBaseRequest
{
return SetParameter(baseRequest, options => options.AppOnly = appOnly);
return SetParameter(baseRequest, options =>
{
options.AppOnly = appOnly;
options.Tenant = tenant;
Copy link
Collaborator

Choose a reason for hiding this comment

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

We might want to throw an ArgumentException if tenant is 'common' or 'organizations' as these should not be used.
cc: @jennyf19, @bgavrilMS

Copy link
Collaborator

Choose a reason for hiding this comment

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

@jmprieur i'll merge but we should implement your suggestion before a release.

});
}

private static T SetParameter<T>(T baseRequest, Action<TokenAcquisitionAuthenticationProviderOption> action) where T : IBaseRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ public async Task AuthenticateRequestAsync(HttpRequestMessage request)
// Default options to settings provided during intialization
var scopes = _initialOptions.Scopes;
bool appOnly = _initialOptions.AppOnly ?? false;
string? tenant = _initialOptions.Tenant ?? null;
// Extract per-request options from the request if present
TokenAcquisitionAuthenticationProviderOption? msalAuthProviderOption = GetMsalAuthProviderOption(request);
if (msalAuthProviderOption != null) {
scopes = msalAuthProviderOption.Scopes ?? scopes;
appOnly = msalAuthProviderOption.AppOnly ?? appOnly;
tenant = msalAuthProviderOption.Tenant ?? tenant;
}

if (!appOnly && scopes == null)
Expand All @@ -50,7 +52,7 @@ public async Task AuthenticateRequestAsync(HttpRequestMessage request)
string token;
if (appOnly)
{
token = await _tokenAcquisition.GetAccessTokenForAppAsync(Constants.DefaultGraphScope).ConfigureAwait(false);
token = await _tokenAcquisition.GetAccessTokenForAppAsync(Constants.DefaultGraphScope, tenant).ConfigureAwait(false);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ internal class TokenAcquisitionAuthenticationProviderOption : IAuthenticationPro
{
public string[]? Scopes { get; set; }
public bool? AppOnly { get; set; }
public string? Tenant { get; set; }
}
}