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

[SDK-2708] Use configured domain for client assets download #2029

Merged
merged 6 commits into from
Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
"webpack-dev-server": "^2.3.0"
},
"dependencies": {
"auth0-js": "^9.16.3",
"auth0-js": "^9.16.4",
"auth0-password-policies": "^1.0.2",
"blueimp-md5": "^2.18.0",
"dompurify": "^2.2.8",
Expand Down
182 changes: 182 additions & 0 deletions src/__tests__/core/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('setup', () => {
mockInit = jest.fn();
jest.resetModules();
});

it('default redirectUrl should not include location.hash', () => {
setURL('https://test.com/path/#not-this-part');
const options = {};
Expand All @@ -38,6 +39,7 @@ describe('setup', () => {
const model = mock.calls[0][1].toJS();
expect(model.auth.redirectUrl).toBe('https://test.com/path/');
});

it('default redirectUrl should work when `window.location.origin` is not available', () => {
setURL('https://test.com/path/#not-this-part', { noOrigin: true });
const options = {};
Expand All @@ -47,19 +49,199 @@ describe('setup', () => {
const model = mock.calls[0][1].toJS();
expect(model.auth.redirectUrl).toBe('https://test.com/path/');
});

it('should work with redirect:false and responseType:id_token', () => {
const options = {
auth: {
redirect: false,
responseType: 'id_token'
}
};

setup('id', 'clientID', 'domain', options, 'hookRunner', 'emitEventFn', 'handleEventFn');
const { mock } = mockInit;
expect(mock.calls.length).toBe(1);
const model = mock.calls[0][1].toJS();
expect(model).toMatchSnapshot();
});

describe('clientBaseUrl', () => {
it('should default to the specified domain', () => {
const { mock } = mockInit;

setup(
'id',
'clientID',
'my-tenant.us.auth0.com',
{},
'hookRunner',
'emitEventFn',
'handleEventFn'
);

expect(mock.calls.length).toBe(1);

const model = mock.calls[0][1].toJS();
expect(model.clientBaseUrl).toBe('https://my-tenant.us.auth0.com');
});

it('should use the clientBaseUrl option if given', () => {
const { mock } = mockInit;

setup(
'id',
'clientID',
'my-tenant.us.auth0.com',
{
clientBaseUrl: 'https://client-base-url.example.com',
configurationBaseUrl: 'https://config-base-url.example.com',
lbalmaceda marked this conversation as resolved.
Show resolved Hide resolved
assetsUrl: 'https://assets-url.example.com'
},
'hookRunner',
'emitEventFn',
'handleEventFn'
);

expect(mock.calls.length).toBe(1);

const model = mock.calls[0][1].toJS();
expect(model.clientBaseUrl).toBe('https://client-base-url.example.com');
});

it('should use configurationBaseUrl if given', () => {
const { mock } = mockInit;

setup(
'id',
'clientID',
'my-tenant.us.auth0.com',
{
configurationBaseUrl: 'https://config-base-url.example.com',
assetsUrl: 'https://assets-url.example.com'
},
'hookRunner',
'emitEventFn',
'handleEventFn'
);

expect(mock.calls.length).toBe(1);

const model = mock.calls[0][1].toJS();
expect(model.clientBaseUrl).toBe('https://config-base-url.example.com');
});

it('should use assetsUrl if given', () => {
const { mock } = mockInit;

setup(
'id',
'clientID',
'my-tenant.us.auth0.com',
{
assetsUrl: 'https://assets-url.example.com'
},
'hookRunner',
'emitEventFn',
'handleEventFn'
);

expect(mock.calls.length).toBe(1);

const model = mock.calls[0][1].toJS();
expect(model.clientBaseUrl).toBe('https://assets-url.example.com');
});
});

describe('tenantBaseUrl', () => {
lbalmaceda marked this conversation as resolved.
Show resolved Hide resolved
it('tenantBaseUrl should default to domain URL when using auth0.com', () => {
lbalmaceda marked this conversation as resolved.
Show resolved Hide resolved
const { mock } = mockInit;

setup(
'id',
'clientID',
'my-tenant.us.auth0.com',
{
__useTenantInfo: true
},
'hookRunner',
'emitEventFn',
'handleEventFn'
);

expect(mock.calls.length).toBe(1);

const model = mock.calls[0][1].toJS();
expect(model.tenantBaseUrl).toBe('https://my-tenant.us.auth0.com/tenants/v1/my-tenant.js');
lbalmaceda marked this conversation as resolved.
Show resolved Hide resolved
});

it('should default to domain URL when using a custom domain', () => {
const { mock } = mockInit;

setup(
'id',
'clientID',
'auth.my-tenant.com',
{
__useTenantInfo: true
},
'hookRunner',
'emitEventFn',
'handleEventFn'
);

expect(mock.calls.length).toBe(1);

const model = mock.calls[0][1].toJS();
expect(model.tenantBaseUrl).toBe('https://auth.my-tenant.com/info-v1.js');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the path differs in this one? what's the condition? you might want to reflect that in the test name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I'm not sure of the history as to why this is. If you use a custom domain, the URL differs when using __useTenantInfo vs using an auth0.com domain. Just trying to keep and verify the same logic here.

});

it('should use configurationBaseUrl if specified', () => {
const { mock } = mockInit;

setup(
'id',
'clientID',
'auth.my-tenant.com',
{
__useTenantInfo: true,
configurationBaseUrl: 'https://config-base-url.com'
},
'hookRunner',
'emitEventFn',
'handleEventFn'
);

expect(mock.calls.length).toBe(1);

const model = mock.calls[0][1].toJS();
expect(model.tenantBaseUrl).toBe('https://config-base-url.com/info-v1.js');
});

it('should use configurationBaseUrl with a custom tenant if specified', () => {
const { mock } = mockInit;

setup(
'id',
'clientID',
'auth.my-tenant.com',
{
__useTenantInfo: true,
configurationBaseUrl: 'https://config-base-url.com',
overrides: {
__tenant: 'custom-tenant'
}
},
'hookRunner',
'emitEventFn',
'handleEventFn'
);

expect(mock.calls.length).toBe(1);

const model = mock.calls[0][1].toJS();
expect(model.tenantBaseUrl).toBe('https://config-base-url.com/tenants/v1/custom-tenant.js');
});
});
});

describe('setResolvedConnection', () => {
Expand Down
29 changes: 6 additions & 23 deletions src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,18 +355,7 @@ function extractClientBaseUrlOption(opts, domain) {
return opts.assetsUrl;
}

const domainUrl = 'https://' + domain;
const hostname = getLocationFromUrl(domainUrl).hostname;
const DOT_AUTH0_DOT_COM = '.auth0.com';
const AUTH0_US_CDN_URL = 'https://cdn.auth0.com';
if (endsWith(hostname, DOT_AUTH0_DOT_COM)) {
const parts = hostname.split('.');
return parts.length > 3
? 'https://cdn.' + parts[parts.length - 3] + DOT_AUTH0_DOT_COM
: AUTH0_US_CDN_URL;
} else {
return domainUrl;
}
return `https://${domain}`;
}

export function extractTenantBaseUrlOption(opts, domain) {
Expand All @@ -385,19 +374,13 @@ export function extractTenantBaseUrlOption(opts, domain) {
const domainUrl = 'https://' + domain;
const hostname = getLocationFromUrl(domainUrl).hostname;
const DOT_AUTH0_DOT_COM = '.auth0.com';
const AUTH0_US_CDN_URL = 'https://cdn.auth0.com';

const parts = hostname.split('.');
const tenant_name = parts[0];
var domain;

if (endsWith(hostname, DOT_AUTH0_DOT_COM)) {
domain =
parts.length > 3
? 'https://cdn.' + parts[parts.length - 3] + DOT_AUTH0_DOT_COM
: AUTH0_US_CDN_URL;
// prettier-ignore
if (endsWith(hostname, DOT_AUTH0_DOT_COM)) { // lgtm [js/incomplete-url-substring-sanitization]
const parts = hostname.split('.');
const tenant_name = parts[0];

return urljoin(domain, 'tenants', 'v1', `${tenant_name}.js`);
return urljoin(domainUrl, 'tenants', 'v1', `${tenant_name}.js`);
} else {
return urljoin(domainUrl, 'info-v1.js');
}
Expand Down
62 changes: 0 additions & 62 deletions test/tenantinfo.test.js

This file was deleted.

8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -690,10 +690,10 @@ atob@^2.1.2:
resolved "https://registry.yarnpkg.com/atob/-/atob-2.1.2.tgz#6d9517eb9e030d2436666651e86bd9f6f13533c9"
integrity sha512-Wm6ukoaOGJi/73p/cl2GvLjTI5JM1k/O14isD73YML8StrH/7/lRFgmg8nICZgD3bZZvjwCGxtMOD3wWNAu8cg==

auth0-js@^9.16.3:
version "9.16.3"
resolved "https://registry.yarnpkg.com/auth0-js/-/auth0-js-9.16.3.tgz#1e9f7e6acab9e5f808c38e0979cb741c38217409"
integrity sha512-MOJH66+S7LiGrzXCzjv1x57gXKulDmyqCmOfYUMn5hzMzncQnJ/AmBuUdHFI+g6gQmO6Q8l8A8v33jzv5ff0Ew==
auth0-js@^9.16.4:
version "9.16.4"
resolved "https://registry.yarnpkg.com/auth0-js/-/auth0-js-9.16.4.tgz#24f4d4f20687674c9f070a9f6f32b1e8a1f3a106"
integrity sha512-H0ddClKHG+6g6CoOXAvJiz9VvnuEr78YQh9rvYvYCnXTr6hcbMA92249VEQiX7a8rFoCsY4FBcEx/J+ZFyW0cg==
dependencies:
base64-js "^1.5.1"
idtoken-verifier "^2.2.1"
Expand Down