Skip to content

Commit

Permalink
Merge pull request #117 from energywebfoundation/task/ICS-62-fix-cach…
Browse files Browse the repository at this point in the history
…e-server-logging

fix(cacheServer): fix re-logging into cache server
  • Loading branch information
Harasz authored Sep 27, 2021
2 parents 5639fc7 + c8ebca2 commit da3fee9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/LoginStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export class LoginStrategy extends BaseStrategy {
private readonly acceptedRoles: Set<string>
private readonly privateKey: string
private readonly cacheServerClient?: CacheServerClient
private isCacheServerClientAvailable: boolean

constructor(
{
Expand Down Expand Up @@ -83,14 +82,13 @@ export class LoginStrategy extends BaseStrategy {
'You need to provide privateKey of an accepted account to login to cache server'
)
}
this.isCacheServerClientAvailable = false;
if (cacheServerUrl && privateKey) {
this.cacheServerClient = new CacheServerClient({
privateKey,
provider: this.provider,
url: cacheServerUrl,
});
this.cacheServerClient.login().then(() => this.isCacheServerClientAvailable = true);
this.cacheServerClient.login()
}
const registrySetting = {
abi: abi1056,
Expand Down Expand Up @@ -119,7 +117,9 @@ export class LoginStrategy extends BaseStrategy {
payload: ITokenPayload,
done: (err?: Error, user?: any, info?: any) => void
): Promise<void> {
const didDocument = this.isCacheServerClientAvailable && this.cacheServerClient ? await this.cacheServerClient.getDidDocument(payload.iss) : await this.didResolver.read(payload.iss)
const didDocument = this.cacheServerClient?.isAvailable
? await this.cacheServerClient.getDidDocument(payload.iss)
: await this.didResolver.read(payload.iss)
const authenticationClaimVerifier = new AuthTokenVerifier(this.privateKey, didDocument)
const did = await authenticationClaimVerifier.verify(token, payload.iss)

Expand Down Expand Up @@ -215,7 +215,7 @@ export class LoginStrategy extends BaseStrategy {
}

async getRoleDefinition(namespace: string) : Promise<any> {
if (this.cacheServerClient && this.isCacheServerClientAvailable) {
if (this.cacheServerClient?.isAvailable) {
return this.cacheServerClient.getRoleDefinition({ namespace })
}
const namespaceHash = namehash(namespace)
Expand All @@ -225,7 +225,7 @@ export class LoginStrategy extends BaseStrategy {
}

async getUserClaims(did: string): Promise<Claim[]> {
if (this.cacheServerClient && this.isCacheServerClientAvailable) {
if (this.cacheServerClient?.isAvailable) {
return this.cacheServerClient.getUserClaims({ did })
}
const didDocument = await this.didResolver.read(did)
Expand Down
8 changes: 8 additions & 0 deletions lib/cacheServerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ export class CacheServerClient {
private readonly provider: providers.JsonRpcProvider
private failedRequests: Array<(token: string) => void> = []
private isAlreadyFetchingAccessToken = false
private _isAvailable = false;

public readonly address: string
public get isAvailable(): boolean {
return this._isAvailable;
}

constructor({
url,
privateKey,
Expand Down Expand Up @@ -77,6 +83,7 @@ export class CacheServerClient {
this.httpClient.defaults.headers.common[
'Authorization'
] = `Bearer ${data.token}`
this._isAvailable = true

return data.token
})
Expand All @@ -103,6 +110,7 @@ export class CacheServerClient {
config.url?.indexOf('/login') === -1
) {
try {
this._isAvailable = false
const retryOriginalRequest = new Promise((resolve) => {
this.addFailedRequest((token) => {
originalRequest.headers.Authorization = 'Bearer ' + token
Expand Down

0 comments on commit da3fee9

Please sign in to comment.