Skip to content

Commit

Permalink
Merge pull request #223 from auth0/webauth-logout-android
Browse files Browse the repository at this point in the history
Enable WebAuth Logout for Android & Fix iOS Logout.
  • Loading branch information
damieng authored Jul 12, 2019
2 parents 631cf9a + 473e411 commit c3eeaa7
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 21 deletions.
2 changes: 2 additions & 0 deletions webauth/__mocks__/auth0.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ export default class A0Auth0 {
oauthParameters(callback) {
callback(this.parameters);
}

bundleIdentifier = 'com.My.App';
}
1 change: 1 addition & 0 deletions webauth/__mocks__/react-native.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ const mock = {};

mock.Linking = new Linking();
mock.NativeModules = { A0Auth0: new A0Auth0() };
mock.Platform = { OS: 'test-os' };

module.exports = mock;
50 changes: 50 additions & 0 deletions webauth/__tests__/webauth.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
jest.mock('react-native');
import Auth from '../../auth';
import WebAuth from '../index';
import { NativeModules } from 'react-native';
import { URL } from 'url';

const A0Auth0 = NativeModules.A0Auth0;

describe('WebAuth', () => {
const auth = new Auth({ baseUrl: 'https://auth0.com', clientId: 'abc123' });
const webauth = new WebAuth(auth);

describe('clearSession', () => {
beforeEach(() => {
NativeModules.A0Auth0 = A0Auth0;
A0Auth0.reset();
});

it('should open log out URL', async () => {
await webauth.clearSession();

const parsedUrl = new URL(A0Auth0.url);
expect(parsedUrl.protocol).toEqual('https:');
expect(parsedUrl.hostname).toEqual('auth0.com');
const urlQuery = parsedUrl.searchParams;
expect(urlQuery.get('returnTo')).toEqual(
'com.my.app://auth0.com/test-os/com.My.App/callback'
);
expect(urlQuery.get('client_id')).toEqual('abc123');
expect(urlQuery.has('federated')).toEqual(false);
expect(urlQuery.has('auth0Client')).toEqual(true);
});

it('should open log out URL with federated=true', async () => {
const options = { federated: true };
await webauth.clearSession(options);

const parsedUrl = new URL(A0Auth0.url);
expect(parsedUrl.protocol).toEqual('https:');
expect(parsedUrl.hostname).toEqual('auth0.com');
const urlQuery = parsedUrl.searchParams;
expect(urlQuery.get('returnTo')).toEqual(
'com.my.app://auth0.com/test-os/com.My.App/callback'
);
expect(urlQuery.get('client_id')).toEqual('abc123');
expect(urlQuery.get('federated')).toEqual('true');
expect(urlQuery.has('auth0Client')).toEqual(true);
});
});
});
36 changes: 15 additions & 21 deletions webauth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ import AuthError from '../auth/authError';

const { A0Auth0 } = NativeModules;

const callbackUri = domain => {
const bundleIdentifier = A0Auth0.bundleIdentifier;
return `${bundleIdentifier.toLowerCase()}://${domain}/${
Platform.OS
}/${bundleIdentifier}/callback`;
};

/**
* Helper to perform Auth against Auth0 hosted login page
*
Expand Down Expand Up @@ -48,18 +55,15 @@ export default class WebAuth {
authorize(options = {}) {
const { clientId, domain, client, agent } = this;
return agent.newTransaction().then(({ state, verifier, ...defaults }) => {
const bundleIdentifier = A0Auth0.bundleIdentifier;
const redirectUri = `${bundleIdentifier.toLowerCase()}://${domain}/${
Platform.OS
}/${bundleIdentifier}/callback`;
const redirectUri = callbackUri(domain);
const expectedState = options.state || state;
let query = {
...defaults,
clientId,
responseType: 'code',
redirectUri,
state: expectedState,
...options,
...options
};
const authorizeUrl = this.client.authorizeUrl(query);
return agent.show(authorizeUrl).then(redirectUrl => {
Expand Down Expand Up @@ -93,7 +97,8 @@ export default class WebAuth {

/**
* Removes Auth0 session and optionally remove the Identity Provider session.
* In iOS it will use `SFSafariViewController`
*
* In iOS it will use `SFSafariViewController` and in Android Chrome Custom Tabs.
*
* @param {Object} parameters parameters to send
* @param {Bool} [parameters.federated] Optionally remove the IdP session.
Expand All @@ -103,21 +108,10 @@ export default class WebAuth {
* @memberof WebAuth
*/
clearSession(options = {}) {
if (Platform.OS !== 'ios') {
return Promise.reject(
new AuthError({
json: {
error: 'a0.platform.not_available',
error_description: `Cannot perform operation in platform ${
Platform.OS
}`
},
status: 0
})
);
}
const { client, agent } = this;
const federated = options.federated || false;
const { client, agent, domain, clientId } = this;
options.clientId = clientId;
options.returnTo = callbackUri(domain);
options.federated = options.federated || false;
const logoutUrl = client.logoutUrl(options);
return agent.show(logoutUrl, true);
}
Expand Down

0 comments on commit c3eeaa7

Please sign in to comment.