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() {}
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
//const token: string = sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN'));
const token: string = AuthService.getSessionToken()
// const token: string = sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN'));
const token: string = AuthService.getSessionToken();
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);

View File

@ -14,13 +14,6 @@ import { BehaviorSubject, Observable } from 'rxjs';
providedIn: 'root',
})
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(
private httpClient: HttpClient,
private loggingService: LoggingService,
@ -28,6 +21,16 @@ export class AuthService {
) {
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> {
await this.mutableKeyStore.loadKeyring();
@ -35,10 +38,6 @@ export class AuthService {
await this.mutableKeyStore.importPrivateKey(localStorage.getItem(btoa('CICADA_PRIVATE_KEY')));
}
}
public static getSessionToken(): string {
return sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN'));
}
setSessionToken(token): void {
sessionStorage.setItem(btoa('CICADA_SESSION_TOKEN'), token);
@ -49,85 +48,80 @@ export class AuthService {
}
getWithToken(): Promise<boolean> {
const headers = {
Authorization: 'Bearer ' + AuthService.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: "" });
const headers = {
Authorization: 'Bearer ' + AuthService.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;
});
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)
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 => {
if (response.status === 401) {
const authHeader: string = response.headers.get('WWW-Authenticate');
return hobaParseChallengeHeader(authHeader);
}
});
return fetch(environment.cicMetaUrl).then((response) => {
if (response.status === 401) {
const authHeader: string = response.headers.get('WWW-Authenticate');
return hobaParseChallengeHeader(authHeader);
}
});
}
async login(): Promise<boolean> {
if (AuthService.getSessionToken()) {
sessionStorage.removeItem(btoa('CICADA_SESSION_TOKEN'));
}
sessionStorage.removeItem(btoa('CICADA_SESSION_TOKEN'));
}
const o = await this.getChallenge();
const r = await signChallenge(
o.challenge,
o.realm,
environment.cicMetaUrl,
this.mutableKeyStore
o.challenge,
o.realm,
environment.cicMetaUrl,
this.mutableKeyStore
);
const tokenResponse = await this.sendSignedChallenge(r)
.then(response => {
const token = response.headers.get('Token')
if (token) {
return token
}
if (response.status === 401) {
let e = new HttpError("You are not authorized to use this system", response.status)
throw e
}
if (!response.ok) {
let e = new HttpError("Unknown error from authentication server", response.status)
throw e
}
})
const tokenResponse = await this.sendSignedChallenge(r).then((response) => {
const token = response.headers.get('Token');
if (token) {
return token;
}
if (response.status === 401) {
throw new HttpError('You are not authorized to use this system', response.status);
}
if (!response.ok) {
throw new HttpError('Unknown error from authentication server', response.status);
}
});
if (tokenResponse) {
this.setSessionToken(tokenResponse);
this.setState('Click button to log in');
return true
this.setSessionToken(tokenResponse);
this.setState('Click button to log in');
return true;
}
return false
return false;
}
loginView(): void {

View File

@ -5,11 +5,10 @@ import { TokenRegistry } from '@app/_eth';
import { HttpGetter } from '@app/_helpers';
import { Web3Service } from '@app/_services/web3.service';
//export interface RegistryCollection {
// cicRegistry: CICRegistry,
// tokenRegistry: TokenRegistry
//}
// export interface RegistryCollection {
// cicRegistry: CICRegistry;
// tokenRegistry: TokenRegistry;
// }
@Injectable({
providedIn: 'root',
@ -17,8 +16,8 @@ import { Web3Service } from '@app/_services/web3.service';
export class RegistryService {
static fileGetter: FileGetter = new HttpGetter();
private static registry: CICRegistry;
private static tokenRegistry: TokenRegistry;
//private static registries: RegistryCollection;
private static tokenRegistry: TokenRegistry;
// private static registries: RegistryCollection;
public static async getRegistry(): Promise<CICRegistry> {
if (!RegistryService.registry) {
@ -30,25 +29,25 @@ export class RegistryService {
['../../assets/js/block-sync/data']
);
RegistryService.registry.declaratorHelper.addTrust(environment.trustedDeclaratorAddress);
await RegistryService.registry.load()
await RegistryService.registry.load();
}
return RegistryService.registry;
}
public static async getTokenRegistry(): Promise<TokenRegistry> {
if (!RegistryService.tokenRegistry) {
//then initial it
const registry = await RegistryService.getRegistry()
const tokenRegistryAddress = await RegistryService.registry.getContractAddressByName('TokenRegistry')
RegistryService.tokenRegistry = new TokenRegistry(tokenRegistryAddress);
return new Promise((resolve, reject) => {
resolve(RegistryService.tokenRegistry)
})
}
if (!RegistryService.tokenRegistry) {
// then initial it
const registry = await RegistryService.getRegistry();
const tokenRegistryAddress = await RegistryService.registry.getContractAddressByName(
'TokenRegistry'
);
RegistryService.tokenRegistry = new TokenRegistry(tokenRegistryAddress);
return new Promise((resolve, reject) => {
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 { RegistryService } from '@app/_services/registry.service';
import { Token } from '@app/_models';
import {BehaviorSubject, Observable, Subject} from 'rxjs';
import { BehaviorSubject, Observable, Subject } from 'rxjs';
@Injectable({
providedIn: 'root',
@ -14,7 +14,9 @@ export class TokenService {
tokenRegistry: TokenRegistry;
onload: (status: boolean) => void;
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();
constructor(private httpClient: HttpClient) {}
@ -22,12 +24,12 @@ export class TokenService {
async init(): Promise<void> {
this.registry = await RegistryService.getRegistry();
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(
// await this.registry.getContractAddressByName('TokenRegistry')
// );
// this.onload(this.tokenRegistry !== undefined);
//};
// };
}
addToken(token: Token): void {

View File

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