Skip to content
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

Use latest jws & jwt functionality, add VC verify and decode functions to match #150 #176

Merged

Conversation

KendallWeihe
Copy link
Contributor

Background

This is a part of a messy multi-step multi-PR campaign to revamp JWS (#143), JWT (#144) and the uses in VC (#150); this PR addresses #150. I will subsequently open a PR removing all the non-v2 code and update bindings accordingly.

Changes

  • Use the latest in the jwt crate (changes in Refactor jwt #144 #173) -- this includes RegisteredClaims, and the new sign/verify/decode functions
  • Added VerifiableCredential::verify() and VerifiableCredential::decode() functions, as to match the paradigm with the latest in jws and jwt

Forward looking

  • I'm not entirely sold on the way I have implemented the verify and decode so we can iterate on the approach there
  • the same goes for VcJwtClaims
  • Created this ticket to consider how we can support both VC Data Model 1.1 and 2.0 Consider differentiating between VC Data Models 1.1 and 2.0 #175 -- both of the above bullet points may be dependent upon how we think about supporting both data models
  • in a subsequent PR I will remove all of the old code, and update bindings accordingly

@@ -85,45 +86,40 @@ impl VerifiableCredential {
vc: self.clone(),
};

let encoded_header = header.encode()?;
let encoded_claims = claims.encode()?;
let jwt = Jwt::sign(bearer_did, key_selector, None, &claims)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eventually I think we will want a concept of a vcJwt as it is not exactly the same as a jwt

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for reference :

https://github.com/TBD54566975/web5-js/blob/main/packages/credentials/src/verifiable-credential.ts#L117C1-L134C4

  public async sign(options: VerifiableCredentialSignOptions): Promise<string> {
    const vcJwt: string = await Jwt.sign({
      signerDid : options.did,
      payload   : {
        vc  : this.vcDataModel,
        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),
        }),
      }
    });

    return vcJwt;
  }

}

pub fn decode(jwt: &str) -> Result<Self, CredentialError> {
let jwt_decoded = Jwt::decode::<VcJwtClaims>(jwt)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah perfect

@KendallWeihe KendallWeihe changed the title Use latest jwt & jwt functionality, add VC verify and decode functions to match #150 Use latest jws & jwt functionality, add VC verify and decode functions to match #150 May 8, 2024
@KendallWeihe KendallWeihe merged commit 0132f8c into kendall/jwt-refactor-144 May 8, 2024
@KendallWeihe KendallWeihe deleted the kendall/vc-jwt-refactor-150-2 branch May 8, 2024 18:31
@KendallWeihe KendallWeihe mentioned this pull request May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants