Skip to content
This repository was archived by the owner on Aug 24, 2021. It is now read-only.

Commit a746e63

Browse files
committed
Updating AspNetCore sample token handling based on updated security guidance
1 parent 60acbec commit a746e63

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Solutions/AspNetCore.Authentication/src/SharePointPnP.IdentityModel/Extensions/S2S/Tokens/JsonWebSecurityTokenHandler.cs

+21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using Microsoft.IdentityModel.Claims;
2+
using System;
3+
using System.Linq;
24
using System.ServiceModel.Security.Tokens;
35
using System.Xml;
46

@@ -412,6 +414,7 @@ private ClaimsIdentityCollection ValidateTokenCore(System.IdentityModel.Tokens.S
412414
ClaimsIdentity claimsIdentity = new ClaimsIdentity("Federation");
413415
if (!isActorToken && jsonWebSecurityToken.ActorToken != null)
414416
{
417+
ValidateActorTokenForAppOnly(jsonWebSecurityToken.ActorToken);
415418
ClaimsIdentityCollection claimsIdentityCollection2 = this.ValidateActorToken(jsonWebSecurityToken.ActorToken);
416419
if (claimsIdentityCollection2.Count > 1)
417420
{
@@ -440,6 +443,24 @@ private ClaimsIdentityCollection ValidateTokenCore(System.IdentityModel.Tokens.S
440443
return claimsIdentityCollection;
441444
}
442445

446+
/// <summary>
447+
///Validates that the actor token is an app token by checking for the lack of user claims
448+
/// </summary>
449+
/// <param name="actorToken"></param>
450+
private static void ValidateActorTokenForAppOnly(JsonWebSecurityToken actorToken)
451+
{
452+
if (actorToken != null)
453+
{
454+
if (actorToken.Claims.FirstOrDefault<JsonWebTokenClaim>(x => x.ClaimType.Equals("scp")) != null
455+
|| actorToken.Claims.FirstOrDefault<JsonWebTokenClaim>(x => x.ClaimType.Equals("upn")) != null
456+
|| actorToken.Claims.FirstOrDefault<JsonWebTokenClaim>(x => x.ClaimType.Equals("unique_name")) != null
457+
|| actorToken.Claims.FirstOrDefault<JsonWebTokenClaim>(x => x.ClaimType.Equals("altsecid")) != null)
458+
{
459+
throw new UnauthorizedAccessException("Invalid actor token.");
460+
}
461+
}
462+
}
463+
443464
public override ClaimsIdentityCollection ValidateToken(System.IdentityModel.Tokens.SecurityToken token)
444465
{
445466
return this.ValidateTokenCore(token, false);

0 commit comments

Comments
 (0)