Add method for loading public keys into keyring.

This commit is contained in:
Spencer Ofwiti 2021-02-16 10:10:52 +03:00
parent c0381a170e
commit 8558ec9e13
4 changed files with 165 additions and 4 deletions

View File

@ -0,0 +1,7 @@
import { MutablePgpKeyStore } from '@app/_helpers/pgp-key-store';
describe('PgpKeyStore', () => {
it('should create an instance', () => {
expect(new MutablePgpKeyStore()).toBeTruthy();
});
});

View File

@ -0,0 +1,135 @@
const openpgp = require('openpgp');
const keyring = new openpgp.Keyring();
interface MutableKeyStore{
getFingerPrint: () => string;
getTrustedKeys: () => Array<any>;
getTrustedActiveKeys: () => Array<any>;
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<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> {
await keyring.load();
// clear any keys already in the keychain
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();
await keyring.publicKeys.importKey(publicKey);
await keyring.privateKeys.importKey(privateKey);
}
async importPublicKey(publicKey: any): Promise<void> {
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<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 {
MutablePgpKeyStore,
MutableKeyStore
};

View File

@ -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<void> {
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);
});
}
}

View File

@ -16,5 +16,6 @@
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script async src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
<script async src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.min.js" integrity="sha384-w1Q4orYjBQndcko6MimVbzY0tgp4pWB4lZ7lr30WKz0vr/aWKhXdBNmNb5D92v7s" crossorigin="anonymous"></script>
<script async src="assets/js/openpgp.min.js"></script>
</body>
</html>