From 6347495b5b69150dae1b6dc46ce38486b7e32bd4 Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Wed, 28 Aug 2024 14:29:36 +0100 Subject: [PATCH] auth: use unicode b64 encoding on browser brought over unicodeB64Encode/Decode from my other filaments project. Fixes #364. --- ts/modules/common.ts | 13 +++++++++++++ ts/modules/login.ts | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/ts/modules/common.ts b/ts/modules/common.ts index 208be19..d87498a 100644 --- a/ts/modules/common.ts +++ b/ts/modules/common.ts @@ -291,3 +291,16 @@ export function bindManualDropdowns() { }; } } + +export function unicodeB64Decode(s: string): string { + const decoded = atob(s); + const byteArray = Uint8Array.from(decoded, (m) => m.codePointAt(0)); + const toUnicode = new TextDecoder().decode(byteArray); + return toUnicode; +} + +export function unicodeB64Encode(s: string): string { + const encoded = new TextEncoder().encode(s); + const bin = String.fromCodePoint(...encoded); + return btoa(bin); +} diff --git a/ts/modules/login.ts b/ts/modules/login.ts index ea74103..71cd590 100644 --- a/ts/modules/login.ts +++ b/ts/modules/login.ts @@ -1,5 +1,5 @@ import { Modal } from "../modules/modal.js"; -import { toggleLoader, _post } from "../modules/common.js"; +import { toggleLoader, _post, unicodeB64Encode } from "../modules/common.js"; export class Login { loggedIn: boolean = false; @@ -68,7 +68,7 @@ export class Login { const refresh = (username == "" && password == ""); req.open("GET", this._url + (refresh ? "token/refresh" : "token/login"), true); if (!refresh) { - req.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password)); + req.setRequestHeader("Authorization", "Basic " + unicodeB64Encode(username + ":" + password)); } req.onreadystatechange = ((req: XMLHttpRequest, _: Event): any => { if (req.readyState == 4) {