summaryrefslogtreecommitdiff
path: root/src/reset_password.html
blob: 9c049074c7a33f0c18fcc58edd3ccfe8720554fb (plain) (blame)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <title>The Mana World</title>
        <style>
            * {
              margin: 0;
              padding: 0;
              box-sizing: border-box;
              -webkit-box-sizing: border-box;
              -moz-box-sizing: border-box;
              -webkit-font-smoothing: antialiased;
              -moz-font-smoothing: antialiased;
              -o-font-smoothing: antialiased;
              font-smoothing: antialiased;
              text-rendering: optimizeLegibility;
            }

            body {
              font-family: "Roboto", Helvetica, Arial, sans-serif;
              font-weight: 100;
              font-size: 12px;
              line-height: 30px;
              color: #777;
              background: rgb(199, 83, 83);
            }

            .container {
              max-width: 400px;
              width: 100%;
              margin: 0 auto;
              position: relative;
            }

            .container > form input[type="text"],
            .container > form input[type="password"],
            .container > form input[type="email"],
            .container > form button {
              font: 400 12px/16px "Roboto", Helvetica, Arial, sans-serif;
            }

            .container > form {
              background: #F9F9F9;
              padding: 25px;
              margin: 150px 0;
              box-shadow: 0 0 20px 0 rgba(0, 0, 0, 0.2), 0 5px 5px 0 rgba(0, 0, 0, 0.24);
            }

            .container > form h3 {
              display: block;
              font-size: 30px;
              font-weight: 300;
              margin-bottom: 10px;
            }

            .container > form h4 {
              margin: 5px 0 15px;
              display: block;
              font-size: 13px;
              font-weight: 400;
            }

            .container .status {
              margin: 15px 0 15px;
              display: block;
              font-size: 13px;
              font-weight: 400;
              display: none;
            }

            fieldset {
              border: medium none !important;
              margin: 0 0 10px;
              min-width: 100%;
              padding: 0;
              width: 100%;
            }

            .container > form input[type="text"],
            .container > form input[type="password"],
            .container > form input[type="email"] {
              width: 100%;
              border: 1px solid #ccc;
              background: #FFF;
              margin: 0 0 5px;
              padding: 10px;
            }

            .container > form input[type="text"]:hover,
            .container > form input[type="password"]:hover,
            .container > form input[type="email"]:hover {
              -webkit-transition: border-color 0.3s ease-in-out;
              -moz-transition: border-color 0.3s ease-in-out;
              transition: border-color 0.3s ease-in-out;
              border: 1px solid #aaa;
            }

            .container > form button {
              cursor: pointer;
              width: 100%;
              border: none;
              background: rgba(0, 0, 0, 0.6);
              color: #FFF;
              margin: 0 0 5px;
              padding: 10px;
              font-size: 15px;
            }

            .container > form button:hover {
              background: rgba(0, 0, 0, 0.9);
              -webkit-transition: background 0.3s ease-in-out;
              -moz-transition: background 0.3s ease-in-out;
              transition: background-color 0.3s ease-in-out;
            }

            .container > form button:active {
              box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.5);
            }

            .container > form input:focus {
              outline: 0;
              border: 1px solid #aaa;
            }

            .container > form.error .status {
                display: block!important;
            }

            input.invalid {
                border-color: #900!important;
                background-color: #FDD!important;
            }

            /*input:focus:required:valid {
                background-color: greenyellow!important;
            }*/

            .warning {
                color: #fff;
                margin: 1em;
                display: inline-block;
                background: orangered;
                padding: 1em;
                border: 3px yellow dashed;
            }

            ::-webkit-input-placeholder {
              color: #888;
            }

            :-moz-placeholder {
              color: #888;
            }

            ::-moz-placeholder {
              color: #888;
            }

            :-ms-input-placeholder {
              color: #888;
            }
        </style>
        <script type="text/javascript">
            /* this block is intentionally left in <head>, for legacy browsers */
            if (!("fetch" in window && "Request" in window && "setCustomValidity" in document.createElement("input"))) {
                document.body.innerHTML = "<h1 style=\"color:#fff;margin:1em\">Please update your browser to continue.</h1>";
                throw new Error("outdated browser");
            }
        </script>
    </head>
    <body>
        <noscript>
            <div class="warning">
                <h1>Javascript is required for this page.</h1>
                <!-- link to license -->
                <!-- link to source code -->
            </div>
        </noscript>

        <div class="container">
            <form action="#" method="POST">
                <h3>The Mana World</h3>
                <h4>Password reset</h4>
                <span class="status">...</span>
                <fieldset>
                    <input name="username" placeholder="Username" type="text" tabindex="1" minlength="4" maxlength="23" pattern="^[a-zA-Z0-9]{4,23}$" title="4-23 characters, alphanumeric" required autofocus>
                </fieldset>
                <fieldset>
                    <input name="password" placeholder="New Password" type="password" tabindex="2" minlength="6" maxlength="23" pattern="^[a-zA-Z0-9]{6,23}$" title="6-23 characters, alphanumeric" required>
                </fieldset>
                <fieldset>
                    <div class="g-recaptcha"
                        data-sitekey="6LdaVUcUAAAAAJ-7cORTu4cZCPSNjDqjz3y4nLVR"
                        data-callback="onCaptchaCompletion"
                        data-size="invisible">
                    </div>
                    <div>
                        <button type="submit">Submit</button>
                    </div>
                </fieldset>
            </form>
        </div>
        <script>
            const uuid = document.location.hash.slice(1);
            var recaptcha_loaded = false;

            const nodes = {
                uname: document.querySelector("input[name=username]"),
                pwd: document.querySelector("input[name=password]"),
                form: document.querySelector(".container > form"),
                status: document.querySelector(".status"),
                button: document.querySelector(".container > form button"),
            };

            if (uuid.length < 6) {
                nodes.form.innerText = "The verification code is missing from the URL.\nCopy-paste the full URL provided in the email and try again.";
            }

            const validateInput = node => {
                node.setCustomValidity("");
                if (node.checkValidity() === false && node.value != "") {
                    node.classList.add("invalid");
                    return false;
                } else {
                    node.classList.remove("invalid");
                    return true;
                }
            };

            nodes.form.addEventListener("input", event => {
                validateInput(event.target);
            });

            nodes.pwd.addEventListener("change", e => {
                if (e.isTrusted && nodes.form.querySelector("input") && Reflect.has(window, "Rusha") && nodes.pwd.checkValidity()) {
                    const full_hash = Rusha.createHash().update(nodes.pwd.value).digest("hex");
                    const hash_prefix = full_hash.substring(0, 5);
                    const hash_suffix = full_hash.substring(5);

                    const req = new Request(`https://api.pwnedpasswords.com/range/${hash_prefix}`, {
                        method: "GET",
                        mode: "cors",
                        cache: "force-cache",
                        referrer: "no-referrer",
                    });

                    fetch(req)
                    .then(response => response.text())
                    .then(response => {
                        const found = response.split("\n").some(h => {
                            const [hs, times] = h.split(":");

                            if (hash_suffix.toUpperCase() === hs.toUpperCase())
                                return true;

                            return false;
                        });

                        if (found === true) {
                            nodes.form.classList.add("error");
                            nodes.status.innerText = "WARNING: This password has previously appeared in a data breach. Please use a more secure alternative.\n>> checked by haveibeenpwned.com\n\n";
                            nodes.status.style.display = "block"; // <= MS Edge bug
                            nodes.pwd.focus();
                            nodes.pwd.classList.add("invalid");
                        } else {
                            nodes.form.classList.remove("error");
                            nodes.status.style.display = "none";
                        }
                    });
                }
            });

            nodes.form.addEventListener("submit", e => {
                e.preventDefault();
                e.stopPropagation();

                if (nodes.pwd.value.length < 12 && !window.confirm("Your password is quite short. For better security you should consider using a password at least 12 characters long.\n\nDo you really want to use a weak password?")) {
                    nodes.pwd.focus();
                    return;
                }

                if (!recaptcha_loaded || !Reflect.has(window, "grecaptcha")) {
                    nodes.form.classList.add("error");
                    nodes.status.innerText = "reCAPTCHA couldn't be loaded. Make sure to whitelist this page in noscript, ghostery or any other ad/tracker blocker.";
                    nodes.status.style.display = "block"; // <= MS Edge bug
                    return;
                }

                grecaptcha.execute();
            });

            function onCaptchaCompletion(token) {
                const req = new Request("/api/tmwa/account", {
                    method: "PUT",
                    mode: "same-origin",
                    cache: "no-cache",
                    redirect: "follow",
                    referrer: "no-referrer",
                    headers: {
                        "Accept": "application/json",
                        "Content-Type": "application/json",
                        "X-CAPTCHA-TOKEN": token
                    },
                    body: JSON.stringify({
                        username: nodes.uname.value,
                        password: nodes.pwd.value,
                        code: uuid,
                    }),
                });

                fetch(req)
                .then(response => {
                    switch(response.status) {
                        case 200:
                        case 201:
                            return response.json();
                        case 400:
                            throw new Error("Malformed request!");
                        case 403:
                            throw new Error("Failed the captcha challenge!");
                        case 404:
                            nodes.uname.focus();
                            throw new Error("No accounts found for this email address!");
                        case 408:
                            throw new Error("Request expired. Please try again later.");
                        case 429:
                            throw new Error("Too many requests. Please try again later.");
                        case 500:
                            throw new Error("Internal server error. Please try again later.");
                        case 502:
                            throw new Error("Couldn't reach the server. Please try again later.");
                        default:
                            throw new Error(`Received status code ${response.status}.`);
                    }
                })
                .then(response => {
                    nodes.form.innerText = "Your password has successfully been reset.\nHave fun playing The Mana World!";
                })
                .catch(error => {
                    nodes.form.classList.add("error");
                    nodes.status.innerText = error;
                    nodes.status.style.display = "block"; // <= MS Edge bug
                    grecaptcha.reset();
                });
            }

            function ReInit() {
                recaptcha_loaded = true;
            }
        </script>
        <script src="https://www.google.com/recaptcha/api.js?onload=ReInit" async defer></script>
        <script src="/rusha.min.js"></script>
    </body>
</html>