Skip to content

Commit

Permalink
Store the local authentication user ID to the local storage in the We…
Browse files Browse the repository at this point in the history
…b browser.
  • Loading branch information
yoichiro committed Dec 14, 2023
1 parent f158496 commit c619887
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/store/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { IMacro, IMacroBuffer, MacroKey } from '../services/macro/Macro';
import { IFirmwareWriter } from '../services/firmware/FirmwareWriter';
import { FirmwareWriterWebApiImpl } from '../services/firmware/FirmwareWriterWebApiImpl';
import { IBootloaderType } from '../services/firmware/Types';
import { getLocalAuthenticationUid } from '../utils/AuthUtils';

export type ISetupPhase =
| 'init'
Expand Down Expand Up @@ -311,6 +312,9 @@ export type RootState = {
image: string;
};
};
localAuthenticationInfo: {
uid: string;
};
};
configure: {
header: {
Expand Down Expand Up @@ -491,6 +495,8 @@ const gitHub = new GitHub();

const firmwareWriter = new FirmwareWriterWebApiImpl();

const localAuthenticationUid = getLocalAuthenticationUid();

export const INIT_STATE: RootState = {
entities: {
device: {
Expand Down Expand Up @@ -566,6 +572,9 @@ export const INIT_STATE: RootState = {
image: '',
},
},
localAuthenticationInfo: {
uid: localAuthenticationUid,
},
},
configure: {
header: {
Expand Down
17 changes: 17 additions & 0 deletions src/utils/AuthUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const getLocalAuthenticationUid = (): string => {
const uid = window.localStorage.getItem('localAuthenticationUid');
if (uid === null) {
const newUid = createRandomString(32);
window.localStorage.setItem('localAuthenticationUid', newUid);
return newUid;
}
return uid;
};

const createRandomString = (length: number): string => {
const chars =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const randomValues = new Uint32Array(length);
window.crypto.getRandomValues(randomValues);
return Array.from(randomValues, (x) => chars[x % chars.length]).join('');
};

0 comments on commit c619887

Please sign in to comment.