Repository navigation
Expand file tree
/
Copy pathClaimActionCollectionMapExtensions.cs
More file actions
100 lines (92 loc) · 5.5 KB
/
Copy pathClaimActionCollectionMapExtensions.cs
File metadata and controls
100 lines (92 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication.OAuth.Claims;
using Newtonsoft.Json.Linq;
namespace Microsoft.AspNetCore.Authentication
{
public static class ClaimActionCollectionMapExtensions
{
/// <summary>
/// Select a top level value from the json user data with the given key name and add it as a Claim.
/// This no-ops if the key is not found or the value is empty.
/// </summary>
/// <param name="collection"></param>
/// <param name="claimType">The value to use for Claim.Type when creating a Claim.</param>
/// <param name="jsonKey">The top level key to look for in the json user data.</param>
public static void MapJsonKey(this ClaimActionCollection collection, string claimType, string jsonKey)
{
collection.MapJsonKey(claimType, jsonKey, ClaimValueTypes.String);
}
/// <summary>
/// Select a top level value from the json user data with the given key name and add it as a Claim.
/// This no-ops if the key is not found or the value is empty.
/// </summary>
/// <param name="collection"></param>
/// <param name="claimType">The value to use for Claim.Type when creating a Claim.</param>
/// <param name="jsonKey">The top level key to look for in the json user data.</param>
/// <param name="valueType">The value to use for Claim.ValueType when creating a Claim.</param>
public static void MapJsonKey(this ClaimActionCollection collection, string claimType, string jsonKey, string valueType)
{
collection.Add(new JsonKeyClaimAction(claimType, valueType, jsonKey));
}
/// <summary>
/// Select a second level value from the json user data with the given top level key name and second level sub key name and add it as a Claim.
/// This no-ops if the keys are not found or the value is empty.
/// </summary>
/// <param name="collection"></param>
/// <param name="claimType">The value to use for Claim.Type when creating a Claim.</param>
/// <param name="jsonKey">The top level key to look for in the json user data.</param>
/// <param name="subKey">The second level key to look for in the json user data.</param>
public static void MapJsonSubKey(this ClaimActionCollection collection, string claimType, string jsonKey, string subKey)
{
collection.MapJsonSubKey(claimType, jsonKey, subKey, ClaimValueTypes.String);
}
/// <summary>
/// Select a second level value from the json user data with the given top level key name and second level sub key name and add it as a Claim.
/// This no-ops if the keys are not found or the value is empty.
/// </summary>
/// <param name="collection"></param>
/// <param name="claimType">The value to use for Claim.Type when creating a Claim.</param>
/// <param name="jsonKey">The top level key to look for in the json user data.</param>
/// <param name="subKey">The second level key to look for in the json user data.</param>
/// <param name="valueType">The value to use for Claim.ValueType when creating a Claim.</param>
public static void MapJsonSubKey(this ClaimActionCollection collection, string claimType, string jsonKey, string subKey, string valueType)
{
collection.Add(new JsonSubKeyClaimAction(claimType, valueType, jsonKey, subKey));
}
/// <summary>
/// Run the given resolver to select a value from the json user data to add as a claim.
/// This no-ops if the returned value is empty.
/// </summary>
/// <param name="collection"></param>
/// <param name="claimType">The value to use for Claim.Type when creating a Claim.</param>
/// <param name="resolver">The Func that will be called to select value from the given json user data.</param>
public static void MapCustomJson(this ClaimActionCollection collection, string claimType, Func<JObject, string> resolver)
{
collection.MapCustomJson(claimType, ClaimValueTypes.String, resolver);
}
/// <summary>
/// Run the given resolver to select a value from the json user data to add as a claim.
/// This no-ops if the returned value is empty.
/// </summary>
/// <param name="collection"></param>
/// <param name="claimType">The value to use for Claim.Type when creating a Claim.</param>
/// <param name="valueType">The value to use for Claim.ValueType when creating a Claim.</param>
/// <param name="resolver">The Func that will be called to select value from the given json user data.</param>
public static void MapCustomJson(this ClaimActionCollection collection, string claimType, string valueType, Func<JObject, string> resolver)
{
collection.Add(new CustomJsonClaimAction(claimType, valueType, resolver));
}
/// <summary>
/// Delete all claims from the given ClaimsIdentity with the given ClaimType.
/// </summary>
/// <param name="collection"></param>
/// <param name="claimType"></param>
public static void DeleteClaim(this ClaimActionCollection collection, string claimType)
{
collection.Add(new DeleteClaimAction(claimType));
}
}
}