key/no-key works and a litle keyring refactor
This commit is contained in:
parent
836c4c03b3
commit
7103f15236
@ -12,13 +12,12 @@ export class AuthGuard implements CanActivate {
|
|||||||
canActivate(
|
canActivate(
|
||||||
route: ActivatedRouteSnapshot,
|
route: ActivatedRouteSnapshot,
|
||||||
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
|
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
|
||||||
//if (sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN'))) {
|
if (localStorage.getItem(btoa('CICADA_PRIVATE_KEY'))) {
|
||||||
// return true;
|
|
||||||
//}
|
|
||||||
|
|
||||||
//this.router.navigate(['/auth']);
|
|
||||||
//return false;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
console.log('authGuard: Hey you need a private key!')
|
||||||
|
this.router.navigate(['/auth']);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -19,20 +19,20 @@ export class LoggingInterceptor implements HttpInterceptor {
|
|||||||
|
|
||||||
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
|
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
|
||||||
return next.handle(request);
|
return next.handle(request);
|
||||||
this.loggingService.sendInfoLevelMessage(request);
|
// this.loggingService.sendInfoLevelMessage(request);
|
||||||
const startTime = Date.now();
|
// const startTime = Date.now();
|
||||||
let status: string;
|
// let status: string;
|
||||||
|
|
||||||
return next.handle(request).pipe(tap(event => {
|
// return next.handle(request).pipe(tap(event => {
|
||||||
status = '';
|
// status = '';
|
||||||
if (event instanceof HttpResponse) {
|
// if (event instanceof HttpResponse) {
|
||||||
status = 'succeeded';
|
// status = 'succeeded';
|
||||||
}
|
// }
|
||||||
}, error => status = 'failed'),
|
// }, error => status = 'failed'),
|
||||||
finalize(() => {
|
// finalize(() => {
|
||||||
const elapsedTime = Date.now() - startTime;
|
// const elapsedTime = Date.now() - startTime;
|
||||||
const message = `${request.method} request for ${request.urlWithParams} ${status} in ${elapsedTime} ms`;
|
// const message = `${request.method} request for ${request.urlWithParams} ${status} in ${elapsedTime} ms`;
|
||||||
this.loggingService.sendInfoLevelMessage(message);
|
// this.loggingService.sendInfoLevelMessage(message);
|
||||||
}));
|
// }));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,20 +15,25 @@ import { HttpError } from '@app/_helpers/global-error-handler';
|
|||||||
export class AuthService {
|
export class AuthService {
|
||||||
sessionToken: any;
|
sessionToken: any;
|
||||||
sessionLoginCount = 0;
|
sessionLoginCount = 0;
|
||||||
privateKey: any;
|
mutableKeyStore: MutableKeyStore;
|
||||||
mutableKeyStore: MutableKeyStore = new MutablePgpKeyStore();
|
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private httpClient: HttpClient,
|
private httpClient: HttpClient,
|
||||||
private loggingService: LoggingService,
|
private loggingService: LoggingService,
|
||||||
private errorDialogService: ErrorDialogService
|
private errorDialogService: ErrorDialogService
|
||||||
) {
|
) {
|
||||||
|
this.mutableKeyStore = new MutablePgpKeyStore()
|
||||||
|
}
|
||||||
|
|
||||||
|
async init(): void {
|
||||||
|
this.mutableKeyStore.loadKeyring();
|
||||||
// TODO setting these together shoulds be atomic
|
// TODO setting these together shoulds be atomic
|
||||||
if (sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN'))) {
|
if (sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN'))) {
|
||||||
this.sessionToken = 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'))) {
|
||||||
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 {
|
login(): boolean {
|
||||||
if (this.sessionToken !== undefined) {
|
// TODO check if we have privatekey
|
||||||
try {
|
// Send us to home if we have a private key
|
||||||
//this.getWithToken();
|
// talk to meta somehow
|
||||||
|
// in the error interceptor if 401/403 handle it
|
||||||
|
// if 200 go /home
|
||||||
|
if (!this.getPrivateKey()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
} catch (e) {
|
// if (this.sessionToken !== undefined) {
|
||||||
this.loggingService.sendErrorLevelMessage('Login token failed', this, {error: e});
|
// try {
|
||||||
}
|
// //this.getWithToken();
|
||||||
} else {
|
// return true;
|
||||||
try {
|
// } catch (e) {
|
||||||
// this.getChallenge();
|
// this.loggingService.sendErrorLevelMessage('Login token failed', this, {error: e});
|
||||||
return true
|
// }
|
||||||
} catch (e) {
|
// } else {
|
||||||
this.loggingService.sendErrorLevelMessage('Login challenge failed', this, {error: e});
|
// try {
|
||||||
}
|
// // this.getChallenge();
|
||||||
}
|
// return true
|
||||||
|
// } catch (e) {
|
||||||
|
// this.loggingService.sendErrorLevelMessage('Login challenge failed', this, {error: e});
|
||||||
|
// }
|
||||||
|
// }
|
||||||
//return false;
|
//return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -153,10 +167,11 @@ export class AuthService {
|
|||||||
if (!isValidKeyCheck) {
|
if (!isValidKeyCheck) {
|
||||||
throw Error('The private key is invalid');
|
throw Error('The private key is invalid');
|
||||||
}
|
}
|
||||||
const isEncryptedKeyCheck = await this.mutableKeyStore.isEncryptedPrivateKey(privateKeyArmored);
|
// TODO leaving this out for now.
|
||||||
if (!isEncryptedKeyCheck) {
|
//const isEncryptedKeyCheck = await this.mutableKeyStore.isEncryptedPrivateKey(privateKeyArmored);
|
||||||
throw Error('The private key doesn\'t have a password!');
|
//if (!isEncryptedKeyCheck) {
|
||||||
}
|
// throw Error('The private key doesn\'t 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);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@ -194,9 +209,7 @@ export class AuthService {
|
|||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getPrivateKeys(): Promise<void> {
|
getPrivateKey(): any {
|
||||||
if (this.privateKey !== undefined) {
|
return this.mutableKeyStore.getPrivateKey();
|
||||||
await this.mutableKeyStore.importPrivateKey(this.privateKey);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,12 +26,11 @@ export class AuthComponent implements OnInit {
|
|||||||
this.keyForm = this.formBuilder.group({
|
this.keyForm = this.formBuilder.group({
|
||||||
key: ['', Validators.required],
|
key: ['', Validators.required],
|
||||||
});
|
});
|
||||||
if (this.authService.privateKey !== undefined) {
|
this.authService.init();
|
||||||
const setKey = await this.authService.setKey(this.authService.privateKey);
|
//if (this.authService.privateKey !== undefined) {
|
||||||
if (setKey && this.authService.sessionToken !== undefined) {
|
// const setKey = await this.authService.setKey(this.authService.privateKey);
|
||||||
this.authService.setState('Click button to log in');
|
// }
|
||||||
}
|
//}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get keyFormStub(): any { return this.keyForm.controls; }
|
get keyFormStub(): any { return this.keyForm.controls; }
|
||||||
|
Loading…
Reference in New Issue
Block a user