Skip to content

Commit

Permalink
Added error condition for when a refresh token isn't stored or no cac…
Browse files Browse the repository at this point in the history
…he exists
  • Loading branch information
Steve Hobbs committed Dec 20, 2019
1 parent ecc8bec commit 04a3743
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 13 deletions.
20 changes: 20 additions & 0 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,26 @@ describe('Auth0', () => {
}
});
});

it('fails with an error when no refresh token is available in the cache', async () => {
const { auth0, cache, utils } = await setup({
useRefreshTokens: true
});

utils.getUniqueScopes.mockReturnValue(
`${TEST_SCOPES} offline_access`
);

cache.get.mockReturnValue({ access_token: TEST_ACCESS_TOKEN });

await auth0.getTokenSilently({ ignoreCache: true }).catch(e => {
expect(e.error).toBe('missing_refresh_token');
expect(e.error_description).toBe(
'No refresh token is available to fetch a new access token. The user should be reauthenticated.'
);
expect(utils.oauthToken).not.toHaveBeenCalled();
});
});
});
});

Expand Down
13 changes: 8 additions & 5 deletions src/Auth0Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
import { InMemoryCache, ICache, LocalStorageCache } from './cache';
import TransactionManager from './transaction-manager';
import { verify as verifyIdToken } from './jwt';
import { AuthenticationError } from './errors';
import { AuthenticationError, GenericError } from './errors';
import * as ClientStorage from './storage';
import { DEFAULT_POPUP_CONFIG_OPTIONS } from './constants';
import version from './version';
Expand Down Expand Up @@ -571,19 +571,22 @@ export default class Auth0Client {
client_id: this.options.client_id
});

if (!cache || !cache.refresh_token) {
throw new GenericError(
'missing_refresh_token',
'No refresh token is available to fetch a new access token. The user should be reauthenticated.'
);
}

const tokenResult = await oauthToken({
baseUrl: this.domainUrl,
client_id: this.options.client_id,
grant_type: 'refresh_token',
refresh_token: cache.refresh_token
} as RefreshTokenOptions);

console.log(tokenResult);

const decodedToken = this._verifyIdToken(tokenResult.id_token);

console.log(decodedToken);

return {
...tokenResult,
decodedToken,
Expand Down
15 changes: 9 additions & 6 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
export class AuthenticationError extends Error {
constructor(
public error: string,
public error_description: string,
public state: string
) {
export class GenericError extends Error {
constructor(public error: string, public error_description: string) {
super(error_description);

Object.setPrototypeOf(this, GenericError.prototype);
}
}
export class AuthenticationError extends GenericError {
constructor(error: string, error_description: string, public state: string) {
super(error, error_description);
//https://github.com/Microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work
Object.setPrototypeOf(this, AuthenticationError.prototype);
}
Expand Down
13 changes: 11 additions & 2 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ <h1 class="mb-5">Auth0 SPA JS Playground</h1>
</button>
</div>

<template v-if="error">
<hr />
<h3>Last error</h3>
<pre><code>{{JSON.stringify(error, null, 2)}}</code></pre>
</template>

<hr />

<div class="card mb-3" v-if="profile">
Expand Down Expand Up @@ -187,7 +193,8 @@ <h1 class="mb-5">Auth0 SPA JS Playground</h1>
id_token: '',
isAuthenticated: false,
domain: data.domain || defaultDomain,
clientId: data.clientId || defaultClientId
clientId: data.clientId || defaultClientId,
error: null
};
},
created() {
Expand Down Expand Up @@ -298,7 +305,9 @@ <h1 class="mb-5">Auth0 SPA JS Playground</h1>
.getTokenSilently({ ignoreCache: !_self.useCache })
.then(function(token) {
_self.access_tokens.push(token);
});
_self.error = null;
})
.catch(e => (_self.error = e));
},
getMultipleTokens() {
var _self = this;
Expand Down

0 comments on commit 04a3743

Please sign in to comment.