Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
// This file is part of Moodle - http://moodle.org/
2
//
3
// Moodle is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, either version 3 of the License, or
6
// (at your option) any later version.
7
//
8
// Moodle is distributed in the hope that it will be useful,
9
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
// GNU General Public License for more details.
12
//
13
// You should have received a copy of the GNU General Public License
14
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
15
 
16
/**
17
 * For collecting WebAuthn authenticator details on factor setup
18
 *
19
 * @module     factor_webauthn/register
20
 * @copyright  Catalyst IT
21
 * @author     Alex Morris <alex.morris@catalyst.net.nz>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
1441 ariadna 25
define([
26
    'factor_webauthn/utils',
27
    'core/prefetch',
28
    'core/str',
29
    'core/toast',
30
], function(
31
    utils,
32
    Prefetch,
33
    Str,
34
    Toast,
35
) {
36
 
1 efrain 37
    /**
38
     * Register the security key.
39
     *
40
     * @param {*} createArgs
41
     */
42
    async function registerSecurityKey(createArgs) {
43
        try {
44
            if (!navigator.credentials || !navigator.credentials.create) {
45
                throw new Error('Browser not supported.');
46
            }
47
 
48
            if (createArgs.success === false) {
49
                throw new Error(createArgs.msg || 'unknown error occurred');
50
            }
51
 
52
            utils.recursiveBase64StrToArrayBuffer(createArgs);
53
            const cred = await navigator.credentials.create(createArgs);
54
            const authenticatorResponse = {
55
                transports: cred.response.getTransports ? cred.response.getTransports() : null,
56
                clientDataJSON: cred.response.clientDataJSON ?
57
                    utils.arrayBufferToBase64(cred.response.clientDataJSON) : null,
58
                attestationObject: cred.response.attestationObject ?
59
                    utils.arrayBufferToBase64(cred.response.attestationObject) : null,
60
            };
1441 ariadna 61
 
62
            const registerSuccess = await Str.getString('registersuccess', 'factor_webauthn');
63
            await Toast.add(registerSuccess, {type: 'success'});
64
 
1 efrain 65
            document.getElementById('id_response_input').value = JSON.stringify(authenticatorResponse);
66
            // Enable the submit button so that we can proceed.
67
            document.getElementById('id_submitbutton').disabled = false;
68
        } catch (e) {
1441 ariadna 69
            const registerError = await Str.getString('registererror', 'factor_webauthn', e.message);
70
            await Toast.add(registerError, {type: 'warning'});
1 efrain 71
        }
72
    }
73
 
74
    return {
75
        init: function(createArgs) {
76
            // Disable the submit button until we have registered a security key.
77
            document.getElementById('id_submitbutton').disabled = true;
1441 ariadna 78
 
79
            Prefetch.prefetchStrings('factor_webauthn', [
80
                'registersuccess',
81
                'registererror',
82
            ]);
83
 
84
            // Register event listeners.
1 efrain 85
            createArgs = JSON.parse(createArgs);
86
            document.getElementById('factor_webauthn-register').addEventListener('click', function() {
87
                registerSecurityKey(createArgs);
88
            });
89
            document.getElementById('factor_webauthn-register').addEventListener('keypress', function() {
90
                registerSecurityKey(createArgs);
91
            });
92
        }
93
    };
94
});