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

feat: [SIW-337] Verifiy Request Object signature #23

Merged
merged 1 commit into from
Aug 4, 2023
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
11 changes: 4 additions & 7 deletions example/src/scenarios/cross-device-flow-with-rp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,18 @@ export default async () => {
// get signature for dpop
const DPoPSignature = await sign(unsignedDPoP, WIA.keytag);

// resolve RP's entity configuration
const entity = await RP.getEntityConfiguration();

// get request object
const requestObj = await SignJWT.appendSignature(
unsignedDPoP,
DPoPSignature
).then((t) => RP.getRequestObject(t));

// resolve RP's entity configuration
const entity = await RP.getEntityConfiguration();
).then((t) => RP.getRequestObject(t, entity));

// Attest Relying Party trust
// TODO [SIW-354]

// Validate Request object signature
// TODO [SIW-337]

// select claims to be disclose from pid
// these would be selected by users in the UI
const claims = [
Expand Down
27 changes: 24 additions & 3 deletions src/rp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
sha256ToBase64,
SignJWT,
EncryptJwe,
verify,
} from "@pagopa/io-react-native-jwt";
import {
QRCodePayload,
Expand Down Expand Up @@ -97,15 +98,18 @@ export class RelyingPartySolution {

/**
* Obtain the Request Object for RP authentication
* @see https://italia.github.io/eudi-wallet-it-docs/versione-corrente/en/relying-party-solution.html
*
* @function
* @async @function
* @param signedWalletInstanceDPoP JWT of the Wallet Instance Attestation DPoP
*
* @returns The Request Object JWT
* @throws {NoSuitableKeysFoundInEntityConfiguration} When the Request Object is signed with a key not listed in RP's entity configuration
*
*/
async getRequestObject(
signedWalletInstanceDPoP: string
signedWalletInstanceDPoP: string,
entity: RpEntityConfiguration
): Promise<RequestObject> {
const decodedJwtDPop = await decodeJwt(signedWalletInstanceDPoP);
const requestUri = decodedJwtDPop.payload.htu as string;
Expand All @@ -119,11 +123,28 @@ export class RelyingPartySolution {

if (response.status === 200) {
const responseText = await response.text();
const responseJwt = await decodeJwt(responseText);
const responseJwt = decodeJwt(responseText);

// verify token signature according to RP's entity configuration
// to ensure the request object is authentic
{
const pubKey = entity.payload.jwks.keys.find(
({ kid }) => kid === responseJwt.protectedHeader.kid
);
if (!pubKey) {
throw new NoSuitableKeysFoundInEntityConfiguration(
"Request Object signature verification"
);
}
await verify(responseText, pubKey);
}

// parse request object it has the expected shape by specification
const requestObj = RequestObject.parse({
header: responseJwt.protectedHeader,
payload: responseJwt.payload,
});

return requestObj;
}

Expand Down