generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
updates to verify #421
Merged
Merged
updates to verify #421
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e11ea2d
updates to verify
nitro-neal 8f8a4c2
Merge branch 'main' into update-verify
nitro-neal d283d25
merge
nitro-neal 0feab6c
updates
nitro-neal 0bcbc08
remove comment
nitro-neal 71d3a77
merge
nitro-neal 72caaac
merge and timestamp update
nitro-neal 4fed152
lint update
nitro-neal 64048e2
updates
nitro-neal f399083
Merge branch 'main' into update-verify
nitro-neal 37c71e0
Merge branch 'main' into update-verify
nitro-neal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
|
||
import { Jwt } from './jwt.js'; | ||
import { SsiValidator } from './validators.js'; | ||
import { getCurrentXmlSchema112Timestamp } from './utils.js'; | ||
import { getCurrentXmlSchema112Timestamp, getXmlSchema112Timestamp } from './utils.js'; | ||
|
||
export const DEFAULT_VC_CONTEXT = 'https://www.w3.org/2018/credentials/v1'; | ||
export const DEFAULT_VC_TYPE = 'VerifiableCredential'; | ||
|
@@ -91,8 +91,14 @@ | |
signerDid : options.did, | ||
payload : { | ||
vc : this.vcDataModel, | ||
iss : this.issuer, | ||
nbf : Math.floor(new Date(this.vcDataModel.issuanceDate).getTime() / 1000), | ||
jti : this.vcDataModel.id, | ||
iss : options.did.uri, | ||
sub : this.subject, | ||
iat : Math.floor(Date.now() / 1000), | ||
...(this.vcDataModel.expirationDate && { | ||
exp: Math.floor(new Date(this.vcDataModel.expirationDate).getTime() / 1000), | ||
}), | ||
} | ||
}); | ||
|
||
|
@@ -174,7 +180,19 @@ | |
* - Identifies the correct Verification Method from the DID Document based on the `kid` parameter. | ||
* - Verifies the JWT's signature using the public key associated with the Verification Method. | ||
* | ||
* If any of these steps fail, the function will throw a [Error] with a message indicating the nature of the failure. | ||
* If any of these steps fail, the function will throw a [Error] with a message indicating the nature of the failure: | ||
* - exp MUST represent the expirationDate property, encoded as a UNIX timestamp (NumericDate). | ||
* - iss MUST represent the issuer property of a verifiable credential or the holder property of a verifiable presentation. | ||
* - nbf MUST represent issuanceDate, encoded as a UNIX timestamp (NumericDate). | ||
* - jti MUST represent the id property of the verifiable credential or verifiable presentation. | ||
* - sub MUST represent the id property contained in the credentialSubject. | ||
* | ||
* Once the verifications are successful, when recreating the VC data model object, this function will: | ||
* - If exp is present, the UNIX timestamp MUST be converted to an [XMLSCHEMA11-2] date-time, and MUST be used to set the value of the expirationDate property of credentialSubject of the new JSON object. | ||
* - If iss is present, the value MUST be used to set the issuer property of the new credential JSON object or the holder property of the new presentation JSON object. | ||
* - If nbf is present, the UNIX timestamp MUST be converted to an [XMLSCHEMA11-2] date-time, and MUST be used to set the value of the issuanceDate property of the new JSON object. | ||
* - If sub is present, the value MUST be used to set the value of the id property of credentialSubject of the new credential JSON object. | ||
* - If jti is present, the value MUST be used to set the value of the id property of the new JSON object. | ||
* | ||
* @example | ||
* ```ts | ||
|
@@ -194,17 +212,71 @@ | |
vcJwt: string | ||
}) { | ||
const { payload } = await Jwt.verify({ jwt: vcJwt }); | ||
const vc = payload['vc'] as VcDataModel; | ||
const { exp, iss, nbf, jti, sub, vc } = payload; | ||
|
||
if (!vc) { | ||
throw new Error('vc property missing.'); | ||
} | ||
|
||
validatePayload(vc); | ||
const vcTyped: VcDataModel = payload['vc'] as VcDataModel; | ||
|
||
// exp MUST represent the expirationDate property, encoded as a UNIX timestamp (NumericDate). | ||
if(exp && vcTyped.expirationDate && exp !== Math.floor(new Date(vcTyped.expirationDate).getTime() / 1000)) { | ||
throw new Error('Verification failed: exp claim does not match expirationDate'); | ||
} | ||
|
||
// If exp is present, the UNIX timestamp MUST be converted to an [XMLSCHEMA11-2] date-time, and MUST be used to set the value of the expirationDate property of credentialSubject of the new JSON object. | ||
if(exp) { | ||
vcTyped.expirationDate = getXmlSchema112Timestamp(exp); | ||
} | ||
|
||
if (!iss) throw new Error('Verification failed: iss claim is required'); | ||
|
||
// iss MUST represent the issuer property of a verifiable credential or the holder property of a verifiable presentation. | ||
if (iss !== vcTyped.issuer) { | ||
throw new Error('Verification failed: iss claim does not match expected issuer'); | ||
} | ||
|
||
// nbf cannot represent time in the future | ||
if(nbf && nbf > Math.floor(Date.now() / 1000)) { | ||
throw new Error('Verification failed: nbf claim is in the future'); | ||
} | ||
|
||
// nbf MUST represent issuanceDate, encoded as a UNIX timestamp (NumericDate). | ||
if(nbf && vcTyped.issuanceDate && nbf !== Math.floor(new Date(vcTyped.issuanceDate).getTime() / 1000)) { | ||
throw new Error('Verification failed: nbf claim does not match issuanceDate'); | ||
} | ||
|
||
// If nbf is present, the UNIX timestamp MUST be converted to an [XMLSCHEMA11-2] date-time, and MUST be used to set the value of the issuanceDate property of the new JSON object. | ||
if(nbf) { | ||
vcTyped.issuanceDate = getXmlSchema112Timestamp(nbf); | ||
} | ||
|
||
// sub MUST represent the id property contained in the credentialSubject. | ||
if(sub && !Array.isArray(vcTyped.credentialSubject) && sub !== vcTyped.credentialSubject.id) { | ||
throw new Error('Verification failed: sub claim does not match credentialSubject.id'); | ||
} | ||
|
||
// If sub is present, the value MUST be used to set the value of the id property of credentialSubject of the new credential JSON object. | ||
if(sub && !Array.isArray(vcTyped.credentialSubject)) { | ||
vcTyped.credentialSubject.id = sub; | ||
} | ||
|
||
// jti MUST represent the id property of the verifiable credential or verifiable presentation. | ||
if(jti && jti !== vcTyped.id) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if there's no jti? shoudl we err? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
throw new Error('Verification failed: jti claim does not match id'); | ||
} | ||
|
||
if(jti) { | ||
vcTyped.id = jti; | ||
} | ||
|
||
validatePayload(vcTyped); | ||
|
||
return { | ||
issuer : payload.iss!, | ||
subject : payload.sub!, | ||
vc : payload['vc'] as VcDataModel | ||
vc : vcTyped | ||
}; | ||
} | ||
|
||
|
@@ -227,6 +299,8 @@ | |
throw Error('Jwt payload missing vc property'); | ||
} | ||
|
||
validatePayload(vcDataModel); | ||
|
||
return new VerifiableCredential(vcDataModel); | ||
} | ||
} | ||
|
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not optional!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lol ok, just saw this: - https://www.w3.org/TR/vc-data-model/#jwt-decoding