diff --git a/src/app/_helpers/pgp-key-store.spec.ts b/src/app/_helpers/pgp-key-store.spec.ts new file mode 100644 index 0000000..e7e84b2 --- /dev/null +++ b/src/app/_helpers/pgp-key-store.spec.ts @@ -0,0 +1,7 @@ +import { MutablePgpKeyStore } from '@app/_helpers/pgp-key-store'; + +describe('PgpKeyStore', () => { + it('should create an instance', () => { + expect(new MutablePgpKeyStore()).toBeTruthy(); + }); +}); diff --git a/src/app/_helpers/pgp-key-store.ts b/src/app/_helpers/pgp-key-store.ts new file mode 100644 index 0000000..4f74275 --- /dev/null +++ b/src/app/_helpers/pgp-key-store.ts @@ -0,0 +1,135 @@ +const openpgp = require('openpgp'); +const keyring = new openpgp.Keyring(); + +interface MutableKeyStore{ + getFingerPrint: () => string; + getTrustedKeys: () => Array; + getTrustedActiveKeys: () => Array; + addPublicKey: (publicKey) => 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 { + 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 { + return this.pubk.trusted; + } + + getFingerPrint(): string { + return this.fingerprint; + } + + getTrustedActiveKeys(): Array { + return this.pubk.active; + } + + async loadKeyring(): Promise { + await keyring.load(); + // clear any keys already in the keychain + keyring.clear(); + await keyring.store(); + } + + async importKeyPair(publicKey, privateKey): Promise { + await keyring.load(); + // clear any keys already in the keychain + keyring.clear(); + await keyring.store(); + await keyring.publicKeys.importKey(publicKey); + await keyring.privateKeys.importKey(privateKey); + } + + async importPublicKey(publicKey: any): Promise { + await keyring.publicKeys.importKey(publicKey); + } + + getPublicKeys(): any { + return keyring.publicKeys.keys; + } + + getKeysForId(keyId): any { + return keyring.getKeysForId(keyId); + } + + getKeysForFingerprint(keyFingerprint): any { + return keyring.getKeysForId(keyFingerprint); + } + + getPublicKeysForSubkeyFingerprint(subkeyFingerprint): any { + return keyring.publicKeys.getForId(subkeyFingerprint, true); + } + + getPublicKeysForId(keyId): any { + return keyring.publicKeys.getForId(keyId); + } + + getPrivateKeysForId(keyId): any { + return keyring.privateKeys.getForId(keyId); + } + + getKeyId(key): any { + return key.getKeyId().toHex(); + } + + isValidKey(key): any { + return typeof key === openpgp.key.Key; + } + + getPublicKeysForAddress(address): any { + return keyring.publicKeys.getForAddress(address); + } + + removeKeysForId(keyId): void { + keyring.removeKeysForId(keyId); + } + + removePublicKeysForId(keyId): any { + return keyring.publicKeys.removeForId(keyId); + } + + async storeKeysInLocalstorage(): Promise { + 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 { + await localstore.storePublic([key]); + } +} + +export { + MutablePgpKeyStore, + MutableKeyStore +}; diff --git a/src/app/_services/auth.service.ts b/src/app/_services/auth.service.ts index 1e86c0c..57d45a3 100644 --- a/src/app/_services/auth.service.ts +++ b/src/app/_services/auth.service.ts @@ -1,7 +1,11 @@ import { Injectable } from '@angular/core'; -import { UnsafeKeyStore } from '../_helpers'; -import { hobaParseChallengeHeader } from '../../assets/js/hoba.js'; -import { signChallenge } from '../../assets/js/hoba-pgp.js'; +import {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'; const pgpKeyStore = new UnsafeKeyStore(); @@ -13,8 +17,11 @@ export class AuthService { sessionToken: any; sessionLoginCount = 0; privateKey: any; + mutableKeyStore: MutablePgpKeyStore = new MutablePgpKeyStore(); - constructor() { + constructor( + private http: HttpClient + ) { if (sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN'))) { this.sessionToken = sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN')); } @@ -131,4 +138,15 @@ export class AuthService { sessionStorage.removeItem(btoa('CICADA_SESSION_TOKEN')); window.location.reload(true); } + + async getPublicKeys(): Promise { + this.http.get(`${environment.publicKeysUrl}/keys.asc`).subscribe(async res => { + 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); + }); + } } diff --git a/src/index.html b/src/index.html index 5f86e7f..dcb3791 100644 --- a/src/index.html +++ b/src/index.html @@ -16,5 +16,6 @@ +