Merge branch 'revert-a93be027' into 'master'

Revert "Merge branch 'bvander/loading-registries' into 'master'"

See merge request grassrootseconomics/cic-staff-client!32
This commit is contained in:
Blair Vanderlugt 2021-06-12 16:22:10 +00:00
commit 71c56392e9
12 changed files with 837 additions and 902 deletions

1469
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -33,10 +33,10 @@
"@angular/platform-browser-dynamic": "~10.2.0",
"@angular/router": "~10.2.0",
"@angular/service-worker": "~10.2.0",
"@cicnet/cic-client": "^0.1.6",
"@cicnet/schemas-data-validator": "*",
"@popperjs/core": "^2.5.4",
"bootstrap": "^4.5.3",
"cic-client": "0.1.4",
"cic-client-meta": "0.0.7-alpha.6",
"ethers": "^5.0.31",
"http-server": "^0.12.3",

View File

@ -1,4 +1,3 @@
import { AuthService } from '@app/_services';
import { Injectable } from '@angular/core';
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';
import { Observable } from 'rxjs';
@ -9,11 +8,10 @@ export class HttpConfigInterceptor implements HttpInterceptor {
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
// 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) });
}
// if (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 {
constructor(
private httpClient: HttpClient,
private loggingService: LoggingService,
private errorDialogService: ErrorDialogService
) {
this.mutableKeyStore = new MutablePgpKeyStore();
}
mutableKeyStore: MutableKeyStore;
trustedUsers: Array<Staff> = [];
private trustedUsersList: BehaviorSubject<Array<Staff>> = new BehaviorSubject<Array<Staff>>(
@ -28,8 +21,12 @@ export class AuthService {
);
trustedUsersSubject: Observable<Array<Staff>> = this.trustedUsersList.asObservable();
public static getSessionToken(): string {
return sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN'));
constructor(
private httpClient: HttpClient,
private loggingService: LoggingService,
private errorDialogService: ErrorDialogService
) {
this.mutableKeyStore = new MutablePgpKeyStore();
}
async init(): Promise<void> {
@ -39,6 +36,10 @@ export class AuthService {
}
}
getSessionToken(): string {
return sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN'));
}
setSessionToken(token): void {
sessionStorage.setItem(btoa('CICADA_SESSION_TOKEN'), token);
}
@ -48,80 +49,85 @@ 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 ' + 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;
});
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'));
if (this.getSessionToken()) {
sessionStorage.removeItem(btoa('CICADA_SESSION_TOKEN'));
} else {
const o = await this.getChallenge();
const r = await signChallenge(
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
}
})
if (tokenResponse) {
this.setSessionToken(tokenResponse);
this.setState('Click button to log in');
return true
}
return false
}
const o = await this.getChallenge();
const r = await signChallenge(
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) {
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;
}
return false;
}
loginView(): void {

View File

@ -1,6 +1,6 @@
import { Injectable } from '@angular/core';
import { Settings } from '@app/_models';
import { TransactionHelper } from '@cicnet/cic-client';
import { TransactionHelper } from 'cic-client';
import { first } from 'rxjs/operators';
import { TransactionService } from '@app/_services/transaction.service';
import { environment } from '@src/environments/environment';

View File

@ -1,10 +1,8 @@
import { Injectable } from '@angular/core';
import { environment } from '@src/environments/environment';
import { CICRegistry, FileGetter } from '@cicnet/cic-client';
import { AccountIndex, TokenRegistry } from '@app/_eth';
import { CICRegistry, FileGetter } from 'cic-client';
import { HttpGetter } from '@app/_helpers';
import { Web3Service } from '@app/_services/web3.service';
import { BehaviorSubject } from 'rxjs';
@Injectable({
providedIn: 'root',
@ -12,9 +10,8 @@ import { BehaviorSubject } from 'rxjs';
export class RegistryService {
static fileGetter: FileGetter = new HttpGetter();
private static registry: CICRegistry;
private static tokenRegistry: TokenRegistry;
private static accountRegistry: AccountIndex;
private static load: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
constructor() {}
public static async getRegistry(): Promise<CICRegistry> {
if (!RegistryService.registry) {
@ -27,43 +24,7 @@ export class RegistryService {
);
RegistryService.registry.declaratorHelper.addTrust(environment.trustedDeclaratorAddress);
await RegistryService.registry.load();
RegistryService.load.next(true);
return RegistryService.registry;
}
return RegistryService.registry;
}
public static async getTokenRegistry(): Promise<TokenRegistry> {
if (!RegistryService.tokenRegistry) {
// then initial it
const registry = await RegistryService.getRegistry();
RegistryService.load.subscribe(async (status: boolean) => {
if (status) {
const tokenRegistryAddress = await registry.getContractAddressByName('TokenRegistry');
RegistryService.tokenRegistry = new TokenRegistry(tokenRegistryAddress);
return RegistryService.tokenRegistry;
}
});
// return new Promise((resolve, reject) => {
// resolve(RegistryService.tokenRegistry);
// });
}
return RegistryService.tokenRegistry;
}
public static async getAccountRegistry(): Promise<AccountIndex> {
if (!RegistryService.accountRegistry) {
const registry = await RegistryService.getRegistry();
RegistryService.load.subscribe(async (status: boolean) => {
if (status) {
const accountIndexAddress: string = await registry.getContractAddressByName(
'AccountRegistry'
);
RegistryService.accountRegistry = new AccountIndex(accountIndexAddress);
return RegistryService.accountRegistry;
}
});
}
return RegistryService.accountRegistry;
}
}

View File

@ -1,9 +1,10 @@
import { Injectable } from '@angular/core';
import { CICRegistry } from '@cicnet/cic-client';
import { CICRegistry } from 'cic-client';
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',
@ -11,19 +12,21 @@ import { BehaviorSubject, Observable, Subject } from 'rxjs';
export class TokenService {
registry: CICRegistry;
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();
load: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
constructor() {}
constructor(private httpClient: HttpClient) {}
async init(): Promise<void> {
this.registry = await RegistryService.getRegistry();
this.tokenRegistry = await RegistryService.getTokenRegistry();
this.load.next(true);
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

@ -14,7 +14,7 @@ import { AuthService } from '@app/_services/auth.service';
import { defaultAccount } from '@app/_models';
import { LoggingService } from '@app/_services/logging.service';
import { HttpClient } from '@angular/common/http';
import { CICRegistry } from '@cicnet/cic-client';
import { CICRegistry } from 'cic-client';
import { RegistryService } from '@app/_services/registry.service';
import Web3 from 'web3';
import { Web3Service } from '@app/_services/web3.service';

View File

@ -7,9 +7,10 @@ import { ArgPair, Envelope, Phone, Syncable, User } from 'cic-client-meta';
import { AccountDetails } from '@app/_models';
import { LoggingService } from '@app/_services/logging.service';
import { TokenService } from '@app/_services/token.service';
import { AccountIndex } from '@app/_eth';
import { MutableKeyStore, PGPSigner, Signer } from '@app/_pgp';
import { RegistryService } from '@app/_services/registry.service';
import { CICRegistry } from '@cicnet/cic-client';
import { CICRegistry } from 'cic-client';
import { AuthService } from '@app/_services/auth.service';
import { personValidation, vcardValidation } from '@app/_helpers';
import { add0x } from '@src/assets/js/ethtx/dist/hex';
@ -42,7 +43,7 @@ export class UserService {
) {}
async init(): Promise<void> {
// await this.authService.init();
await this.authService.init();
await this.tokenService.init();
this.keystore = this.authService.mutableKeyStore;
this.signer = new PGPSigner(this.keystore);
@ -178,8 +179,11 @@ export class UserService {
async loadAccounts(limit: number = 100, offset: number = 0): Promise<void> {
this.resetAccountsList();
const accountRegistry = await RegistryService.getAccountRegistry();
const accountAddresses: Array<string> = await accountRegistry.last(limit);
const accountIndexAddress: string = await this.registry.getContractAddressByName(
'AccountRegistry'
);
const accountIndexQuery = new AccountIndex(accountIndexAddress);
const accountAddresses: Array<string> = await accountIndexQuery.last(limit);
this.loggingService.sendInfoLevelMessage(accountAddresses);
for (const accountAddress of accountAddresses.slice(offset, offset + limit)) {
await this.getAccountByAddress(accountAddress, limit);
@ -197,13 +201,11 @@ export class UserService {
const account: Syncable = Envelope.fromJSON(JSON.stringify(res)).unwrap();
const accountInfo = account.m.data;
await personValidation(accountInfo);
this.tokenService.load.subscribe(async (status: boolean) => {
if (status) {
accountInfo.balance = await this.tokenService.getTokenBalance(
accountInfo.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0]
);
}
});
this.tokenService.onload = async (status: boolean): Promise<void> => {
accountInfo.balance = await this.tokenService.getTokenBalance(
accountInfo.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0]
);
};
accountInfo.vcard = vCard.parse(atob(accountInfo.vcard));
await vcardValidation(accountInfo.vcard);
this.addAccount(accountInfo, limit);

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,

View File

@ -29,11 +29,9 @@ export class TokensComponent implements OnInit {
async ngOnInit(): Promise<void> {
await this.tokenService.init();
this.tokenService.load.subscribe(async (status: boolean) => {
if (status) {
await this.tokenService.getTokens();
}
});
this.tokenService.onload = async (status: boolean): Promise<void> => {
await this.tokenService.getTokens();
};
this.tokenService.tokensSubject.subscribe((tokens) => {
this.loggingService.sendInfoLevelMessage(tokens);
this.dataSource = new MatTableDataSource(tokens);

View File

@ -6,7 +6,7 @@ export const environment = {
logLevel: NgxLoggerLevel.ERROR,
serverLogLevel: NgxLoggerLevel.OFF,
loggingUrl: 'http://localhost:8000',
cicMetaUrl: 'https://meta-auth.dev.grassrootseconomics.net',
cicMetaUrl: 'https://meta.dev.grassrootseconomics.net',
publicKeysUrl: 'https://dev.grassrootseconomics.net/.well-known/publickeys/',
cicCacheUrl: 'https://cache.dev.grassrootseconomics.net',
web3Provider: 'wss://bloxberg-ws.dev.grassrootseconomics.net',