Skip to content

Commit 54154f2

Browse files
committed
clean up some stuff
1 parent a94cf24 commit 54154f2

5 files changed

Lines changed: 25 additions & 20 deletions

File tree

KK.AspNetCore.EasyAuthAuthentication.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
44
VisualStudioVersion = 15.0.26124.0

src/KK.AspNetCore.EasyAuthAuthentication/EasyAuthAuthenticationHandler.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,18 @@ public EasyAuthAuthenticationHandler(
3939
{
4040
}
4141

42-
private static Func<ClaimsPrincipal, bool> isContextUserNotAuthenticated = user => (user == null || user.Identity == null || user.Identity.IsAuthenticated == false);
43-
private static Func<IHeaderDictionary, bool> isAADIdTokenNotSet = headers => !string.IsNullOrEmpty(headers[AuthTokenHeaderNames.AADIdToken].ToString());
44-
private Func<IHeaderDictionary, ClaimsPrincipal, bool> canUseHeaderAuth => (headers, user) => isContextUserNotAuthenticated(user) && !isAADIdTokenNotSet(headers);
45-
private Func<IHeaderDictionary, ClaimsPrincipal, HttpRequest, bool> canUseEasyAuthJson => (headers, user, request) =>
46-
isContextUserNotAuthenticated(user)
47-
&& isAADIdTokenNotSet(headers)
48-
&& request.Path != "/" + $"{this.Options.AuthEndpoint}";
42+
private static Func<ClaimsPrincipal, bool> isContextUserNotAuthenticated =
43+
user => (user == null || user.Identity == null || user.Identity.IsAuthenticated == false);
44+
private static Func<IHeaderDictionary, string, bool> isHeaderSet =
45+
(headers, headerName) => !string.IsNullOrEmpty(headers[headerName].ToString());
46+
private Func<IHeaderDictionary, ClaimsPrincipal, bool> canUseHeaderAuth =
47+
(headers, user) => isContextUserNotAuthenticated(user) &&
48+
isHeaderSet(headers, AuthTokenHeaderNames.AADIdToken);
49+
private static Func<IHeaderDictionary, ClaimsPrincipal, HttpRequest, string, bool> canUseEasyAuthJson =
50+
(headers, user, request, authEndpoint) =>
51+
isContextUserNotAuthenticated(user)
52+
&& !isHeaderSet(headers, AuthTokenHeaderNames.AADIdToken)
53+
&& request.Path != "/" + $"{authEndpoint}";
4954

5055
/// <inheritdoc/>
5156
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
@@ -56,7 +61,7 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
5661
{
5762
return EasyAuthWithHeaderService.AuthUser(this.Logger, this.Context);
5863
}
59-
else if (canUseEasyAuthJson(this.Context.Request.Headers, this.Context.User, this.Context.Request))
64+
else if (canUseEasyAuthJson(this.Context.Request.Headers, this.Context.User, this.Context.Request, this.Options.AuthEndpoint))
6065
{
6166
return await EasyAuthWithAuthMeService.AuthUser(this.Logger, this.Context, this.Options.AuthEndpoint);
6267
}

src/KK.AspNetCore.EasyAuthAuthentication/KK.AspNetCore.EasyAuthAuthentication.csproj

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
<ItemGroup>
1717
<PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.1.2" />
1818
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.1.1" />
19-
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
19+
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
20+
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.3.0" />
2021
</ItemGroup>
2122

2223
<!-- StyleCop Settings -->
@@ -25,12 +26,6 @@
2526
<CodeAnalysisRuleSet>..\..\StyleCop.ruleset</CodeAnalysisRuleSet>
2627
</PropertyGroup>
2728
<ItemGroup>
28-
<PackageReference Include="StyleCop.Analyzers" Version="1.1.0-beta009" privateassets="all" />
29-
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.3.0" />
30-
</ItemGroup>
31-
<ItemGroup>
32-
<Reference Include="System.IdentityModel.Tokens.Jwt">
33-
<HintPath>C:\Program Files\dotnet\sdk\NuGetFallbackFolder\system.identitymodel.tokens.jwt\5.2.0\lib\netstandard1.4\System.IdentityModel.Tokens.Jwt.dll</HintPath>
34-
</Reference>
29+
<PackageReference Include="StyleCop.Analyzers" Version="1.1.1-beta.61" privateassets="all" />
3530
</ItemGroup>
3631
</Project>

src/KK.AspNetCore.EasyAuthAuthentication/Services/EasyAuthWithAuthMeService.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ private EasyAuthWithAuthMeService(
3636
Headers = headers;
3737
AuthEndPoint = authEndPoint;
3838
this.Logger = logger;
39-
4039
}
4140

4241
/// <summary>

src/KK.AspNetCore.EasyAuthAuthentication/Services/EasyAuthWithHeaderService.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ namespace KK.AspNetCore.EasyAuthAuthentication.Services
1414
{
1515
public class EasyAuthWithHeaderService
1616
{
17+
public const string PrincipalNameHeader = "X-MS-CLIENT-PRINCIPAL-NAME";
18+
/// <summary>
19+
/// JWT
20+
/// </summary>
21+
public const string PrincipalObjectHeader = "X-MS-CLIENT-PRINCIPAL";
22+
public const string PrincipalIdpHeaderName = "X-MS-CLIENT-PRINCIPAL-IDP";
1723
public ILogger Logger { get; }
1824
public IHeaderDictionary Headers { get; }
1925

@@ -44,14 +50,14 @@ public static AuthenticateResult AuthUser(ILogger logger, HttpContext context)
4450

4551
private AuthenticationTicket BuildIdentityFromEasyAuthRequestHeaders()
4652
{
47-
var name = this.Headers["X-MS-CLIENT-PRINCIPAL-NAME"][0];
53+
var name = this.Headers[PrincipalNameHeader][0];
4854
this.Logger.LogDebug($"payload was fetched from easyauth headers, name: {name}");
4955

5056
var identity = new GenericIdentity(name, AuthenticationTypesNames.Federation); // setting ClaimsIdentity.AuthenticationType to value that azureAd non-easyauth setups use
5157

5258
this.Logger.LogInformation("building claims from payload...");
5359

54-
var xMsClientPrincipal = JObject.Parse(Encoding.UTF8.GetString(Convert.FromBase64String(this.Headers["X-MS-CLIENT-PRINCIPAL"][0])));
60+
var xMsClientPrincipal = JObject.Parse(Encoding.UTF8.GetString(Convert.FromBase64String(this.Headers[PrincipalObjectHeader][0])));
5561
var claims = xMsClientPrincipal["claims"].Children<JObject>();
5662
var providerName = this.Headers["X-MS-CLIENT-PRINCIPAL-IDP"][0];
5763
return AuthenticationTicketBuilder.Build(claims, name, providerName);

0 commit comments

Comments
 (0)