-
Notifications
You must be signed in to change notification settings - Fork 413
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
jmprieur
wants to merge
16
commits into
dev
Choose a base branch
from
jmprieur/ClaimsMappingImprovement
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
049c4fd
Adding generation of accessors for claims independently of
jmprieur bbdaf02
Fixing spacing
jmprieur 2b89eea
Fixing a comment.
jmprieur bb86cda
Remove the fmt claim from the mapping.
jmprieur 8ae45d9
Adding a generator for some documentation
jmprieur c9f408d
- Fixing names form a few claims accessors
jmprieur 54972e4
Addressing Kalyan's PR feeedback
jmprieur e524d52
Adding a new assembly with the claims accessors
jmprieur 00cfad6
Adding a new assembly with the claims accessors
jmprieur 5729efe
Merge branch 'jmprieur/ClaimsMappingImprovement' of https://github.co…
jmprieur 99b068f
Merge branch 'jmprieur/ClaimsMappingImprovement' of https://github.co…
jmprieur 15f9d67
Merge branch 'jmprieur/ClaimsMappingImprovement' of https://github.co…
jmprieur 0d353f8
Improving the generated .md by expressing the value of the claims types
jmprieur 92a1c0a
Fixing a few names
jmprieur 6140071
Added methods to get the claims equivalent
jmprieur a38cc2f
Fixing the namespaces
jmprieur File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
3,155 changes: 3,155 additions & 0 deletions
3,155
src/System.IdentityModel.Tokens.Jwt/ClaimTypeAccessor.gen.cs
Large diffs are not rendered by default.
Oops, something went wrong.
216 changes: 216 additions & 0 deletions
216
src/System.IdentityModel.Tokens.Jwt/ClaimTypeAccessor.tt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
<#@ template debug="false" hostspecific="false" language="C#" #> | ||
<#@ 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)#>); | ||
} | ||
<# | ||
} | ||
} | ||
#> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
src/System.IdentityModel.Tokens.Jwt/ClaimTypeMapping.tt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<#@ template debug="false" hostspecific="false" language="C#" #> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.