From a746e636a2342905f5d13afb7adfae55838b8797 Mon Sep 17 00:00:00 2001 From: VesaJuvonen Date: Fri, 27 Mar 2020 18:59:53 +0200 Subject: [PATCH] Updating AspNetCore sample token handling based on updated security guidance --- .../S2S/Tokens/JsonWebSecurityTokenHandler.cs | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Solutions/AspNetCore.Authentication/src/SharePointPnP.IdentityModel/Extensions/S2S/Tokens/JsonWebSecurityTokenHandler.cs b/Solutions/AspNetCore.Authentication/src/SharePointPnP.IdentityModel/Extensions/S2S/Tokens/JsonWebSecurityTokenHandler.cs index 7d2fa06fe..c3f10963b 100644 --- a/Solutions/AspNetCore.Authentication/src/SharePointPnP.IdentityModel/Extensions/S2S/Tokens/JsonWebSecurityTokenHandler.cs +++ b/Solutions/AspNetCore.Authentication/src/SharePointPnP.IdentityModel/Extensions/S2S/Tokens/JsonWebSecurityTokenHandler.cs @@ -1,4 +1,6 @@ using Microsoft.IdentityModel.Claims; +using System; +using System.Linq; using System.ServiceModel.Security.Tokens; using System.Xml; @@ -412,6 +414,7 @@ private ClaimsIdentityCollection ValidateTokenCore(System.IdentityModel.Tokens.S ClaimsIdentity claimsIdentity = new ClaimsIdentity("Federation"); if (!isActorToken && jsonWebSecurityToken.ActorToken != null) { + ValidateActorTokenForAppOnly(jsonWebSecurityToken.ActorToken); ClaimsIdentityCollection claimsIdentityCollection2 = this.ValidateActorToken(jsonWebSecurityToken.ActorToken); if (claimsIdentityCollection2.Count > 1) { @@ -440,6 +443,24 @@ private ClaimsIdentityCollection ValidateTokenCore(System.IdentityModel.Tokens.S return claimsIdentityCollection; } + /// + ///Validates that the actor token is an app token by checking for the lack of user claims + /// + /// + private static void ValidateActorTokenForAppOnly(JsonWebSecurityToken actorToken) + { + if (actorToken != null) + { + if (actorToken.Claims.FirstOrDefault(x => x.ClaimType.Equals("scp")) != null + || actorToken.Claims.FirstOrDefault(x => x.ClaimType.Equals("upn")) != null + || actorToken.Claims.FirstOrDefault(x => x.ClaimType.Equals("unique_name")) != null + || actorToken.Claims.FirstOrDefault(x => x.ClaimType.Equals("altsecid")) != null) + { + throw new UnauthorizedAccessException("Invalid actor token."); + } + } + } + public override ClaimsIdentityCollection ValidateToken(System.IdentityModel.Tokens.SecurityToken token) { return this.ValidateTokenCore(token, false);