Repository navigation
Expand file tree
/
Copy pathsim.js
More file actions
76 lines (76 loc) · 2.35 KB
/
Copy pathsim.js
File metadata and controls
76 lines (76 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
export async function startSim(
client,
token,
{ getActiveSim, setActiveSim, server, phone, container, endFlow, jsonPost, showError },
) {
if (getActiveSim()) return;
const login = client.user_flow === "login_sim",
controller = new AbortController();
const attempt = { controller };
setActiveSim(attempt);
const button = container.querySelector(`#${client.user_flow}`);
button.disabled = true;
button.setAttribute("aria-busy", "true");
const pending = document.createElement("div");
pending.className = "showcase-pending";
pending.innerHTML =
'<p role="status">Waiting for SIM verification…</p><button type="button">Cancel</button>';
container.querySelector(".showcase-section").append(pending);
function release() {
if (getActiveSim() !== attempt) return;
setActiveSim(null);
endFlow(token);
pending.remove();
}
pending.querySelector("button").onclick = () => {
controller.abort();
release();
};
try {
const { response, data } = await jsonPost(
"/ts43/auth",
{
client_id: client.client_id,
server_id: server,
login_hint: login ? "anonymous" : phone,
carrier_hint: 51010,
scope: login ? "openid ip:phone" : "openid ip:phone_verify",
...(login ? { operation: "GetPhoneNumber" } : {}),
},
controller.signal,
);
if (!response.ok)
throw new Error(data.error || "Unable to start SIM verification");
const credential = await navigator.credentials.get({
digital: { requests: [data.digital_request] },
signal: controller.signal,
});
if (getActiveSim() !== attempt) return;
if (!credential) throw new Error("SIM verification was cancelled.");
const vp = (credential.token || credential.data)?.vp_token?.[
"ipification.com"
]?.[0];
if (!vp)
throw new Error(
"This browser did not return a supported SIM credential. Choose another method.",
);
await jsonPost(
"/ts43/token",
{
vp_token: vp,
auth_req_id: data.auth_req_id,
client_id: client.client_id,
server_id: server,
nonce: data.nonce,
},
controller.signal,
);
location.assign("/user/info");
} catch (error) {
if (getActiveSim() === attempt) {
showError(error.message || "SIM verification is unavailable.");
}
} finally {
release();
}
}