This repository has been archived by the owner on Nov 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathJWTMacExamples.scala
71 lines (58 loc) · 3.36 KB
/
JWTMacExamples.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
import java.time.Instant
object JWTMacExamples {
import cats.effect.Sync
import cats.syntax.all._
import tsec.jws.mac._
import tsec.jwt._
import tsec.mac.jca._
import scala.concurrent.duration._
/** You can interpret into any target Monad with an instance of Sync[F] using JwtMac */
def jwtMonadic[F[_]: Sync]: F[JWTMac[HMACSHA256]] =
for {
key <- HMACSHA256.generateKey[F]
claims <- JWTClaims.withDuration[F](expiration = Some(10.minutes))
jwt <- JWTMac.build[F, HMACSHA256](claims, key) //You can sign and build a jwt object directly
verifiedFromObj <- JWTMac.verifyFromInstanceBool[F, HMACSHA256](jwt, key) //Verify from an object directly
stringjwt <- JWTMac.buildToString[F, HMACSHA256](claims, key) //Or build it straight to string
isverified <- JWTMac.verifyFromStringBool[F, HMACSHA256](stringjwt, key) //You can verify straight from a string
parsed <- JWTMac.verifyAndParse[F, HMACSHA256](stringjwt, key) //Or verify and return the actual instance
} yield parsed
import io.circe._
import io.circe.generic.semiauto._
import io.circe.syntax._
case class Doge(suchChars: String, much32Bits: Int, so64Bits: Long)
object Doge {
implicit val encoder: Encoder[Doge] = deriveEncoder[Doge]
implicit val decoder: Decoder[Doge] = deriveDecoder[Doge]
val WowSuchClaim = "Doge"
}
JWTClaims(customFields = Seq(Doge.WowSuchClaim -> Doge("w00f", 8008135, 80085L).asJson))
def builderStuff[F[_]: Sync]: F[JWTClaims] =
Sync[F].pure(JWTClaims(customFields = List(Doge.WowSuchClaim -> Doge("w00f", 8008135, 80085L).asJson)))
/** encoding custom claims **/
def jwtWithCustom[F[_]: Sync]: F[(JWTMac[HMACSHA256], Doge)] =
for {
key <- HMACSHA256.generateKey[F]
claims <- JWTClaims
.withDuration[F](
expiration = Some(10.minutes),
customFields = List(Doge.WowSuchClaim -> Doge("w00f", 8008135, 80085L).asJson)
)
jwt <- JWTMac.build[F, HMACSHA256](claims, key)
verifiedFromObj <- JWTMac.verifyFromInstanceBool[F, HMACSHA256](jwt, key)
stringjwt <- JWTMac.buildToString[F, HMACSHA256](claims, key) //Or build it straight to string
isverified <- JWTMac.verifyFromStringBool[F, HMACSHA256](stringjwt, key) //You can verify straight from a string
parsed <- JWTMac.verifyAndParse[F, HMACSHA256](stringjwt, key)
doge <- parsed.body.getCustomF[F, Doge](Doge.WowSuchClaim)
} yield (parsed, doge)
/** Using impure either interpreters */
val impureClaims = JWTClaims(expiration = Some(Instant.now.plusSeconds(10.minutes.toSeconds)))
val jwt: Either[Throwable, JWTMac[HMACSHA256]] = for {
key <- HMACSHA256.generateKey[MacErrorM]
jwt <- JWTMacImpure.build[HMACSHA256](impureClaims, key) //You can sign and build a jwt object directly
verifiedFromObj <- JWTMacImpure.verifyFromInstance[HMACSHA256](jwt, key)
stringjwt <- JWTMacImpure.buildToString[HMACSHA256](impureClaims, key) //Or build it straight to string
isverified <- JWTMacImpure.verifyFromString[HMACSHA256](stringjwt, key) //You can verify straight from a string
parsed <- JWTMacImpure.verifyAndParse[HMACSHA256](stringjwt, key) //Or verify and return the actual instance
} yield parsed
}