Jots

Testing

The jots-testing module depends on the core module and provides ScalaCheck generators and instances, and String interpolators for secrets. Importantly, the testing module also allows us to create a VerifiedJwt from any SignedJwt for testing purposes. This means the module should generally only be used for testing purposes.

Getting Started

To get started with sbt, add the following line to your build.sbt file.

libraryDependencies += "se.vlovgr" %% "jots-testing" % "0.4.0" % Test

If you are using Scala.js or Scala Native, replace the %% with %%% above.

Supported Features

The ScalaCheck generators and instances can be made available with import jots.testing.*, while the String interpolators and syntax for unsafe verification requires import jots.testing.syntax.*. If you're looking for usage examples, the testing module is heavily used by the library tests.

ScalaCheck Support

The testing module provides ScalaCheck Gen generators and Arbitrary instances. Following are some samples from a few generators. Note private and public keys (PrivateKey and PublicKey) are not generated on the fly, but instead choose from a pre-defined list of keys. This is due to the generation being computationally expensive.

import cats.effect.IO
import jots.JwtBuilder
import jots.SignedJwt
import jots.VerifiedJwt
import jots.testing.*
import org.scalacheck.Arbitrary.arbitrary

// Generate an arbitrary token prior to signing
arbitrary[JwtBuilder].sample
// res0: Option[JwtBuilder] = Some(JwtBuilder(JwtHeader(alg -> "HS256",cty -> "Ꭲ䇒멓湊闣锿璹賦飍웗ﷆ壕ᷟ嚰襉ゃꟊ悚怹뾚",kid -> "☳癫陋耚떥穉",typ -> "JWT"),JwtClaims(iss,sub,exp,nbf,iat)))

// Generate an arbitary token with valid signature
arbitrary[SignedJwt].sample
// res1: Option[SignedJwt] = Some(SignedJwt(SignedJwtHeader(alg -> "HS384",cty -> "ꃽᨷ슚됚䰓玎叧䂄愉籣霽兿꫘㉩첑㼄㈂縼5䳶쯗ꁉ풩芟䈻묾㦓箂Ꞡ꓉㷠呌逈콽暥Ԍ堷⺏傱岃㬖躃鏴쿙清ꩋ鸦䢴鹌乽⹜죍ꘚꄯ￯밙",kid -> "垀Ꮝᤦꅆ섨婢ﳺᕻ㐁笡嚟Ž쳷ᶦ⮘Ẃ⧷뢅猒ณ矬룾䌫⿩煄胏銜٦⮶쐺祫躇祢弋筺耗ꁘᦛ欫᪝━ᜤ볐︂ࡧⲠ蒒紽쥔漑Ḕ뜧쳂좸ᙟ믌攫ᑂɎ㎏ឯ惎ᄏ棛ାⶑ",typ -> "풲呥靂劧ﲀᰣᤖ摹荧ᦙ횴ᝰ၂젫慺ݪ닐ᓾ홮讹鄑蠻撣䡡鴫煘ʧ賷灟雒᭄붧휋㻋꼑٠⠭둆킢缋篳䟆⠕﭅ⴉ퍓䀹秲惔쭞✶"),SignedJwtClaims(iss,sub,aud,exp,nbf,iat,jti)))

// Generate an arbitrary token with a verified signature
arbitrary[VerifiedJwt].sample
// res2: Option[VerifiedJwt] = Some(VerifiedJwt(SignedJwtHeader(cty -> "껦ゥ쫠삆儴ᵱ牪䴮ƃᓦ頧⾅ཊ鲌⥸뀧퀛䗟凹쬥膱◔귺䈅밄4슗搆",kid -> "઻證碟癋ၟ駌죃༬욺㫉㘟᠘먤鞆趕ꆜ喜લ㣨鲗䪩洟ϙ폜썍录⇡祔뀥쒙갘볂⩲㘱剞杖ও⩆龢㷸㬃炮踲諿旴胺꡿ᆟ绁㖭ৠ₸Ꙧ牐寅阖쓄㹸㦍耆萙ꄺ炮췃䕛燌秂덻駧觗ႃᯍℴ중颸詡䛞槥䍻빤",typ -> "JWT",alg -> "ES256"),SignedJwtClaims(iss,sub,aud,exp,nbf,iat,jti)))

