Refactor trusted users list.

This commit is contained in:
Spencer Ofwiti 2021-06-02 10:48:02 +03:00
parent 81620600e3
commit 4fba6a8383
4 changed files with 32 additions and 12 deletions

View File

@ -6,7 +6,7 @@ const keyring = new openpgp.Keyring();
interface MutableKeyStore extends KeyStore { interface MutableKeyStore extends KeyStore {
loadKeyring(): void; loadKeyring(): void;
importKeyPair(publicKey: any, privateKey: any): Promise<void>; importKeyPair(publicKey: any, privateKey: any): Promise<void>;
importPublicKey(publicKey: any): void; importPublicKey(publicKey: any): Promise<void>;
importPrivateKey(privateKey: any): Promise<void>; importPrivateKey(privateKey: any): Promise<void>;
getPublicKeys(): Array<any>; getPublicKeys(): Array<any>;
getTrustedKeys(): Array<any>; getTrustedKeys(): Array<any>;
@ -42,8 +42,8 @@ class MutablePgpKeyStore implements MutableKeyStore {
await keyring.privateKeys.importKey(privateKey); await keyring.privateKeys.importKey(privateKey);
} }
importPublicKey(publicKey: any): void { async importPublicKey(publicKey: any): Promise<void> {
keyring.publicKeys.importKey(publicKey); await keyring.publicKeys.importKey(publicKey);
} }
async importPrivateKey(privateKey: any): Promise<void> { async importPrivateKey(privateKey: any): Promise<void> {

View File

@ -7,6 +7,8 @@ import { MutableKeyStore, MutablePgpKeyStore } from '@app/_pgp';
import { ErrorDialogService } from '@app/_services/error-dialog.service'; import { ErrorDialogService } from '@app/_services/error-dialog.service';
import { HttpClient } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { HttpError, rejectBody } from '@app/_helpers/global-error-handler'; import { HttpError, rejectBody } from '@app/_helpers/global-error-handler';
import { Staff } from '@app/_models';
import { BehaviorSubject, Observable } from 'rxjs';
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
@ -14,6 +16,11 @@ import { HttpError, rejectBody } from '@app/_helpers/global-error-handler';
export class AuthService { export class AuthService {
sessionToken: any; sessionToken: any;
mutableKeyStore: MutableKeyStore; mutableKeyStore: MutableKeyStore;
trustedUsers: Array<Staff> = [];
private trustedUsersList: BehaviorSubject<Array<Staff>> = new BehaviorSubject<Array<Staff>>(
this.trustedUsers
);
trustedUsersSubject: Observable<Array<Staff>> = this.trustedUsersList.asObservable();
constructor( constructor(
private httpClient: HttpClient, private httpClient: HttpClient,
@ -190,10 +197,22 @@ export class AuthService {
window.location.reload(); 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 { getTrustedUsers(): any {
const trustedUsers: Array<any> = []; this.mutableKeyStore.getPublicKeys().forEach((key) => {
this.mutableKeyStore.getPublicKeys().forEach((key) => trustedUsers.push(key.users[0].userId)); this.addTrustedUser(key.users[0].userId);
return trustedUsers; });
} }
async getPublicKeys(): Promise<any> { async getPublicKeys(): Promise<any> {

View File

@ -37,6 +37,7 @@ export class AppComponent implements OnInit {
try { try {
const publicKeys = await this.authService.getPublicKeys(); const publicKeys = await this.authService.getPublicKeys();
await this.authService.mutableKeyStore.importPublicKey(publicKeys); await this.authService.mutableKeyStore.importPublicKey(publicKeys);
this.authService.getTrustedUsers();
} catch (error) { } catch (error) {
this.errorDialogService.openDialog({ this.errorDialogService.openDialog({
message: 'Trusted keys endpoint cannot be reached. Please try again later.', message: 'Trusted keys endpoint cannot be reached. Please try again later.',

View File

@ -26,12 +26,12 @@ export class SettingsComponent implements OnInit {
async ngOnInit(): Promise<void> { async ngOnInit(): Promise<void> {
await this.authService.init(); await this.authService.init();
const d = new Date(); this.authService.trustedUsersSubject.subscribe((users) => {
this.date = `${d.getDate()}/${d.getMonth()}/${d.getFullYear()}`; this.dataSource = new MatTableDataSource<any>(users);
this.trustedUsers = this.authService.getTrustedUsers();
this.dataSource = new MatTableDataSource<any>(this.trustedUsers);
this.dataSource.paginator = this.paginator; this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort; this.dataSource.sort = this.sort;
this.trustedUsers = users;
});
this.userInfo = this.authService.getPrivateKeyInfo(); this.userInfo = this.authService.getPrivateKeyInfo();
} }