Skip to content

Commit

Permalink
Merge pull request #334 from energywebfoundation/bug/ICL-145_Forbidde…
Browse files Browse the repository at this point in the history
…n_assets_endpoints

bug/ access token is not refreshed
  • Loading branch information
JGiter committed Nov 23, 2021
2 parents 4d96709 + 8e4d1c7 commit 552b5bb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 11 additions & 8 deletions src/modules/cacheClient/cacheClient.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export class CacheClient implements ICacheClient {
private authEnabled: boolean;
private isBrowser: boolean;
private refresh_token: string | undefined;
private token: string | undefined;

constructor(private _signerService: SignerService) {
this._signerService.onInit(this.init.bind(this));
Expand All @@ -39,9 +38,6 @@ export class CacheClient implements ICacheClient {
}, this.handleError.bind(this));
this.authEnabled = cacheServerSupportsAuth;
this.isBrowser = executionEnvironment() === ExecutionEnvironment.BROWSER;
if (!this.isBrowser) {
this.httpClient.defaults.headers.common["Authorization"] = `Bearer ${this.token}`;
}
}

isAuthEnabled() {
Expand All @@ -55,9 +51,11 @@ export class CacheClient implements ICacheClient {
async authenticate() {
try {
const { refreshToken, token } = await this.refreshToken();
if (!this.isBrowser) {
this.httpClient.defaults.headers.common["Authorization"] = `Bearer ${token}`;
}
if (await this.isAuthenticated()) {
this.refresh_token = refreshToken;
this.token = token;
return;
}
} catch {}
Expand All @@ -68,8 +66,10 @@ export class CacheClient implements ICacheClient {
} = await this.httpClient.post<{ token: string; refreshToken: string }>("/login", {
identityToken: pubKeyAndIdentityToken.identityToken,
});
if (!this.isBrowser) {
this.httpClient.defaults.headers.common["Authorization"] = `Bearer ${token}`;
}
this.refresh_token = refreshToken;
this.token = token;
this.pubKeyAndIdentityToken = pubKeyAndIdentityToken;

this.failedRequests = this.failedRequests.filter((callback) => callback());
Expand Down Expand Up @@ -300,12 +300,15 @@ export class CacheClient implements ICacheClient {
}

/**
* @description Checks that auth token has been created, has not expired and corresponds to signer
* @description Checks that auth token has been created, has not expired and corresponds to signer.
* This is done by a request to the server because the auth token is stored in an HTTP-only cookie and
* so the Javascript has no way to check its validity
*
* @todo specific endpoint on cache server to return login info instead of error
*/
private async isAuthenticated(): Promise<boolean> {
try {
await this.getOwnedAssets(this._signerService.did);
await this.httpClient.get<Asset[]>(`${TEST_LOGIN_ENDPOINT}${this._signerService.did}`);
return true;
} catch (_) {
return false;
Expand Down

0 comments on commit 552b5bb

Please sign in to comment.