/
JwtAuthProviderReader.cs
1190 lines (992 loc) · 42.9 KB
/
JwtAuthProviderReader.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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using ServiceStack.Configuration;
using ServiceStack.Host;
using ServiceStack.Host.Handlers;
using ServiceStack.Text;
using ServiceStack.Web;
namespace ServiceStack.Auth;
/// <summary>
/// Enable access to protected Services using JWT Tokens
/// </summary>
public class JwtAuthProviderReader : AuthProvider, IAuthWithRequest
{
public override string Type => "Bearer";
public static RsaKeyLengths UseRsaKeyLength = RsaKeyLengths.Bit2048;
public const string Name = AuthenticateService.JwtProvider;
public const string Realm = "/auth/" + AuthenticateService.JwtProvider;
public static readonly HashSet<string> IgnoreForOperationTypes = [
nameof(StaticFileHandler)
];
/// <summary>
/// Different HMAC Algorithms supported
/// </summary>
public static readonly Dictionary<string, Func<byte[], byte[], byte[]>> HmacAlgorithms = new()
{
{ "HS256", (key, value) => {
using var sha = new HMACSHA256(key);
return sha.ComputeHash(value);
} },
{ "HS384", (key, value) => {
using var sha = new HMACSHA384(key);
return sha.ComputeHash(value);
} },
{ "HS512", (key, value) => {
using var sha = new HMACSHA512(key);
return sha.ComputeHash(value);
} }
};
/// <summary>
/// Different RSA Signing Algorithms supported
/// </summary>
public static readonly Dictionary<string, Func<RSAParameters, byte[], byte[]>> RsaSignAlgorithms = new()
{
{ "RS256", (key, value) => RsaUtils.Authenticate(value, key, "SHA256", UseRsaKeyLength) },
{ "RS384", (key, value) => RsaUtils.Authenticate(value, key, "SHA384", UseRsaKeyLength) },
{ "RS512", (key, value) => RsaUtils.Authenticate(value, key, "SHA512", UseRsaKeyLength) },
};
public static readonly Dictionary<string, Func<RSAParameters, byte[], byte[], bool>> RsaVerifyAlgorithms = new()
{
{ "RS256", (key, value, sig) => RsaUtils.Verify(value, sig, key, "SHA256", UseRsaKeyLength) },
{ "RS384", (key, value, sig) => RsaUtils.Verify(value, sig, key, "SHA384", UseRsaKeyLength) },
{ "RS512", (key, value, sig) => RsaUtils.Verify(value, sig, key, "SHA512", UseRsaKeyLength) },
};
/// <summary>
/// Whether to only allow access via API Key from a secure connection. (default true)
/// </summary>
public bool RequireSecureConnection { get; set; }
/// <summary>
/// Run custom filter after JWT Header is created
/// </summary>
public Action<JsonObject, IAuthSession> CreateHeaderFilter { get; set; }
/// <summary>
/// Run custom filter after JWT Payload is created
/// </summary>
public Action<JsonObject, IAuthSession> CreatePayloadFilter { get; set; }
/// <summary>
/// Run custom filter after session is restored from a JWT Token
/// </summary>
public Action<IAuthSession, JsonObject, IRequest> PopulateSessionFilter { get; set; }
/// <summary>
/// Run Async custom filter after session is restored from a JWT Token
/// </summary>
public Func<IAuthSession, JsonObject, IRequest, Task> PopulateSessionFilterAsync { get; set; }
/// <summary>
/// Whether to encrypt JWE Payload (default false).
/// Uses RSA-OAEP for Key Encryption and AES/128/CBC HMAC SHA256 for Content Encryption
/// </summary>
public bool EncryptPayload { get; set; }
/// <summary>
/// Which Hash Algorithm should be used to sign the JWT Token. (default HS256)
/// </summary>
public string HashAlgorithm { get; set; }
/// <summary>
/// Whether to only allow processing of JWT Tokens using the configured HashAlgorithm. (default true)
/// </summary>
public bool RequireHashAlgorithm { get; set; }
/// <summary>
/// The Issuer to embed in the token. (default ssjwt)
/// </summary>
public string Issuer { get; set; }
/// <summary>
/// The Audience to embed in the token. (default null)
/// </summary>
public string Audience
{
get => Audiences.Join(",");
set
{
Audiences.Clear();
if (!string.IsNullOrEmpty(value))
{
Audiences.Add(value);
}
}
}
/// <summary>
/// Embed Multiple Audiences in the token. (default none)
/// A JWT is valid if it contains ANY audience in this List
/// </summary>
public List<string> Audiences { get; set; }
/// <summary>
/// Tokens must contain aud which is validated
/// </summary>
public bool RequiresAudience { get; set; }
/// <summary>
/// What Id to use to identify the Key used to sign the token. (default First 3 chars of Base64 Key)
/// </summary>
public string KeyId { get; set; }
/// <summary>
/// The AuthKey used to sign the JWT Token
/// </summary>
public byte[] AuthKey { get; set; }
public string AuthKeyBase64
{
set => AuthKey = Convert.FromBase64String(value);
}
public byte[] GetAuthKey(IRequest req = null) => req.GetRuntimeConfig(nameof(AuthKey), AuthKey);
/// <summary>
/// Allow verification using multiple Auth keys
/// </summary>
public List<byte[]> FallbackAuthKeys { get; set; }
public List<byte[]> GetFallbackAuthKeys(IRequest req = null) => req.GetRuntimeConfig(nameof(FallbackAuthKeys), FallbackAuthKeys);
public RSAParameters? GetPrivateKey(IRequest req = null) => req.GetRuntimeConfig(nameof(PrivateKey), PrivateKey);
/// <summary>
/// The RSA Private Key used to Sign the JWT Token when RSA is used
/// </summary>
private RSAParameters? _privateKey;
public RSAParameters? PrivateKey
{
get => _privateKey;
set
{
_privateKey = value;
if (_privateKey != null)
PublicKey = _privateKey.Value.ToPublicRsaParameters();
}
}
/// <summary>
/// Convenient overload to initialize the Private Key via exported XML
/// </summary>
public string PrivateKeyXml
{
get => PrivateKey?.FromPrivateRSAParameters();
set => PrivateKey = value?.ToPrivateRSAParameters();
}
public RSAParameters? GetPublicKey(IRequest req = null) => req.GetRuntimeConfig(nameof(PublicKey), PublicKey);
/// <summary>
/// The RSA Public Key used to Verify the JWT Token when RSA is used
/// </summary>
public RSAParameters? PublicKey { get; set; }
/// <summary>
/// Convenient overload to initialize the Public Key via exported XML
/// </summary>
public string PublicKeyXml
{
get => PublicKey?.FromPublicRSAParameters();
set => PublicKey = value?.ToPublicRSAParameters();
}
/// <summary>
/// Allow verification using multiple public keys
/// </summary>
public List<RSAParameters> FallbackPublicKeys { get; set; }
public List<RSAParameters> GetFallbackPublicKeys(IRequest req = null) =>
req.GetRuntimeConfig(nameof(FallbackPublicKeys), FallbackPublicKeys);
/// <summary>
/// Allow verification using multiple private keys for JWE tokens
/// </summary>
public List<RSAParameters> FallbackPrivateKeys { get; set; }
public List<RSAParameters> GetFallbackPrivateKeys(IRequest req = null) =>
req.GetRuntimeConfig(nameof(FallbackPrivateKeys), FallbackPrivateKeys);
/// <summary>
/// How long should JWT Tokens be valid for. (default 14 days)
/// </summary>
public TimeSpan ExpireTokensIn { get; set; }
/// <summary>
/// How long should JWT Refresh Tokens be valid for. (default 365 days)
/// </summary>
public TimeSpan ExpireRefreshTokensIn { get; set; }
/// <summary>
/// Convenient overload to initialize ExpireTokensIn with an Integer
/// </summary>
public int ExpireTokensInDays
{
set
{
if (value > 0)
ExpireTokensIn = TimeSpan.FromDays(value);
}
}
/// <summary>
/// Allow custom logic to invalidate JWT Tokens
/// </summary>
public Func<JsonObject, IRequest, bool> ValidateToken { get; set; }
/// <summary>
/// Allow custom logic to invalidate Refresh Tokens
/// </summary>
public Func<JsonObject, IRequest, bool> ValidateRefreshToken { get; set; }
/// <summary>
/// Whether to invalidate all JWT Access Tokens issued before a specified date.
/// </summary>
public DateTime? InvalidateTokensIssuedBefore { get; set; }
/// <summary>
/// Whether to invalidate all JWT Refresh Tokens issued before a specified date.
/// </summary>
public DateTime? InvalidateRefreshTokensIssuedBefore { get; set; }
/// <summary>
/// Modify the registration of ConvertSessionToToken Service
/// </summary>
public Dictionary<Type, string[]> ServiceRoutes { get; set; } = new();
/// <summary>
/// Allow JWT in ?ss-tok=jwt QueryString. (default false)
/// </summary>
public bool AllowInQueryString { get; set; }
/// <summary>
/// Allow JWT in ss-tok=jwt HTML POST FormData. (default false)
/// </summary>
public bool AllowInFormData { get; set; }
/// <summary>
/// Whether to automatically remove expired or invalid cookies
/// </summary>
public bool RemoveInvalidTokenCookie { get; set; }
/// <summary>
/// Whether to also Include Token in ConvertSessionToTokenResponse
/// </summary>
public bool IncludeJwtInConvertSessionToTokenResponse { get; set; }
/// <summary>
/// Whether to store JWTs in Cookies (ss-tok) for successful Authentications (default true)
/// </summary>
public bool UseTokenCookie { get; set; } = true;
/// <summary>
/// Override conversion to Unix Time used in issuing JWTs and validation
/// </summary>
public Func<DateTime,long> ResolveUnixTime { get; set; } = DefaultResolveUnixTime;
public static long DefaultResolveUnixTime(DateTime dateTime) => dateTime.ToUnixTime();
/// <summary>
/// Inspect or modify JWT Payload before validation, return error message if invalid else null
/// </summary>
public Func<Dictionary<string,string>, string> PreValidateJwtPayloadFilter { get; set; }
/// <summary>
/// Change resolution for resolving unique jti id for Access Tokens
/// </summary>
public Func<IRequest,string> ResolveJwtId { get; set; }
/// <summary>
/// Get the next AutoId for usage in jti JWT Access Tokens
/// </summary>
public string NextJwtId() => Interlocked.Increment(ref accessIdCounter).ToString();
private long accessIdCounter;
/// <summary>
/// Get the last jti AutoId generated
/// </summary>
public string LastJwtId() => Interlocked.Read(ref accessIdCounter).ToString();
/// <summary>
/// Change resolution for resolving unique jti id for Refresh Tokens
/// </summary>
public Func<IRequest,string> ResolveRefreshJwtId { get; set; }
/// <summary>
/// Get the next AutoId for usage in jti JWT Refresh Tokens
/// </summary>
public string NextRefreshJwtId() => Interlocked.Decrement(ref refreshIdCounter).ToString();
private long refreshIdCounter;
public string LastRefreshJwtId() => Interlocked.Read(ref refreshIdCounter).ToString();
/// <summary>
/// Invalidate JWTs with ids
/// </summary>
public HashSet<string> InvalidateJwtIds { get; set; } = new();
public JwtAuthProviderReader()
: base(null, Realm, Name)
{
Init();
}
public JwtAuthProviderReader(IAppSettings appSettings)
: base(appSettings, Realm, Name)
{
Init(appSettings);
}
public virtual void Init(IAppSettings appSettings = null)
{
Label = "JWT";
FormLayout =
[
new InputInfo(nameof(IHasBearerToken.BearerToken), Html.Input.Types.Textarea)
{
Label = "JWT",
Placeholder = "JWT Bearer Token",
Required = true,
}
];
RequireSecureConnection = true;
EncryptPayload = false;
HashAlgorithm = "HS256";
RequireHashAlgorithm = true;
RemoveInvalidTokenCookie = true;
Issuer = "ssjwt";
Audiences = [];
ExpireTokensIn = TimeSpan.FromDays(14);
ExpireRefreshTokensIn = TimeSpan.FromDays(365);
FallbackAuthKeys = [];
FallbackPublicKeys = [];
FallbackPrivateKeys = [];
if (appSettings != null)
{
RequireSecureConnection = appSettings.Get("jwt.RequireSecureConnection", RequireSecureConnection);
RequireHashAlgorithm = appSettings.Get("jwt.RequireHashAlgorithm", RequireHashAlgorithm);
EncryptPayload = appSettings.Get("jwt.EncryptPayload", EncryptPayload);
AllowInQueryString = appSettings.Get("jwt.AllowInQueryString", AllowInQueryString);
AllowInFormData = appSettings.Get("jwt.AllowInFormData", AllowInFormData);
RequiresAudience = appSettings.Get("jwt.RequiresAudience", RequiresAudience);
IncludeJwtInConvertSessionToTokenResponse = appSettings.Get("jwt.IncludeJwtInConvertSessionToTokenResponse", IncludeJwtInConvertSessionToTokenResponse);
UseTokenCookie = appSettings.Get("jwt.UseTokenCookie", UseTokenCookie);
Issuer = appSettings.GetString("jwt.Issuer");
KeyId = appSettings.GetString("jwt.KeyId");
Audience = appSettings.GetString("jwt.Audience");
if (appSettings.Exists("jwt.Audiences"))
{
var audiences = appSettings.GetList("jwt.Audiences");
if (!audiences.IsEmpty())
{
Audiences = audiences.ToList();
}
}
var hashAlg = appSettings.GetString("jwt.HashAlgorithm");
if (!string.IsNullOrEmpty(hashAlg))
HashAlgorithm = hashAlg;
var privateKeyXml = appSettings.GetString("jwt.PrivateKeyXml");
if (privateKeyXml != null)
PrivateKeyXml = privateKeyXml;
var publicKeyXml = appSettings.GetString("jwt.PublicKeyXml");
if (publicKeyXml != null)
PublicKeyXml = publicKeyXml;
var base64 = appSettings.GetString("jwt.AuthKeyBase64");
if (base64 != null)
AuthKeyBase64 = base64;
var dateStr = appSettings.GetString("jwt.InvalidateTokensIssuedBefore");
if (!string.IsNullOrEmpty(dateStr))
InvalidateTokensIssuedBefore = dateStr.FromJsv<DateTime>();
ExpireTokensIn = appSettings.Get("jwt.ExpireTokensIn", ExpireTokensIn);
ExpireRefreshTokensIn = appSettings.Get("jwt.ExpireRefreshTokensIn", ExpireRefreshTokensIn);
var intStr = appSettings.GetString("jwt.ExpireTokensInDays");
if (intStr != null)
ExpireTokensInDays = int.Parse(intStr);
string base64Key;
var i = 1;
while ((base64Key = appSettings.GetString("jwt.PrivateKeyXml." + i++)) != null)
{
FallbackPrivateKeys.Add(base64Key.ToPrivateRSAParameters());
var publicKey = base64Key.ToPublicRSAParameters();
FallbackPublicKeys.Add(publicKey);
}
i = 1;
while ((base64Key = appSettings.GetString("jwt.PublicKeyXml." + i++)) != null)
{
var publicKey = base64Key.ToPublicRSAParameters();
FallbackPublicKeys.Add(publicKey);
}
i = 1;
while ((base64Key = appSettings.GetString("jwt.AuthKeyBase64." + i++)) != null)
{
var authKey = Convert.FromBase64String(base64Key);
FallbackAuthKeys.Add(authKey);
}
}
}
public virtual string GetKeyId(IRequest req)
{
if (KeyId != null)
return KeyId;
var authKey = GetAuthKey(req);
if (HmacAlgorithms.ContainsKey(HashAlgorithm) && authKey != null)
return Convert.ToBase64String(authKey).Substring(0, 3);
var publicKey = GetPublicKey(req);
if (RsaSignAlgorithms.ContainsKey(HashAlgorithm) && publicKey != null)
return Convert.ToBase64String(publicKey.Value.Modulus).Substring(0, 3);
return null;
}
public override bool IsAuthorized(IAuthSession session, IAuthTokens tokens, Authenticate request = null)
{
return session.FromToken && session.IsAuthenticated;
}
public override async Task<object> AuthenticateAsync(IServiceBase authService, IAuthSession session, Authenticate request, CancellationToken token = default)
{
// only allow verification of token
if (!string.IsNullOrEmpty(request.Password) && string.IsNullOrEmpty(request.UserName))
{
AuthenticateResponse toAuthResponse(IAuthSession jwtSession)
{
var to = jwtSession.ConvertTo<AuthenticateResponse>();
to.UserId = jwtSession.UserAuthId;
return to;
}
var req = authService.Request;
var bearerToken = request.Password;
var parts = bearerToken.Split('.');
if (parts.Length == 3)
{
var jwtPayload = GetVerifiedJwtPayload(req, parts);
if (jwtPayload == null) //not verified
throw HttpError.Forbidden(ErrorMessages.TokenInvalid.Localize(req));
return (toAuthResponse(await CreateSessionFromPayloadAsync(req, jwtPayload)) as object).InTask();
}
if (parts.Length == 5) //Encrypted JWE Token
{
var jwtPayload = GetVerifiedJwtPayload(req, parts);
if (jwtPayload == null) //not verified
throw HttpError.Forbidden(ErrorMessages.TokenInvalid.Localize(req));
if (ValidateToken != null)
{
if (!ValidateToken(jwtPayload, req))
throw HttpError.Forbidden(ErrorMessages.TokenInvalid.Localize(req));
}
return (toAuthResponse(await CreateSessionFromPayloadAsync(req, jwtPayload)) as object).InTask();
}
}
throw new NotImplementedException("JWT Authenticate() should not be called directly");
}
public async Task PreAuthenticateAsync(IRequest req, IResponse res)
{
if (req.OperationName != null && IgnoreForOperationTypes.Contains(req.OperationName))
return;
Exception origException = null;
string bearerToken = null;
string refreshToken = null;
try
{
bearerToken = req.GetJwtToken();
if (bearerToken != null)
{
if (await AuthenticateBearerTokenAsync(req, res, bearerToken))
return;
}
}
catch (Exception e)
{
refreshToken = req.GetJwtRefreshToken();
Log.ErrorFormat("JWT BearerToken '{0}...' failed: {1}{2}",
bearerToken.SafeSubstring(0,4), e.Message, refreshToken == null ? "" : ", trying Refresh Token...");
if (refreshToken == null)
throw;
origException = e;
}
if (origException == null)
refreshToken = req.GetJwtRefreshToken();
if (refreshToken != null)
{
if (await AuthenticateRefreshTokenAsync(req, res, refreshToken).ConfigAwait())
return;
}
if (origException != null)
throw origException;
}
protected virtual async Task<bool> AuthenticateBearerTokenAsync(IRequest req, IResponse res, string bearerToken)
{
if (bearerToken == null)
throw new ArgumentNullException(nameof(bearerToken));
try
{
var parts = bearerToken.Split('.');
if (parts.Length == 3)
{
if (!req.AllowConnection(RequireSecureConnection))
throw HttpError.Forbidden(ErrorMessages.JwtRequiresSecureConnection.Localize(req));
var jwtPayload = GetVerifiedJwtPayload(req, parts);
if (jwtPayload == null) //not verified
return false;
if (ValidateToken != null)
{
if (!ValidateToken(jwtPayload, req))
throw HttpError.Forbidden(ErrorMessages.TokenInvalid.Localize(req));
}
var session = await CreateSessionFromPayloadAsync(req, jwtPayload);
req.Items[Keywords.Session] = session;
return true;
}
if (parts.Length == 5) //Encrypted JWE Token
{
if (!req.AllowConnection(RequireSecureConnection))
throw HttpError.Forbidden(ErrorMessages.JwtRequiresSecureConnection.Localize(req));
var jwtPayload = GetVerifiedJwtPayload(req, parts);
if (jwtPayload == null) //not verified
return false;
if (ValidateToken != null)
{
if (!ValidateToken(jwtPayload, req))
throw HttpError.Forbidden(ErrorMessages.TokenInvalid.Localize(req));
}
var session = await CreateSessionFromPayloadAsync(req, jwtPayload);
req.Items[Keywords.Session] = session;
return true;
}
}
catch (Exception)
{
if (RemoveInvalidTokenCookie && req.Cookies.ContainsKey(Keywords.TokenCookie))
(res as IHttpResponse)?.Cookies.DeleteCookie(Keywords.TokenCookie);
throw;
}
return false;
}
protected virtual async Task<bool> AuthenticateRefreshTokenAsync(IRequest req, IResponse res, string refreshToken)
{
if (refreshToken == null)
throw new ArgumentNullException(nameof(refreshToken));
try
{
var accessToken = await CreateAccessTokenFromRefreshToken(refreshToken, req).ConfigAwait();
if (accessToken != null)
{
if (await AuthenticateBearerTokenAsync(req, res, accessToken))
{
(res as IHttpResponse)?.SetCookie(new Cookie(Keywords.TokenCookie, accessToken, Cookies.RootPath) {
HttpOnly = true,
Secure = req.IsSecureConnection,
Expires = DateTime.UtcNow.Add(ExpireTokensIn),
});
return true;
}
}
}
catch (Exception e)
{
Log.ErrorFormat("Could not create new BearerToken from RefreshToken '{0}...': {1}",
refreshToken.SafeSubstring(0,4), e.Message);
if (RemoveInvalidTokenCookie && req.Cookies.ContainsKey(Keywords.RefreshTokenCookie))
(res as IHttpResponse)?.Cookies.DeleteCookie(Keywords.RefreshTokenCookie);
return false;
}
return false;
}
public virtual Task<string> CreateAccessTokenFromRefreshToken(string refreshToken, IRequest req) =>
(null as string).AsTaskResult();
public bool IsJwtValid(string jwt) => GetValidJwtPayload(jwt) != null;
public bool IsJwtValid(IRequest req, string jwt) => GetValidJwtPayload(req, jwt) != null;
public bool IsJwtValid(IRequest req) => GetValidJwtPayload(req, req.GetJwtToken()) != null;
public JsonObject GetValidJwtPayload(string jwt) =>
GetValidJwtPayload(null, jwt);
/// <summary>
/// Return token payload which is both verified and still valid
/// </summary>
public virtual JsonObject GetValidJwtPayload(IRequest req) => GetValidJwtPayload(req, req.GetJwtToken());
/// <summary>
/// Return token payload which is both verified and still valid
/// </summary>
public virtual JsonObject GetValidJwtPayload(IRequest req, string jwt)
{
JsonObject verifiedPayload = null;
try
{
verifiedPayload = GetVerifiedJwtPayload(req, jwt.Split('.'));
if (verifiedPayload == null)
return null;
if (ValidateToken != null && !ValidateToken(verifiedPayload, req))
return null;
}
catch (Exception)
{
return null;
}
var invalidError = GetInvalidJwtPayloadError(verifiedPayload);
return invalidError != null
? null
: verifiedPayload;
}
public static Dictionary<string, object> ExtractHeader(string jwt)
{
var headerBase64 = jwt.AsSpan().LeftPart('.');
var headerBytes = headerBase64.ToString().FromBase64UrlSafe();
var headerJson = MemoryProvider.Instance.FromUtf8Bytes(headerBytes);
return (Dictionary<string, object>) JSON.parse(headerJson);
}
public static Dictionary<string, object> ExtractPayload(string jwt)
{
var payloadBase64 = jwt.AsSpan().RightPart('.').LeftPart('.');
var payloadBytes = payloadBase64.ToString().FromBase64UrlSafe();
var payloadJson = MemoryProvider.Instance.FromUtf8Bytes(payloadBytes);
return (Dictionary<string, object>) JSON.parse(payloadJson);
}
public static string Dump(string jwt)
{
var sb = StringBuilderCache.Allocate();
var header = ExtractHeader(jwt);
sb.AppendLine("[JWT Header]").AppendLine();
Inspect.printDump(header);
var payload = ExtractPayload(jwt);
if (payload.TryGetValue("iat", out var iatObj) && iatObj is int iatEpoch)
payload["iat"] = $"{iatEpoch} ({iatEpoch.FromUnixTime():R})";
if (payload.TryGetValue("exp", out var expObj) && expObj is int expEpoch)
payload["exp"] = $"{expEpoch} ({expEpoch.FromUnixTime():R})";
sb.AppendLine("[JWT Payload]");
Inspect.printDump(payload);
return StringBuilderCache.ReturnAndFree(sb);
}
/// <summary>
/// Return token payload which has been verified to be created using the configured encryption key.
/// Use GetValidJwtPayload() instead if you also want the payload validated.
/// </summary>
public virtual JsonObject GetVerifiedJwtPayload(string jwt) => GetVerifiedJwtPayload(null, jwt.Split('.'));
/// <summary>
/// Return token payload which has been verified to be created using the configured encryption key.
/// Use GetValidJwtPayload() instead if you also want the payload validated.
/// </summary>
public virtual JsonObject GetVerifiedJwtPayload(IRequest req, string[] parts)
{
if (parts.Length == 3)
{
var header = parts[0];
var payload = parts[1];
var signatureBytes = parts[2].FromBase64UrlSafe();
var headerJson = header.FromBase64UrlSafe().FromUtf8Bytes();
var payloadBytes = payload.FromBase64UrlSafe();
var headerData = headerJson.FromJson<Dictionary<string, string>>();
var bytesToSign = string.Concat(header, ".", payload).ToUtf8Bytes();
var algorithm = headerData["alg"];
//Potential Security Risk for relying on user-specified algorithm: https://auth0.com/blog/2015/03/31/critical-vulnerabilities-in-json-web-token-libraries/
if (RequireHashAlgorithm && algorithm != HashAlgorithm)
throw new NotSupportedException($"Invalid algorithm '{algorithm}', expected '{HashAlgorithm}'");
if (!VerifyPayload(req, algorithm, bytesToSign, signatureBytes))
return null;
var payloadJson = payloadBytes.FromUtf8Bytes();
var jwtPayload = JsonObject.Parse(payloadJson);
return jwtPayload;
}
if (parts.Length == 5) //Encrypted JWE Token
{
return GetVerifiedJwePayload(req, parts);
}
throw new ArgumentException(ErrorMessages.TokenInvalid.Localize(req));
}
/// <summary>
/// Return token payload which has been verified to be created using the configured encryption key.
/// Use GetValidJwtPayload() instead if you also want the payload validated.
/// </summary>
public virtual JsonObject GetVerifiedJwePayload(string jwt) => GetVerifiedJwePayload(null, jwt.Split('.'));
/// <summary>
/// Return token payload which has been verified to be created using the configured encryption key.
/// Use GetValidJwePayload() instead if you also want the payload validated.
/// </summary>
public virtual JsonObject GetVerifiedJwePayload(IRequest req, string[] parts)
{
if (!VerifyJwePayload(req, parts, out var iv, out var cipherText, out var cryptKey))
return null;
var aes = Aes.Create();
aes.KeySize = 128;
aes.BlockSize = 128;
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
using (aes)
using (var decryptor = aes.CreateDecryptor(cryptKey, iv))
using (var ms = MemoryStreamFactory.GetStream(cipherText))
using (var cryptStream = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
{
var jwtPayloadBytes = cryptStream.ReadFully();
return JsonObject.Parse(jwtPayloadBytes.FromUtf8Bytes());
}
}
public virtual bool VerifyJwePayload(IRequest req, string[] parts, out byte[] iv, out byte[] cipherText, out byte[] cryptKey)
{
var jweHeaderBase64Url = parts[0];
var jweEncKeyBase64Url = parts[1];
var ivBase64Url = parts[2];
var cipherTextBase64Url = parts[3];
var tagBase64Url = parts[4];
var sentTag = tagBase64Url.FromBase64UrlSafe();
var aadBytes = (jweHeaderBase64Url + "." + jweEncKeyBase64Url).ToUtf8Bytes();
iv = ivBase64Url.FromBase64UrlSafe();
cipherText = cipherTextBase64Url.FromBase64UrlSafe();
var jweEncKey = jweEncKeyBase64Url.FromBase64UrlSafe();
var privateKey = GetPrivateKey(req);
if (privateKey == null)
throw new Exception("PrivateKey required to decrypt JWE Token");
var allPrivateKeys = new List<RSAParameters> {
privateKey.Value
};
allPrivateKeys.AddRange(GetFallbackPrivateKeys(req));
var authKey = new byte[128 / 8];
cryptKey = new byte[128 / 8];
foreach (var key in allPrivateKeys)
{
var cryptAuthKeys256 = RsaUtils.Decrypt(jweEncKey, key, UseRsaKeyLength);
Buffer.BlockCopy(cryptAuthKeys256, 0, authKey, 0, authKey.Length);
Buffer.BlockCopy(cryptAuthKeys256, authKey.Length, cryptKey, 0, cryptKey.Length);
using var hmac = new HMACSHA256(authKey);
using var encryptedStream = MemoryStreamFactory.GetStream();
using var writer = new BinaryWriter(encryptedStream);
writer.Write(aadBytes);
writer.Write(iv);
writer.Write(cipherText);
writer.Flush();
var calcTag = hmac.ComputeHash(encryptedStream.GetBuffer(), 0, (int) encryptedStream.Length);
if (calcTag.EquivalentTo(sentTag))
return true;
}
iv = null;
cipherText = null;
cryptKey = null;
return false;
}
public virtual async Task<IAuthSession> ConvertJwtToSessionAsync(IRequest req, string jwt)
{
if (jwt == null)
throw new ArgumentNullException(nameof(jwt));
var jwtPayload = GetVerifiedJwtPayload(req, jwt.Split('.'));
if (jwtPayload == null) //not verified
return null;
if (ValidateToken != null)
{
if (!ValidateToken(jwtPayload, req))
return null;
}
var session = await CreateSessionFromPayloadAsync(req, jwtPayload);
return session;
}
public virtual async Task<IAuthSession> CreateSessionFromPayloadAsync(IRequest req, JsonObject jwtPayload)
{
AssertJwtPayloadIsValid(jwtPayload);
var sessionId = jwtPayload.GetValue("jid", HostContext.AppHost.CreateSessionId);
var session = SessionFeature.CreateNewSession(req, sessionId);
session.AuthProvider = Name;
session.FromToken = true;
session.PopulateFromMap(jwtPayload);
PopulateSessionFilter?.Invoke(session, jwtPayload, req);
if (PopulateSessionFilterAsync != null)
await PopulateSessionFilterAsync(session, jwtPayload, req);
HostContext.AppHost.OnSessionFilter(req, session, sessionId);
return session;
}
public static async Task<IAuthSession> CreateSessionFromJwtAsync(IRequest req)
{
var jwtProvider = AuthenticateService.GetRequiredJwtAuthProvider();
var jwtToken = req.GetJwtToken();
var session = await jwtProvider.ConvertJwtToSessionAsync(req, jwtToken);
return session;
}
public void AssertJwtPayloadIsValid(JsonObject jwtPayload)
{
var errorMessage = GetInvalidJwtPayloadError(jwtPayload);
if (errorMessage != null)
throw new TokenException(errorMessage);
}
public virtual string GetInvalidJwtPayloadError(JsonObject jwtPayload)
{
if (jwtPayload == null)
throw new ArgumentNullException(nameof(jwtPayload));
var errorMsg = PreValidateJwtPayloadFilter?.Invoke(jwtPayload);
if (errorMsg != null)
return errorMsg;
if (HasExpired(jwtPayload))
return ErrorMessages.TokenExpired;
if (HasInvalidNotBefore(jwtPayload))
return ErrorMessages.TokenInvalidNotBefore;
if (HasInvalidatedId(jwtPayload))
return ErrorMessages.TokenInvalidated;
if (InvalidateTokensIssuedBefore != null &&
HasBeenInvalidated(jwtPayload, ResolveUnixTime(InvalidateTokensIssuedBefore.Value)))
return ErrorMessages.TokenInvalidated;
if (HasInvalidAudience(jwtPayload, out var audience))
return ErrorMessages.TokenInvalidAudienceFmt.LocalizeFmt(audience);
return null;
}
public void AssertRefreshJwtPayloadIsValid(JsonObject jwtPayload)
{
var errorMessage = GetInvalidRefreshJwtPayloadError(jwtPayload);
if (errorMessage != null)
throw new TokenException(errorMessage);
}
public virtual string GetInvalidRefreshJwtPayloadError(JsonObject jwtPayload)
{
if (jwtPayload == null)
throw new ArgumentNullException(nameof(jwtPayload));
var errorMsg = PreValidateJwtPayloadFilter?.Invoke(jwtPayload);
if (errorMsg != null)
return errorMsg;
if (HasExpired(jwtPayload))
return ErrorMessages.TokenExpired;
if (HasInvalidNotBefore(jwtPayload))
return ErrorMessages.TokenInvalidNotBefore;
if (HasInvalidatedId(jwtPayload))
return ErrorMessages.TokenInvalidated;
if (InvalidateRefreshTokensIssuedBefore != null &&
HasBeenInvalidated(jwtPayload, ResolveUnixTime(InvalidateRefreshTokensIssuedBefore.Value)))
return ErrorMessages.TokenInvalidated;
if (HasInvalidAudience(jwtPayload, out var audience))
return ErrorMessages.TokenInvalidAudienceFmt.LocalizeFmt(audience);
return null;
}
public virtual bool HasInvalidatedId(JsonObject jwtPayload)
{
if (InvalidateJwtIds.Count > 0 && jwtPayload.TryGetValue("jti", out var jti))
return InvalidateJwtIds.Contains(jti);
return false;
}
public virtual bool HasExpired(JsonObject jwtPayload)
{
var expiresAt = GetUnixTime(jwtPayload, "exp");
var secondsSinceEpoch = ResolveUnixTime(DateTime.UtcNow);
var hasExpired = secondsSinceEpoch >= expiresAt;
return hasExpired;
}
public virtual bool HasInvalidNotBefore(JsonObject jwtPayload)
{
var notValidBefore = GetUnixTime(jwtPayload, "nbf");
if (notValidBefore != null)
{
var secondsSinceEpoch = ResolveUnixTime(DateTime.UtcNow);
var notValidYet = notValidBefore > secondsSinceEpoch;
return notValidYet;
}
return false;
}
public virtual bool HasBeenInvalidated(JsonObject jwtPayload, long unixTime)
{
var issuedAt = GetUnixTime(jwtPayload, "iat");
if (issuedAt == null || issuedAt < unixTime)