auth double click got dropped from the revert I think
This commit is contained in:
parent
81861f5740
commit
ad71df5c52
@ -11,91 +11,90 @@ import { Staff } from '@app/_models';
|
|||||||
import { BehaviorSubject, Observable } from 'rxjs';
|
import { BehaviorSubject, Observable } from 'rxjs';
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root',
|
providedIn: 'root',
|
||||||
})
|
})
|
||||||
export class AuthService {
|
export class AuthService {
|
||||||
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>>(
|
||||||
this.trustedUsers
|
this.trustedUsers
|
||||||
);
|
);
|
||||||
trustedUsersSubject: Observable<Array<Staff>> = this.trustedUsersList.asObservable();
|
trustedUsersSubject: Observable<Array<Staff>> = this.trustedUsersList.asObservable();
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private httpClient: HttpClient,
|
private httpClient: HttpClient,
|
||||||
private loggingService: LoggingService,
|
private loggingService: LoggingService,
|
||||||
private errorDialogService: ErrorDialogService
|
private errorDialogService: ErrorDialogService
|
||||||
) {
|
) {
|
||||||
this.mutableKeyStore = new MutablePgpKeyStore();
|
this.mutableKeyStore = new MutablePgpKeyStore();
|
||||||
}
|
|
||||||
|
|
||||||
async init(): Promise<void> {
|
|
||||||
await this.mutableKeyStore.loadKeyring();
|
|
||||||
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'));
|
|
||||||
}
|
|
||||||
|
|
||||||
setSessionToken(token): void {
|
async init(): Promise<void> {
|
||||||
sessionStorage.setItem(btoa('CICADA_SESSION_TOKEN'), token);
|
await this.mutableKeyStore.loadKeyring();
|
||||||
}
|
if (localStorage.getItem(btoa('CICADA_PRIVATE_KEY'))) {
|
||||||
|
await this.mutableKeyStore.importPrivateKey(localStorage.getItem(btoa('CICADA_PRIVATE_KEY')));
|
||||||
setState(s): void {
|
|
||||||
document.getElementById('state').innerHTML = s;
|
|
||||||
}
|
|
||||||
|
|
||||||
getWithToken(): Promise<boolean> {
|
|
||||||
const headers = {
|
|
||||||
Authorization: 'Bearer ' + this.getSessionToken,
|
|
||||||
'Content-Type': 'application/json;charset=utf-8',
|
|
||||||
'x-cic-automerge': 'none',
|
|
||||||
};
|
|
||||||
const options = {
|
|
||||||
headers,
|
|
||||||
};
|
|
||||||
return fetch(environment.cicMetaUrl, options).then((response) => {
|
|
||||||
if (!response.ok) {
|
|
||||||
this.loggingService.sendErrorLevelMessage('failed to get with auth token.',
|
|
||||||
this,
|
|
||||||
{ error: "" });
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
return true;
|
}
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO rename to send signed challenge and set session. Also separate these responsibilities
|
getSessionToken(): string {
|
||||||
sendSignedChallenge(hobaResponseEncoded: any): Promise<any> {
|
return sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN'));
|
||||||
const headers = {
|
}
|
||||||
Authorization: 'HOBA ' + hobaResponseEncoded,
|
|
||||||
'Content-Type': 'application/json;charset=utf-8',
|
|
||||||
'x-cic-automerge': 'none',
|
|
||||||
};
|
|
||||||
const options = {
|
|
||||||
headers,
|
|
||||||
};
|
|
||||||
return fetch(environment.cicMetaUrl, options)
|
|
||||||
}
|
|
||||||
|
|
||||||
getChallenge(): Promise<any> {
|
setSessionToken(token): void {
|
||||||
return fetch(environment.cicMetaUrl)
|
sessionStorage.setItem(btoa('CICADA_SESSION_TOKEN'), token);
|
||||||
|
}
|
||||||
|
|
||||||
|
setState(s): void {
|
||||||
|
document.getElementById('state').innerHTML = s;
|
||||||
|
}
|
||||||
|
|
||||||
|
getWithToken(): Promise<boolean> {
|
||||||
|
const headers = {
|
||||||
|
Authorization: 'Bearer ' + this.getSessionToken,
|
||||||
|
'Content-Type': 'application/json;charset=utf-8',
|
||||||
|
'x-cic-automerge': 'none',
|
||||||
|
};
|
||||||
|
const options = {
|
||||||
|
headers,
|
||||||
|
};
|
||||||
|
return fetch(environment.cicMetaUrl, options).then((response) => {
|
||||||
|
if (!response.ok) {
|
||||||
|
this.loggingService.sendErrorLevelMessage('failed to get with auth token.',
|
||||||
|
this,
|
||||||
|
{ error: "" });
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO rename to send signed challenge and set session. Also separate these responsibilities
|
||||||
|
sendSignedChallenge(hobaResponseEncoded: any): Promise<any> {
|
||||||
|
const headers = {
|
||||||
|
Authorization: 'HOBA ' + hobaResponseEncoded,
|
||||||
|
'Content-Type': 'application/json;charset=utf-8',
|
||||||
|
'x-cic-automerge': 'none',
|
||||||
|
};
|
||||||
|
const options = {
|
||||||
|
headers,
|
||||||
|
};
|
||||||
|
return fetch(environment.cicMetaUrl, options)
|
||||||
|
}
|
||||||
|
|
||||||
|
getChallenge(): Promise<any> {
|
||||||
|
return fetch(environment.cicMetaUrl)
|
||||||
.then(response => {
|
.then(response => {
|
||||||
if (response.status === 401) {
|
if (response.status === 401) {
|
||||||
const authHeader: string = response.headers.get('WWW-Authenticate');
|
const authHeader: string = response.headers.get('WWW-Authenticate');
|
||||||
return hobaParseChallengeHeader(authHeader);
|
return hobaParseChallengeHeader(authHeader);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
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'));
|
||||||
} else {
|
}
|
||||||
const o = await this.getChallenge();
|
const o = await this.getChallenge();
|
||||||
|
|
||||||
const r = await signChallenge(
|
const r = await signChallenge(
|
||||||
@ -106,20 +105,20 @@ export class AuthService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const tokenResponse = await this.sendSignedChallenge(r)
|
const tokenResponse = await this.sendSignedChallenge(r)
|
||||||
.then(response => {
|
.then(response => {
|
||||||
const token = response.headers.get('Token')
|
const token = response.headers.get('Token')
|
||||||
if (token) {
|
if (token) {
|
||||||
return token
|
return token
|
||||||
}
|
}
|
||||||
if (response.status === 401) {
|
if (response.status === 401) {
|
||||||
let e = new HttpError("You are not authorized to use this system", response.status)
|
let e = new HttpError("You are not authorized to use this system", response.status)
|
||||||
throw e
|
throw e
|
||||||
}
|
}
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
let e = new HttpError("Unknown error from authentication server", response.status)
|
let e = new HttpError("Unknown error from authentication server", response.status)
|
||||||
throw e
|
throw e
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
if (tokenResponse) {
|
if (tokenResponse) {
|
||||||
this.setSessionToken(tokenResponse);
|
this.setSessionToken(tokenResponse);
|
||||||
@ -128,83 +127,82 @@ export class AuthService {
|
|||||||
}
|
}
|
||||||
return false
|
return 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';
|
||||||
this.setState('Click button to log in with PGP key ' + this.mutableKeyStore.getPrivateKeyId());
|
this.setState('Click button to log in with PGP key ' + this.mutableKeyStore.getPrivateKeyId());
|
||||||
}
|
|
||||||
|
|
||||||
async setKey(privateKeyArmored): Promise<boolean> {
|
|
||||||
try {
|
|
||||||
const isValidKeyCheck = await this.mutableKeyStore.isValidKey(privateKeyArmored);
|
|
||||||
if (!isValidKeyCheck) {
|
|
||||||
throw Error('The private key is invalid');
|
|
||||||
}
|
|
||||||
// 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) {
|
|
||||||
this.loggingService.sendErrorLevelMessage(
|
|
||||||
`Failed to set key: ${err.message || err.statusText}`,
|
|
||||||
this,
|
|
||||||
{ error: err }
|
|
||||||
);
|
|
||||||
this.errorDialogService.openDialog({
|
|
||||||
message: `Failed to set key: ${err.message || err.statusText}`,
|
|
||||||
});
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
this.loginView();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
logout(): void {
|
async setKey(privateKeyArmored): Promise<boolean> {
|
||||||
sessionStorage.removeItem(btoa('CICADA_SESSION_TOKEN'));
|
try {
|
||||||
localStorage.removeItem(btoa('CICADA_PRIVATE_KEY'));
|
const isValidKeyCheck = await this.mutableKeyStore.isValidKey(privateKeyArmored);
|
||||||
window.location.reload();
|
if (!isValidKeyCheck) {
|
||||||
}
|
throw Error('The private key is invalid');
|
||||||
|
}
|
||||||
addTrustedUser(user: Staff): void {
|
// TODO leaving this out for now.
|
||||||
const savedIndex = this.trustedUsers.findIndex((staff) => staff.userid === user.userid);
|
// const isEncryptedKeyCheck = await this.mutableKeyStore.isEncryptedPrivateKey(privateKeyArmored);
|
||||||
if (savedIndex === 0) {
|
// if (!isEncryptedKeyCheck) {
|
||||||
return;
|
// throw Error('The private key doesn\'t have a password!');
|
||||||
}
|
// }
|
||||||
if (savedIndex > 0) {
|
const key = await this.mutableKeyStore.importPrivateKey(privateKeyArmored);
|
||||||
this.trustedUsers.splice(savedIndex, 1);
|
localStorage.setItem(btoa('CICADA_PRIVATE_KEY'), privateKeyArmored);
|
||||||
}
|
} catch (err) {
|
||||||
this.trustedUsers.unshift(user);
|
this.loggingService.sendErrorLevelMessage(
|
||||||
this.trustedUsersList.next(this.trustedUsers);
|
`Failed to set key: ${err.message || err.statusText}`,
|
||||||
}
|
this,
|
||||||
|
{ error: err }
|
||||||
getTrustedUsers(): void {
|
);
|
||||||
this.mutableKeyStore.getPublicKeys().forEach((key) => {
|
this.errorDialogService.openDialog({
|
||||||
this.addTrustedUser(key.users[0].userId);
|
message: `Failed to set key: ${err.message || err.statusText}`,
|
||||||
});
|
});
|
||||||
}
|
return false;
|
||||||
|
|
||||||
async getPublicKeys(): Promise<any> {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
fetch(environment.publicKeysUrl).then((res) => {
|
|
||||||
if (!res.ok) {
|
|
||||||
// TODO does angular recommend an error interface?
|
|
||||||
return reject(rejectBody(res));
|
|
||||||
}
|
}
|
||||||
return resolve(res.text());
|
this.loginView();
|
||||||
});
|
return true;
|
||||||
});
|
}
|
||||||
}
|
|
||||||
|
|
||||||
getPrivateKey(): any {
|
logout(): void {
|
||||||
return this.mutableKeyStore.getPrivateKey();
|
sessionStorage.removeItem(btoa('CICADA_SESSION_TOKEN'));
|
||||||
}
|
localStorage.removeItem(btoa('CICADA_PRIVATE_KEY'));
|
||||||
|
window.location.reload();
|
||||||
|
}
|
||||||
|
|
||||||
getPrivateKeyInfo(): any {
|
addTrustedUser(user: Staff): void {
|
||||||
return this.getPrivateKey().users[0].userId;
|
const savedIndex = this.trustedUsers.findIndex((staff) => staff.userid === user.userid);
|
||||||
}
|
if (savedIndex === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (savedIndex > 0) {
|
||||||
|
this.trustedUsers.splice(savedIndex, 1);
|
||||||
|
}
|
||||||
|
this.trustedUsers.unshift(user);
|
||||||
|
this.trustedUsersList.next(this.trustedUsers);
|
||||||
|
}
|
||||||
|
|
||||||
|
getTrustedUsers(): void {
|
||||||
|
this.mutableKeyStore.getPublicKeys().forEach((key) => {
|
||||||
|
this.addTrustedUser(key.users[0].userId);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async getPublicKeys(): Promise<any> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
fetch(environment.publicKeysUrl).then((res) => {
|
||||||
|
if (!res.ok) {
|
||||||
|
// TODO does angular recommend an error interface?
|
||||||
|
return reject(rejectBody(res));
|
||||||
|
}
|
||||||
|
return resolve(res.text());
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
getPrivateKey(): any {
|
||||||
|
return this.mutableKeyStore.getPrivateKey();
|
||||||
|
}
|
||||||
|
|
||||||
|
getPrivateKeyInfo(): any {
|
||||||
|
return this.getPrivateKey().users[0].userId;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user