key/no-key works and a litle keyring refactor

This commit is contained in:
Blair Vanderlugt 2021-04-24 09:46:05 -07:00
parent 836c4c03b3
commit 7103f15236
4 changed files with 63 additions and 52 deletions

View File

@ -12,13 +12,12 @@ export class AuthGuard implements CanActivate {
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
//if (sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN'))) {
// return true;
//}
//this.router.navigate(['/auth']);
//return false;
return true;
if (localStorage.getItem(btoa('CICADA_PRIVATE_KEY'))) {
return true;
}
console.log('authGuard: Hey you need a private key!')
this.router.navigate(['/auth']);
return false;
}
}

View File

@ -19,20 +19,20 @@ export class LoggingInterceptor implements HttpInterceptor {
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
return next.handle(request);
this.loggingService.sendInfoLevelMessage(request);
const startTime = Date.now();
let status: string;
// this.loggingService.sendInfoLevelMessage(request);
// const startTime = Date.now();
// let status: string;
return next.handle(request).pipe(tap(event => {
status = '';
if (event instanceof HttpResponse) {
status = 'succeeded';
}
}, error => status = 'failed'),
finalize(() => {
const elapsedTime = Date.now() - startTime;
const message = `${request.method} request for ${request.urlWithParams} ${status} in ${elapsedTime} ms`;
this.loggingService.sendInfoLevelMessage(message);
}));
// return next.handle(request).pipe(tap(event => {
// status = '';
// if (event instanceof HttpResponse) {
// status = 'succeeded';
// }
// }, error => status = 'failed'),
// finalize(() => {
// const elapsedTime = Date.now() - startTime;
// const message = `${request.method} request for ${request.urlWithParams} ${status} in ${elapsedTime} ms`;
// this.loggingService.sendInfoLevelMessage(message);
// }));
}
}

View File

@ -15,20 +15,25 @@ import { HttpError } from '@app/_helpers/global-error-handler';
export class AuthService {
sessionToken: any;
sessionLoginCount = 0;
privateKey: any;
mutableKeyStore: MutableKeyStore = new MutablePgpKeyStore();
mutableKeyStore: MutableKeyStore;
constructor(
private httpClient: HttpClient,
private loggingService: LoggingService,
private errorDialogService: ErrorDialogService
) {
this.mutableKeyStore = new MutablePgpKeyStore()
}
async init(): void {
this.mutableKeyStore.loadKeyring();
// TODO setting these together shoulds be atomic
if (sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN'))) {
this.sessionToken = sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN'));
}
if (localStorage.getItem(btoa('CICADA_PRIVATE_KEY'))) {
this.privateKey = localStorage.getItem(btoa('CICADA_PRIVATE_KEY'));
this.mutableKeyStore.importPrivateKey(localStorage.getItem(btoa('CICADA_PRIVATE_KEY')))
// this.privateKey = localStorage.getItem(btoa('CICADA_PRIVATE_KEY'));
}
}
@ -94,21 +99,30 @@ export class AuthService {
login(): boolean {
if (this.sessionToken !== undefined) {
try {
//this.getWithToken();
return true;
} catch (e) {
this.loggingService.sendErrorLevelMessage('Login token failed', this, {error: e});
// 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
if (!this.getPrivateKey()) {
return false;
}
} else {
try {
// this.getChallenge();
return true
} catch (e) {
this.loggingService.sendErrorLevelMessage('Login challenge failed', this, {error: e});
}
}
return true;
// if (this.sessionToken !== undefined) {
// try {
// //this.getWithToken();
// return true;
// } catch (e) {
// this.loggingService.sendErrorLevelMessage('Login token failed', this, {error: e});
// }
// } else {
// try {
// // this.getChallenge();
// return true
// } catch (e) {
// this.loggingService.sendErrorLevelMessage('Login challenge failed', this, {error: e});
// }
// }
//return false;
}
@ -153,10 +167,11 @@ export class AuthService {
if (!isValidKeyCheck) {
throw Error('The private key is invalid');
}
const isEncryptedKeyCheck = await this.mutableKeyStore.isEncryptedPrivateKey(privateKeyArmored);
if (!isEncryptedKeyCheck) {
throw Error('The private key doesn\'t have a password!');
}
// TODO leaving this out for now.
//const isEncryptedKeyCheck = await this.mutableKeyStore.isEncryptedPrivateKey(privateKeyArmored);
//if (!isEncryptedKeyCheck) {
// throw Error('The private key doesn\'t have a password!');
//}
const key = await this.mutableKeyStore.importPrivateKey(privateKeyArmored);
localStorage.setItem(btoa('CICADA_PRIVATE_KEY'), privateKeyArmored);
} catch (err) {
@ -194,9 +209,7 @@ export class AuthService {
return data;
}
async getPrivateKeys(): Promise<void> {
if (this.privateKey !== undefined) {
await this.mutableKeyStore.importPrivateKey(this.privateKey);
}
getPrivateKey(): any {
return this.mutableKeyStore.getPrivateKey();
}
}

View File

@ -26,12 +26,11 @@ export class AuthComponent implements OnInit {
this.keyForm = this.formBuilder.group({
key: ['', Validators.required],
});
if (this.authService.privateKey !== undefined) {
const setKey = await this.authService.setKey(this.authService.privateKey);
if (setKey && this.authService.sessionToken !== undefined) {
this.authService.setState('Click button to log in');
}
}
this.authService.init();
//if (this.authService.privateKey !== undefined) {
// const setKey = await this.authService.setKey(this.authService.privateKey);
// }
//}
}
get keyFormStub(): any { return this.keyForm.controls; }