-
Notifications
You must be signed in to change notification settings - Fork 183
/
JWT.cs
611 lines (533 loc) · 33.8 KB
/
JWT.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
using Jose;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Jose
{
public enum JwsAlgorithm
{
none,
HS256,
HS384,
HS512,
RS256,
RS384,
RS512,
PS256,
PS384,
PS512,
ES256, // ECDSA using P-256 curve and SHA-256 hash
ES384, // ECDSA using P-384 curve and SHA-384 hash
ES512 // ECDSA using P-521 curve and SHA-512 hash
}
public enum JweAlgorithm
{
RSA1_5, //RSAES with PKCS #1 v1.5 padding, RFC 3447
RSA_OAEP, //RSAES using Optimal Asymmetric Jwe Padding, RFC 3447
RSA_OAEP_256, //RSAES with SHA-256 using Optimal Asymmetric Jwe Padding, RFC 3447
DIR, //Direct use of pre-shared symmetric key
A128KW, //AES Key Wrap Algorithm using 128 bit keys, RFC 3394
A192KW, //AES Key Wrap Algorithm using 192 bit keys, RFC 3394
A256KW, //AES Key Wrap Algorithm using 256 bit keys, RFC 3394
ECDH_ES, //Elliptic Curve Diffie Hellman key agreement
ECDH_ES_A128KW, //Elliptic Curve Diffie Hellman key agreement with AES Key Wrap using 128 bit key
ECDH_ES_A192KW, //Elliptic Curve Diffie Hellman key agreement with AES Key Wrap using 192 bit key
ECDH_ES_A256KW, //Elliptic Curve Diffie Hellman key agreement with AES Key Wrap using 256 bit key
PBES2_HS256_A128KW, //Password Based Jwe using PBES2 schemes with HMAC-SHA and AES Key Wrap using 128 bit key
PBES2_HS384_A192KW, //Password Based Jwe using PBES2 schemes with HMAC-SHA and AES Key Wrap using 192 bit key
PBES2_HS512_A256KW, //Password Based Jwe using PBES2 schemes with HMAC-SHA and AES Key Wrap using 256 bit key
A128GCMKW, //AES GCM Key Wrap Algorithm using 128 bit keys
A192GCMKW, //AES GCM Key Wrap Algorithm using 192 bit keys
A256GCMKW //AES GCM Key Wrap Algorithm using 256 bit keys
}
public enum JweEncryption
{
A128CBC_HS256, //AES_128_CBC_HMAC_SHA_256 authenticated encryption using a 256 bit key.
A192CBC_HS384, //AES_192_CBC_HMAC_SHA_384 authenticated encryption using a 384 bit key.
A256CBC_HS512, //AES_256_CBC_HMAC_SHA_512 authenticated encryption using a 512 bit key.
A128GCM,
A192GCM,
A256GCM
}
public enum JweCompression
{
DEF //Deflate compression
}
/// <summary>
/// Provides methods for encoding and decoding JSON Web Tokens.
/// </summary>
public static class JWT
{
private static readonly JwtSettings defaultSettings = new JwtSettings();
/// <summary>
/// Global default settings for JWT.
/// </summary>
public static JwtSettings DefaultSettings
{
get { return defaultSettings; }
}
[Obsolete("Custom JsonMappers should be set in DefaultSettings")]
public static IJsonMapper JsonMapper
{
set { defaultSettings.RegisterMapper(value); }
}
/// <summary>
/// Parses JWT token, extracts and unmarshal headers as IDictionary<string, object>.
/// This method is NOT performing integrity checking.
/// </summary>
/// <param name="token">signed JWT token</param>
/// <param name="settings">optional settings to override global DefaultSettings</param>
/// <returns>unmarshalled headers</returns>
public static IDictionary<string, object> Headers(string token, JwtSettings settings = null)
{
return Headers<IDictionary<string, object>>(token, settings);
}
/// <summary>
/// Parses JWT token, extracts and attempts to unmarshal headers to requested type
/// This method is NOT performing integrity checking.
/// </summary>
/// <param name="token">signed JWT token</param>
/// <param name="settings">optional settings to override global DefaultSettings</param>
/// <typeparam name="T">desired type after unmarshalling</typeparam>
/// <returns>unmarshalled headers</returns>
public static T Headers<T>(string token, JwtSettings settings = null)
{
var parts = Compact.Iterate(token);
return GetSettings(settings).JsonMapper.Parse<T>(Encoding.UTF8.GetString(parts.Next()));
}
/// <summary>
/// Parses signed JWT token, extracts and returns payload part as string
/// This method is NOT supported for encrypted JWT tokens.
/// This method is NOT performing integrity checking.
/// </summary>
/// <param name="token">signed JWT token</param>
/// <returns>unmarshalled payload</returns>
/// <exception cref="JoseException">if encrypted JWT token is provided</exception>
public static string Payload(string token, bool b64 = true)
{
var bytes = PayloadBytes(token, b64);
return Encoding.UTF8.GetString(bytes);
}
/// <summary>
/// Parses signed JWT token, extracts and returns payload part as binary data.
/// This method is NOT supported for encrypted JWT tokens.
/// This method is NOT performing integrity checking.
/// </summary>
/// <param name="token">signed JWT token</param>
/// <returns>unmarshalled payload</returns>
/// <exception cref="JoseException">if encrypted JWT token is provided</exception>
public static byte[] PayloadBytes(string token, bool b64 = true)
{
var parts = Compact.Iterate(token);
if (parts.Count < 3)
{
throw new JoseException(
"The given token doesn't follow JWT format and must contains at least three parts.");
}
if (parts.Count > 3)
{
throw new JoseException(
"Getting payload for encrypted tokens is not supported. Please use Jose.JWT.Decode() method instead.");
}
parts.Next(false); //skip header
return parts.Next(b64);
}
/// <summary>
/// Parses signed JWT token, extracts payload part and attempts to unmarshal string to requested type with configured json mapper.
/// This method is NOT supported for encrypted JWT tokens.
/// This method is NOT performing integrity checking.
/// </summary>
/// <typeparam name="T">desired type after unmarshalling</typeparam>
/// <param name="token">signed JWT token</param>
/// <param name="settings">optional settings to override global DefaultSettings</param>
/// <returns>unmarshalled payload</returns>
/// <exception cref="JoseException">if encrypted JWT token is provided</exception>
public static T Payload<T>(string token, JwtSettings settings = null)
{
return GetSettings(settings).JsonMapper.Parse<T>(Payload(token));
}
/// <summary>
/// Serialize and encodes object to JWT token and applies requested encryption/compression algorithms.
/// </summary>
/// <param name="payload">json string to encode</param>
/// <param name="key">key for encryption, suitable for provided JWS algorithm, can be null.</param>
/// <param name="alg">JWT algorithm to be used.</param>
/// <param name="enc">encryption algorithm to be used.</param>
/// <param name="compression">optional compression type to use.</param>
/// <param name="extraHeaders">optional extra headers to pass along with the payload.</param>
/// <param name="settings">optional settings to override global DefaultSettings</param>
/// <returns>JWT in compact serialization form, encrypted and/or compressed.</returns>
public static string Encode(object payload, object key, JweAlgorithm alg, JweEncryption enc, JweCompression? compression = null, IDictionary<string, object> extraHeaders = null, JwtSettings settings = null)
{
return Encode(GetSettings(settings).JsonMapper.Serialize(payload), key, alg, enc, compression, extraHeaders, settings);
}
/// <summary>
/// Encodes given json string to JWT token and applies requested encryption/compression algorithms.
/// Json string to encode will be obtained via configured IJsonMapper implementation.
/// </summary>
/// <param name="payload">json string to encode (not null or whitespace)</param>
/// <param name="key">key for encryption, suitable for provided JWS algorithm, can be null.</param>
/// <param name="alg">JWT algorithm to be used.</param>
/// <param name="enc">encryption algorithm to be used.</param>
/// <param name="compression">optional compression type to use.</param>
/// <param name="extraHeaders">optional extra headers to pass along with the payload.</param>
/// <param name="settings">optional settings to override global DefaultSettings</param>
/// <returns>JWT in compact serialization form, encrypted and/or compressed.</returns>
public static string Encode(string payload, object key, JweAlgorithm alg, JweEncryption enc, JweCompression? compression = null, IDictionary<string, object> extraHeaders = null, JwtSettings settings = null)
{
byte[] plainText = Encoding.UTF8.GetBytes(payload);
return EncodeBytes(plainText, key, alg, enc, compression, extraHeaders, settings);
}
/// <summary>
/// Encodes given binary data to JWT token and applies requested encryption/compression algorithms.
/// </summary>
/// <param name="payload">Binary data to encode (not null)</param>
/// <param name="key">key for encryption, suitable for provided JWS algorithm, can be null.</param>
/// <param name="alg">JWT algorithm to be used.</param>
/// <param name="enc">encryption algorithm to be used.</param>
/// <param name="compression">optional compression type to use.</param>
/// <param name="extraHeaders">optional extra headers to pass along with the payload.</param>
/// <param name="settings">optional settings to override global DefaultSettings</param>
/// <returns>JWT in compact serialization form, encrypted and/or compressed.</returns>
public static string EncodeBytes(byte[] payload, object key, JweAlgorithm alg, JweEncryption enc, JweCompression? compression = null, IDictionary<string, object> extraHeaders = null, JwtSettings settings = null)
{
return JWE.EncryptBytes(payload, new JweRecipient[] { new JweRecipient(alg, key) }, enc, aad: null, SerializationMode.Compact, compression, extraHeaders, null, settings);
}
/// <summary>
/// Serialize and encodes object to JWT token and sign it using given algorithm.
/// Json string to encode will be obtained via configured IJsonMapper implementation.
/// </summary>
/// <param name="payload">object to map to json string and encode</param>
/// <param name="key">key for signing, suitable for provided JWS algorithm, can be null.</param>
/// <param name="algorithm">JWT algorithm to be used.</param>
/// <param name="extraHeaders">optional extra headers to pass along with the payload.</param>
/// <param name="settings">optional settings to override global DefaultSettings</param>
/// <param name="options">additional encoding options</param>
/// <returns>JWT in compact serialization form, digitally signed.</returns>
public static string Encode(object payload, object key, JwsAlgorithm algorithm, IDictionary<string, object> extraHeaders = null, JwtSettings settings = null, JwtOptions options = null)
{
return Encode(GetSettings(settings).JsonMapper.Serialize(payload), key, algorithm, extraHeaders, settings, options);
}
/// <summary>
/// Encodes given json string to JWT token and sign it using given algorithm.
/// </summary>
/// <param name="payload">json string to encode (not null or whitespace)</param>
/// <param name="key">key for signing, suitable for provided JWS algorithm, can be null.</param>
/// <param name="algorithm">JWT algorithm to be used.</param>
/// <param name="extraHeaders">optional extra headers to pass along with the payload.</param>
/// <param name="settings">optional settings to override global DefaultSettings</param>
/// <param name="options">additional encoding options</param>
/// <returns>JWT in compact serialization form, digitally signed.</returns>
public static string Encode(string payload, object key, JwsAlgorithm algorithm, IDictionary<string, object> extraHeaders = null, JwtSettings settings = null, JwtOptions options = null)
{
byte[] payloadBytes = Encoding.UTF8.GetBytes(payload);
return EncodeBytes(payloadBytes, key, algorithm, extraHeaders, settings, options);
}
/// <summary>
/// Encodes given binary data to JWT token and sign it using given algorithm.
/// </summary>
/// <param name="payload">Binary data to encode (not null)</param>
/// <param name="key">key for signing, suitable for provided JWS algorithm, can be null.</param>
/// <param name="algorithm">JWT algorithm to be used.</param>
/// <param name="extraHeaders">optional extra headers to pass along with the payload.</param>
/// <param name="settings">optional settings to override global DefaultSettings</param>
/// <param name="options">additional encoding options</param>
/// <returns>JWT in compact serialization form, digitally signed.</returns>
public static string EncodeBytes(byte[] payload, object key, JwsAlgorithm algorithm, IDictionary<string, object> extraHeaders = null, JwtSettings settings = null, JwtOptions options = null)
{
if (payload == null)
throw new ArgumentNullException(nameof(payload));
var jwtSettings = GetSettings(settings);
var jwtOptions = options ?? JwtOptions.Default;
var jwtHeader = new Dictionary<string, object> { { "alg", jwtSettings.JwsHeaderValue(algorithm) } };
if (extraHeaders == null) //allow overload, but keep backward compatible defaults
{
extraHeaders = new Dictionary<string, object> { { "typ", "JWT" } };
}
if (!jwtOptions.EncodePayload)
{
jwtHeader["b64"] = false;
jwtHeader["crit"] = Collections.Union(new[] { "b64" }, Dictionaries.Get<object>(extraHeaders, "crit"));
}
Dictionaries.Append(jwtHeader, extraHeaders);
byte[] headerBytes = Encoding.UTF8.GetBytes(jwtSettings.JsonMapper.Serialize(jwtHeader));
var jwsAlgorithm = jwtSettings.Jws(algorithm);
if (jwsAlgorithm == null)
{
throw new JoseException(string.Format("Unsupported JWS algorithm requested: {0}", algorithm));
}
byte[] signature = jwsAlgorithm.Sign(securedInput(headerBytes, payload, jwtOptions.EncodePayload), key);
byte[] payloadBytes = jwtOptions.DetachPayload ? new byte[0] : payload;
return jwtOptions.EncodePayload
? Compact.Serialize(headerBytes, payloadBytes, signature)
: Compact.Serialize(headerBytes, Encoding.UTF8.GetString(payloadBytes), signature);
}
/// <summary>
/// Decodes JWT token by performing necessary decompression/decryption and signature verification as defined in JWT token header.
/// Resulting json string is returned untouched (e.g. no parsing or mapping)
/// </summary>
/// <param name="token">JWT token in compact serialization form.</param>
/// <param name="key">key for decoding suitable for JWT algorithm used.</param>
/// <param name="alg">The algorithm type that we expect to receive in the header.</param>
/// <param name="enc">The encryption type that we expect to receive in the header.</param>
/// <param name="settings">optional settings to override global DefaultSettings</param>
/// <returns>decoded json string</returns>
/// <exception cref="IntegrityException">if signature validation failed</exception>
/// <exception cref="EncryptionException">if JWT token can't be decrypted</exception>
/// <exception cref="InvalidAlgorithmException">if JWT signature, encryption or compression algorithm is not supported</exception>
public static string Decode(string token, object key, JweAlgorithm alg, JweEncryption enc, JwtSettings settings = null)
{
return Decode(token, key, null, alg, enc, settings);
}
/// <summary>
/// Decodes JWT token by performing necessary decompression/decryption and signature verification as defined in JWT token header.
/// Resulting bytes of the payload are returned untouched (e.g. no parsing or mapping)
/// </summary>
/// <param name="token">JWT token in compact serialization form.</param>
/// <param name="key">key for decoding suitable for JWT algorithm used.</param>
/// <param name="alg">The algorithm type that we expect to receive in the header.</param>
/// <param name="enc">The encryption type that we expect to receive in the header.</param>
/// <param name="settings">optional settings to override global DefaultSettings</param>
/// <returns>Decrypted payload as binary data</returns>
/// <exception cref="IntegrityException">if signature validation failed</exception>
/// <exception cref="EncryptionException">if JWT token can't be decrypted</exception>
/// <exception cref="InvalidAlgorithmException">if JWT signature, encryption or compression algorithm is not supported</exception>
public static byte[] DecodeBytes(string token, object key, JweAlgorithm alg, JweEncryption enc, JwtSettings settings = null)
{
return DecodeBytes(Compact.Iterate(token), key, null, alg, enc, settings);
}
/// <summary>
/// Decodes JWT token by performing necessary decompression/decryption and signature verification as defined in JWT token header.
/// Resulting json string is returned untouched (e.g. no parsing or mapping)
/// </summary>
/// <param name="token">JWT token in compact serialization form.</param>
/// <param name="key">key for decoding suitable for JWT algorithm used.</param>
/// <param name="alg">The algorithm type that we expect to receive in the header.</param>
/// <param name="settings">optional settings to override global DefaultSettings</param>
/// <param name="payload">optional detached payload</param>
/// <returns>decoded json string</returns>
/// <exception cref="IntegrityException">if signature validation failed</exception>
/// <exception cref="EncryptionException">if JWT token can't be decrypted</exception>
/// <exception cref="InvalidAlgorithmException">if JWT signature, encryption or compression algorithm is not supported</exception>
public static string Decode(string token, object key, JwsAlgorithm alg, JwtSettings settings = null, string payload = null)
{
return Decode(token, key, alg, null, null, settings, payload);
}
/// <summary>
/// Decodes JWT token by performing necessary decompression/decryption and signature verification as defined in JWT token header.
/// Resulting bytes of the payload are returned untouched (e.g. no parsing or mapping)
/// </summary>
/// <param name="token">JWT token in compact serialization form.</param>
/// <param name="key">key for decoding suitable for JWT algorithm used.</param>
/// <param name="alg">The algorithm type that we expect to receive in the header.</param>
/// <param name="settings">optional settings to override global DefaultSettings</param>
/// <param name="payload">optional detached payload</param>
/// <returns>The payload as binary data</returns>
/// <exception cref="IntegrityException">if signature validation failed</exception>
/// <exception cref="EncryptionException">if JWT token can't be decrypted</exception>
/// <exception cref="InvalidAlgorithmException">if JWT signature, encryption or compression algorithm is not supported</exception>
public static byte[] DecodeBytes(string token, object key, JwsAlgorithm alg, JwtSettings settings = null, byte[] payload = null)
{
return DecodeBytes(Compact.Iterate(token), key, alg, null, null, settings, payload);
}
/// <summary>
/// Decodes JWT token by performing necessary decompression/decryption and signature verification as defined in JWT token header.
/// Resulting json string is returned untouched (e.g. no parsing or mapping)
/// </summary>
/// <param name="token">JWT token in compact serialization form.</param>
/// <param name="key">key for decoding suitable for JWT algorithm used, can be null.</param>
/// <param name="settings">optional settings to override global DefaultSettings</param>
/// <param name="payload">optional detached payload</param>
/// <returns>decoded json string</returns>
/// <exception cref="IntegrityException">if signature validation failed</exception>
/// <exception cref="EncryptionException">if JWT token can't be decrypted</exception>
/// <exception cref="InvalidAlgorithmException">if JWT signature, encryption or compression algorithm is not supported</exception>
public static string Decode(string token, object key = null, JwtSettings settings = null, string payload = null)
{
return Decode(token, key, null, null, null, settings, payload);
}
/// <summary>
/// Decodes JWT token by performing necessary decompression/decryption and signature verification as defined in JWT token header.
/// Resulting binary payload is returned untouched (e.g. no parsing or mapping)
/// </summary>
/// <param name="token">JWT token in compact serialization form.</param>
/// <param name="key">key for decoding suitable for JWT algorithm used, can be null.</param>
/// <param name="settings">optional settings to override global DefaultSettings</param>
/// <param name="payload">optional detached payload</param>
/// <returns>The payload as binary data</returns>
/// <exception cref="IntegrityException">if signature validation failed</exception>
/// <exception cref="EncryptionException">if JWT token can't be decrypted</exception>
/// <exception cref="InvalidAlgorithmException">if JWT signature, encryption or compression algorithm is not supported</exception>
public static byte[] DecodeBytes(string token, object key = null, JwtSettings settings = null, byte[] payload = null)
{
return DecodeBytes(Compact.Iterate(token), key, null, null, null, settings, payload);
}
/// <summary>
/// Decodes JWT token by performing necessary decompression/decryption and signature verification as defined in JWT token header.
/// Resulting json string will be parsed and mapped to desired type via configured IJsonMapper implementation.
/// </summary>
/// <typeparam name="T">Deserid object type after json mapping</typeparam>
/// <param name="token">JWT token in compact serialization form.</param>
/// <param name="key">key for decoding suitable for JWT algorithm used.</param>
/// <param name="alg">The algorithm type that we expect to receive in the header.</param>
/// <param name="enc">The encryption type that we expect to receive in the header.</param>
/// <param name="settings">optional settings to override global DefaultSettings</param>
/// <returns>object of provided T, result of decoded json mapping</returns>
/// <exception cref="IntegrityException">if signature validation failed</exception>
/// <exception cref="EncryptionException">if JWT token can't be decrypted</exception>
/// <exception cref="InvalidAlgorithmException">if JWT signature, encryption or compression algorithm is not supported</exception>
public static T Decode<T>(string token, object key, JweAlgorithm alg, JweEncryption enc, JwtSettings settings = null)
{
return GetSettings(settings).JsonMapper.Parse<T>(Decode(token, key, alg, enc, settings));
}
/// <summary>
/// Decodes JWT token by performing necessary decompression/decryption and signature verification as defined in JWT token header.
/// Resulting json string will be parsed and mapped to desired type via configured IJsonMapper implementation.
/// </summary>
/// <typeparam name="T">Deserid object type after json mapping</typeparam>
/// <param name="token">JWT token in compact serialization form.</param>
/// <param name="key">key for decoding suitable for JWT algorithm used.</param>
/// <param name="alg">The algorithm type that we expect to receive in the header.</param>
/// <param name="settings">optional settings to override global DefaultSettings</param>
/// <returns>object of provided T, result of decoded json mapping</returns>
/// <exception cref="IntegrityException">if signature validation failed</exception>
/// <exception cref="EncryptionException">if JWT token can't be decrypted</exception>
/// <exception cref="InvalidAlgorithmException">if JWT signature, encryption or compression algorithm is not supported</exception>
public static T Decode<T>(string token, object key, JwsAlgorithm alg, JwtSettings settings = null)
{
return GetSettings(settings).JsonMapper.Parse<T>(Decode(token, key, alg, settings));
}
/// <summary>
/// Decodes JWT token by performing necessary decompression/decryption and signature verification as defined in JWT token header.
/// Resulting json string will be parsed and mapped to desired type via configured IJsonMapper implementation.
/// </summary>
/// <typeparam name="T">Deserid object type after json mapping</typeparam>
/// <param name="token">JWT token in compact serialization form.</param>
/// <param name="key">key for decoding suitable for JWT algorithm used, can be null.</param>
/// <param name="settings">optional settings to override global DefaultSettings</param>
/// <returns>object of provided T, result of decoded json mapping</returns>
/// <exception cref="IntegrityException">if signature validation failed</exception>
/// <exception cref="EncryptionException">if JWT token can't be decrypted</exception>
/// <exception cref="InvalidAlgorithmException">if JWT signature, encryption or compression algorithm is not supported</exception>
public static T Decode<T>(string token, object key = null, JwtSettings settings = null)
{
return GetSettings(settings).JsonMapper.Parse<T>(Decode(token, key, settings));
}
public static string Verify(string token, object key, JwsAlgorithm? alg = null, JwtSettings settings = null, string payload = null)
{
var detached = payload != null ? Encoding.UTF8.GetBytes(payload) : null;
return Encoding.UTF8.GetString(VerifyBytes(token, key, alg, settings, detached));
}
public static byte[] VerifyBytes(string token, object key, JwsAlgorithm? alg = null, JwtSettings settings = null, byte[] payload = null)
{
var parts = Compact.Iterate(token);
if (parts.Count != 3)
{
throw new JoseException("Unexpected number of parts in signed token: " + parts.Count);
}
return DecodeBytes(parts, key, alg, null, null, settings, payload);
}
public static string Decrypt(string token, object key, JweAlgorithm? alg = null, JweEncryption? enc = null, JwtSettings settings = null)
{
return Encoding.UTF8.GetString(DecryptBytes(token, key, alg, enc, settings));
}
public static byte[] DecryptBytes(string token, object key, JweAlgorithm? alg = null, JweEncryption? enc = null, JwtSettings settings = null)
{
var parts = Compact.Iterate(token);
if (parts.Count != 5)
{
throw new JoseException("Unexpected number of parts in encrypted token: " + parts.Count);
}
return DecodeBytes(parts, key, null, alg, enc, settings);
}
private static byte[] DecodeBytes(Compact.Iterator parts, object key = null, JwsAlgorithm? expectedJwsAlg = null, JweAlgorithm? expectedJweAlg = null, JweEncryption? expectedJweEnc = null, JwtSettings settings = null, byte[] payload = null)
{
Ensure.IsNotEmpty(parts.Token, "Incoming token expected to be in compact serialization form, not empty, whitespace or null.");
if (parts.Count == 5) //encrypted JWT
{
if (expectedJwsAlg != null)
{
throw new InvalidAlgorithmException("Encrypted tokens can't assert signing algorithm type.");
}
return JWE.Decrypt(parts.Token, key, expectedJweAlg, expectedJweEnc, settings).PlaintextBytes;
}
else if (parts.Count == 3) // signed JWT
{
if (expectedJweAlg != null || expectedJweEnc !=null)
{
throw new InvalidAlgorithmException("Signed tokens can't assert encryption type.");
}
//signed or plain JWT
var jwtSettings = GetSettings(settings);
byte[] header = parts.Next();
var headerData = jwtSettings.JsonMapper.Parse<IDictionary<string, object>>(Encoding.UTF8.GetString(header));
bool b64 = true;
object value;
if (headerData.TryGetValue("b64", out value))
{
b64 = (bool)value;
}
byte[] contentPayload = parts.Next(b64);
byte[] signature = parts.Next();
var effectivePayload = payload ?? contentPayload;
var algorithm = (string)headerData["alg"];
var jwsAlgorithm = jwtSettings.JwsAlgorithmFromHeader(algorithm);
if (expectedJwsAlg != null && expectedJwsAlg != jwsAlgorithm)
{
throw new InvalidAlgorithmException(
"The algorithm type passed to the Decode method did not match the algorithm type in the header.");
}
var jwsAlgorithmImpl = jwtSettings.Jws(jwsAlgorithm);
if (jwsAlgorithmImpl == null)
{
throw new JoseException(string.Format("Unsupported JWS algorithm requested: {0}", algorithm));
}
if (!jwsAlgorithmImpl.Verify(signature, securedInput(header, effectivePayload, b64), key))
{
throw new IntegrityException("Invalid signature.");
}
return effectivePayload;
}
throw new JoseException("Expected compact serialized token with 3 or 5 parts, but got: " + parts.Count);
}
private static string Decode(string token, object key = null, JwsAlgorithm? jwsAlg = null, JweAlgorithm? jweAlg = null, JweEncryption? jweEnc = null, JwtSettings settings = null, string payload = null)
{
var detached = payload != null ? Encoding.UTF8.GetBytes(payload) : null;
var payloadBytes = DecodeBytes(Compact.Iterate(token), key, jwsAlg, jweAlg, jweEnc, settings, detached);
return Encoding.UTF8.GetString(payloadBytes);
}
private static JwtSettings GetSettings(JwtSettings settings)
{
return settings ?? defaultSettings;
}
private static byte[] securedInput(byte[] header, byte[] payload, bool b64)
{
return b64
? Encoding.UTF8.GetBytes(Compact.Serialize(header, payload))
: Arrays.Concat(Encoding.UTF8.GetBytes(Compact.Serialize(header)),
Encoding.UTF8.GetBytes("."),
payload);
}
}
public class JoseException : Exception
{
public JoseException(string message) : base(message) { }
public JoseException(string message, Exception innerException) : base(message, innerException) { }
}
public class IntegrityException : JoseException
{
public IntegrityException(string message) : base(message) { }
public IntegrityException(string message, Exception innerException) : base(message, innerException) { }
}
public class EncryptionException : JoseException
{
public EncryptionException(string message) : base(message) { }
public EncryptionException(string message, Exception innerException) : base(message, innerException) { }
}
public class InvalidAlgorithmException : JoseException
{
public InvalidAlgorithmException(string message) : base(message) { }
public InvalidAlgorithmException(string message, Exception innerException) : base(message, innerException) { }
}
}