web_lib

Common web application libraries
git clone https://radroots.dev/git/web_lib.git
Log | Files | Refs | LICENSE

commit b14e41defeee68a90d49d4a7d6b0bbfb6fcb1733
parent f033c41d40d7c35e214b2acd82bd6dddd93b25d4
Author: triesap <137732411+triesap@users.noreply.github.com>
Date:   Fri, 23 Aug 2024 10:18:30 +0000

client: add capacitor keystore api

Diffstat:
Mclient/package.json | 3++-
Mclient/src/capacitor/index.ts | 8+++++++-
Aclient/src/capacitor/keystore.ts | 42++++++++++++++++++++++++++++++++++++++++++
Mclient/src/types.ts | 9+++++++++
4 files changed, 60 insertions(+), 2 deletions(-)

diff --git a/client/package.json b/client/package.json @@ -8,7 +8,8 @@ "dev": "tsc -w" }, "dependencies": { - "@capacitor/core": "^6.1.2" + "@capacitor/core": "^6.1.2", + "@radroots/capacitor-secure-storage": "workspace:*" }, "devDependencies": { "typescript": "^5.3.3" diff --git a/client/src/capacitor/index.ts b/client/src/capacitor/index.ts @@ -1,12 +1,18 @@ import { Capacitor } from "@capacitor/core"; -import type { IClient, IClientPlatform } from "../types"; +import type { IClient, IClientKeystore, IClientPlatform } from "../types"; import { parse_platform } from "../utils"; +import { CapacitorClientKeystore } from "./keystore"; export class ClientCapacitor implements IClient { private _platform: IClientPlatform = parse_platform(Capacitor.getPlatform()); + private _keystore: IClientKeystore = new CapacitorClientKeystore(); public get platform() { return this._platform; } + + public get keystore() { + return this._keystore; + } }; \ No newline at end of file diff --git a/client/src/capacitor/keystore.ts b/client/src/capacitor/keystore.ts @@ -0,0 +1,42 @@ +import { SecureStorage } from "@radroots/capacitor-secure-storage"; +import { IClientKeystore } from "../types"; + +export class CapacitorClientKeystore implements IClientKeystore { + public async init() { + await SecureStorage.setKeyPrefix("radroots-"); + await SecureStorage.setSynchronize(false); + } + + public async set(key: string, val: string): Promise<boolean> { + try { + await SecureStorage.set(key, val, true, false); + return true; + } catch (e) { + return false; + } + } + + public async get(key: string): Promise<string | undefined> { + try { + const res = await SecureStorage.get(key, true, false); + if (typeof res === `string`) return res; + } catch (e) { } + } + + public async keys(): Promise<string[] | undefined> { + try { + const res = await SecureStorage.keys(); + if (res && res.length) return res; + } catch (e) { } + } + + + public async remove(key: string): Promise<boolean> { + try { + const res = await SecureStorage.remove(key, false); + return res; + } catch (e) { + return false; + } + } +} diff --git a/client/src/types.ts b/client/src/types.ts @@ -1,5 +1,14 @@ export type IClient = { platform: IClientPlatform; + keystore: IClientKeystore; }; export type IClientPlatform = `androiď` | `ios` | `web`; + +export type IClientKeystore = { + init: () => Promise<void>; + set(key: string, val: string): Promise<boolean>; + get(key: string): Promise<string | undefined>; + keys(): Promise<string[] | undefined>; + remove(key: string): Promise<boolean>; +};