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 {
loadKeyring(): void;
importKeyPair(publicKey: any, privateKey: any): Promise<void>;
importPublicKey(publicKey: any): void;
importPublicKey(publicKey: any): Promise<void>;
importPrivateKey(privateKey: any): Promise<void>;
getPublicKeys(): Array<any>;
getTrustedKeys(): Array<any>;
@ -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<void> {
await keyring.publicKeys.importKey(publicKey);
}
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 { 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<Staff> = [];
private trustedUsersList: BehaviorSubject<Array<Staff>> = new BehaviorSubject<Array<Staff>>(
this.trustedUsers
);
trustedUsersSubject: Observable<Array<Staff>> = 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<any> = [];
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<any> {

View File

@ -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.',

View File

@ -26,12 +26,12 @@ export class SettingsComponent implements OnInit {
async ngOnInit(): Promise<void> {
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<any>(this.trustedUsers);
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
this.authService.trustedUsersSubject.subscribe((users) => {
this.dataSource = new MatTableDataSource<any>(users);
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
this.trustedUsers = users;
});
this.userInfo = this.authService.getPrivateKeyInfo();
}