Skip to content
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

Adding generation of accessors for equivalent claims #1829

Open
wants to merge 16 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,155 changes: 3,155 additions & 0 deletions src/System.IdentityModel.Tokens.Jwt/ClaimTypeAccessor.gen.cs

Large diffs are not rendered by default.

216 changes: 216 additions & 0 deletions src/System.IdentityModel.Tokens.Jwt/ClaimTypeAccessor.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
<#@ template debug="false" hostspecific="false" language="C#" #>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of general concerns:

  1. some of the categories seems to be AAD specific, IdentityModel has always tried to be neutral?
  2. can users set their own categories?
  3. there is no general spec for the category of claims, how can one say claim 'a' is in a category in general?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Do we want to move this into SAL?
  2. The categories are privacy categories (well defined. could almost be an enum). For 3) will follow-up offline

<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".gen.cs" #>
<#@ include file="ClaimsKnowledge.tti" #>
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation.
// All rights reserved.
//
// This code is licensed under the MIT License.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//------------------------------------------------------------------------------

// THIS FILE IS AUTOMATICALLY GENERATED FROM ClaimsTypeAccessor.tt USING T4
// DON'T UPDATE MANUALLY

using System.Collections.Generic;
using System.Security.Claims;

namespace System.IdentityModel.Tokens.Jwt
{
/// <summary>
/// Defines extension methods to access claims by purpose, and not by name, making it compatible
/// with several versions of tokens.
/// </summary>
public static partial class ClaimPrincipalExtensions
{
<#
foreach(ClaimsKnowledge c in claimsKnowledge)
{
if (c.HasMultipleInstances)
{
#>
/// <summary>
/// Returns all the claims corresponding to <#=c.AccessorName#> on a ClaimsPrincipal:
/// <list type="bullet">
<#
foreach(string s in c.AllClaimNames)
{
#>
/// <item><description><#= s #></description></item>
<#
}
#>
/// </list>
<#
if (c.IsUsableInAuthorizationPolicies)
{
#>
/// This method returns information that is safe to use for authorization.
<#
}
if (!string.IsNullOrEmpty(c.PrivacyCategory))
{
#>
/// The privacy classification of the information returned is <#= c.PrivacyCategory #>.
<#
}
#>
/// </summary>
/// <param name="claimsPrincipal">Claims principal from which to get the <#=c.AccessorName#>.</param>
public static IEnumerable<string> Get<#=c.AccessorName#>(this ClaimsPrincipal claimsPrincipal)
{
if (claimsPrincipal == null)
{
throw new ArgumentNullException(nameof(claimsPrincipal));
}
return claimsPrincipal.GetAllClaimValues(
<#=string.Join(",\n ", c.AllClaimNames)#>);
}

/// <summary>
/// Returns all the claims corresponding to <#=c.AccessorName#> on a ClaimsPrincipal:
/// <list type="bullet">
<#
foreach(string s in c.AllClaimNames)
{
#>
/// <item><description><#= s #></description></item>
<#
}
#>
/// </list>
<#
if (c.IsUsableInAuthorizationPolicies)
{
#>
/// This method returns information that is safe to use for authorization.
<#
}
if (!string.IsNullOrEmpty(c.PrivacyCategory))
{
#>
/// The privacy classification of the information returned is <#= c.PrivacyCategory #>.
<#
}
#>
/// </summary>
/// <param name="claimsIdentity">Claims ClaimsIdentity from which to get the <#=c.AccessorName#>.</param>
public static IEnumerable<string> Get<#=c.AccessorName#>(this ClaimsIdentity claimsIdentity)
{
if (claimsIdentity == null)
{
throw new ArgumentNullException(nameof(claimsIdentity));
}
return claimsIdentity.GetAllClaimValues(
<#=string.Join(",\n ", c.AllClaimNames)#>);
}

<#
}
else
{
#>
/// <summary>
/// Returns the first claim corresponding to <#=c.AccessorName#> claim on a ClaimsPrincipal
/// <list type="bullet">
<#
foreach(string s in c.AllClaimNames)
{
#>
/// <item><description><#= s #></description></item>
<#
}
#>
/// </list>
<#
if (c.IsUsableInAuthorizationPolicies)
{
#>
/// This method returns information that is safe to use for authorization.
<#
}
if (!string.IsNullOrEmpty(c.PrivacyCategory))
{
#>
/// The privacy classification of the information returned is <#= c.PrivacyCategory #>.
<#
}
#>
/// </summary>
/// <param name="claimsPrincipal">Claims ClaimsPrincipal from which to get the <#=c.AccessorName#>.</param>
public static string Get<#=c.AccessorName#>(this ClaimsPrincipal claimsPrincipal)
{
if (claimsPrincipal == null)
{
throw new ArgumentNullException(nameof(claimsPrincipal));
}
return claimsPrincipal.GetClaimValue(
<#=string.Join(",\n ", c.AllClaimNames)#>);
}

/// <summary>
/// Returns the first claim corresponding to <#=c.AccessorName#> claim on a ClaimsIdentity
/// <list type="bullet">
<#
foreach(string s in c.AllClaimNames)
{
#>
/// <item><description><#= s #></description></item>
<#
}
#>
/// </list>
<#
if (c.IsUsableInAuthorizationPolicies)
{
#>
/// This method returns information that is safe to use for authorization.
<#
}
if (!string.IsNullOrEmpty(c.PrivacyCategory))
{
#>
/// The privacy classification of the information returned is <#= c.PrivacyCategory #>.
<#
}
#>
/// </summary>
/// <param name="claimsIdentity">Claims ClaimsIdentity from which to get the <#=c.AccessorName#>.</param>
public static string Get<#=c.AccessorName#>(this ClaimsIdentity claimsIdentity)
{
if (claimsIdentity == null)
{
throw new ArgumentNullException(nameof(claimsIdentity));
}
return claimsIdentity.GetClaimValue(
<#=string.Join(",\n ", c.AllClaimNames)#>);
}
<#
}
}
#>
}
}
6 changes: 5 additions & 1 deletion src/System.IdentityModel.Tokens.Jwt/ClaimTypeMapping.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation.
// All rights reserved.
Expand All @@ -25,6 +25,9 @@
//
//------------------------------------------------------------------------------

// THIS FILE IS AUTOMATICALLY GENERATED FROM ClaimsTypeMapping.tt USING T4
// DON'T UPDATE MANUALLY

using System.Collections.Generic;
using System.Security.Claims;

Expand Down Expand Up @@ -96,6 +99,7 @@ internal static class ClaimTypeMapping
{ "deviceregid", "http://schemas.microsoft.com/2012/01/devicecontext/claims/registrationid" },
{ "endpointpath", "http://schemas.microsoft.com/2012/01/requestcontext/claims/x-ms-endpoint-absolute-path" },
{ "forwardedclientip", "http://schemas.microsoft.com/2012/01/requestcontext/claims/x-ms-forwarded-client-ip" },
{ "fwt", "http://schemas.microsoft.com/2012/01/requestcontext/claims/x-ms-forwarded-client-ip" },
jmprieur marked this conversation as resolved.
Show resolved Hide resolved
{ "group", "http://schemas.xmlsoap.org/claims/Group" },
{ "groupsid", ClaimTypes.GroupSid },
{ "idp", "http://schemas.microsoft.com/identity/claims/identityprovider" },
Expand Down
115 changes: 115 additions & 0 deletions src/System.IdentityModel.Tokens.Jwt/ClaimTypeMapping.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<#@ template debug="false" hostspecific="false" language="C#" #>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a T4 file that generates the ClaimTypeMapping.cs from the ClaimsKnowledge.tti.

<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
<#@ include file="ClaimsKnowledge.tti" #>
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation.
// All rights reserved.
//
// This code is licensed under the MIT License.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//------------------------------------------------------------------------------

// THIS FILE IS AUTOMATICALLY GENERATED FROM ClaimsTypeMapping.tt USING T4
// DON'T UPDATE MANUALLY

using System.Collections.Generic;
using System.Security.Claims;

namespace System.IdentityModel.Tokens.Jwt
{
/// <summary>
/// Defines the inbound and outbound mapping for claim claim types from jwt to .net claim
/// </summary>
internal static class ClaimTypeMapping
{
// This is the short to long mapping.
// key is the long claim type
// value is the short claim type
private static Dictionary<string, string> shortToLongClaimTypeMapping = new Dictionary<string, string>
{
<#
foreach(ClaimsKnowledge c in claimsKnowledge.Where(c => c.GenerateMapping))
{
foreach(string shortClaimType in c.ShortClaimNames)
{
#>
{ <#=shortClaimType#>, <#=c.LongClaimName#> },
<#
}
}
#>
};

private static IDictionary<string, string> longToShortClaimTypeMapping = new Dictionary<string, string>();
private static HashSet<string> inboundClaimFilter = inboundClaimFilter = new HashSet<string>();

/// <summary>
/// Initializes static members of the <see cref="ClaimTypeMapping"/> class.
/// </summary>
static ClaimTypeMapping()
{
foreach (KeyValuePair<string, string> kv in shortToLongClaimTypeMapping)
{
if (longToShortClaimTypeMapping.ContainsKey(kv.Value))
{
continue;
}

longToShortClaimTypeMapping.Add(kv.Value, kv.Key);
}
}

/// <summary>
/// Gets the InboundClaimTypeMap used by JwtSecurityTokenHandler when producing claims from jwt.
/// </summary>
public static IDictionary<string, string> InboundClaimTypeMap
{
get
{
return shortToLongClaimTypeMapping;
}
}

/// <summary>
/// Gets the OutboundClaimTypeMap is used by JwtSecurityTokenHandler to shorten claim types when creating a jwt.
/// </summary>
public static IDictionary<string, string> OutboundClaimTypeMap
{
get
{
return longToShortClaimTypeMapping;
}
}

public static ISet<string> InboundClaimFilter
{
get
{
return inboundClaimFilter;
}
}
}
}
Loading