| 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 login
 | 
        
           |  |  | 18 |  *
 | 
        
           |  |  | 19 |  * @module     factor_webauthn/login
 | 
        
           |  |  | 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'], function(utils) {
 | 
        
           |  |  | 26 |     return {
 | 
        
           |  |  | 27 |         init: function(getArgs) {
 | 
        
           |  |  | 28 |             const idSubmitButton = document.getElementById('id_submitbutton');
 | 
        
           |  |  | 29 |             if (idSubmitButton) {
 | 
        
           |  |  | 30 |                 idSubmitButton.addEventListener('click', async function(e) {
 | 
        
           |  |  | 31 |                     e.preventDefault();
 | 
        
           |  |  | 32 |                     if (!navigator.credentials || !navigator.credentials.create) {
 | 
        
           |  |  | 33 |                         throw new Error('Browser not supported.');
 | 
        
           |  |  | 34 |                     }
 | 
        
           |  |  | 35 |   | 
        
           |  |  | 36 |                     getArgs = JSON.parse(getArgs);
 | 
        
           |  |  | 37 |   | 
        
           |  |  | 38 |                     if (getArgs.success === false) {
 | 
        
           |  |  | 39 |                         throw new Error(getArgs.msg || 'unknown error occured');
 | 
        
           |  |  | 40 |                     }
 | 
        
           |  |  | 41 |   | 
        
           |  |  | 42 |                     utils.recursiveBase64StrToArrayBuffer(getArgs);
 | 
        
           |  |  | 43 |   | 
        
           |  |  | 44 |                     const cred = await navigator.credentials.get(getArgs);
 | 
        
           |  |  | 45 |   | 
        
           |  |  | 46 |                     const authenticatorAttestationResponse = {
 | 
        
           |  |  | 47 |                         id: cred.rawId ? utils.arrayBufferToBase64(cred.rawId) : null,
 | 
        
           |  |  | 48 |                         clientDataJSON:
 | 
        
           |  |  | 49 |                             cred.response.clientDataJSON ? utils.arrayBufferToBase64(cred.response.clientDataJSON) : null,
 | 
        
           |  |  | 50 |                         authenticatorData:
 | 
        
           |  |  | 51 |                             cred.response.authenticatorData ? utils.arrayBufferToBase64(cred.response.authenticatorData) : null,
 | 
        
           |  |  | 52 |                         signature: cred.response.signature ? utils.arrayBufferToBase64(cred.response.signature) : null,
 | 
        
           |  |  | 53 |                         userHandle: cred.response.userHandle ? utils.arrayBufferToBase64(cred.response.userHandle) : null
 | 
        
           |  |  | 54 |                     };
 | 
        
           |  |  | 55 |   | 
        
           |  |  | 56 |                     const responseInput = document.getElementById('id_response_input');
 | 
        
           |  |  | 57 |                     responseInput.value = JSON.stringify(authenticatorAttestationResponse);
 | 
        
           |  |  | 58 |                     responseInput.form.submit();
 | 
        
           |  |  | 59 |                 });
 | 
        
           |  |  | 60 |             }
 | 
        
           |  |  | 61 |         }
 | 
        
           |  |  | 62 |     };
 | 
        
           |  |  | 63 | });
 |