-
Notifications
You must be signed in to change notification settings - Fork 218
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
renaming #701
Merged
jmprieur
merged 3 commits into
jmprieur/EasyAuthWitoutTestProject
from
jennyf/easyAuthName
Oct 20, 2020
Merged
renaming #701
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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
95 changes: 95 additions & 0 deletions
95
src/Microsoft.Identity.Web/AppServicesAuth/AppServicesAuthenticationInformation.cs
This file contains 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,95 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Linq; | ||
|
||
namespace Microsoft.Identity.Web | ||
{ | ||
/// <summary> | ||
/// Information about the App Services configuration on the host. | ||
/// </summary> | ||
public static class AppServicesAuthenticationInformation | ||
{ | ||
// Environment variables. | ||
private const string AppServicesAuthEnabledEnvironmentVariable = "WEBSITE_AUTH_ENABLED"; // True | ||
private const string AppServicesAuthOpenIdIssuerEnvironmentVariable = "WEBSITE_AUTH_OPENID_ISSUER"; // for instance https://sts.windows.net/<tenantId>/ | ||
private const string AppServicesAuthClientIdEnvironmentVariable = "WEBSITE_AUTH_CLIENT_ID"; // A GUID | ||
private const string AppServicesAuthClientSecretEnvironmentVariable = "WEBSITE_AUTH_CLIENT_SECRET"; // A string | ||
private const string AppServicesAuthLogoutPathEnvironmentVariable = "WEBSITE_AUTH_LOGOUT_PATH"; // /.auth/logout | ||
private const string AppServicesAuthIdentityProviderEnvironmentVariable = "WEBSITE_AUTH_DEFAULT_PROVIDER"; // AzureActiveDirectory | ||
private const string AppServicesAuthAzureActiveDirectory = "AzureActiveDirectory"; | ||
|
||
// Artificially added by Microsoft.Identity.Web to help debugging App Services. See the Debug controller of the test app | ||
private const string AppServicesAuthDebugHeadersEnvironmentVariable = "APP_SERVICES_AUTH_LOCAL_DEBUG"; | ||
|
||
/// <summary> | ||
/// Is App Services authentication enabled?. | ||
/// </summary> | ||
public static bool IsAppServicesAadAuthenticationEnabled | ||
{ | ||
get | ||
{ | ||
return (Environment.GetEnvironmentVariable(AppServicesAuthEnabledEnvironmentVariable) == Constants.True) | ||
&& Environment.GetEnvironmentVariable(AppServicesAuthIdentityProviderEnvironmentVariable) == AppServicesAuthAzureActiveDirectory; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Logout URL for App Services Auth web sites. | ||
/// </summary> | ||
public static string? LogoutUrl | ||
{ | ||
get | ||
{ | ||
return Environment.GetEnvironmentVariable(AppServicesAuthLogoutPathEnvironmentVariable); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// ClientID of the App Services Auth web site. | ||
/// </summary> | ||
internal static string? ClientId | ||
{ | ||
get | ||
{ | ||
return Environment.GetEnvironmentVariable(AppServicesAuthClientIdEnvironmentVariable); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Client secret of the App Services Auth web site. | ||
/// </summary> | ||
internal static string? ClientSecret | ||
{ | ||
get | ||
{ | ||
return Environment.GetEnvironmentVariable(AppServicesAuthClientSecretEnvironmentVariable); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Issuer of the App Services Auth web site. | ||
/// </summary> | ||
internal static string? Issuer | ||
{ | ||
get | ||
{ | ||
return Environment.GetEnvironmentVariable(AppServicesAuthOpenIdIssuerEnvironmentVariable); | ||
} | ||
} | ||
|
||
#if DEBUG | ||
/// <summary> | ||
/// Get headers from environment to help debugging App Services authentication. | ||
/// </summary> | ||
internal static string? SimulateGetttingHeaderFromDebugEnvironmentVariable(string header) | ||
{ | ||
string? headerPlusValue = Environment.GetEnvironmentVariable(AppServicesAuthDebugHeadersEnvironmentVariable) | ||
?.Split(';') | ||
?.FirstOrDefault(h => h.StartsWith(header)); | ||
return headerPlusValue?.Substring(header.Length + 1); | ||
} | ||
#endif | ||
} | ||
} |
This file contains 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 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 |
---|---|---|
|
@@ -13,14 +13,14 @@ | |
namespace Microsoft.Identity.Web | ||
{ | ||
/// <summary> | ||
/// Implementation of ITokenAcquisition for App services authentication (EasyAuth). | ||
/// Implementation of ITokenAcquisition for App Services authentication (EasyAuth). | ||
/// </summary> | ||
public class AppServicesAuthenticationTokenAcquisition : ITokenAcquisition | ||
{ | ||
private IConfidentialClientApplication _confidentialClientApplication; | ||
private IHttpContextAccessor _httpContextAccessor; | ||
private IMsalHttpClientFactory _httpClientFactory; | ||
private IMsalTokenCacheProvider _tokenCacheProvider; | ||
private IConfidentialClientApplication? _confidentialClientApplication; | ||
private readonly IHttpContextAccessor _httpContextAccessor; | ||
private readonly IMsalHttpClientFactory _httpClientFactory; | ||
private readonly IMsalTokenCacheProvider _tokenCacheProvider; | ||
|
||
private HttpContext? CurrentHttpContext | ||
{ | ||
|
@@ -36,7 +36,10 @@ private HttpContext? CurrentHttpContext | |
/// <param name="tokenCacheProvider">The App token cache provider.</param> | ||
/// <param name="httpContextAccessor">Access to the HttpContext of the request.</param> | ||
/// <param name="httpClientFactory">HTTP client factory.</param> | ||
public AppServicesAuthenticationTokenAcquisition(IMsalTokenCacheProvider tokenCacheProvider, IHttpContextAccessor httpContextAccessor, IHttpClientFactory httpClientFactory) | ||
public AppServicesAuthenticationTokenAcquisition( | ||
IMsalTokenCacheProvider tokenCacheProvider, | ||
IHttpContextAccessor httpContextAccessor, | ||
IHttpClientFactory httpClientFactory) | ||
{ | ||
_httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor)); | ||
_httpClientFactory = new MsalAspNetCoreHttpClientFactory(httpClientFactory); | ||
|
@@ -49,16 +52,17 @@ private async Task<IConfidentialClientApplication> GetOrCreateApplication() | |
{ | ||
ConfidentialClientApplicationOptions options = new ConfidentialClientApplicationOptions() | ||
{ | ||
ClientId = AppServiceAuthenticationInformation.ClientId, | ||
ClientSecret = AppServiceAuthenticationInformation.ClientSecret, | ||
Instance = AppServiceAuthenticationInformation.Issuer, | ||
ClientId = AppServicesAuthenticationInformation.ClientId, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should consider some refactoring in tokenAcqusition so we don't have to repeat this code. not something for this PR, but something for the future we shoudl consider. |
||
ClientSecret = AppServicesAuthenticationInformation.ClientSecret, | ||
Instance = AppServicesAuthenticationInformation.Issuer, | ||
}; | ||
_confidentialClientApplication = ConfidentialClientApplicationBuilder.CreateWithApplicationOptions(options) | ||
.WithHttpClientFactory(_httpClientFactory) | ||
.Build(); | ||
await _tokenCacheProvider.InitializeAsync(_confidentialClientApplication.AppTokenCache).ConfigureAwait(false); | ||
await _tokenCacheProvider.InitializeAsync(_confidentialClientApplication.UserTokenCache).ConfigureAwait(false); | ||
} | ||
|
||
return _confidentialClientApplication; | ||
} | ||
|
||
|
@@ -74,7 +78,7 @@ public async Task<string> GetAccessTokenForAppAsync( | |
throw new ArgumentNullException(nameof(scope)); | ||
} | ||
|
||
var app = await GetOrCreateApplication(); | ||
var app = await GetOrCreateApplication().ConfigureAwait(false); | ||
AuthenticationResult result = await app.AcquireTokenForClient(new string[] { scope }) | ||
.ExecuteAsync() | ||
.ConfigureAwait(false); | ||
|
@@ -83,28 +87,39 @@ public async Task<string> GetAccessTokenForAppAsync( | |
} | ||
|
||
/// <inheritdoc/> | ||
public Task<string> GetAccessTokenForUserAsync(IEnumerable<string> scopes, string? tenantId = null, string? userFlow = null, ClaimsPrincipal? user = null, TokenAcquisitionOptions? tokenAcquisitionOptions = null) | ||
public async Task<string> GetAccessTokenForUserAsync( | ||
IEnumerable<string> scopes, | ||
string? tenantId = null, | ||
string? userFlow = null, | ||
ClaimsPrincipal? user = null, | ||
TokenAcquisitionOptions? tokenAcquisitionOptions = null) | ||
{ | ||
string accessToken = GetAccessToken(CurrentHttpContext.Request.Headers); | ||
return Task.FromResult(accessToken); | ||
string accessToken = GetAccessToken(CurrentHttpContext?.Request.Headers); | ||
|
||
return await Task.FromResult(accessToken).ConfigureAwait(false); | ||
} | ||
|
||
private string? GetAccessToken(IHeaderDictionary? headers) | ||
private string GetAccessToken(IHeaderDictionary? headers) | ||
{ | ||
const string easyAuthAccessTokenHeader = "X-MS-TOKEN-AAD-ACCESS-TOKEN"; | ||
const string AppServicesAuthAccessTokenHeader = "X-MS-TOKEN-AAD-ACCESS-TOKEN"; | ||
|
||
string? accessToken = null; | ||
if (headers != null) | ||
{ | ||
accessToken = headers[easyAuthAccessTokenHeader]; | ||
accessToken = headers[AppServicesAuthAccessTokenHeader]; | ||
} | ||
#if DEBUG | ||
if (string.IsNullOrEmpty(accessToken)) | ||
{ | ||
accessToken = AppServiceAuthenticationInformation.SimulateGetttingHeaderFromDebugEnvironmentVariable(easyAuthAccessTokenHeader); | ||
accessToken = AppServicesAuthenticationInformation.SimulateGetttingHeaderFromDebugEnvironmentVariable(AppServicesAuthAccessTokenHeader); | ||
} | ||
#endif | ||
return accessToken; | ||
if (!string.IsNullOrEmpty(accessToken)) | ||
{ | ||
return accessToken; | ||
} | ||
|
||
return string.Empty; | ||
} | ||
|
||
/// <inheritdoc/> | ||
|
This file contains 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
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.
That makes me think that we need to check the "ver" claim, and use the right constant (NameClaim or prefered_username, depending on the version.