]> git.proxmox.com Git - proxmox-widget-toolkit.git/commitdiff
add Utils used for u2f and webauthn
authorWolfgang Bumiller <w.bumiller@proxmox.com>
Tue, 9 Nov 2021 11:27:16 +0000 (12:27 +0100)
committerDominik Csapak <d.csapak@proxmox.com>
Wed, 10 Nov 2021 07:37:03 +0000 (08:37 +0100)
base64url parts copied from pbs
render_u2f_error copied from pve

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
src/Utils.js

index c52bef2abc9ed5e6229f90c8aec158173fd5766a..9703bd9f2537fc165cdb82ef8ebb950f64bd9184 100644 (file)
@@ -1172,6 +1172,51 @@ utilities: {
 
        return Proxmox.Utils.unknownText;
     },
+
+    render_u2f_error: function(error) {
+       var ErrorNames = {
+           '1': gettext('Other Error'),
+           '2': gettext('Bad Request'),
+           '3': gettext('Configuration Unsupported'),
+           '4': gettext('Device Ineligible'),
+           '5': gettext('Timeout'),
+       };
+       return "U2F Error: " + ErrorNames[error] || Proxmox.Utils.unknownText;
+    },
+
+    // Convert an ArrayBuffer to a base64url encoded string.
+    // A `null` value will be preserved for convenience.
+    bytes_to_base64url: function(bytes) {
+       if (bytes === null) {
+           return null;
+       }
+
+       return btoa(Array
+           .from(new Uint8Array(bytes))
+           .map(val => String.fromCharCode(val))
+           .join(''),
+       )
+       .replace(/\+/g, '-')
+       .replace(/\//g, '_')
+       .replace(/[=]/g, '');
+    },
+
+    // Convert an a base64url string to an ArrayBuffer.
+    // A `null` value will be preserved for convenience.
+    base64url_to_bytes: function(b64u) {
+       if (b64u === null) {
+           return null;
+       }
+
+       return new Uint8Array(
+           atob(b64u
+               .replace(/-/g, '+')
+               .replace(/_/g, '/'),
+           )
+           .split('')
+           .map(val => val.charCodeAt(0)),
+       );
+    },
 },
 
     singleton: true,