Format files using linter.

This commit is contained in:
Spencer Ofwiti 2021-06-11 09:26:50 +03:00
parent df395b7b61
commit 0866c4c22f
5 changed files with 96 additions and 101 deletions

View File

@ -8,11 +8,11 @@ export class HttpConfigInterceptor implements HttpInterceptor {
constructor() {} constructor() {}
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> { intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
//const token: string = sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN')); // const token: string = sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN'));
const token: string = AuthService.getSessionToken() const token: string = AuthService.getSessionToken();
if (token) { if (token) {
request = request.clone({headers: request.headers.set('Authorization', 'Bearer ' + token)}); request = request.clone({ headers: request.headers.set('Authorization', 'Bearer ' + token) });
} }
return next.handle(request); return next.handle(request);

View File

@ -14,13 +14,6 @@ import { BehaviorSubject, Observable } from 'rxjs';
providedIn: 'root', providedIn: 'root',
}) })
export class AuthService { export class AuthService {
mutableKeyStore: MutableKeyStore;
trustedUsers: Array<Staff> = [];
private trustedUsersList: BehaviorSubject<Array<Staff>> = new BehaviorSubject<Array<Staff>>(
this.trustedUsers
);
trustedUsersSubject: Observable<Array<Staff>> = this.trustedUsersList.asObservable();
constructor( constructor(
private httpClient: HttpClient, private httpClient: HttpClient,
private loggingService: LoggingService, private loggingService: LoggingService,
@ -28,6 +21,16 @@ export class AuthService {
) { ) {
this.mutableKeyStore = new MutablePgpKeyStore(); this.mutableKeyStore = new MutablePgpKeyStore();
} }
mutableKeyStore: MutableKeyStore;
trustedUsers: Array<Staff> = [];
private trustedUsersList: BehaviorSubject<Array<Staff>> = new BehaviorSubject<Array<Staff>>(
this.trustedUsers
);
trustedUsersSubject: Observable<Array<Staff>> = this.trustedUsersList.asObservable();
public static getSessionToken(): string {
return sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN'));
}
async init(): Promise<void> { async init(): Promise<void> {
await this.mutableKeyStore.loadKeyring(); await this.mutableKeyStore.loadKeyring();
@ -35,10 +38,6 @@ export class AuthService {
await this.mutableKeyStore.importPrivateKey(localStorage.getItem(btoa('CICADA_PRIVATE_KEY'))); await this.mutableKeyStore.importPrivateKey(localStorage.getItem(btoa('CICADA_PRIVATE_KEY')));
} }
} }
public static getSessionToken(): string {
return sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN'));
}
setSessionToken(token): void { setSessionToken(token): void {
sessionStorage.setItem(btoa('CICADA_SESSION_TOKEN'), token); sessionStorage.setItem(btoa('CICADA_SESSION_TOKEN'), token);
@ -49,85 +48,80 @@ export class AuthService {
} }
getWithToken(): Promise<boolean> { getWithToken(): Promise<boolean> {
const headers = { const headers = {
Authorization: 'Bearer ' + AuthService.getSessionToken, Authorization: 'Bearer ' + AuthService.getSessionToken,
'Content-Type': 'application/json;charset=utf-8', 'Content-Type': 'application/json;charset=utf-8',
'x-cic-automerge': 'none', 'x-cic-automerge': 'none',
}; };
const options = { const options = {
headers, headers,
}; };
return fetch(environment.cicMetaUrl, options).then((response) => { return fetch(environment.cicMetaUrl, options).then((response) => {
if (!response.ok) { if (!response.ok) {
this.loggingService.sendErrorLevelMessage('failed to get with auth token.', this.loggingService.sendErrorLevelMessage('failed to get with auth token.', this, {
this, error: '',
{ error: "" }); });
return false; return false;
} }
return true; return true;
}); });
} }
// TODO rename to send signed challenge and set session. Also separate these responsibilities // TODO rename to send signed challenge and set session. Also separate these responsibilities
sendSignedChallenge(hobaResponseEncoded: any): Promise<any> { sendSignedChallenge(hobaResponseEncoded: any): Promise<any> {
const headers = { const headers = {
Authorization: 'HOBA ' + hobaResponseEncoded, Authorization: 'HOBA ' + hobaResponseEncoded,
'Content-Type': 'application/json;charset=utf-8', 'Content-Type': 'application/json;charset=utf-8',
'x-cic-automerge': 'none', 'x-cic-automerge': 'none',
}; };
const options = { const options = {
headers, headers,
}; };
return fetch(environment.cicMetaUrl, options) return fetch(environment.cicMetaUrl, options);
} }
getChallenge(): Promise<any> { getChallenge(): Promise<any> {
return fetch(environment.cicMetaUrl) 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 (AuthService.getSessionToken()) { if (AuthService.getSessionToken()) {
sessionStorage.removeItem(btoa('CICADA_SESSION_TOKEN')); sessionStorage.removeItem(btoa('CICADA_SESSION_TOKEN'));
} }
const o = await this.getChallenge(); const o = await this.getChallenge();
const r = await signChallenge( const r = await signChallenge(
o.challenge, o.challenge,
o.realm, o.realm,
environment.cicMetaUrl, environment.cicMetaUrl,
this.mutableKeyStore this.mutableKeyStore
); );
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) { throw 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 if (!response.ok) {
} throw new HttpError('Unknown error from authentication server', response.status);
if (!response.ok) { }
let e = new HttpError("Unknown error from authentication server", response.status) });
throw e
}
})
if (tokenResponse) { if (tokenResponse) {
this.setSessionToken(tokenResponse); this.setSessionToken(tokenResponse);
this.setState('Click button to log in'); this.setState('Click button to log in');
return true return true;
} }
return false return false;
} }
loginView(): void { loginView(): void {

View File

@ -5,11 +5,10 @@ import { TokenRegistry } from '@app/_eth';
import { HttpGetter } from '@app/_helpers'; import { HttpGetter } from '@app/_helpers';
import { Web3Service } from '@app/_services/web3.service'; import { Web3Service } from '@app/_services/web3.service';
// export interface RegistryCollection {
//export interface RegistryCollection { // cicRegistry: CICRegistry;
// cicRegistry: CICRegistry, // tokenRegistry: TokenRegistry;
// tokenRegistry: TokenRegistry // }
//}
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
@ -17,8 +16,8 @@ import { Web3Service } from '@app/_services/web3.service';
export class RegistryService { export class RegistryService {
static fileGetter: FileGetter = new HttpGetter(); static fileGetter: FileGetter = new HttpGetter();
private static registry: CICRegistry; private static registry: CICRegistry;
private static tokenRegistry: TokenRegistry; private static tokenRegistry: TokenRegistry;
//private static registries: RegistryCollection; // private static registries: RegistryCollection;
public static async getRegistry(): Promise<CICRegistry> { public static async getRegistry(): Promise<CICRegistry> {
if (!RegistryService.registry) { if (!RegistryService.registry) {
@ -30,25 +29,25 @@ export class RegistryService {
['../../assets/js/block-sync/data'] ['../../assets/js/block-sync/data']
); );
RegistryService.registry.declaratorHelper.addTrust(environment.trustedDeclaratorAddress); RegistryService.registry.declaratorHelper.addTrust(environment.trustedDeclaratorAddress);
await RegistryService.registry.load() await RegistryService.registry.load();
} }
return RegistryService.registry; return RegistryService.registry;
} }
public static async getTokenRegistry(): Promise<TokenRegistry> { public static async getTokenRegistry(): Promise<TokenRegistry> {
if (!RegistryService.tokenRegistry) { if (!RegistryService.tokenRegistry) {
//then initial it // then initial it
const registry = await RegistryService.getRegistry() const registry = await RegistryService.getRegistry();
const tokenRegistryAddress = await RegistryService.registry.getContractAddressByName('TokenRegistry') const tokenRegistryAddress = await RegistryService.registry.getContractAddressByName(
RegistryService.tokenRegistry = new TokenRegistry(tokenRegistryAddress); 'TokenRegistry'
return new Promise((resolve, reject) => { );
resolve(RegistryService.tokenRegistry) RegistryService.tokenRegistry = new TokenRegistry(tokenRegistryAddress);
})
}
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
resolve(RegistryService.tokenRegistry); resolve(RegistryService.tokenRegistry);
}) });
}
return new Promise((resolve, reject) => {
resolve(RegistryService.tokenRegistry);
});
} }
} }

