Skip to content

Commit

Permalink
feat: exec custom code on verification (#66)
Browse files Browse the repository at this point in the history
* feat: exec custom code first Implementation

* fix: parse verification error
  • Loading branch information
phoebus-84 authored Jul 26, 2024
1 parent 458e7c1 commit 8303026
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 28 deletions.
19 changes: 0 additions & 19 deletions src/lib/fakeCredentials.js

This file was deleted.

6 changes: 4 additions & 2 deletions src/lib/preferences/sidRu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ export type RuAndSid = {
sid: string;
ru: string;
at: number;
code: string;
data:string;
};

export const RU_AND_SID_KEY = 'runAndSid';

export const saveRuAndSid = async (sid: string, ru: string) => {
export const saveRuAndSid = async (sid: string, ru: string, code:string, data:string) => {
const at = dayjs().unix();
const ruandSid = await getRuAndSids();

const r = { sid, ru, at };
const r = { sid, ru, at, code, data };
if (ruandSid) {
const newRuAndSids = [...ruandSid, r]
await setStructuredPreferences(RU_AND_SID_KEY, newRuAndSids);
Expand Down
3 changes: 2 additions & 1 deletion src/lib/slangroom/verificationFlows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,12 @@ export const getVerificationFlow = async (id: string): Promise<VerificationFlow>
pb_address: backendUri,
show_parameters: {
collection: 'verification_flows',
expand: 'relying_party',
expand: 'relying_party, template',
id
}
};
const res = await slangroom.execute(getPbRecord, { data });
console.log(res.result?.output);
//@ts-expect-error output needs to be typed
return res.result?.output;
} catch (e: unknown) {
Expand Down
7 changes: 6 additions & 1 deletion src/routes/[[lang]]/(protected)/[id]/verify/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@
const result = res.result;
qr = result.qrcode;
id = result.sid as string;
await saveRuAndSid(result.sid as string, result.ru as string);
await saveRuAndSid(
result.sid as string,
result.ru as string,
verificationFlow.expand.template.zencode_script,
verificationFlow.expand.template.zencode_data
);
generationDate = dayjs();
return qr;
};
Expand Down
28 changes: 23 additions & 5 deletions src/routes/[[lang]]/(protected)/[id]/verify/_lib/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ export type JwsToIdResponse = {
message?: string;
};

const parseVerificationError = (e: Error):string => {
try {
const message = JSON.parse(e.message);
const trace = message.filter((s)=>s.startsWith("J64 TRACE:"))[0];
const errorBase64 = trace.split("J64 TRACE: ")[1];
const errorArray = JSON.parse(atob(errorBase64));
const errors = errorArray.filter((s)=>s.startsWith("[!]"));
return errors.join("\n");
} catch {
return e.message
}

}

export const jwsToId = async (jws: string): Promise<JwsToIdResponse> => {
let id = '';
try {
Expand All @@ -32,18 +46,22 @@ export const jwsToId = async (jws: string): Promise<JwsToIdResponse> => {
const ruAndSid = await getRuAndSid(id);

if (!ruAndSid) throw new Error(`Could not find ru for id ${id}`);
const { ru } = ruAndSid;
const { ru, code, data:keys } = ruAndSid;
const dataVerify = {
...data,
claims_url: ru
};
const res = await slangroom.execute(verify, { data: dataVerify, keys: JSON.parse(verifyKeys) });
log(JSON.stringify(res));
const result = res.result.result as jwsToIdResult;
const inputToCustomCode = res.result.input_to_custom_code;
// Execute custom code
const customCodeResult = await slangroom.execute(code, { data: inputToCustomCode, keys: JSON.parse(keys) });
console.log(customCodeResult);
return { result, id };
} catch (e) {
log(JSON.stringify(e));
return { result: jwsToIdFailure, id, message: JSON.stringify(e) };
} catch (e:unknown) {
const message = parseVerificationError(e as Error);
log(message);
return { result: jwsToIdFailure, id, message };
}
};

Expand Down

0 comments on commit 8303026

Please sign in to comment.