blob: 81bf94b4689873389b39c456ffbd3118ffc7ec0f [file] [log] [blame]
Adrià Vilanova Martínez5af86512023-12-02 20:44:16 +01001/* (license-header)
2 * hores
3 * Copyright (c) 2023 Adrià Vilanova Martínez
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public
16 * License along with this program.
17 * If not, see http://www.gnu.org/licenses/.
18 */
Copybara botbe50d492023-11-30 00:16:42 +010019function verify() {
20 if (!document.getElementById("code").checkValidity()) {
21 document.querySelector(".mdl-js-snackbar").MaterialSnackbar.showSnackbar({
22 message: "El código de verificación debe tener 6 cifras."
23 });
24
25 return;
26 }
27
28 var body = {
29 code: document.getElementById("code").value
30 };
31
32 var content = document.getElementById("content");
33 content.innerHTML = '<div class="mdl-spinner mdl-js-spinner is-active"></div>';
34 content.style.textAlign = "center";
35 componentHandler.upgradeElements(content);
36
37 fetch("ajax/verifysecuritycode.php", {
38 method: "POST",
39 headers: {
40 "Content-Type": "application/json"
41 },
42 body: JSON.stringify(body)
43 }).then(response => {
44 if (response.status !== 200) {
45 throw new Error("HTTP status is not 200.");
46 }
47
48 return response.json();
49 }).then(response => {
50 switch (response.status) {
51 case "ok":
52 document.location = "index.php";
53 break;
54
55 case "wrongCode":
56 document.location = "index.php?msg=secondfactorwrongcode";
57 break;
58
59 default:
60 console.error("An unknown status code was returned.");
61 }
62 }).catch(err => console.error("An unexpected error occurred.", err));
63}
64
65function verifyKeypress(e) {
66 if (event.keyCode == 13) {
67 verify();
68 }
69}
70
71function startWebauthn() {
72 fetch("ajax/startwebauthnauthentication.php", {
73 method: "POST"
74 }).then(response => {
75 if (response.status !== 200) {
76 response.text(); // @TODO: Remove this. It is only used so the response is available in Chrome Dev Tools
77 throw new Error("HTTP status is not 200.");
78 }
79
80 return response.json();
81 }).then(response => {
82 recursiveBase64StrToArrayBuffer(response);
83 return response;
84 }).then(getCredentialArgs => {
85 return navigator.credentials.get(getCredentialArgs);
86 }).then(cred => {
87 return {
88 id: cred.rawId ? arrayBufferToBase64(cred.rawId) : null,
89 clientDataJSON: cred.response.clientDataJSON ? arrayBufferToBase64(cred.response.clientDataJSON) : null,
90 authenticatorData: cred.response.authenticatorData ? arrayBufferToBase64(cred.response.authenticatorData) : null,
91 signature : cred.response.signature ? arrayBufferToBase64(cred.response.signature) : null
92 };
93 }).then(JSON.stringify).then(AuthenticatorAttestationResponse => {
94 return window.fetch("ajax/completewebauthnauthentication.php", {
95 method: "POST",
96 body: AuthenticatorAttestationResponse,
97 });
98 }).then(response => {
99 if (response.status !== 200) {
100 response.text(); // @TODO: remove this. It is only used so the response is available in Chrome Dev Tools
101 throw new Error("HTTP status is not 200 (2).");
102 }
103
104 return response.json();
105 }).then(json => {
106 if (json.status == "ok") {
107 document.location = "index.php";
108 }
109 }).catch(err => console.error("An unexpected error occurred.", err));
110}
111
112window.addEventListener("load", function() {
113 if (document.getElementById("totp")) {
114 document.getElementById("verify").addEventListener("click", verify);
115 document.getElementById("code").addEventListener("keypress", verifyKeypress);
116 document.getElementById("code").focus();
117 document.querySelector("a[href=\"#totp\"]").addEventListener("click", _ => {
118 document.getElementById("code").focus();
119 });
120 }
121
122 if (document.getElementById("startwebauthn")) {
123 document.getElementById("startwebauthn").addEventListener("click", startWebauthn);
124 }
125});