diff --git a/lib/rp.ts b/lib/rp.ts index 05a9ac22..de488c42 100644 --- a/lib/rp.ts +++ b/lib/rp.ts @@ -107,7 +107,7 @@ const resolver = getResolver('ethr'); export const rp = RP.builder({ requestVersion: SupportedVersion.SIOPv2_ID1 }) .withClientId(identifier.did) .withScope('openid') - .withResponseType('id_token') + .withResponseType('vp_token') .withResponseMode(ResponseMode.POST) .withAuthorizationEndpoint(url_auth) .withRedirectUri(url_res) diff --git a/pages/api/vp/response.ts b/pages/api/vp/response.ts index 2389d597..b5380a5d 100644 --- a/pages/api/vp/response.ts +++ b/pages/api/vp/response.ts @@ -37,48 +37,48 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { const patient_doc = await patients.get(doc.email); console.log(doc); console.log(req.body); - const { payload } = decodeJWT(req.body.id_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 = jose.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); + 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') // } - // 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'}); }