Skip to content

Commit

Permalink
Refactor clear session implementation.
Browse files Browse the repository at this point in the history
Make use of Auth client url builder.
Make iOS implementation reuse existing handlers
  • Loading branch information
hzalaz committed Aug 18, 2017
1 parent fa3704e commit 493ed73
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 28 deletions.
2 changes: 1 addition & 1 deletion android/src/main/java/com/auth0/react/A0Auth0Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public Map<String, Object> getConstants() {
}

@ReactMethod
public void showUrl(String url, Callback callback) {
public void showUrl(String url, boolean closeOnLoad, Callback callback) {
final Activity activity = getCurrentActivity();

this.callback = callback;
Expand Down
6 changes: 6 additions & 0 deletions auth/__tests__/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ Array [
]
`;

exports[`auth logoutUrl should return default logout url 1`] = `"https://samples.auth0.com/v2/logout"`;

exports[`auth logoutUrl should return logout url with extra parameters 1`] = `"https://samples.auth0.com/v2/logout?federated=true&client_id=CLIENT_ID"`;

exports[`auth logoutUrl should return logout url with skipping unknown parameters 1`] = `"https://samples.auth0.com/v2/logout?federated=true"`;

exports[`auth password realm should handle oauth error 1`] = `[invalid_request: Invalid grant]`;

exports[`auth password realm should handle unexpected error 1`] = `[a0.response.invalid: Internal Server Error]`;
Expand Down
21 changes: 21 additions & 0 deletions auth/__tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,27 @@ describe('auth', () => {
});
});

describe('logoutUrl', () => {
it('should return default logout url', () => {
expect(auth.logoutUrl({})).toMatchSnapshot();
});

it('should return logout url with extra parameters', () => {
expect(auth.logoutUrl({
federated: true,
clientId: 'CLIENT_ID',
redirectTo: 'https://auth0.com'
})).toMatchSnapshot();
});

it('should return logout url with skipping unknown parameters', () => {
expect(auth.logoutUrl({
federated: true,
shouldNotBeThere: 'really'
})).toMatchSnapshot();
});
});

describe('code exchange', () => {
it('should send correct payload', async () => {
fetchMock.postOnce('https://samples.auth0.com/oauth/token', tokens);
Expand Down
23 changes: 23 additions & 0 deletions auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,29 @@ export default class Auth {
return this.client.url('/authorize', {...query, client_id: this.clientId}, true);
}

/**
* Builds the full logout endpoint url in the Authorization Server (AS) with given parameters.
*
* @param {Object} parameters parameters to send to `/v2/logout`
* @param {Boolean} [parameters.federated] if the logout should include removing session for federated IdP.
* @param {String} [parameters.clientId] client identifier of the one requesting the logout
* @param {String} [parameters.returnTo] url where the user is redirected to after logout. It must be declared in you Auth0 Dashboard
* @returns {String} logout url with specified parameters
* @see https://auth0.com/docs/api/authentication#logout
*
* @memberof Auth
*/
logoutUrl(parameters = {}) {
const query = apply({
parameters: {
federated: { required: false },
clientId: { required: false, toName: 'client_id' },
returnTo: { required: false }
}
}, parameters);
return this.client.url('/v2/logout', {...query});
}

/**
* Exchanges a code obtained via `/authorize` (w/PKCE) for the user's tokens
*
Expand Down
23 changes: 7 additions & 16 deletions ios/A0Auth0.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@interface A0Auth0 () <SFSafariViewControllerDelegate>
@property (weak, nonatomic) SFSafariViewController *last;
@property (copy, nonatomic) RCTResponseSenderBlock sessionCallback;
@property (copy, nonatomic) RCTResponseSenderBlock didLoadCallback;
@property (assign, nonatomic) BOOL closeOnLoad;
@end

@implementation A0Auth0
Expand All @@ -29,20 +29,12 @@ - (dispatch_queue_t)methodQueue
[self terminateWithError:nil dismissing:YES animated:YES];
}

RCT_EXPORT_METHOD(showUrl:(NSString *)urlString callback:(RCTResponseSenderBlock)callback) {
RCT_EXPORT_METHOD(showUrl:(NSString *)urlString closeOnLoad:(BOOL)closeOnLoad callback:(RCTResponseSenderBlock)callback) {
[self presentSafariWithURL:[NSURL URLWithString:urlString]];
self.closeOnLoad = closeOnLoad;
self.sessionCallback = callback;
}

RCT_EXPORT_METHOD(didLoadURL:(NSString *)urlString callback:(RCTResponseSenderBlock)callback) {
[self presentSafariWithURL:[NSURL URLWithString:urlString]];
__weak A0Auth0 *weakSelf = self;
self.didLoadCallback = ^void(NSArray *response) {
[weakSelf.last.presentingViewController dismissViewControllerAnimated:NO completion:nil];
callback(response);
};
}

RCT_EXPORT_METHOD(oauthParameters:(RCTResponseSenderBlock)callback) {
callback(@[[self generateOAuthParameters]]);
}
Expand Down Expand Up @@ -76,6 +68,7 @@ - (void)terminateWithError:(id)error dismissing:(BOOL)dismissing animated:(BOOL)
}
self.sessionCallback = nil;
self.last = nil;
self.closeOnLoad = NO;
}

- (NSString *)randomValue {
Expand Down Expand Up @@ -133,14 +126,12 @@ - (void)safariViewControllerDidFinish:(SFSafariViewController *)controller {
}

- (void)safariViewController:(SFSafariViewController *)controller didCompleteInitialLoad:(BOOL)didLoadSuccessfully {
if (self.didLoadCallback) {
NSArray *response = didLoadSuccessfully ? @[[NSNull null]] : @[@{@"error": @"failed to load url"}];
self.didLoadCallback(response);
self.didLoadCallback = nil;
if (self.closeOnLoad && didLoadSuccessfully) {
[self terminateWithError:nil dismissing:YES animated:YES];
} else if (!didLoadSuccessfully) {
NSDictionary *error = @{
@"error": @"a0.session.failed_load",
@"error_description": @"Failed to load authorize url"
@"error_description": @"Failed to load url"
};
[self terminateWithError:error dismissing:YES animated:YES];
}
Expand Down
4 changes: 2 additions & 2 deletions webauth/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ const { A0Auth0 } = NativeModules;

export default class Agent {

show(url) {
show(url, closeOnLoad = false) {
return new Promise((resolve, reject) => {
const urlHandler = (event) => {
A0Auth0.hide();
Linking.removeEventListener('url', urlHandler);
resolve(event.url);
};
Linking.addEventListener('url', urlHandler);
A0Auth0.showUrl(url, (err) => {
A0Auth0.showUrl(url, closeOnLoad, (err) => {
Linking.removeEventListener('url', urlHandler);
reject(err);
});
Expand Down
12 changes: 3 additions & 9 deletions webauth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,9 @@ export default class WebAuth {
status: 0
}));
}
const { clientId, domain, client, agent } = this;
const { client, agent } = this;
const federated = options.federated || false;
return new Promise(function (resolve, reject) {
const logoutURL = `https://${domain}/v2/logout${federated ? '?federated' : ''}`;
console.log(logoutURL);
A0Auth0.didLoadURL(logoutURL, function (err, data) {
if (err !== null) { return reject(err); }
resolve();
});
});
const logoutUrl = client.logoutUrl(options);
return agent.show(logoutUrl, true);
}
}

0 comments on commit 493ed73

Please sign in to comment.