-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(agent): validate JWT proof in the credential request, update dem… (
#1165) Signed-off-by: Yurii Shynbuiev <[email protected]> Signed-off-by: Hyperledger Bot <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Hyperledger Bot <[email protected]>
- Loading branch information
1 parent
dc94c10
commit 0a709e3
Showing
8 changed files
with
255 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
.../server/src/main/scala/org/hyperledger/identus/oid4vci/domain/Openid4VCIProofJwtOps.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.hyperledger.identus.oid4vci.domain | ||
|
||
import com.nimbusds.jose.{JOSEObjectType, JWSAlgorithm, JWSHeader, JWSObject, JWSSigner, Payload} | ||
import org.hyperledger.identus.castor.core.model.did.LongFormPrismDID | ||
import org.hyperledger.identus.pollux.vc.jwt.{DidResolver, JWT} | ||
import org.hyperledger.identus.pollux.vc.jwt.JwtSignerImplicits.* | ||
import org.hyperledger.identus.shared.crypto.Secp256k1PrivateKey | ||
import zio.Task | ||
import zio.ZIO | ||
|
||
import java.util.UUID | ||
import scala.jdk.CollectionConverters.* | ||
import scala.util.Try | ||
|
||
trait Openid4VCIProofJwtOps { | ||
|
||
val jwtTypeName = "openid4vci-proof+jwt" | ||
|
||
val supportedAlgorithms: Set[String] = Set("ES256K") | ||
|
||
private def buildHeaderFromLongFormDid(longFormDID: LongFormPrismDID) = { | ||
new JWSHeader.Builder(JWSAlgorithm.ES256K) | ||
.keyID(longFormDID.toString) | ||
.`type`(new JOSEObjectType(jwtTypeName)) | ||
.build() | ||
} | ||
|
||
private def buildPayload(nonce: String, aud: UUID, iat: Int) = { | ||
new Payload(Map("nonce" -> nonce, "aud" -> aud.toString, "iat" -> iat).asJava) | ||
} | ||
|
||
private def makeJwtProof(header: JWSHeader, payload: Payload, signer: JWSSigner): String = { | ||
val jwt = new JWSObject(header, payload) | ||
jwt.sign(signer) | ||
jwt.serialize() | ||
} | ||
|
||
def makeJwtProof( | ||
longFormPrismDID: LongFormPrismDID, | ||
nonce: String, | ||
aud: UUID, | ||
iat: Int, | ||
privateKey: Secp256k1PrivateKey | ||
): JWT = { | ||
val header = buildHeaderFromLongFormDid(longFormPrismDID) | ||
val payload = buildPayload(nonce, aud, iat) | ||
JWT(makeJwtProof(header, payload, privateKey.asJwtSigner)) | ||
} | ||
|
||
def getKeyIdFromJwt(jwt: JWT): Task[String] = { | ||
for { | ||
jwsObject <- ZIO.fromTry(Try(JWSObject.parse(jwt.value))) | ||
keyID = jwsObject.getHeader.getKeyID | ||
_ <- ZIO.fail(new Exception("Key ID not found in JWT header")) when (keyID == null || keyID.isEmpty) | ||
} yield keyID | ||
} | ||
|
||
def getNonceFromJwt(jwt: JWT): Task[String] = { | ||
for { | ||
jwsObject <- ZIO.fromTry(Try(JWSObject.parse(jwt.value))) | ||
payload = jwsObject.getPayload.toJSONObject | ||
nonce = payload.get("nonce").asInstanceOf[String] | ||
_ <- ZIO.fail(new Exception("Nonce not found in JWT payload")) when (nonce == null || nonce.isEmpty) | ||
} yield nonce | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.