From 90c0836eeeb2e442876ad9fce91e4a1348d42584 Mon Sep 17 00:00:00 2001 From: Blair Vanderlugt Date: Wed, 9 Jun 2021 17:07:25 -0700 Subject: [PATCH] auth component logging and error handling --- src/app/_services/auth.service.ts | 63 +++++-------------------------- src/app/auth/auth.component.ts | 27 +++++++------ 2 files changed, 23 insertions(+), 67 deletions(-) diff --git a/src/app/_services/auth.service.ts b/src/app/_services/auth.service.ts index c7771d9..937f2ce 100644 --- a/src/app/_services/auth.service.ts +++ b/src/app/_services/auth.service.ts @@ -14,7 +14,6 @@ import { BehaviorSubject, Observable } from 'rxjs'; providedIn: 'root', }) export class AuthService { - //sessionToken: any; mutableKeyStore: MutableKeyStore; trustedUsers: Array = []; private trustedUsersList: BehaviorSubject> = new BehaviorSubject>( @@ -32,22 +31,17 @@ export class AuthService { async init(): Promise { await this.mutableKeyStore.loadKeyring(); - // TODO setting these together should be atomic - //if (sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN'))) { - // this.sessionToken = sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN')); - //} if (localStorage.getItem(btoa('CICADA_PRIVATE_KEY'))) { await this.mutableKeyStore.importPrivateKey(localStorage.getItem(btoa('CICADA_PRIVATE_KEY'))); } } getSessionToken(): string { - return sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN')); + return sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN')); } setSessionToken(token): void { - console.log('Setting sessiong token! ', token) - sessionStorage.setItem(btoa('CICADA_SESSION_TOKEN'), token); + sessionStorage.setItem(btoa('CICADA_SESSION_TOKEN'), token); } setState(s): void { @@ -65,7 +59,10 @@ export class AuthService { }; return fetch(environment.cicMetaUrl, options).then((response) => { if (!response.ok) { - console.log("failed to getWithToken...maybe try clearing the token and try again?") + this.loggingService.sendErrorLevelMessage('failed to get with auth token.', + this, + { error: "" }); + return false; } return true; @@ -92,20 +89,12 @@ export class AuthService { const authHeader: string = response.headers.get('WWW-Authenticate'); return hobaParseChallengeHeader(authHeader); } - console.log('DEBUG: expected a 401 and www-authenticate header!') }); } async login(): Promise { if (this.getSessionToken()) { sessionStorage.removeItem(btoa('CICADA_SESSION_TOKEN')); - //try { - // // TODO do we need to do this? is it just a test of the token? - // const response: boolean = await this.getWithToken(); - // return response - //} catch (e) { - // this.loggingService.sendErrorLevelMessage('Login token failed', this, { error: e }); - //} } else { const o = await this.getChallenge(); @@ -123,14 +112,12 @@ export class AuthService { return token } if (response.status === 401) { - this.errorDialogService.openDialog({ - message: 'You are not authorized to use this system', - }); - return + let e = new HttpError("You are not authorized to use this system", response.status) + throw e } if (!response.ok) { - console.log("Failed to get a login token with signed challenge 😭", response.statusText) - return + let e = new HttpError("Unknown error from authentication server", response.status) + throw e } }) @@ -143,36 +130,6 @@ export class AuthService { } } - //async loginResponse(o: { challenge: string; realm: any }): Promise { - // const r = await signChallenge( - // o.challenge, - // o.realm, - // environment.cicMetaUrl, - // this.mutableKeyStore - // ); - - // return this.sendSignedChallenge(r); - // // if (error instanceof HttpError) { - // // if (error.status === 403) { - // // this.errorDialogService.openDialog({ - // // message: 'You are not authorized to use this system', - // // }); - // // } else if (error.status === 401) { - // // this.errorDialogService.openDialog({ - // // message: - // // 'Unable to authenticate with the service. ' + - // // 'Please speak with the staff at Grassroots ' + - // // 'Economics for requesting access ' + - // // 'staff@grassrootseconomics.net.', - // // }); - // // } - // // } else { - // // // TODO define this error - // // this.errorDialogService.openDialog({ message: 'Incorrect key passphrase.' }); - // // } - // // resolve(false); - //} - loginView(): void { document.getElementById('one').style.display = 'none'; document.getElementById('two').style.display = 'block'; diff --git a/src/app/auth/auth.component.ts b/src/app/auth/auth.component.ts index 7c33f4c..328997f 100644 --- a/src/app/auth/auth.component.ts +++ b/src/app/auth/auth.component.ts @@ -2,6 +2,8 @@ import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; import { CustomErrorStateMatcher } from '@app/_helpers'; import { AuthService } from '@app/_services'; +import { ErrorDialogService } from '@app/_services/error-dialog.service'; +import { LoggingService } from '@app/_services/logging.service'; import { Router } from '@angular/router'; @Component({ @@ -19,18 +21,14 @@ export class AuthComponent implements OnInit { constructor( private authService: AuthService, private formBuilder: FormBuilder, - private router: Router + private router: Router, + private errorDialogService: ErrorDialogService, ) {} async ngOnInit(): Promise { this.keyForm = this.formBuilder.group({ key: ['', Validators.required], }); - //await this.authService.init(); - // if (this.authService.privateKey !== undefined) { - // const setKey = await this.authService.setKey(this.authService.privateKey); - // } - // } } get keyFormStub(): any { @@ -50,14 +48,15 @@ export class AuthComponent implements OnInit { } async login(): Promise { - // TODO check if we have privatekey - // Send us to home if we have a private key - // talk to meta somehow - // in the error interceptor if 401/403 handle it - // if 200 go /home - const loginResult = await this.authService.login() - if (loginResult) { - this.router.navigate(['/home']); + try { + const loginResult = await this.authService.login() + if (loginResult) { + this.router.navigate(['/home']); + } + } catch (HttpError) { + this.errorDialogService.openDialog({ + message: HttpError.message, + }); } }