Skip to content

Commit 4740931

Browse files
committed
Fix mising username in json mode
- set the source for the user name to ClaimTypes.Upn Co-authored-by: Norbert Baum <mail@norbert-baum.de> +semver: fix
1 parent 4e9d856 commit 4740931

3 files changed

Lines changed: 35 additions & 19 deletions

File tree

src/KK.AspNetCore.EasyAuthAuthentication/AuthenticationTicketBuilder.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,21 @@ internal static class AuthenticationTicketBuilder
1212
/// Build a `AuthenticationTicket` from the given payload, the principal name and the provider name.
1313
/// </summary>
1414
/// <param name="claimsPayload">A array of JObjects that have a `type` and a `val` property.</param>
15+
/// <param name="userid">The user ID of the current user.</param>
1516
/// <param name="providerName">The provider name of the current auth provider.</param>
1617
/// <returns>A `AuthenticationTicket`.</returns>
17-
public static AuthenticationTicket Build(IEnumerable<JObject> claimsPayload, string providerName)
18+
public static AuthenticationTicket Build(IEnumerable<JObject> claimsPayload, string userid, string providerName)
1819
{
1920
// setting ClaimsIdentity.AuthenticationType to value that Azure AD non-EasyAuth setups use
2021
var identity = new ClaimsIdentity(
2122
CreateClaims(claimsPayload),
22-
AuthenticationTypesNames.Federation
23+
AuthenticationTypesNames.Federation,
24+
ClaimTypes.Upn,
25+
ClaimTypes.Role
2326
);
2427

2528
AddScopeClaim(identity);
29+
AddUserIdClaim(identity, userid);
2630
AddProviderNameClaim(identity, providerName);
2731
var genericPrincipal = new ClaimsPrincipal(identity);
2832

@@ -74,5 +78,13 @@ private static void AddProviderNameClaim(ClaimsIdentity identity, string provide
7478
identity.AddClaim(new Claim("provider_name", providerName));
7579
}
7680
}
81+
82+
private static void AddUserIdClaim(ClaimsIdentity identity, string userid)
83+
{
84+
if (!identity.Claims.Any(claim => claim.Type == ClaimTypes.Upn))
85+
{
86+
identity.AddClaim(new Claim(ClaimTypes.Upn, userid));
87+
}
88+
}
7789
}
78-
}
90+
}

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,17 @@ private async Task<AuthenticationTicket> CreateUserTicket()
9191

9292
private AuthenticationTicket BuildIdentityFromEasyAuthMeJson(JObject payload)
9393
{
94-
var name = payload["user_id"].Value<string>(); // X-MS-CLIENT-PRINCIPAL-NAME
95-
this.Logger.LogDebug($"payload was fetched from easyauth me json, name: {name}");
96-
97-
var identity = new GenericIdentity(name, AuthenticationTypesNames.Federation); // setting ClaimsIdentity.AuthenticationType to value that azuread non-easyauth setups use
94+
var userid = payload["user_id"].Value<string>();
95+
this.Logger.LogDebug($"payload was fetched from easyauth me json, name: {userid}");
96+
var providerName = payload["provider_name"].Value<string>();
97+
this.Logger.LogDebug($"payload was fetched from easyauth me json, provider: {providerName}");
9898

9999
this.Logger.LogInformation("building claims from payload...");
100-
var providerName = payload["provider_name"].Value<string>();
101-
return AuthenticationTicketBuilder.Build(payload["user_claims"].Children<JObject>(), providerName);
100+
return AuthenticationTicketBuilder.Build(
101+
payload["user_claims"].Children<JObject>(),
102+
userid,
103+
providerName
104+
);
102105
}
103106

104107
private async Task<JArray> GetAuthMe(HttpClientHandler handler, HttpRequestMessage httpRequest)
@@ -179,4 +182,4 @@ private HttpClientHandler CreateHandler(ref CookieContainer cookieContainer)
179182
return handler;
180183
}
181184
}
182-
}
185+
}

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,21 @@ public static AuthenticateResult AuthUser(ILogger logger, HttpContext context)
4545

4646
private AuthenticationTicket BuildIdentityFromEasyAuthRequestHeaders()
4747
{
48-
var name = this.Headers[PrincipalNameHeader][0];
49-
this.Logger.LogDebug($"payload was fetched from EasyAuth headers, name: {name}");
48+
var userid = this.Headers[PrincipalNameHeader][0];
49+
this.Logger.LogDebug($"payload was fetched from EasyAuth headers, name: {userid}");
50+
var providerName = this.Headers[PrincipalIdpHeaderName][0];
51+
this.Logger.LogDebug($"payload was fetched from easyauth me json, provider: {providerName}");
5052

5153
this.Logger.LogInformation("building claims from payload...");
5254
var xMsClientPrincipal = JObject.Parse(
53-
Encoding.UTF8.GetString(
54-
Convert.FromBase64String(this.Headers[PrincipalObjectHeader][0])
55-
)
56-
);
55+
Encoding.UTF8.GetString(
56+
Convert.FromBase64String(this.Headers[PrincipalObjectHeader][0])
57+
)
58+
);
5759

5860
var claims = xMsClientPrincipal["claims"].Children<JObject>();
59-
var providerName = this.Headers[PrincipalIdpHeaderName][0];
6061

61-
return AuthenticationTicketBuilder.Build(claims, providerName);
62+
return AuthenticationTicketBuilder.Build(claims, userid, providerName);
6263
}
6364
}
64-
}
65+
}

0 commit comments

Comments
 (0)