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

OpenID4VP: E2EE implementation - Encrypted Response #362

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions src/lib/services/OpenID4VPRelyingParty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Verify } from "../utils/Verify";
import { HasherAlgorithm, HasherAndAlgorithm, SdJwt } from "@sd-jwt/core";
import { VerifiableCredentialFormat } from "../schemas/vc";
import { generateRandomIdentifier } from "../utils/generateRandomIdentifier";
import { base64url, importX509, jwtVerify } from "jose";
import { base64url, EncryptJWT, importJWK, importX509, jwtVerify } from "jose";
import { OpenID4VPRelyingPartyState } from "../types/OpenID4VPRelyingPartyState";
import { OpenID4VPRelyingPartyStateRepository } from "./OpenID4VPRelyingPartyStateRepository";
import { IHttpProxy } from "../interfaces/IHttpProxy";
Expand Down Expand Up @@ -34,7 +34,7 @@ export class OpenID4VPRelyingParty implements IOpenID4VPRelyingParty {
let state = authorizationRequest.searchParams.get('state') as string;
let presentation_definition = authorizationRequest.searchParams.get('presentation_definition') ? JSON.parse(authorizationRequest.searchParams.get('presentation_definition')) : null;
let presentation_definition_uri = authorizationRequest.searchParams.get('presentation_definition_uri');

let client_metadata = authorizationRequest.searchParams.get('client_metadata') ? JSON.parse(authorizationRequest.searchParams.get('client_metadata')) : null;

if (presentation_definition_uri) {
const presentationDefinitionFetch = await this.httpProxy.get(presentation_definition_uri, {});
Expand All @@ -61,6 +61,7 @@ export class OpenID4VPRelyingParty implements IOpenID4VPRelyingParty {
client_id = p.client_id;
presentation_definition = p.presentation_definition;
response_uri = p.response_uri ?? p.redirect_uri;
client_metadata = p.client_metadata;

state = p.state;
nonce = p.nonce;
Expand Down Expand Up @@ -119,7 +120,8 @@ export class OpenID4VPRelyingParty implements IOpenID4VPRelyingParty {
nonce,
response_uri,
client_id,
state
state,
client_metadata
));

const mapping = new Map<string, { credentials: string[], requestedFields: string[] }>();
Expand Down Expand Up @@ -285,11 +287,29 @@ export class OpenID4VPRelyingParty implements IOpenID4VPRelyingParty {
};

const formData = new URLSearchParams();
formData.append('vp_token', generatedVPs[0]);
formData.append('presentation_submission', JSON.stringify(presentationSubmission));
if (S.state) {
formData.append('state', S.state);

if (S.client_metadata.authorization_encrypted_response_alg && S.client_metadata.jwks.keys.length > 0) {
const rp_eph_pub_jwk = S.client_metadata.jwks.keys[0];
const rp_eph_pub = await importJWK(rp_eph_pub_jwk, S.client_metadata.authorization_encrypted_response_alg);
const jwe = await new EncryptJWT({
vp_token: generatedVPs[0],
presentation_submission: presentationSubmission,
state: S.state ?? undefined
})
.setProtectedHeader({ alg: S.client_metadata.authorization_encrypted_response_alg, enc: S.client_metadata.authorization_encrypted_response_enc, kid: rp_eph_pub_jwk.kid })
.encrypt(rp_eph_pub);

formData.append('response', jwe);
console.log("JWE = ", jwe)
}
else {
formData.append('vp_token', generatedVPs[0]);
formData.append('presentation_submission', JSON.stringify(presentationSubmission));
if (S.state) {
formData.append('state', S.state);
}
}


const credentialIdentifiers = originalVCs.map((vc) => vc.credentialIdentifier);

Expand Down
14 changes: 12 additions & 2 deletions src/lib/types/OpenID4VPRelyingPartyState.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { JWK } from "jose";
import { PresentationDefinitionType } from "./presentationDefinition.type";


type ClientMetadata = {
jwks?: { keys: JWK[] },
authorization_encrypted_response_alg?: string;
authorization_encrypted_response_enc?: string;
vp_formats: any;
}
/**
* serializable
*/
Expand All @@ -11,6 +19,7 @@ export class OpenID4VPRelyingPartyState {
public response_uri: string,
public client_id: string,
public state: string,
public client_metadata: ClientMetadata
) { }

public serialize(): string {
Expand All @@ -20,11 +29,12 @@ export class OpenID4VPRelyingPartyState {
response_uri: this.response_uri,
client_id: this.client_id,
state: this.state,
client_metadata: this.client_metadata,
});
}

public static deserialize(storedValue: string): OpenID4VPRelyingPartyState {
const { presentation_definition, nonce, response_uri, client_id, state } = JSON.parse(storedValue) as OpenID4VPRelyingPartyState;
return new OpenID4VPRelyingPartyState(presentation_definition, nonce, response_uri, client_id, state);
const { presentation_definition, nonce, response_uri, client_id, state, client_metadata } = JSON.parse(storedValue) as OpenID4VPRelyingPartyState;
return new OpenID4VPRelyingPartyState(presentation_definition, nonce, response_uri, client_id, state, client_metadata);
}
}
Loading