-
Notifications
You must be signed in to change notification settings - Fork 138
/
Jwt.scala
861 lines (737 loc) · 39.4 KB
/
Jwt.scala
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
package pdi.jwt
import scala.util.Try
import javax.crypto.SecretKey
import java.security.{Key, PrivateKey, PublicKey}
import pdi.jwt.algorithms._
import pdi.jwt.exceptions._
/**
* Default implementation of [[JwtCore]] using only Strings. Most of the time, you should use a lib
* implementing JSON and shouldn't be using this object. But just in case you need pure Scala support,
* here it is.
*
* To see a full list of samples, check the [[http://pauldijou.fr/jwt-scala/samples/jwt-core/ online documentation]].
*
* '''Warning''': since there is no JSON support in Scala, this object doesn't have any way to parse
* a JSON string as an AST, so it only uses regex with all the limitations it implies. Try not to use
* keys like `exp` and `nbf` in sub-objects of the claim. For example, if you try to use the following
* claim: `{"user":{"exp":1},"exp":1300819380}`, it should be correct but it will fail because the regex
* extracting the expiration will return `1` instead of `1300819380`. Sorry about that.
*/
object Jwt extends JwtCore[String, String] {
protected def parseHeader(header: String): String = header
protected def parseClaim(claim: String): String = claim
private val extractAlgorithmRegex = "\"alg\" *: *\"([a-zA-Z0-9]+)\"".r
protected def extractAlgorithm(header: String): Option[JwtAlgorithm] =
(extractAlgorithmRegex findFirstMatchIn header).map(_.group(1)).flatMap {
case "none" => None
case name: String => Some(JwtAlgorithm.fromString(name))
}
private val extractExpirationRegex = "\"exp\" *: *([0-9]+)".r
protected def extractExpiration(claim: String): Option[Long] =
(extractExpirationRegex findFirstMatchIn claim).map(_.group(1)).map(_.toLong)
private val extractNotBeforeRegex = "\"nbf\" *: *([0-9]+)".r
protected def extractNotBefore(claim: String): Option[Long] =
(extractNotBeforeRegex findFirstMatchIn claim).map(_.group(1)).map(_.toLong)
}
/** Provide the main logic around Base64 encoding / decoding and signature using the correct algorithm.
* '''H''' and '''C''' types are respesctively the header type and the claim type. For the core project,
* they will be String but you are free to extend this trait using other types like
* JsObject or anything else.
*
* Please, check implementations, like [[Jwt]], for code samples.
*
* @tparam H the type of the extracted header from a JSON Web Token
* @tparam C the type of the extracted claim from a JSON Web Token
*
* @define token a JSON Web Token as a Base64 url-safe encoded String which can be used inside an HTTP header
* @define headerString a valid stringified JSON representing the header of the token
* @define claimString a valid stringified JSON representing the claim of the token
* @define key the key that will be used to check the token signature
* @define algo the algorithm to sign the token
* @define algos a list of possible algorithms that the token can use. See [[http://pauldijou.fr/jwt-scala/#security-concerns Security concerns]] for more infos.
*
*/
trait JwtCore[H, C] {
// Abstract methods
protected def parseHeader(header: String): H
protected def parseClaim(claim: String): C
def encode(header: String, claim: String): String = {
JwtBase64.encodeString(header) + "." + JwtBase64.encodeString(claim) + "."
}
/** Encode a JSON Web Token from its different parts. Both the header and the claim will be encoded to Base64 url-safe, then a signature will be eventually generated from it if you did pass a key and an algorithm, and finally, those three parts will be merged as a single string, using dots as separator.
*
* @return $token
* @param header $headerString
* @param claim $claimString
* @param key $key
* @param algorithm $algo
*/
def encode(header: String, claim: String, key: String, algorithm: JwtAlgorithm): String = {
val data = JwtBase64.encodeString(header) + "." + JwtBase64.encodeString(claim)
data + "." + JwtBase64.encodeString(JwtUtils.sign(data, key, algorithm))
}
def encode(header: String, claim: String, key: SecretKey, algorithm: JwtHmacAlgorithm): String = {
val data = JwtBase64.encodeString(header) + "." + JwtBase64.encodeString(claim)
data + "." + JwtBase64.encodeString(JwtUtils.sign(data, key, algorithm))
}
def encode(header: String, claim: String, key: PrivateKey, algorithm: JwtAsymetricAlgorithm): String = {
val data = JwtBase64.encodeString(header) + "." + JwtBase64.encodeString(claim)
data + "." + JwtBase64.encodeString(JwtUtils.sign(data, key, algorithm))
}
/** An alias to `encode` which will provide an automatically generated header.
*
* @return $token
* @param claim $claimString
*/
def encode(claim: String): String = encode(JwtHeader().toJson, claim)
/** An alias to `encode` which will provide an automatically generated header and allowing you to get rid of Option
* for the key and the algorithm.
*
* @return $token
* @param claim $claimString
* @param key $key
* @param algorithm $algo
*/
def encode(claim: String, key: String, algorithm: JwtAlgorithm): String =
encode(JwtHeader(algorithm).toJson, claim, key, algorithm)
/** An alias to `encode` which will provide an automatically generated header and allowing you to get rid of Option
* for the key and the algorithm.
*
* @return $token
* @param claim $claimString
* @param key $key
* @param algorithm $algo
*/
def encode(claim: String, key: SecretKey, algorithm: JwtHmacAlgorithm): String =
encode(JwtHeader(algorithm).toJson, claim, key, algorithm)
/** An alias to `encode` which will provide an automatically generated header and allowing you to get rid of Option
* for the key and the algorithm.
*
* @return $token
* @param claim $claimString
* @param key $key
* @param algorithm $algo
*/
def encode(claim: String, key: PrivateKey, algorithm: JwtAsymetricAlgorithm): String =
encode(JwtHeader(algorithm).toJson, claim, key, algorithm)
/** An alias to `encode` which will provide an automatically generated header and setting both key and algorithm
* to None.
*
* @return $token
* @param claim the claim of the JSON Web Token
*/
def encode(claim: JwtClaim): String = encode(claim.toJson)
/** An alias to `encode` which will provide an automatically generated header and use the claim as a case class.
*
* @return $token
* @param claim the claim of the JSON Web Token
* @param key $key
* @param algorithm $algo
*/
def encode(claim: JwtClaim, key: String, algorithm: JwtAlgorithm): String =
encode(claim.toJson, key, algorithm)
/** An alias to `encode` which will provide an automatically generated header and use the claim as a case class.
*
* @return $token
* @param claim the claim of the JSON Web Token
* @param key $key
* @param algorithm $algo
*/
def encode(claim: JwtClaim, key: SecretKey, algorithm: JwtHmacAlgorithm): String =
encode(claim.toJson, key, algorithm)
/** An alias to `encode` which will provide an automatically generated header and use the claim as a case class.
*
* @return $token
* @param claim the claim of the JSON Web Token
* @param key $key
* @param algorithm $algo
*/
def encode(claim: JwtClaim, key: PrivateKey, algorithm: JwtAsymetricAlgorithm): String =
encode(claim.toJson, key, algorithm)
/** An alias to `encode` if you want to use case classes for the header and the claim rather than strings, they will just be stringified to JSON format.
*
* @return $token
* @param header the header to stringify as a JSON before encoding the token
* @param claim the claim to stringify as a JSON before encoding the token
*/
def encode(header: JwtHeader, claim: JwtClaim): String = header.algorithm match {
case None => encode(header.toJson, claim.toJson)
case _ => throw new JwtNonEmptyAlgorithmException()
}
/** An alias of `encode` if you only want to pass a string as the key, the algorithm will be deduced from the header.
*
* @return $token
* @param header the header to stringify as a JSON before encoding the token
* @param claim the claim to stringify as a JSON before encoding the token
* @param key the secret key to use to sign the token (note that the algorithm will be deduced from the header)
*/
def encode(header: JwtHeader, claim: JwtClaim, key: String): String = header.algorithm match {
case Some(algo: JwtAlgorithm) => encode(header.toJson, claim.toJson, key, algo)
case _ => throw new JwtEmptyAlgorithmException()
}
/** An alias of `encode` if you only want to pass a string as the key, the algorithm will be deduced from the header.
*
* @return $token
* @param header the header to stringify as a JSON before encoding the token
* @param claim the claim to stringify as a JSON before encoding the token
* @param key the secret key to use to sign the token (note that the algorithm will be deduced from the header)
*/
def encode(header: JwtHeader, claim: JwtClaim, key: Key): String = (header.algorithm, key) match {
case (Some(algo: JwtHmacAlgorithm), k: SecretKey) => encode(header.toJson, claim.toJson, k, algo)
case (Some(algo: JwtAsymetricAlgorithm), k: PrivateKey) => encode(header.toJson, claim.toJson, k, algo)
case _ => throw new JwtValidationException("The key type doesn't match the algorithm type. It's either a SecretKey and a HMAC algorithm or a PrivateKey and a RSA or ECDSA algorithm. And an algorithm is required of course.")
}
/**
* @return a tuple of (header64, header, claim64, claim, Option(signature as bytes))
* @throws JwtLengthException if there is not 2 or 3 parts in the token
*/
private def splitToken(token: String): (String, String, String, String, String) = {
val parts = token.split("\\.")
val signature = parts.length match {
case 2 => ""
case 3 => parts(2)
case _ => throw new JwtLengthException(s"Expected token [$token] to be composed of 2 or 3 parts separated by dots.")
}
(parts(0), JwtBase64.decodeString(parts(0)), parts(1), JwtBase64.decodeString(parts(1)), signature)
}
/** Will try to decode a JSON Web Token to raw strings
*
* @return if successful, a tuple of 3 strings, the header, the claim and the signature
* @param token $token
*/
def decodeRawAll(token: String, options: JwtOptions): Try[(String, String, String)] = Try {
val (header64, header, claim64, claim, signature) = splitToken(token)
validate(parseHeader(header), parseClaim(claim), signature, options)
(header, claim, signature)
}
def decodeRawAll(token: String): Try[(String, String, String)] = decodeRawAll(token, JwtOptions.DEFAULT)
/** Will try to decode a JSON Web Token to raw strings using a HMAC algorithm
*
* @return if successful, a tuple of 3 strings, the header, the claim and the signature
* @param token $token
* @param key $key
* @param algorithms $algos
*/
def decodeRawAll(token: String, key: String, algorithms: Seq[JwtHmacAlgorithm], options: JwtOptions): Try[(String, String, String)] = Try {
val (header64, header, claim64, claim, signature) = splitToken(token)
validate(header64, parseHeader(header), claim64, parseClaim(claim), signature, key, algorithms, options)
(header, claim, signature)
}
def decodeRawAll(token: String, key: String, algorithms: Seq[JwtHmacAlgorithm]): Try[(String, String, String)] =
decodeRawAll(token, key, algorithms, JwtOptions.DEFAULT)
/** Will try to decode a JSON Web Token to raw strings using an asymetric algorithm
*
* @return if successful, a tuple of 3 strings, the header, the claim and the signature
* @param token $token
* @param key $key
* @param algorithms $algos
*/
def decodeRawAll(token: String, key: String, algorithms: => Seq[JwtAsymetricAlgorithm], options: JwtOptions): Try[(String, String, String)] = Try {
val (header64, header, claim64, claim, signature) = splitToken(token)
validate(header64, parseHeader(header), claim64, parseClaim(claim), signature, key, algorithms, options)
(header, claim, signature)
}
def decodeRawAll(token: String, key: String, algorithms: => Seq[JwtAsymetricAlgorithm]): Try[(String, String, String)] =
decodeRawAll(token, key, algorithms, JwtOptions.DEFAULT)
/** Will try to decode a JSON Web Token to raw strings using a HMAC algorithm
*
* @return if successful, a tuple of 3 strings, the header, the claim and the signature
* @param token $token
* @param key $key
* @param algorithms $algos
*/
def decodeRawAll(token: String, key: SecretKey, algorithms: Seq[JwtHmacAlgorithm], options: JwtOptions): Try[(String, String, String)] = Try {
val (header64, header, claim64, claim, signature) = splitToken(token)
validate(header64, parseHeader(header), claim64, parseClaim(claim), signature, key, algorithms, options)
(header, claim, signature)
}
def decodeRawAll(token: String, key: SecretKey, algorithms: Seq[JwtHmacAlgorithm]): Try[(String, String, String)] =
decodeRawAll(token, key, algorithms, JwtOptions.DEFAULT)
def decodeRawAll(token: String, key: SecretKey, options: JwtOptions): Try[(String, String, String)] =
decodeRawAll(token, key, JwtAlgorithm.allHmac, options)
def decodeRawAll(token: String, key: SecretKey): Try[(String, String, String)] =
decodeRawAll(token, key, JwtOptions.DEFAULT)
/** Will try to decode a JSON Web Token to raw strings using an asymetric algorithm
*
* @return if successful, a tuple of 3 strings, the header, the claim and the signature
* @param token $token
* @param key $key
* @param algorithms $algos
*/
def decodeRawAll(token: String, key: PublicKey, algorithms: Seq[JwtAsymetricAlgorithm], options: JwtOptions): Try[(String, String, String)] = Try {
val (header64, header, claim64, claim, signature) = splitToken(token)
validate(header64, parseHeader(header), claim64, parseClaim(claim), signature, key, algorithms, options)
(header, claim, signature)
}
def decodeRawAll(token: String, key: PublicKey, algorithms: Seq[JwtAsymetricAlgorithm]): Try[(String, String, String)] =
decodeRawAll(token, key, algorithms, JwtOptions.DEFAULT)
def decodeRawAll(token: String, key: PublicKey, options: JwtOptions): Try[(String, String, String)] =
decodeRawAll(token, key, JwtAlgorithm.allAsymetric, options)
def decodeRawAll(token: String, key: PublicKey): Try[(String, String, String)] =
decodeRawAll(token, key, JwtOptions.DEFAULT)
/** Same as `decodeRawAll` but only return the claim (you only care about the claim most of the time)
*
* @return if successful, a string representing the JSON version of the claim
* @param token $token
*/
def decodeRaw(token: String, options: JwtOptions): Try[String] = decodeRawAll(token, options).map(_._2)
def decodeRaw(token: String): Try[String] = decodeRaw(token, JwtOptions.DEFAULT)
/** Same as `decodeRawAll` but only return the claim (you only care about the claim most of the time)
*
* @return if successful, a string representing the JSON version of the claim
* @param token $token
* @param key $key
* @param algorithms $algos
*/
def decodeRaw(token: String, key: String, algorithms: Seq[JwtHmacAlgorithm], options: JwtOptions): Try[String] =
decodeRawAll(token, key, algorithms, options).map(_._2)
def decodeRaw(token: String, key: String, algorithms: Seq[JwtHmacAlgorithm]): Try[String] =
decodeRaw(token, key, algorithms, JwtOptions.DEFAULT)
/** Same as `decodeRawAll` but only return the claim (you only care about the claim most of the time)
*
* @return if successful, a string representing the JSON version of the claim
* @param token $token
* @param key $key
* @param algorithms $algos
*/
def decodeRaw(token: String, key: String, algorithms: => Seq[JwtAsymetricAlgorithm], options: JwtOptions): Try[String] =
decodeRawAll(token, key, algorithms, options).map(_._2)
def decodeRaw(token: String, key: String, algorithms: => Seq[JwtAsymetricAlgorithm]): Try[String] =
decodeRaw(token, key, algorithms, JwtOptions.DEFAULT)
/** Same as `decodeRawAll` but only return the claim (you only care about the claim most of the time)
*
* @return if successful, a string representing the JSON version of the claim
* @param token $token
* @param key $key
* @param algorithms $algos
*/
def decodeRaw(token: String, key: SecretKey, algorithms: Seq[JwtHmacAlgorithm], options: JwtOptions): Try[String] =
decodeRawAll(token, key, algorithms, options).map(_._2)
def decodeRaw(token: String, key: SecretKey, algorithms: Seq[JwtHmacAlgorithm]): Try[String] =
decodeRaw(token, key, algorithms, JwtOptions.DEFAULT)
/** Same as `decodeRawAll` but only return the claim (you only care about the claim most of the time)
*
* @return if successful, a string representing the JSON version of the claim
* @param token $token
* @param key $key
*/
def decodeRaw(token: String, key: SecretKey, options: JwtOptions): Try[String] = decodeRaw(token, key, JwtAlgorithm.allHmac, options)
def decodeRaw(token: String, key: SecretKey): Try[String] = decodeRaw(token, key, JwtOptions.DEFAULT)
/** Same as `decodeRawAll` but only return the claim (you only care about the claim most of the time)
*
* @return if successful, a string representing the JSON version of the claim
* @param token $token
* @param key $key
* @param algorithms $algos
*/
def decodeRaw(token: String, key: PublicKey, algorithms: Seq[JwtAsymetricAlgorithm], options: JwtOptions): Try[String] =
decodeRawAll(token, key, algorithms, options).map(_._2)
def decodeRaw(token: String, key: PublicKey, algorithms: Seq[JwtAsymetricAlgorithm]): Try[String] =
decodeRaw(token, key, algorithms, JwtOptions.DEFAULT)
/** Same as `decodeRawAll` but only return the claim (you only care about the claim most of the time)
*
* @return if successful, a string representing the JSON version of the claim
* @param token $token
* @param key $key
*/
def decodeRaw(token: String, key: PublicKey, options: JwtOptions): Try[String] = decodeRaw(token, key, JwtAlgorithm.allAsymetric, options)
def decodeRaw(token: String, key: PublicKey): Try[String] = decodeRaw(token, key, JwtOptions.DEFAULT)
/** Same as `decodeRawAll` but return the real header and claim types
*
* @return if successful, a tuple representing the header, the claim and eventually the signature
* @param token $token
*/
def decodeAll(token: String, options: JwtOptions): Try[(H, C, String)] = Try {
val (header64, header, claim64, claim, signature) = splitToken(token)
val (h, c) = (parseHeader(header), parseClaim(claim))
validate(h, c, signature, options)
(h, c, signature)
}
def decodeAll(token: String): Try[(H, C, String)] = decodeAll(token, JwtOptions.DEFAULT)
/** Same as `decodeRawAll` but return the real header and claim types
*
* @return if successful, a tuple representing the header, the claim and eventually the signature
* @param token $token
* @param key $key
* @param algorithms $algos
*/
def decodeAll(token: String, key: String, algorithms: Seq[JwtHmacAlgorithm], options: JwtOptions): Try[(H, C, String)] = Try {
val (header64, header, claim64, claim, signature) = splitToken(token)
val (h, c) = (parseHeader(header), parseClaim(claim))
validate(header64, h, claim64, c, signature, key, algorithms, options)
(h, c, signature)
}
def decodeAll(token: String, key: String, algorithms: Seq[JwtHmacAlgorithm]): Try[(H, C, String)] =
decodeAll(token, key, algorithms, JwtOptions.DEFAULT)
/** Same as `decodeRawAll` but return the real header and claim types
*
* @return if successful, a tuple representing the header, the claim and eventually the signature
* @param token $token
* @param key $key
* @param algorithms $algos
*/
def decodeAll(token: String, key: String, algorithms: => Seq[JwtAsymetricAlgorithm], options: JwtOptions): Try[(H, C, String)] = Try {
val (header64, header, claim64, claim, signature) = splitToken(token)
val (h, c) = (parseHeader(header), parseClaim(claim))
validate(header64, h, claim64, c, signature, key, algorithms, options)
(h, c, signature)
}
def decodeAll(token: String, key: String, algorithms: => Seq[JwtAsymetricAlgorithm]): Try[(H, C, String)] =
decodeAll(token, key, algorithms, JwtOptions.DEFAULT)
/** Same as `decodeRawAll` but return the real header and claim types
*
* @return if successful, a tuple representing the header, the claim and eventually the signature
* @param token $token
* @param key $key
* @param algorithms $algos
*/
def decodeAll(token: String, key: SecretKey, algorithms: Seq[JwtHmacAlgorithm], options: JwtOptions): Try[(H, C, String)] = Try {
val (header64, header, claim64, claim, signature) = splitToken(token)
val (h, c) = (parseHeader(header), parseClaim(claim))
validate(header64, h, claim64, c, signature, key, algorithms, options)
(h, c, signature)
}
def decodeAll(token: String, key: SecretKey, algorithms: Seq[JwtHmacAlgorithm]): Try[(H, C, String)] =
decodeAll(token, key, algorithms, JwtOptions.DEFAULT)
/** Same as `decodeRawAll` but return the real header and claim types
*
* @return if successful, a tuple representing the header, the claim and eventually the signature
* @param token $token
* @param key $key
*/
def decodeAll(token: String, key: SecretKey, options: JwtOptions): Try[(H, C, String)] =
decodeAll(token, key, JwtAlgorithm.allHmac, options)
def decodeAll(token: String, key: SecretKey): Try[(H, C, String)] =
decodeAll(token, key, JwtOptions.DEFAULT)
/** Same as `decodeRawAll` but return the real header and claim types
*
* @return if successful, a tuple representing the header, the claim and eventually the signature
* @param token $token
* @param key $key
* @param algorithms $algos
*/
def decodeAll(token: String, key: PublicKey, algorithms: Seq[JwtAsymetricAlgorithm], options: JwtOptions): Try[(H, C, String)] = Try {
val (header64, header, claim64, claim, signature) = splitToken(token)
val (h, c) = (parseHeader(header), parseClaim(claim))
validate(header64, h, claim64, c, signature, key, algorithms, options)
(h, c, signature)
}
def decodeAll(token: String, key: PublicKey, algorithms: Seq[JwtAsymetricAlgorithm]): Try[(H, C, String)] =
decodeAll(token, key, algorithms, JwtOptions.DEFAULT)
/** Same as `decodeRawAll` but return the real header and claim types
*
* @return if successful, a tuple representing the header, the claim and eventually the signature
* @param token $token
* @param key $key
*/
def decodeAll(token: String, key: PublicKey, options: JwtOptions): Try[(H, C, String)] =
decodeAll(token, key, JwtAlgorithm.allAsymetric, options)
def decodeAll(token: String, key: PublicKey): Try[(H, C, String)] =
decodeAll(token, key, JwtOptions.DEFAULT)
/** Same as `decodeAll` but only return the claim
*
* @return if successful, the claim of the token in its correct type
* @param token $token
*/
def decode(token: String, options: JwtOptions): Try[C] = decodeAll(token, options).map(_._2)
def decode(token: String): Try[C] = decode(token, JwtOptions.DEFAULT)
/** Same as `decodeAll` but only return the claim
*
* @return if successful, the claim of the token in its correct type
* @param token $token
* @param key $key
* @param algorithms $algos
*/
def decode(token: String, key: String, algorithms: Seq[JwtHmacAlgorithm], options: JwtOptions): Try[C] =
decodeAll(token, key, algorithms, options).map(_._2)
def decode(token: String, key: String, algorithms: Seq[JwtHmacAlgorithm]): Try[C] =
decode(token, key, algorithms, JwtOptions.DEFAULT)
/** Same as `decodeAll` but only return the claim
*
* @return if successful, the claim of the token in its correct type
* @param token $token
* @param key $key
* @param algorithms $algos
*/
def decode(token: String, key: String, algorithms: => Seq[JwtAsymetricAlgorithm], options: JwtOptions): Try[C] =
decodeAll(token, key, algorithms, options).map(_._2)
def decode(token: String, key: String, algorithms: => Seq[JwtAsymetricAlgorithm]): Try[C] =
decode(token, key, algorithms, JwtOptions.DEFAULT)
/** Same as `decodeAll` but only return the claim
*
* @return if successful, the claim of the token in its correct type
* @param token $token
* @param key $key
* @param algorithms $algos
*/
def decode(token: String, key: SecretKey, algorithms: Seq[JwtHmacAlgorithm], options: JwtOptions): Try[C] =
decodeAll(token, key, algorithms, options).map(_._2)
def decode(token: String, key: SecretKey, algorithms: Seq[JwtHmacAlgorithm]): Try[C] =
decode(token, key, algorithms, JwtOptions.DEFAULT)
/** Same as `decodeAll` but only return the claim
*
* @return if successful, the claim of the token in its correct type
* @param token $token
* @param key $key
*/
def decode(token: String, key: SecretKey, options: JwtOptions): Try[C] = decode(token, key, JwtAlgorithm.allHmac, options)
def decode(token: String, key: SecretKey): Try[C] = decode(token, key, JwtOptions.DEFAULT)
/** Same as `decodeAll` but only return the claim
*
* @return if successful, the claim of the token in its correct type
* @param token $token
* @param key $key
* @param algorithms $algos
*/
def decode(token: String, key: PublicKey, algorithms: Seq[JwtAsymetricAlgorithm], options: JwtOptions): Try[C] =
decodeAll(token, key, algorithms, options).map(_._2)
def decode(token: String, key: PublicKey, algorithms: Seq[JwtAsymetricAlgorithm]): Try[C] =
decode(token, key, algorithms, JwtOptions.DEFAULT)
/** Same as `decodeAll` but only return the claim
*
* @return if successful, the claim of the token in its correct type
* @param token $token
* @param key $key
*/
def decode(token: String, key: PublicKey, options: JwtOptions): Try[C] = decode(token, key, JwtAlgorithm.allAsymetric, options)
def decode(token: String, key: PublicKey): Try[C] = decode(token, key, JwtOptions.DEFAULT)
// Validate
protected def extractAlgorithm(header: H): Option[JwtAlgorithm]
protected def extractExpiration(claim: C): Option[Long]
protected def extractNotBefore(claim: C): Option[Long]
protected def validateTiming(claim: C, options: JwtOptions) {
val maybeExpiration: Option[Long] =
if (options.expiration) extractExpiration(claim) else None
val maybeNotBefore: Option[Long] =
if (options.notBefore) extractNotBefore(claim) else None
JwtTime.validateNowIsBetweenSeconds(
maybeNotBefore.map(_ - options.leeway),
maybeExpiration.map(_ + options.leeway)
)
}
// Validate if an algorithm is inside the authorized range
protected def validateHmacAlgorithm(algorithm: JwtHmacAlgorithm, algorithms: Seq[JwtHmacAlgorithm]): Boolean = {
algorithms.contains(algorithm)
}
// Validate if an algorithm is inside the authorized range
protected def validateAsymetricAlgorithm(algorithm: JwtAsymetricAlgorithm, algorithms: Seq[JwtAsymetricAlgorithm]): Boolean = {
algorithms.contains(algorithm)
}
// Validation when no key and no algorithm
protected def validate(header: H, claim: C, signature: String, options: JwtOptions) {
if (options.signature && !signature.isEmpty) {
throw new JwtNonEmptySignatureException()
} else if (options.signature && !extractAlgorithm(header).isEmpty) {
throw new JwtNonEmptyAlgorithmException()
}
validateTiming(claim, options)
}
// Validation when both key and algorithm
protected def validate(
header64: String,
header: H,
claim64: String,
claim: C,
signature: String,
options: JwtOptions,
verify: (Array[Byte], Array[Byte], JwtAlgorithm) => Boolean): Unit = {
if (options.signature) {
val maybeAlgo = extractAlgorithm(header)
if (options.signature && signature.isEmpty) {
throw new JwtEmptySignatureException()
} else if (maybeAlgo.isEmpty) {
throw new JwtEmptyAlgorithmException()
} else if (!verify(JwtUtils.bytify(header64 +"."+ claim64), JwtBase64.decode(signature), maybeAlgo.get)) {
throw new JwtValidationException("Invalid signature for this token or wrong algorithm.")
}
}
validateTiming(claim, options)
}
// Generic validation on String Key for HMAC algorithms
protected def validate(header64: String, header: H, claim64: String, claim: C, signature: String, key: String, algorithms: Seq[JwtHmacAlgorithm], options: JwtOptions): Unit = {
validate(header64, header, claim64, claim, signature, options, (data: Array[Byte], signature: Array[Byte], algorithm: JwtAlgorithm) => algorithm match {
case algo: JwtHmacAlgorithm => validateHmacAlgorithm(algo, algorithms) && JwtUtils.verify(data, signature, key, algo)
case _ => false
})
}
// Generic validation on String Key for asymetric algorithms
protected def validate(header64: String, header: H, claim64: String, claim: C, signature: String, key: String, algorithms: => Seq[JwtAsymetricAlgorithm], options: JwtOptions): Unit = {
validate(header64, header, claim64, claim, signature, options, (data: Array[Byte], signature: Array[Byte], algorithm: JwtAlgorithm) => algorithm match {
case algo: JwtAsymetricAlgorithm => validateAsymetricAlgorithm(algo, algorithms) && JwtUtils.verify(data, signature, key, algo)
case _ => false
})
}
// Validation for HMAC algorithm using a SecretKey
protected def validate(header64: String, header: H, claim64: String, claim: C, signature: String, key: SecretKey, algorithms: Seq[JwtHmacAlgorithm], options: JwtOptions): Unit = {
validate(header64, header, claim64, claim, signature, options, (data: Array[Byte], signature: Array[Byte], algorithm: JwtAlgorithm) => algorithm match {
case algo: JwtHmacAlgorithm => validateHmacAlgorithm(algo, algorithms) && JwtUtils.verify(data, signature, key, algo)
case _ => false
})
}
// Validation for RSA and ECDSA algorithms using PublicKey
protected def validate(header64: String, header: H, claim64: String, claim: C, signature: String, key: PublicKey, algorithms: Seq[JwtAsymetricAlgorithm], options: JwtOptions): Unit = {
validate(header64, header, claim64, claim, signature, options, (data: Array[Byte], signature: Array[Byte], algorithm: JwtAlgorithm) => algorithm match {
case algo: JwtAsymetricAlgorithm => validateAsymetricAlgorithm(algo, algorithms) && JwtUtils.verify(data, signature, key, algo)
case _ => false
})
}
/** Valid a token: doesn't return anything but will thrown exceptions if there are any errors.
*
* @param token $token
* @throws JwtValidationException default validation exeption
* @throws JwtLengthException the number of parts separated by dots is wrong
* @throws JwtNotBeforeException the token isn't valid yet because its `notBefore` attribute is in the future
* @throws JwtExpirationException the token isn't valid anymore because its `expiration` attribute is in the past
* @throws IllegalArgumentException couldn't decode the token since it's not a valid base64 string
*/
def validate(token: String, options: JwtOptions): Unit = {
val (header64, header, claim64, claim, signature) = splitToken(token)
validate(parseHeader(header), parseClaim(claim), signature, options)
}
def validate(token: String): Unit = validate(token, JwtOptions.DEFAULT)
/** An alias of `validate` in case you want to directly pass a string key for HMAC algorithms.
*
* @param token $token
* @param key $key
* @param algorithms $algos
* @throws JwtValidationException default validation exeption
* @throws JwtLengthException the number of parts separated by dots is wrong
* @throws JwtNotBeforeException the token isn't valid yet because its `notBefore` attribute is in the future
* @throws JwtExpirationException the token isn't valid anymore because its `expiration` attribute is in the past
* @throws IllegalArgumentException couldn't decode the token since it's not a valid base64 string
*/
def validate(token: String, key: String, algorithms: Seq[JwtHmacAlgorithm], options: JwtOptions): Unit = {
val (header64, header, claim64, claim, signature) = splitToken(token)
validate(header64, parseHeader(header), claim64, parseClaim(claim), signature, key, algorithms, options)
}
def validate(token: String, key: String, algorithms: Seq[JwtHmacAlgorithm]): Unit = validate(token, key, algorithms, JwtOptions.DEFAULT)
/** An alias of `validate` in case you want to directly pass a string key for asymetric algorithms.
*
* @param token $token
* @param key $key
* @param algorithms $algos
* @throws JwtValidationException default validation exeption
* @throws JwtLengthException the number of parts separated by dots is wrong
* @throws JwtNotBeforeException the token isn't valid yet because its `notBefore` attribute is in the future
* @throws JwtExpirationException the token isn't valid anymore because its `expiration` attribute is in the past
* @throws IllegalArgumentException couldn't decode the token since it's not a valid base64 string
*/
def validate(token: String, key: String, algorithms: => Seq[JwtAsymetricAlgorithm], options: JwtOptions): Unit = {
val (header64, header, claim64, claim, signature) = splitToken(token)
validate(header64, parseHeader(header), claim64, parseClaim(claim), signature, key, algorithms, options)
}
def validate(token: String, key: String, algorithms: => Seq[JwtAsymetricAlgorithm]): Unit = validate(token, key, algorithms, JwtOptions.DEFAULT)
/** An alias of `validate` in case you want to directly pass a string key.
*
* @param token $token
* @param key $key
* @param algorithms $algos
* @throws JwtValidationException default validation exeption
* @throws JwtLengthException the number of parts separated by dots is wrong
* @throws JwtNotBeforeException the token isn't valid yet because its `notBefore` attribute is in the future
* @throws JwtExpirationException the token isn't valid anymore because its `expiration` attribute is in the past
* @throws IllegalArgumentException couldn't decode the token since it's not a valid base64 string
*/
def validate(token: String, key: SecretKey, algorithms: Seq[JwtHmacAlgorithm], options: JwtOptions): Unit = {
val (header64, header, claim64, claim, signature) = splitToken(token)
validate(header64, parseHeader(header), claim64, parseClaim(claim), signature, key, algorithms, options)
}
def validate(token: String, key: SecretKey, algorithms: Seq[JwtHmacAlgorithm]): Unit = validate(token, key, algorithms, JwtOptions.DEFAULT)
def validate(token: String, key: SecretKey, options: JwtOptions): Unit = validate(token, key, JwtAlgorithm.allHmac, options)
def validate(token: String, key: SecretKey): Unit = validate(token, key, JwtOptions.DEFAULT)
/** An alias of `validate` in case you want to directly pass a string key.
*
* @param token $token
* @param key $key
* @param algorithms $algos
* @throws JwtValidationException default validation exeption
* @throws JwtLengthException the number of parts separated by dots is wrong
* @throws JwtNotBeforeException the token isn't valid yet because its `notBefore` attribute is in the future
* @throws JwtExpirationException the token isn't valid anymore because its `expiration` attribute is in the past
* @throws IllegalArgumentException couldn't decode the token since it's not a valid base64 string
*/
def validate(token: String, key: PublicKey, algorithms: Seq[JwtAsymetricAlgorithm], options: JwtOptions): Unit = {
val (header64, header, claim64, claim, signature) = splitToken(token)
validate(header64, parseHeader(header), claim64, parseClaim(claim), signature, key, algorithms, options)
}
def validate(token: String, key: PublicKey, algorithms: Seq[JwtAsymetricAlgorithm]): Unit = validate(token, key, algorithms, JwtOptions.DEFAULT)
def validate(token: String, key: PublicKey, options: JwtOptions): Unit = validate(token, key, JwtAlgorithm.allAsymetric, options)
def validate(token: String, key: PublicKey): Unit = validate(token, key, JwtOptions.DEFAULT)
/** Test if a token is valid. Doesn't throw any exception.
*
* @return a boolean value indicating if the token is valid or not
* @param token $token
*/
def isValid(token: String, options: JwtOptions): Boolean =
try {
validate(token, options)
true
} catch {
case _ : Throwable => false
}
def isValid(token: String): Boolean = isValid(token, JwtOptions.DEFAULT)
/** An alias for `isValid` if you want to directly pass a string as the key for HMAC algorithms
*
* @return a boolean value indicating if the token is valid or not
* @param token $token
* @param key $key
* @param algorithms $algos
*/
def isValid(token: String, key: String, algorithms: Seq[JwtHmacAlgorithm], options: JwtOptions): Boolean =
try {
validate(token, key, algorithms, options)
true
} catch {
case _ : Throwable => false
}
def isValid(token: String, key: String, algorithms: Seq[JwtHmacAlgorithm]): Boolean = isValid(token, key, algorithms, JwtOptions.DEFAULT)
/** An alias for `isValid` if you want to directly pass a string as the key for asymetric algorithms
*
* @return a boolean value indicating if the token is valid or not
* @param token $token
* @param key $key
* @param algorithms $algos
*/
def isValid(token: String, key: String, algorithms: => Seq[JwtAsymetricAlgorithm], options: JwtOptions): Boolean =
try {
validate(token, key, algorithms, options)
true
} catch {
case _ : Throwable => false
}
def isValid(token: String, key: String, algorithms: => Seq[JwtAsymetricAlgorithm]): Boolean = isValid(token, key, algorithms, JwtOptions.DEFAULT)
/** An alias for `isValid` if you want to directly pass a string as the key
*
* @return a boolean value indicating if the token is valid or not
* @param token $token
* @param key $key
* @param algorithms $algos
*/
def isValid(token: String, key: SecretKey, algorithms: Seq[JwtHmacAlgorithm], options: JwtOptions): Boolean =
try {
validate(token, key, algorithms, options)
true
} catch {
case _ : Throwable => false
}
def isValid(token: String, key: SecretKey, algorithms: Seq[JwtHmacAlgorithm]): Boolean = isValid(token, key, algorithms, JwtOptions.DEFAULT)
def isValid(token: String, key: SecretKey, options: JwtOptions): Boolean = isValid(token, key, JwtAlgorithm.allHmac, options)
def isValid(token: String, key: SecretKey): Boolean = isValid(token, key, JwtOptions.DEFAULT)
/** An alias for `isValid` if you want to directly pass a string as the key
*
* @return a boolean value indicating if the token is valid or not
* @param token $token
* @param key $key
* @param algorithms $algos
*/
def isValid(token: String, key: PublicKey, algorithms: Seq[JwtAsymetricAlgorithm], options: JwtOptions): Boolean =
try {
validate(token, key, algorithms, options)
true
} catch {
case _ : Throwable => false
}
def isValid(token: String, key: PublicKey, algorithms: Seq[JwtAsymetricAlgorithm]): Boolean = isValid(token, key, algorithms, JwtOptions.DEFAULT)
def isValid(token: String, key: PublicKey, options: JwtOptions): Boolean = isValid(token, key, JwtAlgorithm.allAsymetric, options)
def isValid(token: String, key: PublicKey): Boolean = isValid(token, key, JwtOptions.DEFAULT)
}