Skip to content

Commit

Permalink
feat: Test JWT OBJECT as Issuer (#1343)
Browse files Browse the repository at this point in the history
Signed-off-by: Bassam Riman <[email protected]>
  • Loading branch information
CryptoKnightIOG authored Sep 11, 2024
1 parent 7ad34f6 commit 7208d95
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ case class CredentialSchema(
`type`: String
)

case class CredentialIssuer(id: String)
case class CredentialIssuer(
id: String,
`type`: String
)

sealed trait CredentialPayload {
def maybeSub: Option[String]
Expand Down Expand Up @@ -216,7 +219,8 @@ object CredentialPayload {
(credentialIssuer: CredentialIssuer) =>
Json
.obj(
("id", credentialIssuer.id.asJson)
("id", credentialIssuer.id.asJson),
("type", credentialIssuer.`type`.asJson)
)

implicit val credentialStatusPurposeEncoder: Encoder[StatusPurpose] = (a: StatusPurpose) => a.toString.asJson
Expand Down Expand Up @@ -334,8 +338,9 @@ object CredentialPayload {
(c: HCursor) =>
for {
id <- c.downField("id").as[String]
`type` <- c.downField("type").as[String]
} yield {
CredentialIssuer(id = id)
CredentialIssuer(id = id, `type` = `type`)
}

implicit val credentialStatusPurposeDecoder: Decoder[StatusPurpose] = (c: HCursor) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ object JWTVerificationTest extends ZIOSpecDefault {
|}
|""".stripMargin

private def createJwtCredential(issuer: IssuerWithKey): JWT = {
private def createJwtCredential(issuer: IssuerWithKey, issuerAsObject: Boolean = false): JWT = {
val validFrom = Instant.parse("2010-01-05T00:00:00Z") // ISSUANCE DATE
val jwtCredentialNbf = Instant.parse("2010-01-01T00:00:00Z") // ISSUANCE DATE
val validUntil = Instant.parse("2010-01-09T00:00:00Z") // EXPIRATION DATE
Expand All @@ -81,7 +81,10 @@ object JWTVerificationTest extends ZIOSpecDefault {
maybeTermsOfUse = None,
maybeValidFrom = Some(validFrom),
maybeValidUntil = Some(validUntil),
maybeIssuer = Some(Left(issuer.issuer.did.toString))
maybeIssuer = Some(
if (issuerAsObject) Right(CredentialIssuer(issuer.issuer.did.toString, "Profile"))
else Left(issuer.issuer.did.toString)
)
),
nbf = jwtCredentialNbf, // ISSUANCE DATE
aud = Set.empty,
Expand Down Expand Up @@ -199,6 +202,21 @@ object JWTVerificationTest extends ZIOSpecDefault {
)
} yield assertTrue(validation.fold(_ => false, _ => true))
},
test("validate issuer happy path") {
val issuer = createUser("did:prism:issuer")
val jwtCredential = createJwtCredential(issuer, false)
val jwtCredentialWithObjectIssuer = createJwtCredential(issuer, true)
for {
jwt <- JwtCredential
.decodeJwt(jwtCredential)
jwtWithObjectIssuer <- JwtCredential
.decodeJwt(jwtCredentialWithObjectIssuer)
jwtWithObjectIssuerIssuer = jwtWithObjectIssuer.vc.maybeIssuer.get.toOption.get.id
jwtIssuer = jwt.vc.maybeIssuer.get.left.toOption.get
} yield assertTrue(
jwtWithObjectIssuerIssuer.equals(jwtIssuer)
)
},
test("validate dates should fail given after valid until") {
val issuer = createUser("did:prism:issuer")
val jwtCredential = createJwtCredential(issuer)
Expand Down

0 comments on commit 7208d95

Please sign in to comment.