Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to store the refresh token #8

Merged
merged 2 commits into from
Sep 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ export default useAuth0({
// Store the id_token in the session. Defaults to false.
storeIdToken: false,
// Store the access_token in the session. Defaults to false.
storeAccessToken: false
storeAccessToken: false,
// Store the refresh_token in the session. Defaults to false.
storeRefreshToken: false
},
httpClient: {
// Optionally configure the timeout for the HTTP client.
Expand Down
1 change: 1 addition & 0 deletions src/handlers/callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export default function callbackHandler(
},
idToken: tokenSet.id_token,
accessToken: tokenSet.access_token,
refreshToken: tokenSet.refresh_token,
createdAt: Date.now()
};

Expand Down
6 changes: 5 additions & 1 deletion src/session/cookie-store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default class CookieSessionStore implements ISessionStore {
async save(_: IncomingMessage, res: ServerResponse, session: ISession): Promise<void> {
const { cookieSecret, cookieName, cookiePath, cookieLifetime } = this.settings;

const { idToken, accessToken, user, createdAt } = session;
const { idToken, accessToken, refreshToken, user, createdAt } = session;
const persistedSession = new Session(user, createdAt);

if (this.settings.storeIdToken && idToken) {
Expand All @@ -52,6 +52,10 @@ export default class CookieSessionStore implements ISessionStore {
persistedSession.accessToken = accessToken;
}

if (this.settings.storeRefreshToken && refreshToken) {
persistedSession.refreshToken = refreshToken;
}

const encryptedSession = await Iron.seal(persistedSession, cookieSecret, Iron.defaults);
setCookie(res, {
name: cookieName,
Expand Down
9 changes: 9 additions & 0 deletions src/session/cookie-store/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ export interface ICookieSessionStoreSettings {
* Defaults to 'false'
*/
storeAccessToken?: boolean;

/**
* Save the refresh_token in the cookie.
* Defaults to 'false'
*/
storeRefreshToken?: boolean;
}

export default class CookieSessionStoreSettings {
Expand All @@ -48,6 +54,8 @@ export default class CookieSessionStoreSettings {

readonly storeAccessToken: boolean;

readonly storeRefreshToken: boolean;

constructor(settings: ICookieSessionStoreSettings) {
this.cookieSecret = settings.cookieSecret;
if (!this.cookieSecret || !this.cookieSecret.length) {
Expand All @@ -72,5 +80,6 @@ export default class CookieSessionStoreSettings {

this.storeIdToken = settings.storeIdToken || false;
this.storeAccessToken = settings.storeAccessToken || false;
this.storeRefreshToken = settings.storeRefreshToken || false;
}
}
7 changes: 7 additions & 0 deletions src/session/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ export interface ISession {
*/
readonly accessToken?: string | undefined;

/**
* The refresh token.
*/
readonly refreshToken?: string | undefined;

/**
* The time on which the session was created.
*/
Expand All @@ -34,6 +39,8 @@ export default class Session implements ISession {

accessToken?: string | undefined;

refreshToken?: string | undefined;

createdAt: number;

constructor(user: IClaims, createdAt?: number) {
Expand Down
1 change: 1 addition & 0 deletions tests/handlers/profile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ describe('profile handler', () => {
},
idToken: 'my-id-token',
accessToken: 'my-access-token',
refreshToken: 'my-refresh-token',
createdAt: Date.now()
});

Expand Down
6 changes: 4 additions & 2 deletions tests/handlers/session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ describe('session handler', () => {
sub: '123'
},
createdAt: now,
idToken: 'my-id-token'
idToken: 'my-id-token',
refreshToken: 'my-refresh-token'
});
},
save(): Promise<void> {
Expand All @@ -28,7 +29,8 @@ describe('session handler', () => {
expect(session).toEqual({
user: { sub: '123' },
createdAt: now,
idToken: 'my-id-token'
idToken: 'my-id-token',
refreshToken: 'my-refresh-token'
});
});
});
67 changes: 65 additions & 2 deletions tests/session/cookie-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ describe('cookie store', () => {
});

describe('with storeIdToken', () => {
describe('configured', () => {
describe('not configured', () => {
const store = getStore({});

test('should not store the id_token', async () => {
Expand Down Expand Up @@ -150,7 +150,7 @@ describe('cookie store', () => {
});
});

describe('not configured', () => {
describe('configured', () => {
const store = getStore({
storeIdToken: true
});
Expand Down Expand Up @@ -181,4 +181,67 @@ describe('cookie store', () => {
});
});
});

describe('with storeRefreshToken', () => {
describe('not configured', () => {
const store = getStore({});

test('should not store the refresh_token', async () => {
const { req, res, setHeaderFn } = getRequestResponse();
const now = Date.now();

await store.save(req, res, {
user: {
sub: '123'
},
createdAt: now,
idToken: 'my-id-token',
refreshToken: 'my-refresh-token'
});

const [, cookie] = setHeaderFn.mock.calls[0];
req.headers = {
cookie: `a0:session=${parse(cookie)['a0:session']}`
};

const session = await store.read(req);
expect(session).toEqual({
createdAt: now,
user: { sub: '123' }
});
});
});

describe('configured', () => {
const store = getStore({
storeRefreshToken: true
});

test('should store the refresh_token', async () => {
const { req, res, setHeaderFn } = getRequestResponse();
const now = Date.now();

await store.save(req, res, {
user: {
sub: '123'
},
createdAt: now,
idToken: 'my-id-token',
refreshToken: 'my-refresh-token'
});

const [, cookie] = setHeaderFn.mock.calls[0];
req.headers = {
cookie: `a0:session=${parse(cookie)['a0:session']}`
};

const session = await store.read(req);
expect(session).toEqual({
createdAt: now,
refreshToken: 'my-refresh-token',
user: { sub: '123' }
});
});
});
});
});