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

FEAT override cookie domain option #885

Merged
merged 4 commits into from
Mar 25, 2022
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
10 changes: 6 additions & 4 deletions __tests__/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ describe('CookieStorage', () => {
it('saves a cookie with options', () => {
const key = 'key';
const value = { some: 'value' };
const options = { daysUntilExpire: 1 };
const options = { daysUntilExpire: 1, cookieDomain: '.example.com' };

CookieStorage.save(key, value, options);

expect(cookieMock.set).toHaveBeenCalledWith(key, JSON.stringify(value), {
expires: options.daysUntilExpire
expires: options.daysUntilExpire,
domain: options.cookieDomain
});
});

Expand Down Expand Up @@ -90,12 +91,13 @@ describe('CookieStorageWithLegacySameSite', () => {
it('saves object', () => {
const key = 'key';
const value = { some: 'value' };
const options = { daysUntilExpire: 1 };
const options = { daysUntilExpire: 1, cookieDomain: '.example.com' };

CookieStorageWithLegacySameSite.save(key, value, options);

expect(cookieMock.set).toHaveBeenCalledWith(key, JSON.stringify(value), {
expires: options.daysUntilExpire
expires: options.daysUntilExpire,
domain: options.cookieDomain
});

expect(cookieMock.set).toHaveBeenCalledWith(
Expand Down
26 changes: 15 additions & 11 deletions src/Auth0Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,8 @@ export default class Auth0Client {
await this.cacheManager.set(cacheEntry);

this.cookieStorage.save(this.isAuthenticatedCookieName, true, {
daysUntilExpire: this.sessionCheckExpiryDays
daysUntilExpire: this.sessionCheckExpiryDays,
cookieDomain: this.options.cookieDomain
});

this._processOrgIdHint(decodedToken.claims.org_id);
Expand Down Expand Up @@ -719,7 +720,8 @@ export default class Auth0Client {
});

this.cookieStorage.save(this.isAuthenticatedCookieName, true, {
daysUntilExpire: this.sessionCheckExpiryDays
daysUntilExpire: this.sessionCheckExpiryDays,
cookieDomain: this.options.cookieDomain
});

this._processOrgIdHint(decodedToken.claims.org_id);
Expand Down Expand Up @@ -761,7 +763,8 @@ export default class Auth0Client {
} else {
// Migrate the existing cookie to the new name scoped by client ID
this.cookieStorage.save(this.isAuthenticatedCookieName, true, {
daysUntilExpire: this.sessionCheckExpiryDays
daysUntilExpire: this.sessionCheckExpiryDays,
cookieDomain: this.options.cookieDomain
});

this.cookieStorage.remove(OLD_IS_AUTHENTICATED_COOKIE_NAME);
Expand Down Expand Up @@ -803,15 +806,15 @@ export default class Auth0Client {
* ```
*
* If there's a valid token stored and it has more than 60 seconds
* remaining before expiration, return the token. Otherwise, attempt
* to obtain a new token.
* remaining before expiration, return the token. Otherwise, attempt
* to obtain a new token.
*
* A new token will be obtained either by opening an iframe or a
* A new token will be obtained either by opening an iframe or a
* refresh token (if `useRefreshTokens` is `true`)
* If iframes are used, opens an iframe with the `/authorize` URL
* using the parameters provided as arguments. Random and secure `state`
* and `nonce` parameters will be auto-generated. If the response is successful,

* If iframes are used, opens an iframe with the `/authorize` URL
* using the parameters provided as arguments. Random and secure `state`
* and `nonce` parameters will be auto-generated. If the response is successful,
* results will be validated according to their expiration times.
*
* If refresh tokens are used, the token endpoint is called directly with the
Expand Down Expand Up @@ -901,7 +904,8 @@ export default class Auth0Client {
});

this.cookieStorage.save(this.isAuthenticatedCookieName, true, {
daysUntilExpire: this.sessionCheckExpiryDays
daysUntilExpire: this.sessionCheckExpiryDays,
cookieDomain: this.options.cookieDomain
});

if (options.detailedResponse) {
Expand Down
13 changes: 13 additions & 0 deletions src/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,19 @@ export interface Auth0ClientOptions extends BaseLoginOptions {
*/
sessionCheckExpiryDays?: number;

/**
* The domain the cookie is accessible from. If not set, the cookie is scoped to
* the current domain, including the subdomain.
*
* Note: setting this incorrectly may cause silent authentication to stop working
* on page load.
*
*
* To keep a user logged in across multiple subdomains set this to your
* top-level domain and prefixed with a `.` (eg: `.example.com`).
*/
cookieDomain?: string;

/**
* When true, data to the token endpoint is transmitted as x-www-form-urlencoded data instead of JSON. The default is false, but will default to true in a
* future major version.
Expand Down
5 changes: 5 additions & 0 deletions src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as Cookies from 'es-cookie';

interface ClientStorageOptions {
daysUntilExpire: number;
cookieDomain?: string;
}

/**
Expand Down Expand Up @@ -41,6 +42,10 @@ export const CookieStorage = {
cookieAttributes.expires = options.daysUntilExpire;
}

if (options?.cookieDomain) {
cookieAttributes.domain = options.cookieDomain;
}

Cookies.set(key, JSON.stringify(value), cookieAttributes);
},

Expand Down