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 |
* WebAuthn utility functions, for handling array buffers.
|
|
|
18 |
*
|
|
|
19 |
* @module factor_webauthn/utils
|
|
|
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([], function() {
|
|
|
26 |
return {
|
|
|
27 |
recursiveBase64StrToArrayBuffer: function(obj) {
|
|
|
28 |
let prefix = '=?BINARY?B?';
|
|
|
29 |
let suffix = '?=';
|
|
|
30 |
if (typeof obj === 'object') {
|
|
|
31 |
for (let key in obj) {
|
|
|
32 |
let isString = true;
|
|
|
33 |
if (typeof obj[key] !== 'string') {
|
|
|
34 |
this.recursiveBase64StrToArrayBuffer(obj[key]);
|
|
|
35 |
isString = false;
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
let str = obj[key];
|
|
|
39 |
if (isString && str.substring(0, prefix.length) === prefix &&
|
|
|
40 |
str.substring(str.length - suffix.length) === suffix) {
|
|
|
41 |
str = str.substring(prefix.length, str.length - suffix.length);
|
|
|
42 |
|
|
|
43 |
const binaryString = window.atob(str);
|
|
|
44 |
let len = binaryString.length;
|
|
|
45 |
let bytes = new Uint8Array(len);
|
|
|
46 |
for (let i = 0; i < len; i++) {
|
|
|
47 |
bytes[i] = binaryString.charCodeAt(i);
|
|
|
48 |
}
|
|
|
49 |
obj[key] = bytes.buffer;
|
|
|
50 |
}
|
|
|
51 |
}
|
|
|
52 |
}
|
|
|
53 |
},
|
|
|
54 |
arrayBufferToBase64: function(buffer) {
|
|
|
55 |
let binary = '';
|
|
|
56 |
let bytes = new Uint8Array(buffer);
|
|
|
57 |
let len = bytes.byteLength;
|
|
|
58 |
for (let i = 0; i < len; i++) {
|
|
|
59 |
binary += String.fromCharCode(bytes[i]);
|
|
|
60 |
}
|
|
|
61 |
return window.btoa(binary);
|
|
|
62 |
},
|
|
|
63 |
};
|
|
|
64 |
});
|