Skip to content

Commit

Permalink
Add support for tenant selection when using AppOnly Microsoft Graph. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
hajekj authored Nov 30, 2020
1 parent 4ab0da4 commit 8324126
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
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;
});
}

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; }
}
}

0 comments on commit 8324126

Please # to comment.