-
Notifications
You must be signed in to change notification settings - Fork 1
/
Commons.cs
185 lines (161 loc) · 7.31 KB
/
Commons.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using custom_idp.Models;
using Microsoft.IdentityModel.Tokens;
using System.Security.Cryptography.X509Certificates;
using System.IdentityModel.Tokens.Jwt;
using Microsoft.AspNetCore.Http;
using Microsoft.ApplicationInsights;
using System.Text.Json;
using custom_idp.proxy.Controllers;
namespace custom_idp
{
public class Commons
{
public static Lazy<X509SigningCredentials> LoadCertificate()
{
return new Lazy<X509SigningCredentials>(() =>
{
X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
certStore.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certCollection = certStore.Certificates.Find(
X509FindType.FindByThumbprint,
AppSettings.SigningCertThumbprint,
false);
// Get the first cert with the thumb-print
if (certCollection.Count > 0)
{
return new X509SigningCredentials(certCollection[0]);
}
throw new Exception("Certificate not found");
});
}
public static string BuildJwtToken(X509SigningCredentials SigningCredentials, HttpRequest request, string tenantId, string ClientId, string Name, string email)
{
string issuer = $"{request.Scheme}://{request.Host}{request.PathBase.Value}/{tenantId}";
// Token issuance date and time
DateTime time = DateTime.Now;
// All parameters send to Azure AD B2C needs to be sent as claims
IList<System.Security.Claims.Claim> claims = new List<System.Security.Claims.Claim>();
claims.Add(new System.Security.Claims.Claim("sub", email, System.Security.Claims.ClaimValueTypes.String, issuer));
claims.Add(new System.Security.Claims.Claim("iat", ((DateTimeOffset)time).ToUnixTimeSeconds().ToString(), System.Security.Claims.ClaimValueTypes.Integer, issuer));
claims.Add(new System.Security.Claims.Claim("name", email, System.Security.Claims.ClaimValueTypes.String, issuer));
claims.Add(new System.Security.Claims.Claim("given_name", Name.Split(' ')[0], System.Security.Claims.ClaimValueTypes.String, issuer));
claims.Add(new System.Security.Claims.Claim("family_name", Name.Split(' ')[1], System.Security.Claims.ClaimValueTypes.String, issuer));
claims.Add(new System.Security.Claims.Claim("email", email, System.Security.Claims.ClaimValueTypes.String, issuer));
claims.Add(new System.Security.Claims.Claim("tenantId", tenantId, System.Security.Claims.ClaimValueTypes.String, issuer));
// Create the token
JwtSecurityToken token = new JwtSecurityToken(
issuer,
ClientId,
claims,
time,
time.AddHours(24),
SigningCredentials);
// Get the representation of the signed token
JwtSecurityTokenHandler jwtHandler = new JwtSecurityTokenHandler();
return jwtHandler.WriteToken(token);
}
public static async Task LogRequestAsync(
HttpRequest Request,
TelemetryClient telemetry,
SettingsEntity settings,
string tenantId,
string page,
HttpResponseMessage Response = null,
string? responseBody = null,
string? additionalData = null)
{
if (string.IsNullOrEmpty(settings.InstrumentationKey))
{
return;
}
telemetry.InstrumentationKey = settings.InstrumentationKey;
// Request page
telemetry.TrackPageView($"{tenantId}_{page}");
Dictionary<string, string> log = new Dictionary<string, string>();
log.Add("RequestMethod", Request.Method);
log.Add("RequestURL", $"{Request.Scheme}://{Request.Host}{Request.Path}{Request.QueryString}");
log.Add("TenantId", tenantId);
// Get the target URL
if (Request.RouteValues.ContainsKey("area") && Request.RouteValues["area"]!.ToString() == "proxy" &&
Request.RouteValues.ContainsKey("id"))
{
Uri targetUri = HttpRequestHelper.GetTargetUrl(Request, Request.RouteValues["id"]!.ToString());
log.Add("IdpEndpointUrl", targetUri.ToString());
}
// Request headers
string headers = JsonSerializer.Serialize(Request.Headers);
log.Add("RequestHeaders", headers);
// Request body
if (!string.IsNullOrEmpty(responseBody))
{
log.Add("RequestBody", responseBody);
}
else
{
try
{
if (Request.Method == "POST" && Request.Body != null)
{
string body = "";
using (StreamReader stream = new StreamReader(Request.Body))
{
body = await stream.ReadToEndAsync();
log.Add("RequestBody", body);
}
}
}
catch (ObjectDisposedException e)
{
// The body object has been disposed in earlier call to this function
}
catch (System.Exception)
{
// Don't throw error;
}
}
// Response body
if (Response != null)
{
log.Add("ResponseStatusCode", Response.StatusCode.ToString());
}
// Response body
if (!string.IsNullOrEmpty(responseBody))
{
log.Add("ResponseBody", responseBody);
}
if (!string.IsNullOrEmpty(additionalData))
{
log.Add("AdditionalData", additionalData);
}
telemetry.TrackEvent($"{tenantId}_{page}", log);
telemetry.Flush();
}
public static void LogError(HttpRequest Request, TelemetryClient telemetry, SettingsEntity settings, string tenantId, string page, string error, HttpResponseMessage Response = null)
{
if (string.IsNullOrEmpty(settings.InstrumentationKey))
{
return;
}
telemetry.InstrumentationKey = settings.InstrumentationKey;
Dictionary<string, string> log = new Dictionary<string, string>();
log.Add("RequestMethod", Request.Method);
log.Add("RequestURL", $"{Request.Scheme}://{Request.Host}{Request.Path}{Request.QueryString}");
log.Add("Error", error);
log.Add("TenantId", tenantId);
// Response body
if (Response != null)
{
log.Add("ResponseStatusCode", Response.StatusCode.ToString());
}
telemetry.TrackEvent($"{tenantId}_{page}", log);
telemetry.Flush();
}
}
}