-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomClaimsProviderTrigger.cs
95 lines (72 loc) · 2.82 KB
/
CustomClaimsProviderTrigger.cs
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
namespace Heimdall.ClaimsProviderDemo;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using System.Text.Json;
using System.Text.Json.Serialization;
public class CustomClaimsProviderTrigger(ILogger<CustomClaimsProviderTrigger> logger)
{
private readonly ILogger<CustomClaimsProviderTrigger> _logger = logger;
[Function("CustomClaimsProviderTrigger")]
public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req)
{
// Fetch request body
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
// Deserialize the request body
dynamic? data = JsonSerializer.Deserialize<Data>(requestBody);
// Read the correlation ID from the Azure AD request
string? correlationId = data?.data.authenticationContext.correlationId;
// Claims to return to Azure AD
ResponseContent response = new();
response.Data.Actions[0].Claims.CorrelationId = correlationId;
response.Data.Actions[0].Claims.ApiVersion = "1.0.0";
response.Data.Actions[0].Claims.DateOfBirth = "01/01/2000";
response.Data.Actions[0].Claims.CustomRoles.Add("Writer");
response.Data.Actions[0].Claims.CustomRoles.Add("Editor");
return new OkObjectResult(response);
}
public class ResponseContent{
[JsonPropertyName("data")]
public Data Data { get; set; }
public ResponseContent()
{
Data = new Data();
}
}
public class Data{
[JsonPropertyName("@odata.type")]
public string ODataType { get; set; }
[JsonPropertyName("actions")]
public List<Action> Actions { get; set; }
public Data()
{
ODataType = "microsoft.graph.onTokenIssuanceStartResponseData";
Actions = [new Action()];
}
}
public class Action{
[JsonPropertyName("@odata.type")]
public string ODataType { get; set; }
[JsonPropertyName("claims")]
public Claims Claims { get; set; }
public Action()
{
ODataType = "microsoft.graph.tokenIssuanceStart.provideClaimsForToken";
Claims = new Claims();
}
}
public class Claims{
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string? CorrelationId { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string? DateOfBirth { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string? ApiVersion { get; set; }
public List<string> CustomRoles { get; set; }
public Claims()
{
CustomRoles = [];
}
}
}