1
0
mirror of https://github.com/bitwarden/directory-connector synced 2026-01-01 08:03:31 +00:00
Files
directory-connector/src/services/keytarSecureStorage.service.ts
Addison Beck c259962279 [AC-1743] pt. 1: Unpackage-ify jslib (#374)
* Unpackage-ify jslib

* Adjust .tsconfig path for root and apply to jslib

* Rebuild package-lock.json

* Disable husky in CI

* Revert an incorrect find/replace

* Add jslib/shared/.eslintrc rules to root eslintrc

* Revert package.json change to ignore spec files when linting

* Ensure custom matcher gets imported in jslib tests

* Fix small workflow bugs from merging

* Try and get CI builds moving again

* Always sign and notorize builds in CI

* Revert erroneous verion bump
2023-12-20 11:33:33 -05:00

32 lines
919 B
TypeScript

import { deletePassword, getPassword, setPassword } from "keytar";
import { StorageService } from "@/jslib/common/src/abstractions/storage.service";
export class KeytarSecureStorageService implements StorageService {
constructor(private serviceName: string) {}
get<T>(key: string): Promise<T> {
return getPassword(this.serviceName, key).then((val) => {
return JSON.parse(val) as T;
});
}
async has(key: string): Promise<boolean> {
return (await this.get(key)) != null;
}
save(key: string, obj: any): Promise<any> {
// keytar throws if you try to save a falsy value: https://github.com/atom/node-keytar/issues/86
// handle this by removing the key instead
if (!obj) {
return this.remove(key);
}
return setPassword(this.serviceName, key, JSON.stringify(obj));
}
remove(key: string): Promise<any> {
return deletePassword(this.serviceName, key);
}
}