Skip to content

Commit

Permalink
maho-passkey-tools.js
Browse files Browse the repository at this point in the history
  • Loading branch information
fballiano committed Jan 19, 2025
1 parent c177523 commit f19863c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 46 deletions.
47 changes: 1 addition & 46 deletions app/design/adminhtml/default/default/template/login.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<link rel="icon" type="image/svg+xml" href="<?= $this->getSkinUrl('images/favicon.svg') ?>" sizes="any">
<link rel="icon" type="image/png" href="<?= $this->getSkinUrl('images/favicon.png') ?>">
<script type="text/javascript" src="<?= $this->getJsUrl('validation.js') ?>"></script>
<script type="text/javascript" src="<?= $this->getJsUrl('maho-passkey-tools.js') ?>"></script>
</head>
<body id="page-login">
<div class="login-container">
Expand Down Expand Up @@ -68,52 +69,6 @@
<p class="legal">Powered by Maho</p>
</form>
<div class="bottom"></div>
<script>
/**
* convert RFC 1342-like base64 strings to array buffer
* @param {mixed} obj
* @returns {undefined}
*/
function recursiveBase64StrToArrayBuffer(obj) {
let prefix = '=?BINARY?B?';
let suffix = '?=';
if (typeof obj === 'object') {
for (let key in obj) {
if (typeof obj[key] === 'string') {
let str = obj[key];
if (str.substring(0, prefix.length) === prefix && str.substring(str.length - suffix.length) === suffix) {
str = str.substring(prefix.length, str.length - suffix.length);

let binary_string = window.atob(str);
let len = binary_string.length;
let bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
obj[key] = bytes.buffer;
}
} else {
recursiveBase64StrToArrayBuffer(obj[key]);
}
}
}
}

/**
* Convert a ArrayBuffer to Base64
* @param {ArrayBuffer} buffer
* @returns {String}
*/
function arrayBufferToBase64(buffer) {
let binary = '';
let bytes = new Uint8Array(buffer);
let len = bytes.byteLength;
for (let i = 0; i < len; i++) {
binary += String.fromCharCode( bytes[ i ] );
}
return window.btoa(binary);
}
</script>
<script type="module">
history.replaceState(null, null, window.location.href);
const formEl = document.getElementById('loginForm');
Expand Down
55 changes: 55 additions & 0 deletions public/js/maho-passkey-tools.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Maho
*
* @category Maho
* @package js
* @copyright Copyright (c) 2018 Thomas Bleeker (CBOR & ByteBuffer part)
* @copyright Copyright (c) 2022 Lukas Buchs
* @copyright Copyright (c) 2025 Maho (https://mahocommerce.com)
* @license https://opensource.org/license/mit MIT
*/

/**
* convert RFC 1342-like base64 strings to array buffer
* @param {mixed} obj
* @returns {undefined}
*/
function recursiveBase64StrToArrayBuffer(obj) {
let prefix = '=?BINARY?B?';
let suffix = '?=';
if (typeof obj === 'object') {
for (let key in obj) {
if (typeof obj[key] === 'string') {
let str = obj[key];
if (str.substring(0, prefix.length) === prefix && str.substring(str.length - suffix.length) === suffix) {
str = str.substring(prefix.length, str.length - suffix.length);

let binary_string = window.atob(str);
let len = binary_string.length;
let bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
obj[key] = bytes.buffer;
}
} else {
recursiveBase64StrToArrayBuffer(obj[key]);
}
}
}
}

/**
* Convert a ArrayBuffer to Base64
* @param {ArrayBuffer} buffer
* @returns {String}
*/
function arrayBufferToBase64(buffer) {
let binary = '';
let bytes = new Uint8Array(buffer);
let len = bytes.byteLength;
for (let i = 0; i < len; i++) {
binary += String.fromCharCode( bytes[ i ] );
}
return window.btoa(binary);
}

0 comments on commit f19863c

Please sign in to comment.