-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDnn.AuthServices.Jwt_01.00.00.xml
1479 lines (1428 loc) · 61.9 KB
/
Dnn.AuthServices.Jwt_01.00.00.xml
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
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<root file="Dnn.AuthServices.Jwt.dll" version="01.00.00" generated="2017-08-10 13:31:06Z" generationTime="2.5590643">
<namespace name="Dnn.AuthServices.Jwt.Auth">
<class name="JwtAuthMessageHandler">
<declaration><![CDATA[public class JwtAuthMessageHandler : AuthMessageHandlerBase]]></declaration>
<documentation>
<summary>
This class implements Json Web Token (JWT) authentication scheme.
For detailed description of JWT refer to:
<para>- JTW standard https://tools.ietf.org/html/rfc7519 </para>
<para>- Introduction to JSON Web Tokens http://jwt.io/introduction/ </para>
</summary>
</documentation>
<constructors>
<constructor name=".ctor">
<declaration><![CDATA[private readonly IJwtController _jwtController = ServiceLocator<IJwtController, JwtController>.Instance;]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="49" sc="9" el="62" ec="10">Auth\JwtAuthMessageHandler.cs</location>
<body hash="d59bf2382c7ebc391ef04a79c47f9728"><![CDATA[private readonly IJwtController _jwtController = JwtController.Instance;
#endregion
#region constructor
public JwtAuthMessageHandler(bool includeByDefault, bool forceSsl)
: base(includeByDefault, forceSsl)
{
// Once an instance is enabled and gets registered in
// ServicesRoutingManager.RegisterAuthenticationHandlers()
// this scheme gets marked as enabled.
IsEnabled = true;
}]]></body>
</codeblock>
</constructor>
<constructor name=".cctor">
<declaration><![CDATA[static JwtAuthMessageHandler()]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="43" sc="9" el="43" ec="110">Auth\JwtAuthMessageHandler.cs</location>
<body hash="a7a149dd185bc519ff86da4d2a9cdf4c"><![CDATA[private static readonly ILog Logger = LoggerSource.Instance.GetLogger(typeof(JwtAuthMessageHandler))]]></body>
</codeblock>
</constructor>
</constructors>
<methods>
<method name="OnInboundRequest">
<declaration><![CDATA[public override HttpResponseMessage OnInboundRequest(HttpRequestMessage request, CancellationToken cancellationToken)]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="69" sc="9" el="76" ec="10">Auth\JwtAuthMessageHandler.cs</location>
<body hash="c20018388946f2dd22902af23cf99e67"><![CDATA[{
if (NeedsAuthentication(request))
{
TryToAuthenticate(request);
}
return base.OnInboundRequest(request, cancellationToken);
}]]></body>
</codeblock>
</method>
<method name="TryToAuthenticate">
<declaration><![CDATA[private void TryToAuthenticate(HttpRequestMessage request)]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="79" sc="9" el="93" ec="10">Auth\JwtAuthMessageHandler.cs</location>
<body hash="1e1757c98a5cec87c367241f33af7f96"><![CDATA[{
try
{
var username = _jwtController.ValidateToken(request);
if (!string.IsNullOrEmpty(username))
{
if (Logger.IsTraceEnabled) Logger.Trace($"Authenticated user '{username}'");
SetCurrentPrincipal(new GenericPrincipal(new GenericIdentity(username, AuthScheme), null), request);
}
}
catch (Exception ex)
{
Logger.Error("Unexpected error in authenticating the user. " + ex);
}
}]]></body>
</codeblock>
</method>
</methods>
<fields>
<field name="_jwtController">
<declaration><![CDATA[private readonly IJwtController _jwtController = ServiceLocator<IJwtController, JwtController>.Instance;]]></declaration>
<documentation>
</documentation>
</field>
<field name="Logger">
<declaration><![CDATA[private static readonly ILog Logger = LoggerSource.Instance.GetLogger(typeof(JwtAuthMessageHandler));]]></declaration>
<documentation>
</documentation>
</field>
</fields>
<properties>
<property name="AuthScheme">
<declaration><![CDATA[public override string AuthScheme]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="45" sc="46" el="45" ec="71">Auth\JwtAuthMessageHandler.cs</location>
<body hash="3d6bd80cc6bfb149bf86782bf63a1f63"><![CDATA[_jwtController.SchemeTyp]]></body>
</codeblock>
</property>
<property name="BypassAntiForgeryToken">
<declaration><![CDATA[public override bool BypassAntiForgeryToken]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="46" sc="56" el="46" ec="60">Auth\JwtAuthMessageHandler.cs</location>
<body hash="f8354de67d79a99e880000c5f3cae57d"><![CDATA[tru]]></body>
</codeblock>
</property>
<property name="IsEnabled">
<declaration><![CDATA[internal static bool IsEnabled]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="48" sc="42" el="48" ec="46">Auth\JwtAuthMessageHandler.cs</location>
<body hash="b5eda0a74558a342cf659187f06f746f"><![CDATA[get]]></body>
</codeblock>
<codeblock>
<location sl="48" sc="47" el="48" ec="51">Auth\JwtAuthMessageHandler.cs</location>
<body hash="cdaeeeba9b4a4c5ebf042c0215a7bb0e"><![CDATA[set]]></body>
</codeblock>
</property>
</properties>
<events>
</events>
</class>
</namespace>
<namespace name="Dnn.AuthServices.Jwt.Components.Common.Controllers">
<class name="IJwtController">
<declaration><![CDATA[public interface IJwtController]]></declaration>
<documentation>
</documentation>
<constructors>
</constructors>
<methods>
<method name="LoginUser">
<declaration><![CDATA[LoginResultData LoginUser(HttpRequestMessage request, LoginData loginData);]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="2147483647" sc="0" el="0" ec="0">
</location>
<body hash="d41d8cd98f00b204e9800998ecf8427e"><![CDATA[]]></body>
</codeblock>
</method>
<method name="LogoutUser">
<declaration><![CDATA[bool LogoutUser(HttpRequestMessage request);]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="2147483647" sc="0" el="0" ec="0">
</location>
<body hash="d41d8cd98f00b204e9800998ecf8427e"><![CDATA[]]></body>
</codeblock>
</method>
<method name="RenewToken">
<declaration><![CDATA[LoginResultData RenewToken(HttpRequestMessage request, string renewalToken);]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="2147483647" sc="0" el="0" ec="0">
</location>
<body hash="d41d8cd98f00b204e9800998ecf8427e"><![CDATA[]]></body>
</codeblock>
</method>
<method name="ValidateToken">
<declaration><![CDATA[string ValidateToken(HttpRequestMessage request);]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="2147483647" sc="0" el="0" ec="0">
</location>
<body hash="d41d8cd98f00b204e9800998ecf8427e"><![CDATA[]]></body>
</codeblock>
</method>
</methods>
<fields>
</fields>
<properties>
<property name="SchemeType">
<declaration><![CDATA[string SchemeType]]></declaration>
<documentation>
</documentation>
</property>
</properties>
<events>
</events>
</class>
<class name="JwtController">
<declaration><![CDATA[internal class JwtController : ServiceLocator<IJwtController, JwtController>, IJwtController]]></declaration>
<documentation>
</documentation>
<constructors>
<constructor name=".ctor">
<declaration><![CDATA[public readonly IDataService DataProvider = ComponentBase<IDataService, DataService>.Instance;]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="69" sc="9" el="69" ec="74">Components\Common\Controllers\JwtController.cs</location>
<body hash="75c9f5d254e2ae06effd4fba09573fdc"><![CDATA[public readonly IDataService DataProvider = DataService.Instance]]></body>
</codeblock>
</constructor>
<constructor name=".cctor">
<declaration><![CDATA[static JwtController()]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="48" sc="9" el="55" ec="70">Components\Common\Controllers\JwtController.cs</location>
<body hash="42898b5571daf0380a670ddfeda44a44"><![CDATA[private static readonly ILog Logger = LoggerSource.Instance.GetLogger(typeof(JwtController));
private static readonly HashAlgorithm Hasher = SHA384.Create();
private const int ClockSkew = 5; // in minutes; default for clock skew
private const int SessionTokenTtl = 60; // in minutes = 1 hour
private const int RenewalTokenTtl = 14; // in days = 2 weeks
private const string SessionClaimType = "sid";
private static readonly Encoding TextEncoder = Encoding.UTF8;]]></body>
</codeblock>
</constructor>
</constructors>
<methods>
<method name="CreateJwtToken">
<declaration><![CDATA[private static JwtSecurityToken CreateJwtToken(byte[] symmetricKey, string issuer, PersistedToken ptoken, IEnumerable<string> roles)]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="283" sc="9" el="299" ec="10">Components\Common\Controllers\JwtController.cs</location>
<body hash="8ee12bcace347b78d56d56f02d82a8a4"><![CDATA[{
//var key = Convert.FromBase64String(symmetricKey);
var credentials = new SigningCredentials(
new InMemorySymmetricSecurityKey(symmetricKey),
"http://www.w3.org/2001/04/xmldsig-more#hmac-sha256",
"http://www.w3.org/2001/04/xmlenc#sha256");
var claimsIdentity = new ClaimsIdentity();
claimsIdentity.AddClaim(new Claim(SessionClaimType, ptoken.TokenId));
claimsIdentity.AddClaims(roles.Select(r => new Claim(ClaimTypes.Role, r)));
var notBefore = DateTime.UtcNow.AddMinutes(-ClockSkew);
var notAfter = ptoken.TokenExpiry;
var tokenHandler = new JwtSecurityTokenHandler();
var token = tokenHandler.CreateToken(issuer, null, claimsIdentity, notBefore, notAfter, credentials);
return token;
}]]></body>
</codeblock>
</method>
<method name="DecodeBase64">
<declaration><![CDATA[private static string DecodeBase64(string b64Str)]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="472" sc="9" el="477" ec="10">Components\Common\Controllers\JwtController.cs</location>
<body hash="b7403666c9d9f4755fa1dce133489d4b"><![CDATA[{
// fix Base64 string padding
var mod = b64Str.Length % 4;
if (mod != 0) b64Str += new string('=', 4 - mod);
return TextEncoder.GetString(Convert.FromBase64String(b64Str));
}]]></body>
</codeblock>
</method>
<method name="EmptyWithError">
<declaration><![CDATA[private static LoginResultData EmptyWithError(string error)]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="278" sc="9" el="280" ec="10">Components\Common\Controllers\JwtController.cs</location>
<body hash="a3e6e225d4f5134ea53cc558817cef04"><![CDATA[{
return new LoginResultData { Error = error };
}]]></body>
</codeblock>
</method>
<method name="EncodeBase64">
<declaration><![CDATA[private static string EncodeBase64(byte[] data)]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="480" sc="9" el="482" ec="10">Components\Common\Controllers\JwtController.cs</location>
<body hash="911c14c5923edb0ad828d0f28c27f2da"><![CDATA[{
return Convert.ToBase64String(data).TrimEnd('=');
}]]></body>
</codeblock>
</method>
<method name="GetAndValidateJwt">
<declaration><![CDATA[private static JwtSecurityToken GetAndValidateJwt(string rawToken, bool checkExpiry)]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="370" sc="9" el="400" ec="10">Components\Common\Controllers\JwtController.cs</location>
<body hash="3b42fdde32d61bc433c3f71f0ed065ae"><![CDATA[{
JwtSecurityToken jwt;
try
{
jwt = new JwtSecurityToken(rawToken);
}
catch (Exception ex)
{
Logger.Error("Unable to construct JWT object from authorization value. " + ex.Message);
return null;
}
if (checkExpiry)
{
var now = DateTime.UtcNow;
if (now < jwt.ValidFrom || now > jwt.ValidTo)
{
if (Logger.IsTraceEnabled) Logger.Trace("Token is expired");
return null;
}
}
var sessionId = GetJwtSessionValue(jwt);
if (string.IsNullOrEmpty(sessionId))
{
if (Logger.IsTraceEnabled) Logger.Trace("Invaid session ID claim");
return null;
}
return jwt;
}]]></body>
</codeblock>
</method>
<method name="GetFactory">
<declaration><![CDATA[protected override Func<IJwtController> GetFactory()]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="65" sc="9" el="67" ec="10">Components\Common\Controllers\JwtController.cs</location>
<body hash="ae0eef57e1d6e0bab6d7c30ca12891d8"><![CDATA[{
return () => new JwtController();
}]]></body>
</codeblock>
</method>
<method name="GetHashedStr">
<declaration><![CDATA[private static string GetHashedStr(string data)]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="485" sc="9" el="487" ec="10">Components\Common\Controllers\JwtController.cs</location>
<body hash="643aed960278487867a069a2fca15da7"><![CDATA[{
return EncodeBase64(Hasher.ComputeHash(TextEncoder.GetBytes(data)));
}]]></body>
</codeblock>
</method>
<method name="GetJwtSessionValue">
<declaration><![CDATA[private static string GetJwtSessionValue(JwtSecurityToken jwt)]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="459" sc="9" el="462" ec="10">Components\Common\Controllers\JwtController.cs</location>
<body hash="6b35d07fed159a9c36496b7250580c53"><![CDATA[{
var sessionClaim = jwt?.Claims?.FirstOrDefault(claim => SessionClaimType.Equals(claim.Type));
return sessionClaim?.Value;
}]]></body>
</codeblock>
</method>
<method name="IsValidSchemeType">
<declaration><![CDATA[private bool IsValidSchemeType(JwtHeader header)]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="359" sc="9" el="367" ec="10">Components\Common\Controllers\JwtController.cs</location>
<body hash="eda3dc55ddd1e68f1162f87f314c7cbe"><![CDATA[{
if (!SchemeType.Equals(header["typ"] as string, StringComparison.OrdinalIgnoreCase))
{
if (Logger.IsTraceEnabled) Logger.Trace("Unsupported authentication scheme type " + header.Typ);
return false;
}
return true;
}]]></body>
</codeblock>
</method>
<method name="LoginUser">
<declaration><![CDATA[public LoginResultData LoginUser(HttpRequestMessage request, LoginData loginData)]]></declaration>
<documentation>
<summary>
Validates user login credentials and returns result when successful
</summary>
</documentation>
<codeblock>
<location sl="120" sc="9" el="185" ec="10">Components\Common\Controllers\JwtController.cs</location>
<body hash="4e49e08f08018381076a30732813a9a1"><![CDATA[{
if (!JwtAuthMessageHandler.IsEnabled)
{
Logger.Trace(SchemeType + " is not registered/enabled in web.config file");
return EmptyWithError("disabled");
}
var portalSettings = PortalController.Instance.GetCurrentPortalSettings();
if (portalSettings == null)
{
Logger.Trace("portalSettings = null");
return EmptyWithError("no-portal");
}
var status = UserLoginStatus.LOGIN_FAILURE;
var ipAddress = request.GetIPAddress() ?? "";
var userInfo = UserController.ValidateUser(portalSettings.PortalId,
loginData.Username, loginData.Password, "DNN", "", AuthScheme, ipAddress, ref status);
if (userInfo == null)
{
Logger.Trace("user = null");
return EmptyWithError("bad-credentials");
}
var valid =
status == UserLoginStatus.LOGIN_SUCCESS ||
status == UserLoginStatus.LOGIN_SUPERUSER ||
status == UserLoginStatus.LOGIN_INSECUREADMINPASSWORD ||
status == UserLoginStatus.LOGIN_INSECUREHOSTPASSWORD;
if (!valid)
{
Logger.Trace("login status = " + status);
return EmptyWithError("bad-credentials");
}
// save hash values in DB so no one with access can create JWT header from existing data
var sessionId = NewSessionId;
var now = DateTime.UtcNow;
var renewalToken = EncodeBase64(Hasher.ComputeHash(Guid.NewGuid().ToByteArray()));
var ptoken = new PersistedToken
{
TokenId = sessionId,
UserId = userInfo.UserID,
TokenExpiry = now.AddMinutes(SessionTokenTtl),
RenewalExpiry = now.AddDays(RenewalTokenTtl),
//TokenHash = GetHashedStr(accessToken), -- not computed yet
RenewalHash = GetHashedStr(renewalToken),
};
var secret = ObtainSecret(sessionId, portalSettings.GUID, userInfo.Membership.LastPasswordChangeDate);
var jwt = CreateJwtToken(secret, portalSettings.PortalAlias.HTTPAlias, ptoken, userInfo.Roles);
var accessToken = jwt.RawData;
ptoken.TokenHash = GetHashedStr(accessToken);
DataProvider.AddToken(ptoken);
return new LoginResultData
{
UserId = userInfo.UserID,
DisplayName = userInfo.DisplayName,
AccessToken = accessToken,
RenewalToken = renewalToken
};
}]]></body>
</codeblock>
</method>
<method name="LogoutUser">
<declaration><![CDATA[public bool LogoutUser(HttpRequestMessage request)]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="91" sc="9" el="114" ec="10">Components\Common\Controllers\JwtController.cs</location>
<body hash="b2f928ccffc70d69330295aa6f3317d6"><![CDATA[{
if (!JwtAuthMessageHandler.IsEnabled)
{
Logger.Trace(SchemeType + " is not registered/enabled in web.config file");
return false;
}
var rawToken = ValidateAuthHeader(request.Headers.Authorization);
if (string.IsNullOrEmpty(rawToken))
{
return false;
}
var jwt = new JwtSecurityToken(rawToken);
var sessionId = GetJwtSessionValue(jwt);
if (string.IsNullOrEmpty(sessionId))
{
if (Logger.IsTraceEnabled) Logger.Trace("Session ID not found in the claim");
return false;
}
DataProvider.DeleteToken(sessionId);
return true;
}]]></body>
</codeblock>
</method>
<method name="ObtainSecret">
<declaration><![CDATA[private static byte[] ObtainSecret(string sessionId, Guid portalGuid, DateTime userCreationDate)]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="465" sc="9" el="469" ec="10">Components\Common\Controllers\JwtController.cs</location>
<body hash="51242dd8bcc3273c12f42f89f4da9f0d"><![CDATA[{
// The secret should contain unpredictable components that can't be inferred from the JWT string.
var stext = string.Join(".", sessionId, portalGuid.ToString("N"), userCreationDate.ToUniversalTime().ToString("O"));
return TextEncoder.GetBytes(stext);
}]]></body>
</codeblock>
</method>
<method name="RenewToken">
<declaration><![CDATA[public LoginResultData RenewToken(HttpRequestMessage request, string renewalToken)]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="188" sc="9" el="241" ec="10">Components\Common\Controllers\JwtController.cs</location>
<body hash="e59b850322bae4a6a84a5bc7ed0eb3e4"><![CDATA[{
if (!JwtAuthMessageHandler.IsEnabled)
{
Logger.Trace(SchemeType + " is not registered/enabled in web.config file");
return EmptyWithError("disabled");
}
var rawToken = ValidateAuthHeader(request.Headers.Authorization);
if (string.IsNullOrEmpty(rawToken))
{
return EmptyWithError("bad-credentials");
}
var jwt = GetAndValidateJwt(rawToken, false);
if (jwt == null)
{
return EmptyWithError("bad-jwt");
}
var sessionId = GetJwtSessionValue(jwt);
if (string.IsNullOrEmpty(sessionId))
{
if (Logger.IsTraceEnabled) Logger.Trace("Session ID not found in the claim");
return EmptyWithError("bad-claims");
}
var ptoken = DataProvider.GetTokenById(sessionId);
if (ptoken == null)
{
if (Logger.IsTraceEnabled) Logger.Trace("Token not found in DB");
return EmptyWithError("not-found");
}
if (ptoken.RenewalExpiry <= DateTime.UtcNow)
{
if (Logger.IsTraceEnabled) Logger.Trace("Token can't bwe renewed anymore");
return EmptyWithError("not-more-renewal");
}
var userInfo = TryGetUser(jwt, false);
if (userInfo == null)
{
if (Logger.IsTraceEnabled) Logger.Trace("Token not found in DB");
return EmptyWithError("not-found");
}
if ((ptoken.TokenHash != GetHashedStr(rawToken)) || (ptoken.UserId != userInfo.UserID))
{
if (Logger.IsTraceEnabled) Logger.Trace("Mismatch in received token");
return EmptyWithError("bad-token");
}
return UpdateToken(renewalToken, ptoken, userInfo);
}]]></body>
</codeblock>
</method>
<method name="TryGetUser">
<declaration><![CDATA[private UserInfo TryGetUser(JwtSecurityToken jwt, bool checkExpiry)]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="403" sc="9" el="456" ec="10">Components\Common\Controllers\JwtController.cs</location>
<body hash="03142f9562a3fe2b8218cc1e6d380f77"><![CDATA[{
// validate against DB saved data
var sessionId = GetJwtSessionValue(jwt);
var ptoken = DataProvider.GetTokenById(sessionId);
if (ptoken == null)
{
if (Logger.IsTraceEnabled) Logger.Trace("Token not found in DB");
return null;
}
if (checkExpiry)
{
var now = DateTime.UtcNow;
if (now > ptoken.TokenExpiry || now > ptoken.RenewalExpiry)
{
if (Logger.IsTraceEnabled) Logger.Trace("DB Token is expired");
return null;
}
}
if (ptoken.TokenHash != GetHashedStr(jwt.RawData))
{
if (Logger.IsTraceEnabled) Logger.Trace("Mismatch data in received token");
return null;
}
var portalSettings = PortalController.Instance.GetCurrentPortalSettings();
if (portalSettings == null)
{
Logger.Trace("Unable to retrieve portal settings");
return null;
}
var userInfo = UserController.GetUserById(portalSettings.PortalId, ptoken.UserId);
if (userInfo == null)
{
if (Logger.IsTraceEnabled) Logger.Trace("Invalid user");
return null;
}
var status = UserController.ValidateUser(userInfo, portalSettings.PortalId, false);
var valid =
status == UserValidStatus.VALID ||
status == UserValidStatus.UPDATEPROFILE ||
status == UserValidStatus.UPDATEPASSWORD;
if (!valid && Logger.IsTraceEnabled)
{
Logger.Trace("Inactive user status: " + status);
return null;
}
return userInfo;
}]]></body>
</codeblock>
</method>
<method name="UpdateToken">
<declaration><![CDATA[private LoginResultData UpdateToken(string renewalToken, PersistedToken ptoken, UserInfo userInfo)]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="244" sc="9" el="269" ec="10">Components\Common\Controllers\JwtController.cs</location>
<body hash="999a7d1520151a64077154730bd20b22"><![CDATA[{
var expiry = DateTime.UtcNow.AddMinutes(SessionTokenTtl);
if (expiry > ptoken.RenewalExpiry)
{
// don't extend beyond renewal expiry and make sure it is marked in UTC
expiry = new DateTime(ptoken.RenewalExpiry.Ticks, DateTimeKind.Utc);
}
ptoken.TokenExpiry = expiry;
var portalSettings = PortalController.Instance.GetCurrentPortalSettings();
var secret = ObtainSecret(ptoken.TokenId, portalSettings.GUID, userInfo.Membership.LastPasswordChangeDate);
var jwt = CreateJwtToken(secret, portalSettings.PortalAlias.HTTPAlias, ptoken, userInfo.Roles);
var accessToken = jwt.RawData;
// save hash values in DB so no one with access can create JWT header from existing data
ptoken.TokenHash = GetHashedStr(accessToken);
DataProvider.UpdateToken(ptoken);
return new LoginResultData
{
UserId = userInfo.UserID,
DisplayName = userInfo.DisplayName,
AccessToken = accessToken,
RenewalToken = renewalToken
};
}]]></body>
</codeblock>
</method>
<method name="ValidateAuthHeader">
<declaration><![CDATA[private string ValidateAuthHeader(AuthenticationHeaderValue authHdr)]]></declaration>
<documentation>
<summary>
Checks for Authorization header and validates it is JWT scheme. If successful, it returns the token string.
</summary>
<param name="authHdr">The request auhorization header.</param>
<returns>The JWT passed in the request; otherwise, it returns null.</returns></documentation>
<codeblock>
<location sl="307" sc="9" el="328" ec="10">Components\Common\Controllers\JwtController.cs</location>
<body hash="5b19c4bf055c4bee0d7ffd1afd96c8a2"><![CDATA[{
if (authHdr == null)
{
//if (Logger.IsTraceEnabled) Logger.Trace("Authorization header not present in the request"); // too verbose; shows in all web requests
return null;
}
if (!string.Equals(authHdr.Scheme, AuthScheme, StringComparison.CurrentCultureIgnoreCase))
{
if (Logger.IsTraceEnabled) Logger.Trace("Authorization header scheme in the request is not equal to " + SchemeType);
return null;
}
var authorization = authHdr.Parameter;
if (string.IsNullOrEmpty(authorization))
{
if (Logger.IsTraceEnabled) Logger.Trace("Missing authorization header value in the request");
return null;
}
return authorization;
}]]></body>
</codeblock>
</method>
<method name="ValidateAuthorizationValue">
<declaration><![CDATA[private string ValidateAuthorizationValue(string authorization)]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="331" sc="9" el="356" ec="10">Components\Common\Controllers\JwtController.cs</location>
<body hash="ac967f1e0ee3a9b066f3df277c30bc2a"><![CDATA[{
var parts = authorization.Split('.');
if (parts.Length < 3)
{
if (Logger.IsTraceEnabled) Logger.Trace("Token must have [header:claims:signature] parts at least");
return null;
}
var decoded = DecodeBase64(parts[0]);
if (decoded.IndexOf("\"" + SchemeType + "\"", StringComparison.InvariantCultureIgnoreCase) < 0)
{
if (Logger.IsTraceEnabled) Logger.Trace($"This is not a {SchemeType} autentication scheme.");
return null;
}
var header = JsonConvert.DeserializeObject<JwtHeader>(decoded);
if (!IsValidSchemeType(header))
return null;
var jwt = GetAndValidateJwt(authorization, true);
if (jwt == null)
return null;
var userInfo = TryGetUser(jwt, true);
return userInfo?.Username;
}]]></body>
</codeblock>
</method>
<method name="ValidateToken">
<declaration><![CDATA[public string ValidateToken(HttpRequestMessage request)]]></declaration>
<documentation>
<summary>
Validates the received JWT against the databas eand returns username when successful.
</summary>
</documentation>
<codeblock>
<location sl="79" sc="9" el="88" ec="10">Components\Common\Controllers\JwtController.cs</location>
<body hash="1b3315ed8b4b1206cee70ce96fc7412a"><![CDATA[{
if (!JwtAuthMessageHandler.IsEnabled)
{
Logger.Trace(SchemeType + " is not registered/enabled in web.config file");
return null;
}
var authorization = ValidateAuthHeader(request.Headers.Authorization);
return string.IsNullOrEmpty(authorization) ? null : ValidateAuthorizationValue(authorization);
}]]></body>
</codeblock>
</method>
</methods>
<fields>
<field name="AuthScheme">
<declaration><![CDATA[public const string AuthScheme = "Bearer";]]></declaration>
<documentation>
</documentation>
</field>
<field name="ClockSkew">
<declaration><![CDATA[private const int ClockSkew = 5;]]></declaration>
<documentation>
</documentation>
</field>
<field name="DataProvider">
<declaration><![CDATA[public readonly IDataService DataProvider = ComponentBase<IDataService, DataService>.Instance;]]></declaration>
<documentation>
</documentation>
</field>
<field name="Hasher">
<declaration><![CDATA[private static readonly HashAlgorithm Hasher = SHA384.Create();]]></declaration>
<documentation>
</documentation>
</field>
<field name="Logger">
<declaration><![CDATA[private static readonly ILog Logger = LoggerSource.Instance.GetLogger(typeof(JwtController));]]></declaration>
<documentation>
</documentation>
</field>
<field name="RenewalTokenTtl">
<declaration><![CDATA[private const int RenewalTokenTtl = 14;]]></declaration>
<documentation>
</documentation>
</field>
<field name="SessionClaimType">
<declaration><![CDATA[private const string SessionClaimType = "sid";]]></declaration>
<documentation>
</documentation>
</field>
<field name="SessionTokenTtl">
<declaration><![CDATA[private const int SessionTokenTtl = 60;]]></declaration>
<documentation>
</documentation>
</field>
<field name="TextEncoder">
<declaration><![CDATA[private static readonly Encoding TextEncoder = Encoding.UTF8;]]></declaration>
<documentation>
</documentation>
</field>
</fields>
<properties>
<property name="NewSessionId">
<declaration><![CDATA[private static string NewSessionId]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="275" sc="47" el="275" ec="129">Components\Common\Controllers\JwtController.cs</location>
<body hash="e401b6eae00f5553b35d1343e5807beb"><![CDATA[DateTime.UtcNow.Ticks.ToString("x16") + Guid.NewGuid().ToString("N").Substring(16]]></body>
</codeblock>
</property>
<property name="SchemeType">
<declaration><![CDATA[public string SchemeType]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="58" sc="37" el="58" ec="42">Components\Common\Controllers\JwtController.cs</location>
<body hash="cfe600598c119f294952871b1968a474"><![CDATA["JWT]]></body>
</codeblock>
</property>
</properties>
<events>
</events>
</class>
</namespace>
<namespace name="Dnn.AuthServices.Jwt.Components.Entity">
<class name="LoginData">
<declaration><![CDATA[public struct LoginData]]></declaration>
<documentation>
<summary>
Structure used for the Login to obtain a Json Web Token (JWT).
</summary>
</documentation>
<constructors>
</constructors>
<methods>
</methods>
<fields>
<field name="Password">
<declaration><![CDATA[public string Password;]]></declaration>
<documentation>
</documentation>
</field>
<field name="Username">
<declaration><![CDATA[public string Username;]]></declaration>
<documentation>
</documentation>
</field>
</fields>
<properties>
</properties>
<events>
</events>
</class>
<class name="LoginResultData">
<declaration><![CDATA[public class LoginResultData]]></declaration>
<documentation>
</documentation>
<constructors>
<constructor name=".ctor">
<declaration><![CDATA[public LoginResultData()]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="2147483647" sc="0" el="0" ec="0">
</location>
<body hash="d41d8cd98f00b204e9800998ecf8427e"><![CDATA[]]></body>
</codeblock>
</constructor>
</constructors>
<methods>
</methods>
<fields>
</fields>
<properties>
<property name="AccessToken">
<declaration><![CDATA[public string AccessToken]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="34" sc="37" el="34" ec="41">Components\Entity\LoginResultData.cs</location>
<body hash="b5eda0a74558a342cf659187f06f746f"><![CDATA[get]]></body>
</codeblock>
<codeblock>
<location sl="34" sc="42" el="34" ec="46">Components\Entity\LoginResultData.cs</location>
<body hash="cdaeeeba9b4a4c5ebf042c0215a7bb0e"><![CDATA[set]]></body>
</codeblock>
</property>
<property name="DisplayName">
<declaration><![CDATA[public string DisplayName]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="32" sc="37" el="32" ec="41">Components\Entity\LoginResultData.cs</location>
<body hash="b5eda0a74558a342cf659187f06f746f"><![CDATA[get]]></body>
</codeblock>
<codeblock>
<location sl="32" sc="42" el="32" ec="46">Components\Entity\LoginResultData.cs</location>
<body hash="cdaeeeba9b4a4c5ebf042c0215a7bb0e"><![CDATA[set]]></body>
</codeblock>
</property>
<property name="Error">
<declaration><![CDATA[public string Error]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="38" sc="31" el="38" ec="35">Components\Entity\LoginResultData.cs</location>
<body hash="b5eda0a74558a342cf659187f06f746f"><![CDATA[get]]></body>
</codeblock>
<codeblock>
<location sl="38" sc="36" el="38" ec="40">Components\Entity\LoginResultData.cs</location>
<body hash="cdaeeeba9b4a4c5ebf042c0215a7bb0e"><![CDATA[set]]></body>
</codeblock>
</property>
<property name="RenewalToken">
<declaration><![CDATA[public string RenewalToken]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="36" sc="38" el="36" ec="42">Components\Entity\LoginResultData.cs</location>
<body hash="b5eda0a74558a342cf659187f06f746f"><![CDATA[get]]></body>
</codeblock>
<codeblock>
<location sl="36" sc="43" el="36" ec="47">Components\Entity\LoginResultData.cs</location>
<body hash="cdaeeeba9b4a4c5ebf042c0215a7bb0e"><![CDATA[set]]></body>
</codeblock>
</property>
<property name="UserId">
<declaration><![CDATA[public int UserId]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="30" sc="29" el="30" ec="33">Components\Entity\LoginResultData.cs</location>
<body hash="b5eda0a74558a342cf659187f06f746f"><![CDATA[get]]></body>
</codeblock>
<codeblock>
<location sl="30" sc="34" el="30" ec="38">Components\Entity\LoginResultData.cs</location>
<body hash="cdaeeeba9b4a4c5ebf042c0215a7bb0e"><![CDATA[set]]></body>
</codeblock>
</property>
</properties>
<events>
</events>
</class>
<class name="PersistedToken">
<declaration><![CDATA[public class PersistedToken]]></declaration>
<documentation>
</documentation>
<constructors>
<constructor name=".ctor">
<declaration><![CDATA[public PersistedToken()]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="2147483647" sc="0" el="0" ec="0">
</location>
<body hash="d41d8cd98f00b204e9800998ecf8427e"><![CDATA[]]></body>
</codeblock>
</constructor>
</constructors>
<methods>
</methods>
<fields>
</fields>
<properties>
<property name="RenewalExpiry">
<declaration><![CDATA[public DateTime RenewalExpiry]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="12" sc="41" el="12" ec="45">Components\Entity\PersistedToken.cs</location>
<body hash="b5eda0a74558a342cf659187f06f746f"><![CDATA[get]]></body>
</codeblock>
<codeblock>
<location sl="12" sc="46" el="12" ec="50">Components\Entity\PersistedToken.cs</location>
<body hash="cdaeeeba9b4a4c5ebf042c0215a7bb0e"><![CDATA[set]]></body>
</codeblock>
</property>
<property name="RenewalHash">
<declaration><![CDATA[public string RenewalHash]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="14" sc="37" el="14" ec="41">Components\Entity\PersistedToken.cs</location>
<body hash="b5eda0a74558a342cf659187f06f746f"><![CDATA[get]]></body>
</codeblock>
<codeblock>
<location sl="14" sc="42" el="14" ec="46">Components\Entity\PersistedToken.cs</location>
<body hash="cdaeeeba9b4a4c5ebf042c0215a7bb0e"><![CDATA[set]]></body>
</codeblock>
</property>
<property name="RenewCount">
<declaration><![CDATA[public int RenewCount]]></declaration>
<documentation>
</documentation>
<codeblock>
<location sl="10" sc="33" el="10" ec="37">Components\Entity\PersistedToken.cs</location>
<body hash="b5eda0a74558a342cf659187f06f746f"><![CDATA[get]]></body>
</codeblock>
<codeblock>
<location sl="10" sc="38" el="10" ec="42">Components\Entity\PersistedToken.cs</location>
<body hash="cdaeeeba9b4a4c5ebf042c0215a7bb0e"><![CDATA[set]]></body>
</codeblock>
</property>