Skip to content

Commit

Permalink
call /ssodata when inside the universal login page (#1245)
Browse files Browse the repository at this point in the history
* call /ssodata if we're in the universal login page

* Handling embedded and hosted scenarios in quick-auth
  • Loading branch information
luisrudge authored Feb 2, 2018
1 parent fc16f19 commit 5f2b6e0
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"sinon": "^1.15.4",
"stylus": "^0.54.5",
"stylus-loader": "^2.3.1",
"superagent": "^3.3.1",
"superagent": "^3.8.2",
"uglify-js": "^2.7.4",
"unminified-webpack-plugin": "^1.1.1",
"unreleased": "^0.1.0",
Expand Down
20 changes: 17 additions & 3 deletions src/__tests__/core/sso/last_login_screen.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { mount } from 'enzyme';
import Immutable from 'immutable';

import { expectComponent, extractPropsFromWrapper, mockComponent } from 'testUtils';
import { expectComponent, extractPropsFromWrapper, mockComponent, setURL } from 'testUtils';

jest.mock('ui/pane/quick_auth_pane', () => mockComponent('quick_auth_pane'));

Expand All @@ -26,7 +26,8 @@ describe('LastLoginScreen', () => {
}));

jest.mock('core/index', () => ({
id: () => 'id'
id: () => 'id',
domain: () => 'me.auth0.com'
}));

