-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added MultiSignaturePolicy to validate JWT credentials with mul…
…tiple embedded signatures based on JWS JSON Serialization
- Loading branch information
1 parent
7aa0054
commit f3deccf
Showing
2 changed files
with
38 additions
and
0 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
37 changes: 37 additions & 0 deletions
37
src/main/kotlin/id/walt/auditor/policies/MultiSignaturePolicy.kt
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,37 @@ | ||
package id.walt.auditor.policies | ||
|
||
import id.walt.auditor.SimpleVerificationPolicy | ||
import id.walt.auditor.VerificationPolicyResult | ||
import id.walt.credentials.w3c.VerifiableCredential | ||
|
||
class JwtHelper(val credential: String) { | ||
val header get() = credential.substringBefore(".") | ||
val payload get() = credential.substringAfter(".").substringBefore(".") | ||
val signature get() = credential.substringAfterLast(".") | ||
val jwsSignaturePart get() = mapOf( | ||
"protected" to header, | ||
"signature" to signature | ||
) | ||
|
||
companion object { | ||
fun fromJWS(payload: String, sig: Map<String, String>): JwtHelper { | ||
val h = sig["protected"] ?: throw Exception("No header found") | ||
val s = sig["signature"] ?: throw Exception("No sig found") | ||
return JwtHelper("$h.$payload.$s") | ||
} | ||
} | ||
} | ||
|
||
class MultiSignaturePolicy: SimpleVerificationPolicy() { | ||
override val description: String | ||
get() = "JWS Multi Signature Verification Policy" | ||
|
||
override fun doVerify(vc: VerifiableCredential): VerificationPolicyResult { | ||
val payload = (vc.credentialSubject?.properties?.get("payload") as? String) ?: return VerificationPolicyResult.failure() | ||
val signatures = (vc.credentialSubject?.properties?.get("signatures") as? List<Map<String, String>>) ?: return VerificationPolicyResult.failure() | ||
val credentials = signatures.map { JwtHelper.fromJWS(payload, it).credential } | ||
return if(credentials.all { SignaturePolicy().verify(VerifiableCredential.fromString(it)).isSuccess }) { | ||
VerificationPolicyResult.success() | ||
} else VerificationPolicyResult.failure() | ||
} | ||
} |