Skip to content

Commit 622dd46

Browse files
committed
Add preliminary shl query to check passcode requirement prior to prompt
1 parent b059d0a commit 622dd46

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

src/lib/utils/shlClient.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ export function flag(config: { shl: string }) {
6060
return parsedShl?.flag;
6161
}
6262

63+
export function url(config: { shl: string }) {
64+
const shlBody = config.shl.split(/^(?:.+:\/.+#)?shlink:\//)[1];
65+
const parsedShl: SHLDecoded = decodeBase64urlToJson(shlBody);
66+
return parsedShl?.url;
67+
}
68+
6369
function needPasscode(config: { shl: string }) {
6470
const shlBody = config.shl.split(/^(?:.+:\/.+#)?shlink:\//)[1];
6571
const parsedShl: SHLDecoded = decodeBase64urlToJson(shlBody);

src/routes/ips/+page.svelte

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,32 @@
9595
async function retrieve(){
9696
const recipient = "WA Health Summary Viewer";
9797
98+
let retrieveResult;
9899
let passcode;
99-
const needPasscode = shlClient.flag({ shl: shl ?? "" })?.includes('P');
100-
if (needPasscode) {
101-
passcode = prompt("WA Health Summary Viewer\n----------------------------------------\nEnter a passcode to access this SMART Health Link\nIf no passcode was set, just click \"OK\"");
100+
try {
101+
retrieveResult = await fetch(shlClient.url({ shl: shl ?? "" }), {
102+
method: 'POST',
103+
headers: {
104+
'content-type': 'application/json',
105+
},
106+
body: JSON.stringify({
107+
passcode: "",
108+
recipient: recipient,
109+
}),
110+
});
111+
let message = await retrieveResult.text();
112+
message = JSON.parse(message)?.message;
113+
if (!retrieveResult.ok && retrieveResult.status === 400 && message === "Passcode required") {
114+
// Failed the password requirement
115+
const needPasscode = shlClient.flag({ shl: shl ?? "" })?.includes('P');
116+
if (needPasscode) {
117+
passcode = prompt("WA Health Summary Viewer\n----------------------------------------\nEnter a passcode to access this SMART Health Link\nIf no passcode was set, just click \"OK\"");
118+
}
119+
}
120+
} catch (e) {
121+
console.log(e);
102122
}
103-
let retrieveResult;
123+
104124
try {
105125
retrieveResult = await shlClient.retrieve({
106126
shl: shl ?? "",
@@ -123,7 +143,7 @@
123143
} else if (retrieveResult.status === 401) {
124144
// Failed the password requirement
125145
while (retrieveResult.status === 401) {
126-
passcode = prompt(`WA Health Summary Viewer\n----------------------------------------\nEnter a passcode to access this SMART Health Link\nIf no passcode was set, just click \"OK\"${retrieveResult.error.remainingAttempts !== undefined ? "\nAttempts remaining: "+retrieveResult.error.remainingAttempts : ""}`);
146+
passcode = prompt(`WA Health Summary Viewer\n----------------------------------------\nEnter a passcode to access this SMART Health Link`);
127147
try {
128148
retrieveResult = await shlClient.retrieve({
129149
shl: shl ?? "",
@@ -138,7 +158,7 @@
138158
}
139159
if (retrieveResult.error) {
140160
const managerLink = `<a href="${new URL(import.meta.url).origin}/view/${shlClient.id({ shl: shl ?? "" })}">Manage or reactivate it here</a>`;
141-
errorMsg = `<p>The requested SMART Health Link has been deactivated due to too many failed password attempts.</p><p>Are you the owner of this link? ${managerLink}</p>`;
161+
errorMsg = `<p>You have been locked from accessing this SMART Health Link due to too many failed password attempts.</p><p>Are you the owner of this link? ${managerLink}</p>`;
142162
}
143163
} else {
144164
errorMsg = retrieveResult.error;

0 commit comments

Comments
 (0)