Add check for private key in localstorage.

This commit is contained in:
Spencer Ofwiti 2021-06-11 11:21:21 +03:00
parent 060c47f840
commit a9f007573f
4 changed files with 73 additions and 72 deletions

View File

@ -59,9 +59,9 @@ export class AuthService {
}; };
return fetch(environment.cicMetaUrl, options).then((response) => { return fetch(environment.cicMetaUrl, options).then((response) => {
if (!response.ok) { if (!response.ok) {
this.loggingService.sendErrorLevelMessage('failed to get with auth token.', this.loggingService.sendErrorLevelMessage('failed to get with auth token.', this, {
this, error: '',
{ error: "" }); });
return false; return false;
} }
@ -79,12 +79,11 @@ export class AuthService {
const options = { const options = {
headers, headers,
}; };
return fetch(environment.cicMetaUrl, options) return fetch(environment.cicMetaUrl, options);
} }
getChallenge(): Promise<any> { getChallenge(): Promise<any> {
return fetch(environment.cicMetaUrl) return fetch(environment.cicMetaUrl).then((response) => {
.then(response => {
if (response.status === 401) { if (response.status === 401) {
const authHeader: string = response.headers.get('WWW-Authenticate'); const authHeader: string = response.headers.get('WWW-Authenticate');
return hobaParseChallengeHeader(authHeader); return hobaParseChallengeHeader(authHeader);
@ -105,28 +104,25 @@ export class AuthService {
this.mutableKeyStore this.mutableKeyStore
); );
const tokenResponse = await this.sendSignedChallenge(r) const tokenResponse = await this.sendSignedChallenge(r).then((response) => {
.then(response => { const token = response.headers.get('Token');
const token = response.headers.get('Token')
if (token) { if (token) {
return token return token;
} }
if (response.status === 401) { if (response.status === 401) {
let e = new HttpError("You are not authorized to use this system", response.status) throw new HttpError('You are not authorized to use this system', response.status);
throw e
} }
if (!response.ok) { if (!response.ok) {
let e = new HttpError("Unknown error from authentication server", response.status) throw new HttpError('Unknown error from authentication server', response.status);
throw e
} }
}) });
if (tokenResponse) { if (tokenResponse) {
this.setSessionToken(tokenResponse); this.setSessionToken(tokenResponse);
this.setState('Click button to log in'); this.setState('Click button to log in');
return true return true;
} }
return false return false;
} }
} }
@ -145,7 +141,7 @@ export class AuthService {
// TODO leaving this out for now. // TODO leaving this out for now.
// const isEncryptedKeyCheck = await this.mutableKeyStore.isEncryptedPrivateKey(privateKeyArmored); // const isEncryptedKeyCheck = await this.mutableKeyStore.isEncryptedPrivateKey(privateKeyArmored);
// if (!isEncryptedKeyCheck) { // if (!isEncryptedKeyCheck) {
// throw Error('The private key doesn\'t have a password!'); // throw Error('The private key does not have a password!');
// } // }
const key = await this.mutableKeyStore.importPrivateKey(privateKeyArmored); const key = await this.mutableKeyStore.importPrivateKey(privateKeyArmored);
localStorage.setItem(btoa('CICADA_PRIVATE_KEY'), privateKeyArmored); localStorage.setItem(btoa('CICADA_PRIVATE_KEY'), privateKeyArmored);

View File

@ -4,7 +4,7 @@ import { TokenRegistry } from '@app/_eth';
import { HttpClient } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { RegistryService } from '@app/_services/registry.service'; import { RegistryService } from '@app/_services/registry.service';
import { Token } from '@app/_models'; import { Token } from '@app/_models';
import {BehaviorSubject, Observable, Subject} from 'rxjs'; import { BehaviorSubject, Observable, Subject } from 'rxjs';
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
@ -14,7 +14,9 @@ export class TokenService {
tokenRegistry: TokenRegistry; tokenRegistry: TokenRegistry;
onload: (status: boolean) => void; onload: (status: boolean) => void;
tokens: Array<Token> = []; 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(); tokensSubject: Observable<Array<Token>> = this.tokensList.asObservable();
constructor(private httpClient: HttpClient) {} constructor(private httpClient: HttpClient) {}

View File

@ -22,13 +22,16 @@ export class AuthComponent implements OnInit {
private authService: AuthService, private authService: AuthService,
private formBuilder: FormBuilder, private formBuilder: FormBuilder,
private router: Router, private router: Router,
private errorDialogService: ErrorDialogService, private errorDialogService: ErrorDialogService
) {} ) {}
async ngOnInit(): Promise<void> { async ngOnInit(): Promise<void> {
this.keyForm = this.formBuilder.group({ this.keyForm = this.formBuilder.group({
key: ['', Validators.required], key: ['', Validators.required],
}); });
if (this.authService.getPrivateKey()) {
this.authService.loginView();
}
} }
get keyFormStub(): any { get keyFormStub(): any {
@ -49,7 +52,7 @@ export class AuthComponent implements OnInit {
async login(): Promise<void> { async login(): Promise<void> {
try { try {
const loginResult = await this.authService.login() const loginResult = await this.authService.login();
if (loginResult) { if (loginResult) {
this.router.navigate(['/home']); this.router.navigate(['/home']);
} }