auth component logging and error handling
This commit is contained in:
parent
849f60307e
commit
90c0836eee
@ -14,7 +14,6 @@ import { BehaviorSubject, Observable } from 'rxjs';
|
|||||||
providedIn: 'root',
|
providedIn: 'root',
|
||||||
})
|
})
|
||||||
export class AuthService {
|
export class AuthService {
|
||||||
//sessionToken: any;
|
|
||||||
mutableKeyStore: MutableKeyStore;
|
mutableKeyStore: MutableKeyStore;
|
||||||
trustedUsers: Array<Staff> = [];
|
trustedUsers: Array<Staff> = [];
|
||||||
private trustedUsersList: BehaviorSubject<Array<Staff>> = new BehaviorSubject<Array<Staff>>(
|
private trustedUsersList: BehaviorSubject<Array<Staff>> = new BehaviorSubject<Array<Staff>>(
|
||||||
@ -32,22 +31,17 @@ export class AuthService {
|
|||||||
|
|
||||||
async init(): Promise<void> {
|
async init(): Promise<void> {
|
||||||
await this.mutableKeyStore.loadKeyring();
|
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'))) {
|
if (localStorage.getItem(btoa('CICADA_PRIVATE_KEY'))) {
|
||||||
await this.mutableKeyStore.importPrivateKey(localStorage.getItem(btoa('CICADA_PRIVATE_KEY')));
|
await this.mutableKeyStore.importPrivateKey(localStorage.getItem(btoa('CICADA_PRIVATE_KEY')));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getSessionToken(): string {
|
getSessionToken(): string {
|
||||||
return sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN'));
|
return sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN'));
|
||||||
}
|
}
|
||||||
|
|
||||||
setSessionToken(token): void {
|
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 {
|
setState(s): void {
|
||||||
@ -65,7 +59,10 @@ export class AuthService {
|
|||||||
};
|
};
|
||||||
return fetch(environment.cicMetaUrl, options).then((response) => {
|
return fetch(environment.cicMetaUrl, options).then((response) => {
|
||||||
if (!response.ok) {
|
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 false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -92,20 +89,12 @@ export class AuthService {
|
|||||||
const authHeader: string = response.headers.get('WWW-Authenticate');
|
const authHeader: string = response.headers.get('WWW-Authenticate');
|
||||||
return hobaParseChallengeHeader(authHeader);
|
return hobaParseChallengeHeader(authHeader);
|
||||||
}
|
}
|
||||||
console.log('DEBUG: expected a 401 and www-authenticate header!')
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async login(): Promise<boolean> {
|
async login(): Promise<boolean> {
|
||||||
if (this.getSessionToken()) {
|
if (this.getSessionToken()) {
|
||||||
sessionStorage.removeItem(btoa('CICADA_SESSION_TOKEN'));
|
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 {
|
} else {
|
||||||
const o = await this.getChallenge();
|
const o = await this.getChallenge();
|
||||||
|
|
||||||
@ -123,14 +112,12 @@ export class AuthService {
|
|||||||
return token
|
return token
|
||||||
}
|
}
|
||||||
if (response.status === 401) {
|
if (response.status === 401) {
|
||||||
this.errorDialogService.openDialog({
|
let e = new HttpError("You are not authorized to use this system", response.status)
|
||||||
message: 'You are not authorized to use this system',
|
throw e
|
||||||
});
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
console.log("Failed to get a login token with signed challenge 😭", response.statusText)
|
let e = new HttpError("Unknown error from authentication server", response.status)
|
||||||
return
|
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 {
|
loginView(): void {
|
||||||
document.getElementById('one').style.display = 'none';
|
document.getElementById('one').style.display = 'none';
|
||||||
document.getElementById('two').style.display = 'block';
|
document.getElementById('two').style.display = 'block';
|
||||||
|
@ -2,6 +2,8 @@ import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
|
|||||||
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
|
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
|
||||||
import { CustomErrorStateMatcher } from '@app/_helpers';
|
import { CustomErrorStateMatcher } from '@app/_helpers';
|
||||||
import { AuthService } from '@app/_services';
|
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';
|
import { Router } from '@angular/router';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
@ -19,18 +21,14 @@ export class AuthComponent implements OnInit {
|
|||||||
constructor(
|
constructor(
|
||||||
private authService: AuthService,
|
private authService: AuthService,
|
||||||
private formBuilder: FormBuilder,
|
private formBuilder: FormBuilder,
|
||||||
private router: Router
|
private router: Router,
|
||||||
|
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],
|
||||||
});
|
});
|
||||||
//await this.authService.init();
|
|
||||||
// if (this.authService.privateKey !== undefined) {
|
|
||||||
// const setKey = await this.authService.setKey(this.authService.privateKey);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get keyFormStub(): any {
|
get keyFormStub(): any {
|
||||||
@ -50,14 +48,15 @@ export class AuthComponent implements OnInit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async login(): Promise<void> {
|
async login(): Promise<void> {
|
||||||
// TODO check if we have privatekey
|
try {
|
||||||
// Send us to home if we have a private key
|
const loginResult = await this.authService.login()
|
||||||
// talk to meta somehow
|
if (loginResult) {
|
||||||
// in the error interceptor if 401/403 handle it
|
this.router.navigate(['/home']);
|
||||||
// if 200 go /home
|
}
|
||||||
const loginResult = await this.authService.login()
|
} catch (HttpError) {
|
||||||
if (loginResult) {
|
this.errorDialogService.openDialog({
|
||||||
this.router.navigate(['/home']);
|
message: HttpError.message,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user