-
Notifications
You must be signed in to change notification settings - Fork 8
/
SignService.cs
320 lines (254 loc) · 10.8 KB
/
SignService.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.Json.Serialization;
using IronPdf;
using Jose;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.IdentityModel.Protocols;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
namespace aspnet_sign;
public class SignService
{
private readonly IHttpClientFactory _httpClientFactory;
private readonly IConfiguration _configuration;
private readonly IConfigurationManager<OpenIdConnectConfiguration> _configurationManager;
private readonly X509Certificate2 _cert;
private readonly ILogger<SignService> _logger;
public SignService(IHttpClientFactory httpClientFactory, IConfiguration configuration,
IConfigurationManager<OpenIdConnectConfiguration> configurationManager,
X509Certificate2 cert, ILogger<SignService> logger)
{
_httpClientFactory = httpClientFactory;
_configuration = configuration;
_configurationManager = configurationManager;
_cert = cert;
_logger = logger;
}
private async Task<IDictionary<string, object>> PrepareDocumentClaims(DocumentDescriptor document, int priority)
{
using var sha = SHA256Managed.Create();
await using var ms = new MemoryStream(document.Contents);
var hash = await sha.ComputeHashAsync(ms);
return new Dictionary<string, object>
{
{ "priority", priority },
{ "document_id", document.Id },
{ "document_hash", BitConverter.ToString(hash).Replace("-", "").ToLower() },
{ "hash_alg", "2.16.840.1.101.3.4.2.1" }, // SHA256 https://oidref.com/2.16.840.1.101.3.4.2.1
{ "document_title", document.Title },
{ "document_subject", document.Subject },
{ "document_language", "cs.CZ" },
{ "document_created", document.CreationTime },
{ "document_author", document.Author },
{ "document_pages", document.Pages },
{ "document_size", document.Contents.Length },
{ "document_read_by_enduser", true },
{
"sign_area", new Dictionary<string, object>
{
{ "page", 0 },
{ "x-coordinate", 350 },
{ "y-coordinate", 150 },
{ "x-dist", 140 },
{ "y-dist", 50 }
}
}
};
}
public async Task<IDictionary<string, object>> PrepareRequestObject(Guid txId, SigningRequest request)
{
var structuredScopeClaims = new Dictionary<string, object>
{
{
"signObject", new Dictionary<string, object>
{
{ "fields", new List<object>() }
}
}
};
if (request.DocumentObject != null)
{
structuredScopeClaims["documentObject"] = await PrepareDocumentClaims(request.DocumentObject.Document, 0);
}
if (request.DocumentObjects != null)
{
structuredScopeClaims["documentObjects"] = new Dictionary<string, object>
{
{ "envelope_name", request.DocumentObjects.EnvelopeName },
{ "documents", await Task.WhenAll(request.DocumentObjects.Documents.Select(async (x, i) => await PrepareDocumentClaims(x, i + 1))) }
};
}
return new Dictionary<string, object>
{
{ "txn", txId.ToString() },
{ "client_id", _configuration["BankID:ClientID"] },
{ "nonce", txId.ToString() },
{ "state", txId.ToString() },
{ "max_age", 3600 },
{ "response_type", "code" },
{ "scope", "openid offline_access" },
{ "structured_scope", structuredScopeClaims },
};
}
public async Task<JwkSet> DownloadJWKS(OpenIdConnectConfiguration config, CancellationToken ct = default)
{
_logger.LogDebug("Downloading JWKS from {JwksUri}", config.JwksUri);
using var client = _httpClientFactory.CreateClient();
var resp = await client.GetStringAsync(config.JwksUri, ct);
return JwkSet.FromJson(resp, new JsonMapper());
}
internal Jwk GetSigningJwkPublic()
{
Jwk key = new Jwk(_cert.GetECDsaPublicKey(), isPrivate: false);
key.KeyId = "key1";
return key;
}
internal ECDsa GetSigningKeyPrivate()
{
return _cert.GetECDsaPrivateKey();
}
/// <summary>
/// Transforms request object into encrypted JWT
/// </summary>
/// <param name="jwks">BankID encryption keys, get those from DownloadJWKS</param>
/// <param name="requestObject">Request object claims, get these from PrepareRequestObject</param>
/// <returns>JWE request object</returns>
public string EncryptRequestObjectIntoJWE(JwkSet jwks, IDictionary<string, object> requestObject)
{
_logger.LogInformation("Creating JWE from request object");
var signKey = GetSigningKeyPrivate();
string signedToken = Jose.JWT.Encode(requestObject, signKey, JwsAlgorithm.ES256);
var encJwk = jwks.Keys.First(x => x.Use == "enc");
string encryptedToken = Jose.JWT.EncodeBytes(Encoding.ASCII.GetBytes(signedToken), encJwk, JweAlgorithm.RSA_OAEP_256, JweEncryption.A256GCM,
extraHeaders: new
Dictionary<string,
object>
{
{ "kid", encJwk.KeyId }
});
return encryptedToken;
}
/// <summary>
/// Calls BankID ROS EndPoint with a request object encrypted as JWE
/// </summary>
/// <param name="jwe">Encrypted request object. Get this from EncryptRequestObjectIntoJWE</param>
/// <returns>Returns ROS EndPoint response</returns>
public async Task<RosResponse> RegisterRequestObject(string jwe, OpenIdConnectConfiguration config, CancellationToken ct = default)
{
var url = config.AdditionalData["ros_endpoint"].ToString();
_logger.LogInformation("Calling ROS at {Url}", url);
using var client = _httpClientFactory.CreateClient();
var content = new StringContent(jwe, Encoding.Default, "application/jwe");
var resp = await client.PostAsync(url, content, ct);
resp.EnsureSuccessStatusCode();
var data = await resp.Content.ReadFromJsonAsync<RosResponse>(cancellationToken: ct);
return data;
}
public async Task UploadFile(Uri url, byte[] file, CancellationToken ct = default)
{
_logger.LogInformation("Uploading file to {Url}", url);
using var client = _httpClientFactory.CreateClient();
var form = new MultipartFormDataContent();
form.Add(new ByteArrayContent(file)
{
Headers = { ContentType = new MediaTypeHeaderValue("application/pdf") }
}, "file", "doc.pdf");
var resp = await client.PostAsync(url, form, ct);
resp.EnsureSuccessStatusCode();
}
public async Task UploadFiles(SigningRequest request, RosResponse ros, CancellationToken ct = default)
{
if (request.DocumentObject != null)
{
if (ros.UploadUri == null)
{
throw new InvalidOperationException();
}
await UploadFile(new Uri(ros.UploadUri), request.DocumentObject.Document.Contents, ct);
}
if (request.DocumentObjects != null)
{
if (ros.UploadUris == null)
{
throw new InvalidOperationException();
}
foreach (var doc in request.DocumentObjects.Documents)
{
var uri = ros.UploadUris[doc.Id];
if (uri == null)
{
throw new InvalidOperationException();
}
await UploadFile(new Uri(uri), doc.Contents, ct);
}
}
}
public async Task<Tokens> ExchangeCodeForTokens(string code, string originalRedirectUri, OpenIdConnectConfiguration config, CancellationToken ct = default)
{
using var client = _httpClientFactory.CreateClient();
var content = new FormUrlEncodedContent(new Dictionary<string, string>
{
{ "code", code },
{ "grant_type", "authorization_code" },
{ "client_id", _configuration["BankID:ClientID"] },
{ "client_secret", _configuration["BankID:ClientSecret"] },
{ "redirect_uri", originalRedirectUri },
}.ToList());
var resp = await client.PostAsync(config.TokenEndpoint, content, ct);
resp.EnsureSuccessStatusCode();
var data = await resp.Content.ReadFromJsonAsync<Tokens>(cancellationToken: ct);
return data;
}
public async Task<string> GetAuthorizeRedirectUri(string requestUri, string callbackUri, CancellationToken ct = default)
{
var config = await _configurationManager.GetConfigurationAsync(ct);
var oidcParams = new Dictionary<string, string?>
{
{ "request_uri", requestUri },
{ "redirect_uri", callbackUri },
};
return QueryHelpers.AddQueryString(config.AdditionalData["authorize_endpoint"].ToString(), oidcParams);
}
}
public class Tokens
{
[JsonPropertyName("id_token")] public string IdToken { get; init; }
[JsonPropertyName("access_token")] public string AccessToken { get; init; }
}
public class RosResponse
{
[JsonPropertyName("request_uri")] public string RequestUri { get; init; }
[JsonPropertyName("upload_uri")] public string? UploadUri { get; init; }
[JsonPropertyName("upload_uris")] public Dictionary<string, string>? UploadUris { get; init; }
[JsonPropertyName("exp")] public long? Exp { get; init; }
}
public class DocumentDescriptor
{
public DocumentDescriptor(FileInfo path)
{
Contents = File.ReadAllBytes(path.FullName);
var pdf = PdfDocument.FromFile(path.FullName);
Id = pdf.MetaData.CustomProperties["documentId"];
if (string.IsNullOrEmpty(Id))
{
throw new InvalidOperationException($"Unable to read documentId metadata from pdf at {path.FullName}");
}
Title = pdf.MetaData.Title;
Subject = pdf.MetaData.Subject;
CreationTime = pdf.MetaData.CreationDate;
Author = pdf.MetaData.Author;
Pages = pdf.PageCount;
}
public byte[] Contents { get; }
public string Id { get; }
public string Title { get; }
public string Subject { get; }
public string Author { get; }
public int Pages { get; }
public DateTime CreationTime { get; }
}
public record DocumentObjects(DocumentDescriptor[] Documents, string EnvelopeName);
public record DocumentObject(DocumentDescriptor Document);
public record SigningRequest(DocumentObjects? DocumentObjects, DocumentObject? DocumentObject);