Refactor keystore into singleton service.
This commit is contained in:
parent
060c47f840
commit
df008fcace
@ -3,12 +3,13 @@ import { hobaParseChallengeHeader } from '@src/assets/js/hoba.js';
|
||||
import { signChallenge } from '@src/assets/js/hoba-pgp.js';
|
||||
import { environment } from '@src/environments/environment';
|
||||
import { LoggingService } from '@app/_services/logging.service';
|
||||
import { MutableKeyStore, MutablePgpKeyStore } from '@app/_pgp';
|
||||
import { MutableKeyStore } 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';
|
||||
import { KeystoreService } from '@app/_services/keystore.service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
@ -25,12 +26,10 @@ export class AuthService {
|
||||
private httpClient: HttpClient,
|
||||
private loggingService: LoggingService,
|
||||
private errorDialogService: ErrorDialogService
|
||||
) {
|
||||
this.mutableKeyStore = new MutablePgpKeyStore();
|
||||
}
|
||||
) {}
|
||||
|
||||
async init(): Promise<void> {
|
||||
await this.mutableKeyStore.loadKeyring();
|
||||
this.mutableKeyStore = await KeystoreService.getKeystore();
|
||||
if (localStorage.getItem(btoa('CICADA_PRIVATE_KEY'))) {
|
||||
await this.mutableKeyStore.importPrivateKey(localStorage.getItem(btoa('CICADA_PRIVATE_KEY')));
|
||||
}
|
||||
@ -59,9 +58,9 @@ export class AuthService {
|
||||
};
|
||||
return fetch(environment.cicMetaUrl, options).then((response) => {
|
||||
if (!response.ok) {
|
||||
this.loggingService.sendErrorLevelMessage('failed to get with auth token.',
|
||||
this,
|
||||
{ error: "" });
|
||||
this.loggingService.sendErrorLevelMessage('failed to get with auth token.', this, {
|
||||
error: '',
|
||||
});
|
||||
|
||||
return false;
|
||||
}
|
||||
@ -79,12 +78,11 @@ export class AuthService {
|
||||
const options = {
|
||||
headers,
|
||||
};
|
||||
return fetch(environment.cicMetaUrl, options)
|
||||
return fetch(environment.cicMetaUrl, options);
|
||||
}
|
||||
|
||||
getChallenge(): Promise<any> {
|
||||
return fetch(environment.cicMetaUrl)
|
||||
.then(response => {
|
||||
return fetch(environment.cicMetaUrl).then((response) => {
|
||||
if (response.status === 401) {
|
||||
const authHeader: string = response.headers.get('WWW-Authenticate');
|
||||
return hobaParseChallengeHeader(authHeader);
|
||||
@ -105,28 +103,25 @@ export class AuthService {
|
||||
this.mutableKeyStore
|
||||
);
|
||||
|
||||
const tokenResponse = await this.sendSignedChallenge(r)
|
||||
.then(response => {
|
||||
const token = response.headers.get('Token')
|
||||
const tokenResponse = await this.sendSignedChallenge(r).then((response) => {
|
||||
const token = response.headers.get('Token');
|
||||
if (token) {
|
||||
return token
|
||||
return token;
|
||||
}
|
||||
if (response.status === 401) {
|
||||
let e = new HttpError("You are not authorized to use this system", response.status)
|
||||
throw e
|
||||
throw new HttpError('You are not authorized to use this system', response.status);
|
||||
}
|
||||
if (!response.ok) {
|
||||
let e = new HttpError("Unknown error from authentication server", response.status)
|
||||
throw e
|
||||
throw new HttpError('Unknown error from authentication server', response.status);
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
if (tokenResponse) {
|
||||
this.setSessionToken(tokenResponse);
|
||||
this.setState('Click button to log in');
|
||||
return true
|
||||
return true;
|
||||
}
|
||||
return false
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -7,3 +7,5 @@ export * from '@app/_services/location.service';
|
||||
export * from '@app/_services/logging.service';
|
||||
export * from '@app/_services/error-dialog.service';
|
||||
export * from '@app/_services/web3.service';
|
||||
export * from '@app/_services/keystore.service';
|
||||
export * from '@app/_services/registry.service';
|
||||
|
16
src/app/_services/keystore.service.spec.ts
Normal file
16
src/app/_services/keystore.service.spec.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { KeystoreService } from './keystore.service';
|
||||
|
||||
describe('KeystoreService', () => {
|
||||
let service: KeystoreService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(KeystoreService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
});
|
20
src/app/_services/keystore.service.ts
Normal file
20
src/app/_services/keystore.service.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { MutableKeyStore, MutablePgpKeyStore } from '@app/_pgp';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class KeystoreService {
|
||||
private static mutableKeyStore: MutableKeyStore;
|
||||
|
||||
constructor() {}
|
||||
|
||||
public static async getKeystore(): Promise<MutableKeyStore> {
|
||||
if (!KeystoreService.mutableKeyStore) {
|
||||
this.mutableKeyStore = new MutablePgpKeyStore();
|
||||
await this.mutableKeyStore.loadKeyring();
|
||||
return KeystoreService.mutableKeyStore;
|
||||
}
|
||||
return KeystoreService.mutableKeyStore;
|
||||
}
|
||||
}
|
@ -14,7 +14,9 @@ export class TokenService {
|
||||
tokenRegistry: TokenRegistry;
|
||||
onload: (status: boolean) => void;
|
||||
tokens: Array<Token> = [];
|
||||
private tokensList: BehaviorSubject<Array<Token>> = new BehaviorSubject<Array<Token>>(this.tokens);
|
||||
private tokensList: BehaviorSubject<Array<Token>> = new BehaviorSubject<Array<Token>>(
|
||||
this.tokens
|
||||
);
|
||||
tokensSubject: Observable<Array<Token>> = this.tokensList.asObservable();
|
||||
|
||||
constructor(private httpClient: HttpClient) {}
|
||||
|
@ -18,6 +18,7 @@ import { CICRegistry } from 'cic-client';
|
||||
import { RegistryService } from '@app/_services/registry.service';
|
||||
import Web3 from 'web3';
|
||||
import { Web3Service } from '@app/_services/web3.service';
|
||||
import { KeystoreService } from '@app/_services/keystore.service';
|
||||
const vCard = require('vcard-parser');
|
||||
|
||||
@Injectable({
|
||||
@ -170,7 +171,8 @@ export class TransactionService {
|
||||
tx.value = toValue(value);
|
||||
tx.data = data;
|
||||
const txMsg = tx.message();
|
||||
const privateKey = this.authService.mutableKeyStore.getPrivateKey();
|
||||
const keystore = await KeystoreService.getKeystore();
|
||||
const privateKey = keystore.getPrivateKey();
|
||||
if (!privateKey.isDecrypted()) {
|
||||
const password = window.prompt('password');
|
||||
await privateKey.decrypt(password);
|
||||
|
@ -14,6 +14,7 @@ import { CICRegistry } from 'cic-client';
|
||||
import { AuthService } from '@app/_services/auth.service';
|
||||
import { personValidation, vcardValidation } from '@app/_helpers';
|
||||
import { add0x } from '@src/assets/js/ethtx/dist/hex';
|
||||
import { KeystoreService } from '@app/_services/keystore.service';
|
||||
const vCard = require('vcard-parser');
|
||||
|
||||
@Injectable({
|
||||
@ -45,7 +46,7 @@ export class UserService {
|
||||
async init(): Promise<void> {
|
||||
await this.authService.init();
|
||||
await this.tokenService.init();
|
||||
this.keystore = this.authService.mutableKeyStore;
|
||||
this.keystore = await KeystoreService.getKeystore();
|
||||
this.signer = new PGPSigner(this.keystore);
|
||||
this.registry = await RegistryService.getRegistry();
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ export class AuthComponent implements OnInit {
|
||||
private authService: AuthService,
|
||||
private formBuilder: FormBuilder,
|
||||
private router: Router,
|
||||
private errorDialogService: ErrorDialogService,
|
||||
private errorDialogService: ErrorDialogService
|
||||
) {}
|
||||
|
||||
async ngOnInit(): Promise<void> {
|
||||
@ -49,7 +49,7 @@ export class AuthComponent implements OnInit {
|
||||
|
||||
async login(): Promise<void> {
|
||||
try {
|
||||
const loginResult = await this.authService.login()
|
||||
const loginResult = await this.authService.login();
|
||||
if (loginResult) {
|
||||
this.router.navigate(['/home']);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user