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.
@ -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`.
## 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
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();
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>;
getTrustedActiveKeys: () => Array<any>;
addPublicKey: (publicKey) => void;
revokeKey: (publicKey) => void;
getPrivateKeys: () => Array<any>;
getPrivateKey: () => any;
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{
fingerprint: string;
pubk = {
active: [],
trusted: [],
encrypt: [],
};
getPublicKey(publicKey: any): any {
return openpgp.readArmoredKey(publicKey);
}
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;
}
// revokeKey(publicKey): void {
// this.pubk.active.splice(this.pubk.active.indexOf(publicKey), 1);
// this.pubk.trusted.splice(this.pubk.trusted.indexOf(publicKey), 1);
// }
async loadKeyring(): Promise<void> {
await keyring.load();
// clear any keys already in the keychain
keyring.clear();
// keyring.clear();
await keyring.store();
}
async importKeyPair(publicKey, privateKey): Promise<void> {
await keyring.load();
// clear any keys already in the keychain
keyring.clear();
await keyring.store();
async importKeyPair(publicKey: any, privateKey: any): Promise<void> {
await keyring.publicKeys.importKey(publicKey);
await keyring.privateKeys.importKey(privateKey);
}
@ -66,67 +48,70 @@ class MutablePgpKeyStore implements MutableKeyStore{
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;
}
getKeysForId(keyId): any {
return keyring.getKeysForId(keyId);
getTrustedKeys(): Array<any> {
return keyring.publicKeys.keys;
}
getKeysForFingerprint(keyFingerprint): any {
return keyring.getKeysForId(keyFingerprint);
getPrivateKeys(): Array<any> {
return keyring.privateKeys.keys;
}
getPublicKeysForSubkeyFingerprint(subkeyFingerprint): any {
return keyring.publicKeys.getForId(subkeyFingerprint, true);
getPrivateKey(): any {
return keyring.privateKeys.keys[0];
}
getPublicKeysForId(keyId): any {
return keyring.publicKeys.getForId(keyId);
isValidKey(key): boolean {
return typeof key === openpgp.Key;
}
getPrivateKeysForId(keyId): any {
return keyring.privateKeys.getForId(keyId);
getFingerPrint(): string {
return this.fingerprint;
}
getKeyId(key): any {
getKeyId(key: any): string {
return key.getKeyId().toHex();
}
isValidKey(key): any {
return typeof key === openpgp.key.Key;
getKeysForId(keyId: string): Array<any> {
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);
}
removeKeysForId(keyId): void {
keyring.removeKeysForId(keyId);
removeKeysForId(keyId): Array<any> {
return keyring.removeKeysForId(keyId);
}
removePublicKeysForId(keyId): any {
return keyring.publicKeys.removeForId(keyId);
}
async storeKeysInLocalstorage(): Promise<void> {
await keyring.load();
keyring.clear();
await keyring.store();
}
clearKeysInKeyring(): void {
keyring.clear();
}
createCustomizedLocalstorage(customPrefix): any {
return new openpgp.Keyring.localstore(customPrefix);
}
async addKeyToLocalstorage(localstore, key): Promise<void> {
await localstore.storePublic([key]);
}
}
export {

View File

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