jest.mock('core/sso/index', () => ({
Expand Down Expand Up @@ -117,7 +118,8 @@ describe('LastLoginScreen', () => {
expectComponent(<Component {...defaultProps} />).toMatchSnapshot();
});
});
it('calls logIn in the buttonClickHandler', () => {
it('calls checkSession in the buttonClickHandler when outside of the universal login page', () => {
setURL('https://other-url.auth0.com');
const Component = getComponent();
const wrapper = mount(<Component {...defaultProps} />);
const props = extractPropsFromWrapper(wrapper);
Expand All @@ -128,6 +130,18 @@ describe('LastLoginScreen', () => {
expect(mock.calls[0][1].get()).toBe('lastUsedConnection');
expect(mock.calls[0][2]).toBe('lastUsedUsername');
});
it('calls logIn in the buttonClickHandler when inside of the universal login page', () => {
setURL('https://me.auth0.com');
const Component = getComponent();
const wrapper = mount(<Component {...defaultProps} />);
const props = extractPropsFromWrapper(wrapper);
props.buttonClickHandler();
const { mock } = require('quick-auth/actions').logIn;
expect(mock.calls.length).toBe(1);
expect(mock.calls[0][0]).toBe('id');
expect(mock.calls[0][1].get()).toBe('lastUsedConnection');
expect(mock.calls[0][2]).toBe('lastUsedUsername');
});
it('calls skipQuickAuth in the alternativeClickHandler', () => {
const Component = getComponent();
const wrapper = mount(<Component {...defaultProps} />);
Expand Down
19 changes: 12 additions & 7 deletions src/__tests__/core/web_api/__snapshots__/p2_api.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Auth0APIClient getUserCountry should call getUserCountry 1`] = `
Array [
"cb",
]
`;

exports[`Auth0APIClient init with overrides forwards options to WebAuth 1`] = `
Object {
"_sendTelemetry": true,
"_telemetryInfo": Object {},
"audience": "foo",
"clientID": "cid",
"domain": "domain",
"domain": "me.auth0.com",
"leeway": 60,
"overrides": Object {
"__tenant": "tenant1",
Expand All @@ -23,16 +29,11 @@ Object {
}
`;

exports[`Auth0APIClient getUserCountry should call getUserCountry 1`] = `
Array [
"cb",
]
`;

exports[`Auth0APIClient logIn with credentials should call client.login 1`] = `
Object {
"nonce": undefined,
"realm": undefined,
"sso": undefined,
"state": undefined,
"username": "foo",
}
Expand All @@ -41,6 +42,7 @@ Object {
exports[`Auth0APIClient logIn with credentials should call popup.loginWithCredentials when redirect is false and sso is false 1`] = `
Object {
"nonce": undefined,
"sso": undefined,
"state": undefined,
"username": "foo",
}
Expand All @@ -49,6 +51,7 @@ Object {
exports[`Auth0APIClient logIn with credentials should call popup.loginWithCredentials when redirect is false and sso is true 1`] = `
Object {
"nonce": undefined,
"sso": undefined,
"state": undefined,
"username": "foo",
}
Expand All @@ -57,6 +60,7 @@ Object {
exports[`Auth0APIClient logIn with social/enterprise (without username and email) should call authorize when redirect===true 1`] = `
Object {
"nonce": undefined,
"sso": undefined,
"state": undefined,
}
`;
Expand All @@ -65,6 +69,7 @@ exports[`Auth0APIClient logIn with social/enterprise (without username and email
Object {
"nonce": undefined,
"owp": true,
"sso": undefined,
"state": undefined,
}
`;
Expand Down
29 changes: 28 additions & 1 deletion src/__tests__/core/web_api/p2_api.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { setURL } from 'testUtils';

jest.mock('auth0-js');

const getClient = (options = {}) => {
const lockId = 'lockId';
const clientId = 'cid';
const domain = 'domain';
const domain = 'me.auth0.com';
const Auth0APIClient = require('core/web_api/p2_api').default;
const client = new Auth0APIClient(lockId, clientId, domain, options);
client.client.popup = {
Expand Down Expand Up @@ -44,6 +46,31 @@ describe('Auth0APIClient', () => {
expect(mock.WebAuth.mock.calls[0][0]).toMatchSnapshot();
});
});

describe('should set authOpt according options', () => {
it('should set sso:true when inside the universal login page', () => {
setURL('https://me.auth0.com/');
const options = {
sso: true
};
const client = getClient(options);
expect(client.authOpt.sso).toBe(true);
});
it('should set sso:false when inside the universal login page', () => {
setURL('https://me.auth0.com/');
const options = {
sso: false
};
const client = getClient(options);
expect(client.authOpt.sso).toBe(false);
});
it('should set sso:undefined when outside the universal login page', () => {
setURL('https://other-url.auth0.com/');
const options = {};
const client = getClient(options);
expect(client.authOpt.sso).toBe(undefined);
});
});
});
describe('logIn', () => {
const assertCallWithCallback = (mock, callbackFunction) => {
Expand Down
9 changes: 7 additions & 2 deletions src/core/sso/last_login_screen.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import Screen from '../screen';
import QuickAuthPane from '../../ui/pane/quick_auth_pane';
import { checkSession, skipQuickAuth } from '../../quick-auth/actions';
import { logIn, checkSession, skipQuickAuth } from '../../quick-auth/actions';
import { lastUsedConnection, lastUsedUsername } from './index';
import * as l from '../index';
import { renderSignedInConfirmation } from '../signed_in_confirmation';
Expand All @@ -27,7 +27,12 @@ const Component = ({ i18n, model }) => {
const buttonIcon = buttonTheme && buttonTheme.get('icon');

const buttonClickHandler = () => {
checkSession(l.id(model), lastUsedConnection(model), lastUsedUsername(model));
const isUniversalLogin = window.location.host === l.domain(model);
if (isUniversalLogin) {
logIn(l.id(model), lastUsedConnection(model), lastUsedUsername(model));
} else {
checkSession(l.id(model), lastUsedConnection(model), lastUsedUsername(model));
}
};
const buttonLabel =
lastUsedUsername(model) || SOCIAL_STRATEGIES[connectionName] || connectionName;
Expand Down
19 changes: 16 additions & 3 deletions src/core/web_api/p2_api.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import auth0 from 'auth0-js';
import CordovaAuth0Plugin from 'auth0-js/plugins/cordova';
import superagent from 'superagent';
import * as l from '../index';
import { getEntity, read } from '../../store/index';
import { normalizeError, loginCallback, normalizeAuthParams, webAuthOverrides } from './helper';
Expand All @@ -9,6 +10,8 @@ class Auth0APIClient {
this.lockID = lockID;
this.client = null;
this.authOpt = null;
this.domain = domain;
this.isUniversalLogin = window.location.host === domain;

const default_telemetry = {
name: 'lock.js',
Expand All @@ -34,7 +37,8 @@ class Auth0APIClient {
popup: !opts.redirect,
popupOptions: opts.popupOptions,
nonce: opts.nonce,
state: opts.state
state: opts.state,
sso: this.isUniversalLogin ? opts.sso : undefined
};
}

Expand Down Expand Up @@ -102,8 +106,17 @@ class Auth0APIClient {
this.getUserInfo(token, callback);
}

getSSOData(...args) {
return this.client.client.getSSOData(...args);
getSSOData(cb) {
if (this.isUniversalLogin) {
superagent
.get(`https://${this.domain}/user/ssodata`)
.withCredentials()
.end((err, res) => {
cb(err, res.body);
});
} else {
return this.client.client.getSSOData(cb);
}
}

getUserCountry(cb) {
Expand Down
3 changes: 2 additions & 1 deletion support/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ <h1 class="navbar-brand">
email: 'johnfoo@gmail.com'
},
passwordlessMethod: 'code',
allowedConnections: ['email', 'acme']
allowedConnections: ['email', 'acme', 'twitter'],
auth: { sso: true }
};
function initLock() {
var lock = new Auth0Lock(clientId, domain, defaultOptions);
Expand Down

0 comments on commit 5f2b6e0

Please sign in to comment.