View File

@ -4,7 +4,7 @@ import { TokenRegistry } from '@app/_eth';
import { HttpClient } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { RegistryService } from '@app/_services/registry.service'; import { RegistryService } from '@app/_services/registry.service';
import { Token } from '@app/_models'; import { Token } from '@app/_models';
import {BehaviorSubject, Observable, Subject} from 'rxjs'; import { BehaviorSubject, Observable, Subject } from 'rxjs';
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
@ -14,7 +14,9 @@ export class TokenService {
tokenRegistry: TokenRegistry; tokenRegistry: TokenRegistry;
onload: (status: boolean) => void; onload: (status: boolean) => void;
tokens: Array<Token> = []; tokens: Array<Token> = [];
private tokensList: BehaviorSubject<Array<Token>> = new BehaviorSubject<Array<Token>>(this.tokens); private tokensList: BehaviorSubject<Array<Token>> = new BehaviorSubject<Array<Token>>(
this.tokens
);
tokensSubject: Observable<Array<Token>> = this.tokensList.asObservable(); tokensSubject: Observable<Array<Token>> = this.tokensList.asObservable();
constructor(private httpClient: HttpClient) {} constructor(private httpClient: HttpClient) {}
@ -22,12 +24,12 @@ export class TokenService {
async init(): Promise<void> { async init(): Promise<void> {
this.registry = await RegistryService.getRegistry(); this.registry = await RegistryService.getRegistry();
this.tokenRegistry = await RegistryService.getTokenRegistry(); this.tokenRegistry = await RegistryService.getTokenRegistry();
//this.registry.onload = async (address: string): Promise<void> => { // this.registry.onload = async (address: string): Promise<void> => {
// this.tokenRegistry = new TokenRegistry( // this.tokenRegistry = new TokenRegistry(
// await this.registry.getContractAddressByName('TokenRegistry') // await this.registry.getContractAddressByName('TokenRegistry')
// ); // );
// this.onload(this.tokenRegistry !== undefined); // this.onload(this.tokenRegistry !== undefined);
//}; // };
} }
addToken(token: Token): void { addToken(token: Token): void {

View File

@ -22,7 +22,7 @@ export class AuthComponent implements OnInit {
private authService: AuthService, private authService: AuthService,
private formBuilder: FormBuilder, private formBuilder: FormBuilder,
private router: Router, private router: Router,
private errorDialogService: ErrorDialogService, private errorDialogService: ErrorDialogService
) {} ) {}
async ngOnInit(): Promise<void> { async ngOnInit(): Promise<void> {
@ -49,10 +49,10 @@ export class AuthComponent implements OnInit {
async login(): Promise<void> { async login(): Promise<void> {
try { try {
const loginResult = await this.authService.login() const loginResult = await this.authService.login();
if (loginResult) { if (loginResult) {
this.router.navigate(['/home']); this.router.navigate(['/home']);
} }
} catch (HttpError) { } catch (HttpError) {
this.errorDialogService.openDialog({ this.errorDialogService.openDialog({
message: HttpError.message, message: HttpError.message,