// Generate an arbitary signing instance using ECDSA
ecdsaJwtSigningGen[IO].sample
// res3: Option[JwtSigning[[A >: Nothing <: Any] => IO[A]]] = Some(jots.JwtSigning$$anon$2@17923ae7)

// Generate a matching RSA private and public key
rsaKeyPairGen.sample
// res4: Option[Tuple2[PrivateKey, PublicKey]] = Some((PrivateKey(**),PublicKey(-----BEGIN PUBLIC KEY-----
// MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAn51WIz3xwJztQxQE203/
// KYWHEDpASmT3QIW25h3yU+ZNgA3tm661QCpjiD/OoaEnGmJjVBgvqu5n3cpm2rI0
// YiijvbrOTskeAJ9jt79iab0oxlfrd9B9u9lubTbpyzmJ3I0+SyFU22efNASf201D
// exVonPRCNX66n93363bLvMHjRjHj+yCrOATRuJdq1Neeexm7/ZVP1jXa8+Hl52nF
// prymh31MocK2jXMxYNQvpxBcWz0GviPkQDMu/gEFVvIzMjBHbtQy9od+sToXi8y4
// Wb2f6hzDTud4SxQa+YAaDCuQ2hiosU1pOwe6uaoqTq3JI8/XT0FNQBDkVuF/d6Pt
// 2wIDAQAB
// -----END PUBLIC KEY-----)))

String Interpolators

The jots-crypto module provides syntax for compile-time parsing of public keys (PublicKey). The testing module provides additional syntax for private keys (PrivateKey), secret keys (SecretKey), and tokens (SignedJwt). These are generally sensitive and should not be in source code, except when they are non-secret for testing purposes.

import jots.SignedJwt
import jots.crypto.PrivateKey
import jots.crypto.SecretKey
import jots.testing.syntax.*

val privateKey: PrivateKey =
  privateKey"""
    -----BEGIN PRIVATE KEY-----
    MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgevZzL1gdAFr88hb2
    OF/2NxApJCzGCEDdfSp6VQO30hyhRANCAAQRWz+jn65BtOMvdyHKcvjBeBSDZH2r
    1RTwjmYSi9R/zpBnuQ4EiMnCqfMPWiZqB4QdbAd0E7oH50VpuZ1P087G
    -----END PRIVATE KEY-----
  """

val secretKey: SecretKey =
  secretKey"5BpYD67PafjVoefV11a06MVMGCmr1zoLrFGL019EEuoMtZszHqqpAd6frHFFgGXZ"

val signedJwt: SignedJwt =
  signedJwt"eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.eyJ1c2VySWQiOiI4ZDNiYmQxNC1kZmQ5LTQ3ZmEtYWFiNC1kNzZkYWYwMGI0ZjEiLCJleHAiOjMzNDUwNjI0MDAsImlhdCI6MTc2NzIyNTYwMH0.8i3xidY8bcAjoBYSKktcyihSdICGXBSBnjp13JYmO_DE5v4_oxY4bSBtZxdoic7OWFKZCcE63I1fFlukzgxVZA"

Unsafe Verification

It is possible to extend the default verifications for custom verifications. The core modules enforces that all instances of VerifiedJwt must have gone through signature verification. For testing purposes, the testing module allows us to create a VerifiedJwt from any SignedJwt without verification.

import jots.testing.syntax.*

signedJwt.toVerifiedUnsafe
// res5: VerifiedJwt = VerifiedJwt(SignedJwtHeader(typ -> "JWT",alg -> "ES256"),SignedJwtClaims(userId,exp,iat))