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

Allow to customize the redirect URI / return to URL #279

Merged
merged 6 commits into from
Jan 29, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ abstract class CallbackHelper {
*
* @return the callback Uri.
*/
public static String getCallbackUri(@NonNull String scheme, @NonNull String packageName, @NonNull String domain) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

class is package private already 👍

static String getCallbackUri(@NonNull String scheme, @NonNull String packageName, @NonNull String domain) {
if (!URLUtil.isValidUrl(domain)) {
Log.e(TAG, "The Domain is invalid and the Callback URI will not be set. You used: " + domain);
return null;
Expand All @@ -62,7 +62,7 @@ public static String getCallbackUri(@NonNull String scheme, @NonNull String pack
}

@NonNull
public static Map<String, String> getValuesFromUri(@Nullable Uri uri) {
static Map<String, String> getValuesFromUri(@Nullable Uri uri) {
if (uri == null) {
return Collections.emptyMap();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public static class LogoutBuilder {

private final Auth0 account;
private String scheme;
private String returnToUrl;
private CustomTabsOptions ctOptions;

LogoutBuilder(Auth0 account) {
Expand All @@ -90,7 +91,7 @@ public LogoutBuilder withCustomTabsOptions(@NonNull CustomTabsOptions options) {
/**
* Specify a custom Scheme to use on the Return To Uri. Default scheme is 'https'.
lbalmaceda marked this conversation as resolved.
Show resolved Hide resolved
*
* @param scheme to use in the Callback Uri.
* @param scheme to use in the Return To URL.
* @return the current builder instance
*/
public LogoutBuilder withScheme(@NonNull String scheme) {
Expand All @@ -102,6 +103,19 @@ public LogoutBuilder withScheme(@NonNull String scheme) {
return this;
}

/**
* Specify a custom Redirect To URL to use to invoke the app on redirection.
* Normally, you wouldn't need to call this method manually as the default value is autogenerated for you.
* The {@link LogoutBuilder#withScheme(String)} configuration is ignored when this method is called. It is your responsibility to pass a well-formed URL.
*
* @param returnToUrl to use to invoke the app on redirection.
* @return the current builder instance
*/
public LogoutBuilder withReturnToUrl(@NonNull String returnToUrl) {
this.returnToUrl = returnToUrl;
return this;
}

/**
* Request the user session to be cleared. When successful, the callback will get invoked
*
Expand All @@ -118,7 +132,9 @@ public void start(Context context, VoidCallback callback) {
return;
}

String returnToUrl = CallbackHelper.getCallbackUri(scheme, context.getApplicationContext().getPackageName(), account.getDomainUrl());
if (returnToUrl == null) {
returnToUrl = CallbackHelper.getCallbackUri(scheme, context.getApplicationContext().getPackageName(), account.getDomainUrl());
}
LogoutManager logoutManager = new LogoutManager(this.account, callback, returnToUrl);
logoutManager.setCustomTabsOptions(ctOptions);

Expand All @@ -141,6 +157,7 @@ public static class Builder {
private boolean useFullscreen;
private PKCE pkce;
private String scheme;
private String redirectUri;
private CustomTabsOptions ctOptions;
private Integer leeway;

Expand Down Expand Up @@ -241,9 +258,9 @@ public Builder withAudience(@NonNull String audience) {
}

/**
* Specify a custom Scheme to use on the Callback Uri. Default scheme is 'https'.
* Specify a custom Scheme to use on the Redirect URI. Default scheme is 'https'.
*
* @param scheme to use in the Callback Uri.
* @param scheme to use in the Redirect URI.
* @return the current builder instance
*/
public Builder withScheme(@NonNull String scheme) {
Expand All @@ -255,6 +272,19 @@ public Builder withScheme(@NonNull String scheme) {
return this;
}

/**
* Specify a custom Redirect URI to use to invoke the app on redirection.
* Normally, you wouldn't need to call this method manually as the default value is autogenerated for you.
* The {@link Builder#withScheme(String)} configuration is ignored when this method is called. It is your responsibility to pass a well-formed Uri.
lbalmaceda marked this conversation as resolved.
Show resolved Hide resolved
*
* @param redirectUri to use to invoke the app on redirection.
* @return the current builder instance
*/
public Builder withRedirectUri(@NonNull String redirectUri) {
this.redirectUri = redirectUri;
return this;
}

/**
* Give a scope for this request.
*
Expand Down Expand Up @@ -389,7 +419,9 @@ public void start(@NonNull Activity activity, @NonNull AuthCallback callback, in

managerInstance = manager;

String redirectUri = CallbackHelper.getCallbackUri(scheme, activity.getApplicationContext().getPackageName(), account.getDomainUrl());
if (redirectUri == null) {
redirectUri = CallbackHelper.getCallbackUri(scheme, activity.getApplicationContext().getPackageName(), account.getDomainUrl());
}
manager.startAuthentication(activity, redirectUri, requestCode);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,20 @@ public void shouldHaveRedirectUriOnLogin() {
assertThat(uri.getQueryParameter("redirect_uri"), is("https://test.domain.com/android/com.auth0.android.auth0.test/callback"));
}

@Test
public void shouldSetRedirectUriIgnoringSchemeOnLogin() {
lbalmaceda marked this conversation as resolved.
Show resolved Hide resolved
WebAuthProvider.init(account)
.withScheme("https")
.withRedirectUri("myapp:https://app.company.com/mobile/callback")
.start(activity, callback);

verify(activity).startActivity(intentCaptor.capture());
Uri uri = intentCaptor.getValue().getParcelableExtra(AuthenticationActivity.EXTRA_AUTHORIZE_URI);
assertThat(uri, is(notNullValue()));

assertThat(uri.getQueryParameter("redirect_uri"), is("myapp:https://app.company.com/mobile/callback"));
}

//response type

@Test
Expand Down Expand Up @@ -2437,6 +2451,20 @@ public void shouldHaveReturnToUriOnLogout() {
assertThat(uri.getQueryParameter("returnTo"), is("https://test.domain.com/android/com.auth0.android.auth0.test/callback"));
}

@Test
public void shouldSetReturnToUrlIgnoringSchemeOnLogout() {
WebAuthProvider.logout(account)
.withScheme("https")
.withReturnToUrl("myapp:https://app.company.com/mobile/callback")
.start(activity, voidCallback);

verify(activity).startActivity(intentCaptor.capture());
Uri uri = intentCaptor.getValue().getParcelableExtra(AuthenticationActivity.EXTRA_AUTHORIZE_URI);
assertThat(uri, is(notNullValue()));

assertThat(uri.getQueryParameter("returnTo"), is("myapp:https://app.company.com/mobile/callback"));
}


// Launch log out

Expand Down