Proyectos de Subversion Moodle

Rev

| 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
 
25
define(['factor_webauthn/utils', 'core/log'], function(utils, Log) {
26
    /**
27
     * Register the security key.
28
     *
29
     * @param {*} createArgs
30
     */
31
    async function registerSecurityKey(createArgs) {
32
        try {
33
            if (!navigator.credentials || !navigator.credentials.create) {
34
                throw new Error('Browser not supported.');
35
            }
36
 
37
            if (createArgs.success === false) {
38
                throw new Error(createArgs.msg || 'unknown error occurred');
39
            }
40
 
41
            utils.recursiveBase64StrToArrayBuffer(createArgs);
42
            const cred = await navigator.credentials.create(createArgs);
43
            const authenticatorResponse = {
44
                transports: cred.response.getTransports ? cred.response.getTransports() : null,
45
                clientDataJSON: cred.response.clientDataJSON ?
46
                    utils.arrayBufferToBase64(cred.response.clientDataJSON) : null,
47
                attestationObject: cred.response.attestationObject ?
48
                    utils.arrayBufferToBase64(cred.response.attestationObject) : null,
49
            };
50
            document.getElementById('id_response_input').value = JSON.stringify(authenticatorResponse);
51
            // Enable the submit button so that we can proceed.
52
            document.getElementById('id_submitbutton').disabled = false;
53
        } catch (e) {
54
            Log.debug('The request timed out or you have canceled the request. Please try again later.');
55
        }
56
    }
57
 
58
    return {
59
        init: function(createArgs) {
60
            // Disable the submit button until we have registered a security key.
61
            document.getElementById('id_submitbutton').disabled = true;
62
            createArgs = JSON.parse(createArgs);
63
            // Register event listeners.
64
            document.getElementById('factor_webauthn-register').addEventListener('click', function() {
65
                registerSecurityKey(createArgs);
66
            });
67
            document.getElementById('factor_webauthn-register').addEventListener('keypress', function() {
68
                registerSecurityKey(createArgs);
69
            });
70
        }
71
    };
72
});