2020-12-28 10:09:11 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
2021-02-17 12:13:08 +01:00
|
|
|
import {MutableKeyStore, MutablePgpKeyStore} from '@app/_helpers';
|
2021-02-16 08:10:52 +01:00
|
|
|
import { hobaParseChallengeHeader } from '@src/assets/js/hoba.js';
|
|
|
|
import { signChallenge } from '@src/assets/js/hoba-pgp.js';
|
|
|
|
import {environment} from '@src/environments/environment';
|
|
|
|
import {HttpClient} from '@angular/common/http';
|
2020-12-28 10:09:11 +01:00
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class AuthService {
|
2021-01-18 14:04:16 +01:00
|
|
|
sessionToken: any;
|
2020-12-28 10:09:11 +01:00
|
|
|
sessionLoginCount = 0;
|
2021-01-18 14:04:16 +01:00
|
|
|
privateKey: any;
|
2021-02-16 14:18:41 +01:00
|
|
|
mutableKeyStore: MutableKeyStore = new MutablePgpKeyStore();
|
2020-12-28 10:09:11 +01:00
|
|
|
|
2021-02-16 08:10:52 +01:00
|
|
|
constructor(
|
|
|
|
private http: HttpClient
|
|
|
|
) {
|
2021-01-18 14:04:16 +01:00
|
|
|
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'));
|
|
|
|
}
|
|
|
|
}
|
2020-12-28 10:09:11 +01:00
|
|
|
|
|
|
|
setState(s): void {
|
|
|
|
(document.getElementById('state') as HTMLInputElement).value = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
getWithToken(): void {
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
xhr.responseType = 'text';
|
2021-02-17 19:22:06 +01:00
|
|
|
xhr.open('GET', environment.cicAuthUrl + window.location.search.substring(1));
|
2020-12-28 10:09:11 +01:00
|
|
|
xhr.setRequestHeader('Authorization', 'Bearer ' + this.sessionToken);
|
|
|
|
xhr.setRequestHeader('Content-Type', 'application/json');
|
|
|
|
xhr.setRequestHeader('x-cic-automerge', 'none');
|
|
|
|
xhr.addEventListener('load', (e) => {
|
|
|
|
if (xhr.status === 401) {
|
|
|
|
throw new Error('login rejected');
|
|
|
|
}
|
|
|
|
this.sessionLoginCount++;
|
|
|
|
this.setState('click to perform login ' + this.sessionLoginCount + ' with token ' + this.sessionToken);
|
|
|
|
return;
|
|
|
|
});
|
|
|
|
xhr.send();
|
|
|
|
}
|
|
|
|
|
|
|
|
sendResponse(hobaResponseEncoded): void {
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
xhr.responseType = 'text';
|
2021-02-17 19:22:06 +01:00
|
|
|
xhr.open('GET', environment.cicAuthUrl + window.location.search.substring(1));
|
2020-12-28 10:09:11 +01:00
|
|
|
xhr.setRequestHeader('Authorization', 'HOBA ' + hobaResponseEncoded);
|
|
|
|
xhr.setRequestHeader('Content-Type', 'application/json');
|
|
|
|
xhr.setRequestHeader('x-cic-automerge', 'none');
|
|
|
|
xhr.addEventListener('load', (e) => {
|
|
|
|
if (xhr.status === 401) {
|
|
|
|
throw new Error('login rejected');
|
|
|
|
}
|
|
|
|
this.sessionToken = xhr.getResponseHeader('Token');
|
2021-01-18 14:04:16 +01:00
|
|
|
sessionStorage.setItem(btoa('CICADA_SESSION_TOKEN'), this.sessionToken);
|
2020-12-28 10:09:11 +01:00
|
|
|
this.sessionLoginCount++;
|
|
|
|
this.setState('click to perform login ' + this.sessionLoginCount + ' with token ' + this.sessionToken);
|
|
|
|
return;
|
|
|
|
});
|
|
|
|
xhr.send();
|
|
|
|
}
|
|
|
|
|
2021-01-18 14:04:16 +01:00
|
|
|
getChallenge(): void {
|
2020-12-28 10:09:11 +01:00
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
xhr.responseType = 'arraybuffer';
|
2021-02-17 19:22:06 +01:00
|
|
|
xhr.open('GET', environment.cicAuthUrl + window.location.search.substring(1));
|
2020-12-28 10:09:11 +01:00
|
|
|
xhr.onload = (e) => {
|
|
|
|
if (xhr.status === 401) {
|
|
|
|
const authHeader = xhr.getResponseHeader('WWW-Authenticate');
|
|
|
|
const o = hobaParseChallengeHeader(authHeader);
|
2021-01-18 14:04:16 +01:00
|
|
|
this.loginResponse(o).then();
|
2020-12-28 10:09:11 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
xhr.send();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
login(): boolean {
|
|
|
|
if (this.sessionToken !== undefined) {
|
|
|
|
try {
|
|
|
|
this.getWithToken();
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
console.error('login token failed', e);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try {
|
2021-01-18 14:04:16 +01:00
|
|
|
const o = this.getChallenge();
|
2020-12-28 10:09:11 +01:00
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
console.error('login challenge failed', e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-18 14:04:16 +01:00
|
|
|
async loginResponse(o): Promise<any> {
|
2021-02-17 19:22:06 +01:00
|
|
|
const r = await signChallenge(o.challenge, o.realm, environment.cicAuthUrl, this.mutableKeyStore);
|
2020-12-28 10:09:11 +01:00
|
|
|
this.sendResponse(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
loginView(): void {
|
|
|
|
document.getElementById('one').style.display = 'none';
|
|
|
|
document.getElementById('two').style.display = 'block';
|
2021-02-17 12:13:08 +01:00
|
|
|
this.setState('click to log in with PGP key ' + this.mutableKeyStore.getPrivateKeyId());
|
2020-12-28 10:09:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async setKey(privateKeyArmored): Promise<boolean> {
|
|
|
|
try {
|
2021-02-16 14:18:41 +01:00
|
|
|
await this.mutableKeyStore.importPrivateKey(privateKeyArmored);
|
2021-02-17 12:13:08 +01:00
|
|
|
localStorage.setItem(btoa('CICADA_PRIVATE_KEY'), privateKeyArmored);
|
2020-12-28 10:09:11 +01:00
|
|
|
} catch (e) {
|
|
|
|
console.error('failed setting key', e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
this.loginView();
|
|
|
|
return true;
|
|
|
|
}
|
2021-01-18 14:04:16 +01:00
|
|
|
|
|
|
|
logout(): void {
|
|
|
|
sessionStorage.removeItem(btoa('CICADA_SESSION_TOKEN'));
|
|
|
|
window.location.reload(true);
|
|
|
|
}
|
2021-02-16 08:10:52 +01:00
|
|
|
|
|
|
|
async getPublicKeys(): Promise<void> {
|
|
|
|
this.http.get(`${environment.publicKeysUrl}/keys.asc`).subscribe(async res => {
|
2021-02-17 12:13:08 +01:00
|
|
|
await this.mutableKeyStore.importPublicKey(res);
|
2021-02-16 08:10:52 +01:00
|
|
|
}, error => {
|
|
|
|
console.error('There was an error!', error);
|
|
|
|
});
|
2021-02-16 14:18:41 +01:00
|
|
|
if (this.privateKey !== undefined) {
|
|
|
|
await this.mutableKeyStore.importPrivateKey(this.privateKey);
|
|
|
|
}
|
2021-02-16 08:10:52 +01:00
|
|
|
}
|
2020-12-28 10:09:11 +01:00
|
|
|
}
|