Skip to content

Commit

Permalink
feat(pollux): [ATL-2235] Verfiable Prensentation Adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
CryptoKnightIOG committed Dec 3, 2022
1 parent f493ba9 commit 198d611
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ case class JwtVc(
case class JwtCredentialPayload(
override val iss: String,
override val maybeSub: Option[String],
val vc: JwtVc,
vc: JwtVc,
override val nbf: Instant,
override val aud: Set[String],
override val maybeExp: Option[Instant],
Expand Down Expand Up @@ -658,7 +658,7 @@ object JwtCredential {
.toZIO
}

def validateEncodedJWT(jwt: JWT, leeway: TemporalAmount)(implicit clock: Clock): Validation[String, Unit] = {
def verifyDates(jwt: JWT, leeway: TemporalAmount)(implicit clock: Clock): Validation[String, Unit] = {
val now = clock.instant()

val decodeJWT =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ sealed trait PresentationPayload(
`@context`: IndexedSeq[String],
`type`: IndexedSeq[String],
verifiableCredential: IndexedSeq[VerifiableCredentialPayload],
maybeIss: Option[String],
iss: String,
maybeNbf: Option[Instant],
aud: IndexedSeq[String],
maybeExp: Option[Instant],
Expand All @@ -33,7 +33,7 @@ sealed trait PresentationPayload(
) {
def toJwtPresentationPayload: JwtPresentationPayload =
JwtPresentationPayload(
maybeIss = maybeIss,
iss = iss,
vp = JwtVp(
`@context` = `@context`,
`type` = `type`,
Expand All @@ -46,23 +46,18 @@ sealed trait PresentationPayload(
maybeNonce = maybeNonce
)

def toW3CPresentationPayload: Validation[String, W3cPresentationPayload] =
Validation.validateWith(
Validation.fromOptionWith("Iss must be defined")(maybeIss),
Validation.fromOptionWith("Nbf must be defined")(maybeNbf)
) { (iss, nbf) =>
W3cPresentationPayload(
`@context` = `@context`.distinct,
maybeId = maybeJti,
`type` = `type`.distinct,
verifiableCredential = verifiableCredential,
holder = iss,
verifier = aud,
issuanceDate = nbf,
maybeExpirationDate = maybeExp,
maybeNonce = maybeNonce
)
}
def toW3CPresentationPayload: W3cPresentationPayload =
W3cPresentationPayload(
`@context` = `@context`.distinct,
maybeId = maybeJti,
`type` = `type`.distinct,
verifiableCredential = verifiableCredential,
holder = iss,
verifier = aud,
maybeIssuanceDate = maybeNbf,
maybeExpirationDate = maybeExp,
maybeNonce = maybeNonce
)
}

case class W3cPresentationPayload(
Expand All @@ -72,7 +67,7 @@ case class W3cPresentationPayload(
verifiableCredential: IndexedSeq[VerifiableCredentialPayload],
holder: String,
verifier: IndexedSeq[String],
issuanceDate: Instant,
maybeIssuanceDate: Option[Instant],
maybeExpirationDate: Option[Instant],

/** Not part of W3C Presentation but included to preserve in case of conversion from JWT. */
Expand All @@ -83,8 +78,8 @@ case class W3cPresentationPayload(
maybeJti = maybeId,
verifiableCredential = verifiableCredential,
aud = verifier,
maybeIss = Some(holder),
maybeNbf = Some(issuanceDate),
iss = holder,
maybeNbf = maybeIssuanceDate,
maybeExp = maybeExpirationDate,
maybeNonce = maybeNonce
)
Expand All @@ -96,15 +91,15 @@ case class JwtVp(
)

case class JwtPresentationPayload(
maybeIss: Option[String],
iss: String,
vp: JwtVp,
maybeNbf: Option[Instant],
aud: IndexedSeq[String],
maybeExp: Option[Instant],
maybeJti: Option[String],
maybeNonce: Option[String]
) extends PresentationPayload(
maybeIss = maybeIss,
iss = iss,
`@context` = vp.`@context`,
`type` = vp.`type`,
verifiableCredential = vp.verifiableCredential,
Expand Down Expand Up @@ -132,7 +127,7 @@ object PresentationPayload {
("verifiableCredential", w3cPresentationPayload.verifiableCredential.asJson),
("holder", w3cPresentationPayload.holder.asJson),
("verifier", w3cPresentationPayload.verifier.asJson),
("issuanceDate", w3cPresentationPayload.issuanceDate.asJson),
("issuanceDate", w3cPresentationPayload.maybeIssuanceDate.asJson),
("expirationDate", w3cPresentationPayload.maybeExpirationDate.asJson)
)
.deepDropNullValues
Expand All @@ -153,7 +148,7 @@ object PresentationPayload {
(jwtPresentationPayload: JwtPresentationPayload) =>
Json
.obj(
("iss", jwtPresentationPayload.maybeIss.asJson),
("iss", jwtPresentationPayload.iss.asJson),
("vp", jwtPresentationPayload.vp.asJson),
("nbf", jwtPresentationPayload.maybeNbf.asJson),
("aud", jwtPresentationPayload.aud.asJson),
Expand Down Expand Up @@ -191,7 +186,7 @@ object PresentationPayload {
.as[Option[String]]
.map(_.iterator.toIndexedSeq)
.orElse(c.downField("verifier").as[Option[IndexedSeq[String]]].map(_.iterator.toIndexedSeq.flatten))
issuanceDate <- c.downField("issuanceDate").as[Instant]
maybeIssuanceDate <- c.downField("issuanceDate").as[Option[Instant]]
maybeExpirationDate <- c.downField("expirationDate").as[Option[Instant]]
} yield {
W3cPresentationPayload(
Expand All @@ -201,7 +196,7 @@ object PresentationPayload {
verifiableCredential = verifiableCredential.distinct,
holder = holder,
verifier = verifier.distinct,
issuanceDate = issuanceDate,
maybeIssuanceDate = maybeIssuanceDate,
maybeExpirationDate = maybeExpirationDate,
maybeNonce = Option.empty
)
Expand Down Expand Up @@ -232,7 +227,7 @@ object PresentationPayload {
implicit val JwtPresentationPayloadDecoder: Decoder[JwtPresentationPayload] =
(c: HCursor) =>
for {
maybeIss <- c.downField("iss").as[Option[String]]
iss <- c.downField("iss").as[String]
vp <- c.downField("vp").as[JwtVp]
maybeNbf <- c.downField("nbf").as[Option[Instant]]
aud <- c
Expand All @@ -245,7 +240,7 @@ object PresentationPayload {
maybeNonce <- c.downField("nonce").as[Option[String]]
} yield {
JwtPresentationPayload(
maybeIss = maybeIss,
iss = iss,
vp = vp,
maybeNbf = maybeNbf,
aud = aud.distinct,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ import java.time.*
println("==================")
println("Create JWT Credential")
println("==================")
val nbf = Instant.parse("2010-01-01T00:00:00Z")
val exp = Instant.parse("2010-01-12T00:00:00Z")
val nbf = Instant.parse("2010-01-01T00:00:00Z") // ISSUANCE DATE
val exp = Instant.parse("2010-01-12T00:00:00Z") // EXPIRATION DATE
val jwtCredentialPayload =
JwtCredentialPayload(
iss = "https://example.edu/issuers/565049", // ISSUER DID
Expand Down Expand Up @@ -91,61 +91,64 @@ import java.time.*

println("")
println("==================")
println("Validate JWT between nbf and exp")
println("Validate JWT between ISSUANCE DATE and EXPIRATION DATE")
println("==================")
val clockWithCurrentTime = Clock.fixed(nbf.plus(Duration.ofDays(1)), ZoneId.systemDefault)
val validAtCurrentTime =
JwtCredential.validateEncodedJWT(jwt = encodedJWT, leeway = Duration.ZERO)(clock = clockWithCurrentTime)
JwtCredential.verifyDates(jwt = encodedJWT, leeway = Duration.ZERO)(clock = clockWithCurrentTime)
println(s"Is Valid at current time? $validAtCurrentTime")

println("")
println("==================")
println("Validate JWT on NBF")
println("Validate JWT on ISSUANCE DATE")
println("==================")
val clockWithFixedTimeAtNbf = Clock.fixed(nbf, ZoneId.systemDefault)
val validAtNbf =
JwtCredential.validateEncodedJWT(jwt = encodedJWT, leeway = Duration.ZERO)(clock = clockWithFixedTimeAtNbf)
println(s"Is Valid at NBF time? $validAtNbf")
JwtCredential.verifyDates(jwt = encodedJWT, leeway = Duration.ZERO)(clock = clockWithFixedTimeAtNbf)
println(s"Is Valid at ISSUANCE DATE? $validAtNbf")

println("")
println("==================")
println("Validate JWT on EXP")
println("Validate JWT on EXPIRATION DATE")
println("==================")
val clockWithFixedTimeAtExp = Clock.fixed(exp, ZoneId.systemDefault)
val validAtExp =
JwtCredential.validateEncodedJWT(jwt = encodedJWT, leeway = Duration.ZERO)(clock = clockWithFixedTimeAtExp)
println(s"Is Valid at Exp time? $validAtExp")
JwtCredential.verifyDates(jwt = encodedJWT, leeway = Duration.ZERO)(clock = clockWithFixedTimeAtExp)
println(s"Is Valid at EXPIRATION DATE? $validAtExp")

println("")
println("==================")
println("Validate JWT before NBF")
println("Validate JWT before ISSUANCE DATE")
println("==================")
val clockWithFixedTimeBeforeNbf = Clock.fixed(nbf.minus(Duration.ofDays(1)), ZoneId.systemDefault)
val validBeforeNbf =
JwtCredential.validateEncodedJWT(jwt = encodedJWT, leeway = Duration.ZERO)(clock = clockWithFixedTimeBeforeNbf)
println(s"Is Valid before NBF time? $validBeforeNbf")
JwtCredential.verifyDates(jwt = encodedJWT, leeway = Duration.ZERO)(clock = clockWithFixedTimeBeforeNbf)
println(s"Is Valid before ISSUANCE DATE? $validBeforeNbf")

println("")
println("==================")
println("Validate JWT after EXP")
println("Validate JWT after EXPIRATION DATE")
println("==================")
val clockWithFixedTimeAfterExp = Clock.fixed(exp.plus(Duration.ofDays(1)), ZoneId.systemDefault)
val validAfterExp =
JwtCredential.validateEncodedJWT(jwt = encodedJWT, leeway = Duration.ZERO)(clock = clockWithFixedTimeAfterExp)
println(s"Is Valid after EXP time? $validAfterExp")
JwtCredential.verifyDates(jwt = encodedJWT, leeway = Duration.ZERO)(clock = clockWithFixedTimeAfterExp)
println(s"Is Valid after EXPIRATION DATE? $validAfterExp")

println("")
println("==================")
println("Validate JWT before NBF with 1 Day Leeway")
println("Validate JWT before ISSUANCE DATE with 1 Day Leeway")
println("==================")
val leeway = Duration.ofDays(1)
val validBeforeNbfWithLeeway =
JwtCredential.validateEncodedJWT(jwt = encodedJWT, leeway = Duration.ofDays(1))(clock = clockWithFixedTimeBeforeNbf)
println(s"Is Valid before NBF time with 1 Day Leeway? $validBeforeNbfWithLeeway")
JwtCredential.verifyDates(jwt = encodedJWT, leeway = leeway)(clock = clockWithFixedTimeBeforeNbf)
println(
s"Is Valid before ISSUANCE DATE with 1 Day Leeway? $validBeforeNbfWithLeeway with leeway:$leeway"
)

println("")
println("==================")
println("Validate JWT after EXP with 1 Day Leeway")
println("Validate JWT after EXPIRATION DATE with 1 Day Leeway")
println("==================")
val validAfterExpWithLeeway =
JwtCredential.validateEncodedJWT(jwt = encodedJWT, leeway = Duration.ofDays(1))(clock = clockWithFixedTimeAfterExp)
println(s"Is Valid after EXP time with 1 Day Leeway? $validAfterExpWithLeeway")
JwtCredential.verifyDates(jwt = encodedJWT, leeway = leeway)(clock = clockWithFixedTimeAfterExp)
println(s"Is Valid after EXPIRATION DATE with 1 Day Leeway? $validAfterExpWithLeeway with leeway:$leeway")
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ import java.time.{Instant, ZonedDateTime}
verifiableCredential = Vector(w3cVerifiableCredentialPayload, jwtVerifiableCredentialPayload),
holder = "https://example.edu/holder/565049",
verifier = Vector("https://example.edu/issuers/565049"),
issuanceDate = Instant.parse("2010-01-01T00:00:00Z"),
maybeIssuanceDate = Some(Instant.parse("2010-01-01T00:00:00Z")),
maybeExpirationDate = Some(Instant.parse("2010-01-12T00:00:00Z"))
)
println(w3cPresentationPayload.asJson.toString())
Expand Down

0 comments on commit 198d611

Please sign in to comment.