auth component logging and error handling

This commit is contained in:
Blair Vanderlugt 2021-06-09 17:07:25 -07:00
parent 849f60307e
commit 90c0836eee
2 changed files with 23 additions and 67 deletions

View File

@ -14,7 +14,6 @@ import { BehaviorSubject, Observable } from 'rxjs';
providedIn: 'root',
})
export class AuthService {
//sessionToken: any;
mutableKeyStore: MutableKeyStore;
trustedUsers: Array<Staff> = [];
private trustedUsersList: BehaviorSubject<Array<Staff>> = new BehaviorSubject<Array<Staff>>(
@ -32,22 +31,17 @@ export class AuthService {
async init(): Promise<void> {
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<boolean> {
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<any> {
// 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';

View File

@ -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<void> {
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<void> {
// 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,
});
}
}