From 4fba6a8383b6027e0881320321da49aae5fb130a Mon Sep 17 00:00:00 2001 From: Spencer Ofwiti Date: Wed, 2 Jun 2021 10:48:02 +0300 Subject: [PATCH] Refactor trusted users list. --- src/app/_pgp/pgp-key-store.ts | 6 ++--- src/app/_services/auth.service.ts | 25 +++++++++++++++++--- src/app/app.component.ts | 1 + src/app/pages/settings/settings.component.ts | 12 +++++----- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/app/_pgp/pgp-key-store.ts b/src/app/_pgp/pgp-key-store.ts index 51eef35..492664d 100644 --- a/src/app/_pgp/pgp-key-store.ts +++ b/src/app/_pgp/pgp-key-store.ts @@ -6,7 +6,7 @@ const keyring = new openpgp.Keyring(); interface MutableKeyStore extends KeyStore { loadKeyring(): void; importKeyPair(publicKey: any, privateKey: any): Promise; - importPublicKey(publicKey: any): void; + importPublicKey(publicKey: any): Promise; importPrivateKey(privateKey: any): Promise; getPublicKeys(): Array; getTrustedKeys(): Array; @@ -42,8 +42,8 @@ class MutablePgpKeyStore implements MutableKeyStore { await keyring.privateKeys.importKey(privateKey); } - importPublicKey(publicKey: any): void { - keyring.publicKeys.importKey(publicKey); + async importPublicKey(publicKey: any): Promise { + await keyring.publicKeys.importKey(publicKey); } async importPrivateKey(privateKey: any): Promise { diff --git a/src/app/_services/auth.service.ts b/src/app/_services/auth.service.ts index 3be119e..130a425 100644 --- a/src/app/_services/auth.service.ts +++ b/src/app/_services/auth.service.ts @@ -7,6 +7,8 @@ import { MutableKeyStore, MutablePgpKeyStore } from '@app/_pgp'; import { ErrorDialogService } from '@app/_services/error-dialog.service'; import { HttpClient } from '@angular/common/http'; import { HttpError, rejectBody } from '@app/_helpers/global-error-handler'; +import { Staff } from '@app/_models'; +import { BehaviorSubject, Observable } from 'rxjs'; @Injectable({ providedIn: 'root', @@ -14,6 +16,11 @@ import { HttpError, rejectBody } from '@app/_helpers/global-error-handler'; export class AuthService { sessionToken: any; mutableKeyStore: MutableKeyStore; + trustedUsers: Array = []; + private trustedUsersList: BehaviorSubject> = new BehaviorSubject>( + this.trustedUsers + ); + trustedUsersSubject: Observable> = this.trustedUsersList.asObservable(); constructor( private httpClient: HttpClient, @@ -190,10 +197,22 @@ export class AuthService { window.location.reload(); } + addTrustedUser(user: Staff): void { + const savedIndex = this.trustedUsers.findIndex((staff) => staff.userid === user.userid); + if (savedIndex === 0) { + return; + } + if (savedIndex > 0) { + this.trustedUsers.splice(savedIndex, 1); + } + this.trustedUsers.unshift(user); + this.trustedUsersList.next(this.trustedUsers); + } + getTrustedUsers(): any { - const trustedUsers: Array = []; - this.mutableKeyStore.getPublicKeys().forEach((key) => trustedUsers.push(key.users[0].userId)); - return trustedUsers; + this.mutableKeyStore.getPublicKeys().forEach((key) => { + this.addTrustedUser(key.users[0].userId); + }); } async getPublicKeys(): Promise { diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 4ac8e8b..d9e4a2e 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -37,6 +37,7 @@ export class AppComponent implements OnInit { try { const publicKeys = await this.authService.getPublicKeys(); await this.authService.mutableKeyStore.importPublicKey(publicKeys); + this.authService.getTrustedUsers(); } catch (error) { this.errorDialogService.openDialog({ message: 'Trusted keys endpoint cannot be reached. Please try again later.', diff --git a/src/app/pages/settings/settings.component.ts b/src/app/pages/settings/settings.component.ts index 2b6825f..8e167b0 100644 --- a/src/app/pages/settings/settings.component.ts +++ b/src/app/pages/settings/settings.component.ts @@ -26,12 +26,12 @@ export class SettingsComponent implements OnInit { async ngOnInit(): Promise { await this.authService.init(); - const d = new Date(); - this.date = `${d.getDate()}/${d.getMonth()}/${d.getFullYear()}`; - this.trustedUsers = this.authService.getTrustedUsers(); - this.dataSource = new MatTableDataSource(this.trustedUsers); - this.dataSource.paginator = this.paginator; - this.dataSource.sort = this.sort; + this.authService.trustedUsersSubject.subscribe((users) => { + this.dataSource = new MatTableDataSource(users); + this.dataSource.paginator = this.paginator; + this.dataSource.sort = this.sort; + this.trustedUsers = users; + }); this.userInfo = this.authService.getPrivateKeyInfo(); }