Refactor MutableKeyStore interface.

This commit is contained in:
Spencer Ofwiti 2021-02-16 16:18:41 +03:00
parent e6379aa2ef
commit 1718ae01ba
3 changed files with 72 additions and 79 deletions

View File

@ -1,4 +1,6 @@
# CicStaffClient # CICADA
Angular web client for managing users and transactions in the CIC network.
This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 10.2.0. This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 10.2.0.
@ -10,6 +12,10 @@ Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app w
Run `ng generate component component-name` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module`. Run `ng generate component component-name` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module`.
## Lazy-loading feature modules
Run `ng generate module module-name --route module-name --module app.module` to generate a new module on route `/module-name` in the app module.
## Build ## Build
Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory. Use the `--prod` flag for a production build. Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory. Use the `--prod` flag for a production build.

View File

@ -2,62 +2,44 @@ const openpgp = require('openpgp');
const keyring = new openpgp.Keyring(); const keyring = new openpgp.Keyring();
interface MutableKeyStore{ interface MutableKeyStore{
getFingerPrint: () => string; loadKeyring: () => Promise<void>;
importKeyPair: (publicKey: any, privateKey: any) => Promise<void>;
importPublicKey: (publicKey: any) => Promise<void>;
importPrivateKey: (privateKey: any) => Promise<void>;
getPublicKeys: () => Array<any>;
getTrustedKeys: () => Array<any>; getTrustedKeys: () => Array<any>;
getTrustedActiveKeys: () => Array<any>; getPrivateKeys: () => Array<any>;
addPublicKey: (publicKey) => void; getPrivateKey: () => any;
revokeKey: (publicKey) => void; isValidKey: (key: any) => boolean;
getFingerPrint: () => string;
getKeyId: (key: any) => string;
getKeysForId: (keyId: string) => Array<any>;
getPublicKeyForId: (keyId: string) => any;
getPrivateKeyForId: (keyId: string) => any;
getPublicKeyForSubkeyId: (subkeyId: string) => any;
getPublicKeysForAddress: (address: string) => Array<any>;
removeKeysForId: (keyId: string) => Array<any>;
removePublicKeysForId: (keyId: string) => any;
clearKeysInKeyring: () => void;
// revokeKey: (publicKey) => void;
} }
class MutablePgpKeyStore implements MutableKeyStore{ class MutablePgpKeyStore implements MutableKeyStore{
fingerprint: string; fingerprint: string;
pubk = {
active: [],
trusted: [],
encrypt: [],
};
getPublicKey(publicKey: any): any { // revokeKey(publicKey): void {
return openpgp.readArmoredKey(publicKey); // this.pubk.active.splice(this.pubk.active.indexOf(publicKey), 1);
} // this.pubk.trusted.splice(this.pubk.trusted.indexOf(publicKey), 1);
// }
async loadPublicKeys(armoredKeys: any): Promise<any> {
return await openpgp.readArmoredKey(armoredKeys);
}
addPublicKey(publicKey): void {
this.pubk.active.push(publicKey);
}
revokeKey(publicKey): void {
this.pubk.active.splice(this.pubk.active.indexOf(publicKey), 1);
this.pubk.trusted.splice(this.pubk.trusted.indexOf(publicKey), 1);
}
getTrustedKeys(): Array<any> {
return this.pubk.trusted;
}
getFingerPrint(): string {
return this.fingerprint;
}
getTrustedActiveKeys(): Array<any> {
return this.pubk.active;
}
async loadKeyring(): Promise<void> { async loadKeyring(): Promise<void> {
await keyring.load(); await keyring.load();
// clear any keys already in the keychain // clear any keys already in the keychain
keyring.clear(); // keyring.clear();
await keyring.store(); await keyring.store();
} }
async importKeyPair(publicKey, privateKey): Promise<void> { async importKeyPair(publicKey: any, privateKey: any): Promise<void> {
await keyring.load();
// clear any keys already in the keychain
keyring.clear();
await keyring.store();
await keyring.publicKeys.importKey(publicKey); await keyring.publicKeys.importKey(publicKey);
await keyring.privateKeys.importKey(privateKey); await keyring.privateKeys.importKey(privateKey);
} }
@ -66,67 +48,70 @@ class MutablePgpKeyStore implements MutableKeyStore{
await keyring.publicKeys.importKey(publicKey); await keyring.publicKeys.importKey(publicKey);
} }
getPublicKeys(): any { async importPrivateKey(privateKey: any): Promise<void> {
await keyring.privateKeys.importKey(privateKey);
this.fingerprint = keyring.privateKeys.keys[0].keyPacket.fingerprint;
}
getPublicKeys(): Array<any> {
return keyring.publicKeys.keys; return keyring.publicKeys.keys;
} }
getKeysForId(keyId): any { getTrustedKeys(): Array<any> {
return keyring.getKeysForId(keyId); return keyring.publicKeys.keys;
} }
getKeysForFingerprint(keyFingerprint): any { getPrivateKeys(): Array<any> {
return keyring.getKeysForId(keyFingerprint); return keyring.privateKeys.keys;
} }
getPublicKeysForSubkeyFingerprint(subkeyFingerprint): any { getPrivateKey(): any {
return keyring.publicKeys.getForId(subkeyFingerprint, true); return keyring.privateKeys.keys[0];
} }
getPublicKeysForId(keyId): any { isValidKey(key): boolean {
return keyring.publicKeys.getForId(keyId); return typeof key === openpgp.Key;
} }
getPrivateKeysForId(keyId): any { getFingerPrint(): string {
return keyring.privateKeys.getForId(keyId); return this.fingerprint;
} }
getKeyId(key): any { getKeyId(key: any): string {
return key.getKeyId().toHex(); return key.getKeyId().toHex();
} }
isValidKey(key): any { getKeysForId(keyId: string): Array<any> {
return typeof key === openpgp.key.Key; return keyring.getKeysForId(keyId);
} }
getPublicKeysForAddress(address): any { getPublicKeyForId(keyId): any {
return keyring.publicKeys.getForId(keyId);
}
getPrivateKeyForId(keyId): any {
return keyring.privateKeys.getForId(keyId);
}
getPublicKeyForSubkeyId(subkeyId): any {
return keyring.publicKeys.getForId(subkeyId, true);
}
getPublicKeysForAddress(address): Array<any> {
return keyring.publicKeys.getForAddress(address); return keyring.publicKeys.getForAddress(address);
} }
removeKeysForId(keyId): void { removeKeysForId(keyId): Array<any> {
keyring.removeKeysForId(keyId); return keyring.removeKeysForId(keyId);
} }
removePublicKeysForId(keyId): any { removePublicKeysForId(keyId): any {
return keyring.publicKeys.removeForId(keyId); return keyring.publicKeys.removeForId(keyId);
} }
async storeKeysInLocalstorage(): Promise<void> {
await keyring.load();
keyring.clear();
await keyring.store();
}
clearKeysInKeyring(): void { clearKeysInKeyring(): void {
keyring.clear(); keyring.clear();
} }
createCustomizedLocalstorage(customPrefix): any {
return new openpgp.Keyring.localstore(customPrefix);
}
async addKeyToLocalstorage(localstore, key): Promise<void> {
await localstore.storePublic([key]);
}
} }
export { export {

View File

@ -1,10 +1,9 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import {MutablePgpKeyStore, UnsafeKeyStore} from '@app/_helpers'; import {MutableKeyStore, MutablePgpKeyStore, UnsafeKeyStore} from '@app/_helpers';
import { hobaParseChallengeHeader } from '@src/assets/js/hoba.js'; import { hobaParseChallengeHeader } from '@src/assets/js/hoba.js';
import { signChallenge } from '@src/assets/js/hoba-pgp.js'; import { signChallenge } from '@src/assets/js/hoba-pgp.js';
import {environment} from '@src/environments/environment'; import {environment} from '@src/environments/environment';
import {HttpClient} from '@angular/common/http'; import {HttpClient} from '@angular/common/http';
import {first} from 'rxjs/operators';
const openpgp = require('openpgp'); const openpgp = require('openpgp');
const origin = 'http://localhost:4444'; const origin = 'http://localhost:4444';
@ -17,7 +16,7 @@ export class AuthService {
sessionToken: any; sessionToken: any;
sessionLoginCount = 0; sessionLoginCount = 0;
privateKey: any; privateKey: any;
mutableKeyStore: MutablePgpKeyStore = new MutablePgpKeyStore(); mutableKeyStore: MutableKeyStore = new MutablePgpKeyStore();
constructor( constructor(
private http: HttpClient private http: HttpClient
@ -126,6 +125,7 @@ export class AuthService {
try { try {
await pgpKeyStore.set(privateKeyArmored); await pgpKeyStore.set(privateKeyArmored);
localStorage.setItem(btoa('CICADA_PRIVATE_KEY'), privateKeyArmored); localStorage.setItem(btoa('CICADA_PRIVATE_KEY'), privateKeyArmored);
await this.mutableKeyStore.importPrivateKey(privateKeyArmored);
} catch (e) { } catch (e) {
console.error('failed setting key', e); console.error('failed setting key', e);
return false; return false;
@ -144,9 +144,11 @@ export class AuthService {
const armoredPublicKeys = res; const armoredPublicKeys = res;
await this.mutableKeyStore.loadKeyring(); await this.mutableKeyStore.loadKeyring();
await this.mutableKeyStore.importPublicKey(armoredPublicKeys); await this.mutableKeyStore.importPublicKey(armoredPublicKeys);
console.log(this.mutableKeyStore.getPublicKeys());
}, error => { }, error => {
console.error('There was an error!', error); console.error('There was an error!', error);
}); });
if (this.privateKey !== undefined) {
await this.mutableKeyStore.importPrivateKey(this.privateKey);
}
} }
} }