Skip to content

Commit

Permalink
debug
Browse files Browse the repository at this point in the history
  • Loading branch information
shihjay2 committed Jan 24, 2025
1 parent 814e140 commit bb85ea4
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 68 deletions.
23 changes: 21 additions & 2 deletions lib/rp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ const verifyDidJWT = async(jwt: string, resolver: Resolvable, options: JWTVerify

const resolver = getResolver('ethr');

export const rp = RP.builder({ requestVersion: SupportedVersion.SIOPv2_ID1 })
export const rp = (type:string, id:string) => {
return RP.builder({ requestVersion: SupportedVersion.SIOPv2_ID1 })
.withClientId(identifier.did)
.withScope('openid')
.withResponseType('vp_token')
Expand All @@ -115,6 +116,23 @@ export const rp = RP.builder({ requestVersion: SupportedVersion.SIOPv2_ID1 })
.withRequestBy(PassBy.VALUE)
.withCreateJwtCallback(createJwtCallback())
.withSupportedVersions(SupportedVersion.SIOPv2_ID1)
.withPresentationDefinition({
definition: {
id: id,
input_descriptors: [
{
"id": "1",
"name": type + " Verifiable Credential",
"purpose": "We want a VC of this type to proof provider claim",
"schema": [
{
"uri": "https://www.w3.org/2018/credentials/v1"
}
]
}
]
}
})
.withClientMetadata({
client_id: identifier.did,
idTokenSigningAlgValuesSupported: [SigningAlgo.EDDSA, SigningAlgo.ES256],
Expand All @@ -132,4 +150,5 @@ export const rp = RP.builder({ requestVersion: SupportedVersion.SIOPv2_ID1 })
clientName: 'Trustee',
clientPurpose: "Grant Negotiation and Authorization Protocol (GNAP) Server"
})
.build();
.build();
}
2 changes: 1 addition & 1 deletion pages/api/vp/authorize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
console.log(req.body);
console.log(req.body.vp_token);
const { payload } = decodeJWT(req.body.vp_token);
const verifiedAuthResponse = await rp.verifyAuthorizationResponse(payload, {
const verifiedAuthResponse = await rp(doc.vc_type, doc.vc_id).verifyAuthorizationResponse(payload, {
correlationId: doc._id,
audience: url.protocol + "//" + url.hostname + "/api/vp/vp_response",
})
Expand Down
74 changes: 37 additions & 37 deletions pages/api/vp/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,47 +38,47 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
const patient_doc = await patients.get(doc.email);
console.log(doc);
const { payload } = decodeJWT(req.body.vp_token);
// try {
// const verifiedAuthResponse = await rp.verifyAuthorizationResponse(req.body.id_token, {
// correlationId: doc._id,
// state: req.body.state,
// audience: url.protocol + "//" + url.hostname + "/api/vp/vp_response",
// })
// console.log(verifiedAuthResponse)
// if (objectPath.get(verifiedAuthResponse, 'payload.state') === doc.vp_state) {
// console.log('state matches')
// }
// if (objectPath.get(verifiedAuthResponse, 'payload.nonce') === doc.vp_state) {
// console.log('state matches')
// }
// res.status(200).json({message: 'OK'});
// } catch (e) {
// console.log(e)
// res.status(400).json({error: 'invalid_request'});
// }
if (objectPath.has(payload, 'vp.verifiableCredential')) {
const vc = decodeJWT(objectPath.get(payload, 'vp.verifiableCredential.0'));
if (objectPath.has(doc, 'vc')) {
const vc_arr = objectPath.get(doc, 'vc');
vc_arr.push(vc);
objectPath.set(doc, 'vc', vc_arr);
} else {
objectPath.set(doc, 'vc.0', vc);
try {
const verifiedAuthResponse = await rp(doc.vc_type, doc.vc_id).verifyAuthorizationResponse(req.body.id_token, {
correlationId: doc._id,
state: req.body.state,
audience: url.protocol + "//" + url.hostname + "/api/vp/vp_response",
})
console.log(verifiedAuthResponse)
if (objectPath.get(verifiedAuthResponse, 'payload.state') === doc.vp_state) {
console.log('state matches')
}
objectPath.set(doc, 'vp_status', 'complete');
await gnap.insert(doc);
if (objectPath.has(patient_doc, 'vc')) {
const vc_arr1 = objectPath.get(patient_doc, 'vc');
vc_arr1.push(vc);
objectPath.set(patient_doc, 'vc', vc_arr1);
} else {
objectPath.set(patient_doc, 'vc.0', vc);
if (objectPath.get(verifiedAuthResponse, 'payload.nonce') === doc.vp_state) {
console.log('state matches')
}
await patients.insert(patient_doc);
res.status(200).json({message: 'OK'});
} else {
res.status(400).json({error: 'invalid_token'});
} catch (e) {
console.log(e)
res.status(400).json({error: 'invalid_request'});
}
// if (objectPath.has(payload, 'vp.verifiableCredential')) {
// const vc = decodeJWT(objectPath.get(payload, 'vp.verifiableCredential.0'));
// if (objectPath.has(doc, 'vc')) {
// const vc_arr = objectPath.get(doc, 'vc');
// vc_arr.push(vc);
// objectPath.set(doc, 'vc', vc_arr);
// } else {
// objectPath.set(doc, 'vc.0', vc);
// }
// objectPath.set(doc, 'vp_status', 'complete');
// await gnap.insert(doc);
// if (objectPath.has(patient_doc, 'vc')) {
// const vc_arr1 = objectPath.get(patient_doc, 'vc');
// vc_arr1.push(vc);
// objectPath.set(patient_doc, 'vc', vc_arr1);
// } else {
// objectPath.set(patient_doc, 'vc.0', vc);
// }
// await patients.insert(patient_doc);
// res.status(200).json({message: 'OK'});
// } else {
// res.status(400).json({error: 'invalid_token'});
// }
} else {
res.status(400).json({error: 'invalid_request'});
}
Expand Down
57 changes: 29 additions & 28 deletions pages/api/vp/share.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,39 +34,40 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
objectPath.set(doc, 'vp_status', 'pending');
const url_req = url.protocol + "//" + url.hostname + "/api/vp/vp_request/" + vp_id;
const link = "openid-vc://?request_uri=" + encodeURIComponent(url_req);
const authrequest = await rp.createAuthorizationRequestURI({
const authrequest = await rp(doc.vc_type, doc.vp_id).createAuthorizationRequestURI({
correlationId: req.body._id,
nonce: nonce,
state: state,
jwtIssuer: {method: 'did', alg: 'EdDSA', didUrl: identifier.did},
claims: {
"vp_token": {
"presentation_definition": {
"id": vp_id,
"input_descriptors": [
{
"id": "1",
"name": doc.vc_type + " Verifiable Credential",
"purpose": "We want a VC of this type to proof provider claim",
"schema": [
{
"uri": "https://www.w3.org/2018/credentials/v1"
// "uri": "VerifiableCredential"
}
]
// "format": {
// "jwt_vc_json": {
// "alg": [
// "EdDSA"
// ]
// }
// }
}
]
}
}
}
// claims: {
// "vp_token": {
// "presentation_definition": {
// "id": vp_id,
// "input_descriptors": [
// {
// "id": "1",
// "name": doc.vc_type + " Verifiable Credential",
// "purpose": "We want a VC of this type to proof provider claim",
// "schema": [
// {
// "uri": "https://www.w3.org/2018/credentials/v1"
// // "uri": "VerifiableCredential"
// }
// ]
// // "format": {
// // "jwt_vc_json": {
// // "alg": [
// // "EdDSA"
// // ]
// // }
// // }
// }
// ]
// }
// }
// }
});
console.log(authrequest)
objectPath.set(doc, 'vp_jwt', authrequest.requestObjectJwt)
try {
const response = await gnap.insert(doc);
Expand Down

0 comments on commit bb85ea4

Please sign in to comment.