diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-02-07 21:16:03 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-02-07 21:16:03 +0300 |
commit | 6a7fcb14aaff70d8438167b652b365406b37613c (patch) | |
tree | fd1b65c5e60e32c02a102158ef0bd9bad412e5d1 /app/assets/javascripts/authentication | |
parent | 468bcfb9c6d04b0244c349eda62cd7ed1496cca3 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/assets/javascripts/authentication')
-rw-r--r-- | app/assets/javascripts/authentication/webauthn/util.js | 49 |
1 files changed, 27 insertions, 22 deletions
diff --git a/app/assets/javascripts/authentication/webauthn/util.js b/app/assets/javascripts/authentication/webauthn/util.js index 5f06c000afe..eeda2bfaeaf 100644 --- a/app/assets/javascripts/authentication/webauthn/util.js +++ b/app/assets/javascripts/authentication/webauthn/util.js @@ -14,31 +14,36 @@ export function isHTTPS() { export const FLOW_AUTHENTICATE = 'authenticate'; export const FLOW_REGISTER = 'register'; -// adapted from https://stackoverflow.com/a/21797381/8204697 -function base64ToBuffer(base64) { - const binaryString = window.atob(base64); - const len = binaryString.length; - const bytes = new Uint8Array(len); - for (let i = 0; i < len; i += 1) { - bytes[i] = binaryString.charCodeAt(i); - } - return bytes.buffer; -} - -// adapted from https://stackoverflow.com/a/9458996/8204697 -function bufferToBase64(buffer) { - if (typeof buffer === 'string') { - return buffer; +/** + * Converts a base64 string to an ArrayBuffer + * + * @param {String} str - A base64 encoded string + * @returns {ArrayBuffer} + */ +export const base64ToBuffer = (str) => { + const rawStr = atob(str); + const buffer = new ArrayBuffer(rawStr.length); + const arr = new Uint8Array(buffer); + for (let i = 0; i < rawStr.length; i += 1) { + arr[i] = rawStr.charCodeAt(i); } + return arr.buffer; +}; - let binary = ''; - const bytes = new Uint8Array(buffer); - const len = bytes.byteLength; - for (let i = 0; i < len; i += 1) { - binary += String.fromCharCode(bytes[i]); +/** + * Converts ArrayBuffer to a base64-encoded string + * + * @param {ArrayBuffer, String} str - + * @returns {String} - ArrayBuffer to a base64-encoded string. + * When input is a string, returns the input as-is. + */ +export const bufferToBase64 = (input) => { + if (typeof input === 'string') { + return input; } - return window.btoa(binary); -} + const arr = new Uint8Array(input); + return btoa(String.fromCharCode(...arr)); +}; /** * Returns a copy of the given object with the id property converted to buffer |