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

Change WebAuthProvider (Login) callback type #415

Merged
merged 3 commits into from
Jan 7, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
86 changes: 41 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,22 +147,18 @@ Finally, authenticate by showing the **Auth0 Universal Login**:
```java
//Configure and launch the authentication
WebAuthProvider.login(account)
.start(MainActivity.this, authCallback);
.start(MainActivity.this, callback);

//Declare the callback that will receive the result
AuthCallback authCallback = new AuthCallback() {
@Override
public void onFailure(@NonNull Dialog dialog) {
//failed with a dialog
}
BaseCallback<Credentials, AuthenticationException> callback = new BaseCallback<Credentials, AuthenticationException>() {

@Override
public void onFailure(AuthenticationException exception) {
public void onFailure(@NonNull AuthenticationException exception) {
//failed with an exception
}

@Override
public void onSuccess(@NonNull Credentials credentials) {
public void onSuccess(@Nullable Credentials credentials) {
//succeeded!
}
};
Expand All @@ -183,7 +179,7 @@ Auth0 account = new Auth0("{YOUR_AUTH0_CLIENT_ID}", "{YOUR_CUSTOM_DOMAIN}");

WebAuthProvider.login(account)
.withIdTokenVerificationIssuer("https://{YOUR_AUTH0_DOMAIN}/")
.start(MainActivity.this, authCallback);
.start(MainActivity.this, callback);
```

#### Those who don't need Web Authentication in their app
Expand Down Expand Up @@ -213,7 +209,7 @@ If you've followed this documents' configuration steps you've noticed that the d
```java
WebAuthProvider.login(account)
.withScheme("myapp")
.start(MainActivity.this, authCallback);
.start(MainActivity.this, callback);
```


Expand All @@ -222,7 +218,7 @@ WebAuthProvider.login(account)
```java
WebAuthProvider.login(account)
.withConnection("twitter")
.start(MainActivity.this, authCallback);
.start(MainActivity.this, callback);
```

#### Specify audience
Expand All @@ -232,7 +228,7 @@ The snippet below requests the "userinfo" audience in order to guarantee OIDC co
```java
WebAuthProvider.login(account)
.withAudience("https://{YOUR_AUTH0_DOMAIN}/userinfo")
.start(MainActivity.this, authCallback);
.start(MainActivity.this, callback);
```

> Replace `{YOUR_AUTH0_DOMAIN}` with your actual Auth0 domain (i.e. `mytenant.auth0.com`). If you've set up the tenant to use "Custom Domains", use that value here.
Expand All @@ -242,7 +238,7 @@ WebAuthProvider.login(account)
```java
WebAuthProvider.login(account)
.withScope("openid profile email")
.start(MainActivity.this, authCallback);
.start(MainActivity.this, callback);
```

> The default scope used is `openid`
Expand All @@ -252,7 +248,7 @@ WebAuthProvider.login(account)
```java
WebAuthProvider.login(account)
.withConnectionScope("email", "profile", "calendar:read")
.start(MainActivity.this, authCallback);
.start(MainActivity.this, callback);
```


Expand All @@ -268,7 +264,7 @@ CustomTabsOptions options = CustomTabsOptions.newBuilder()

WebAuthProvider.login(account)
.withCustomTabsOptions(options)
.start(MainActivity.this, authCallback);
.start(MainActivity.this, callback);
```


Expand Down Expand Up @@ -297,14 +293,14 @@ WebAuthProvider.logout(account)
.start(MainActivity.this, logoutCallback);

//Declare the callback that will receive the result
BaseCallback logoutCallback = new BaseCallback<Void, Auth0Exception>() {
BaseCallback<Void, AuthenticationException> logoutCallback = new BaseCallback<Void, AuthenticationException>() {
@Override
public void onFailure(Auth0Exception exception) {
public void onFailure(@NonNull Auth0Exception exception) {
//failed with an exception
}

@Override
public void onSuccess(@NonNull Void payload) {
public void onSuccess(@Nullable Void payload) {
//succeeded!
}
};
Expand Down Expand Up @@ -368,12 +364,12 @@ authentication
.login("info@auth0.com", "a secret password", "my-database-connection")
.start(new BaseCallback<Credentials, AuthenticationException>() {
@Override
public void onSuccess(Credentials payload) {
public void onSuccess(@Nullable Credentials payload) {
//Logged in!
}

@Override
public void onFailure(AuthenticationException error) {
public void onFailure(@NonNull AuthenticationException error) {
//Error!
}
});
Expand All @@ -394,12 +390,12 @@ authentication
.loginWithOTP("the mfa token", "123456")
.start(new BaseCallback<Credentials, AuthenticationException>() {
@Override
public void onSuccess(Credentials payload) {
public void onSuccess(@Nullable Credentials payload) {
//Logged in!
}

@Override
public void onFailure(AuthenticationException error) {
public void onFailure(@NonNull AuthenticationException error) {
//Error!
}
});
Expand All @@ -425,7 +421,7 @@ authentication
}

@Override
public void onFailure(AuthenticationException error) {
public void onFailure(@NonNull AuthenticationException error) {
//Error!
}
});
Expand All @@ -440,12 +436,12 @@ authentication
.loginWithEmail("info@auth0.com", "123456", "my-passwordless-connection")
.start(new BaseCallback<Credentials, AuthenticationException>() {
@Override
public void onSuccess(Credentials payload) {
public void onSuccess(@Nullable Credentials payload) {
//Logged in!
}

@Override
public void onFailure(AuthenticationException error) {
public void onFailure(@NonNull AuthenticationException error) {
//Error!
}
});
Expand All @@ -459,12 +455,12 @@ authentication
.signUp("info@auth0.com", "a secret password", "my-database-connection")
.start(new BaseCallback<Credentials, AuthenticationException>() {
@Override
public void onSuccess(Credentials payload) {
public void onSuccess(@Nullable Credentials payload) {
//Signed Up & Logged in!
}

@Override
public void onFailure(AuthenticationException error) {
public void onFailure(@NonNull AuthenticationException error) {
//Error!
}
});
Expand All @@ -478,12 +474,12 @@ authentication
.userInfo("user access_token")
.start(new BaseCallback<UserProfile, AuthenticationException>() {
@Override
public void onSuccess(UserProfile payload) {
public void onSuccess(@Nullable UserProfile payload) {
//Got the profile!
}

@Override
public void onFailure(AuthenticationException error) {
public void onFailure(@NonNull AuthenticationException error) {
//Error!
}
});
Expand All @@ -503,7 +499,12 @@ authentication.login(email, password, realm)
.start(new BaseCallback<Credentials, AuthenticationException>() {

@Override
public void onFailure(AuthenticationException error) {
public void onSuccess(@Nullable Credentials payload) {
// Handle API success
}

@Override
public void onFailure(@NonNull AuthenticationException error) {
if (error.isVerificationRequired()){
Map<String, Object> params = new HashMap<>();
params.put("login_hint", email); // So the user doesn't have to type it again
Expand All @@ -530,11 +531,6 @@ authentication.login(email, password, realm)
});
}
}

@Override
public void onSuccess(Credentials payload) {
// Handle API success
}
});
```

Expand Down Expand Up @@ -569,7 +565,7 @@ users
}

@Override
public void onFailure(ManagementException error) {
public void onFailure(@NonNull ManagementException error) {
//Error!
}
});
Expand All @@ -587,7 +583,7 @@ users
}

@Override
public void onFailure(ManagementException error) {
public void onFailure(@NonNull ManagementException error) {
//Error!
}
});
Expand All @@ -600,12 +596,12 @@ users
.getProfile("user id")
.start(new BaseCallback<UserProfile, ManagementException>() {
@Override
public void onSuccess(UserProfile payload) {
public void onSuccess(@Nullable UserProfile payload) {
//Profile received
}

@Override
public void onFailure(ManagementException error) {
public void onFailure(@NonNull ManagementException error) {
//Error!
}
});
Expand All @@ -622,12 +618,12 @@ users
.updateMetadata("user id", metadata)
.start(new BaseCallback<UserProfile, ManagementException>() {
@Override
public void onSuccess(UserProfile payload) {
public void onSuccess(@Nullable UserProfile payload) {
//User Metadata updated
}

@Override
public void onFailure(ManagementException error) {
public void onFailure(@NonNull ManagementException error) {
//Error!
}
});
Expand Down Expand Up @@ -664,13 +660,13 @@ authentication
.setScope("openid offline_access")
.start(new BaseCallback<Credentials, AuthenticationException>() {
@Override
public void onSuccess(Credentials payload) {
public void onSuccess(@Nullable Credentials payload) {
//Save the credentials
manager.saveCredentials(credentials);
}

@Override
public void onFailure(AuthenticationException error) {
public void onFailure(@NonNull AuthenticationException error) {
//Error!
}
});
Expand All @@ -691,12 +687,12 @@ Existing credentials will be returned if they are still valid, otherwise the `re
```java
manager.getCredentials(new BaseCallback<Credentials, CredentialsManagerException>() {
@Override
public void onSuccess(Credentials credentials){
public void onSuccess(@Nullable Credentials credentials){
//Use the Credentials
}

@Override
public void onFailure(CredentialsManagerException error){
public void onFailure(@NonNull CredentialsManagerException error){
//Error!
}
});
Expand Down
3 changes: 2 additions & 1 deletion V2_MIGRATION_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,14 @@ The ability to make requests to the [/delegation](https://auth0.com/docs/api/aut
- `public static Builder init(@NonNull Auth0 account)`. Use `public static Builder login(@NonNull Auth0 account)` instead.
- `public static Builder init(@NonNull Context context)`. Use `public static Builder login(@NonNull Auth0 account)` instead.
- `public static boolean resume(int requestCode, int resultCode, @Nullable Intent intent)`. Use `public static boolean resume(@Nullable Intent intent)` instead.
- `public static Builder init(@NonNull Auth0 account)`. Use `public static Builder login(@NonNull Auth0 account)` instead.

#### WebAuthProvider.Builder

- `public Builder useCodeGrant(boolean useCodeGrant)`. There is no replacement; only Code + PKCE flow supported in v2.
- `public Builder useBrowser(boolean useBrowser)`. There is no replacement; Google no longer supports WebView authentication.
- `public Builder useFullscreen(boolean useFullscreen)`. There is no replacement; Google no longer supports WebView authentication.
- `public void start(@NonNull Activity activity, @NonNull AuthCallback callback, int requestCode)`. Use `public void start(@NonNull Activity activity, @NonNull AuthCallback callback)` instead.
- `public void start(@NonNull Activity activity, @NonNull AuthCallback callback, int requestCode)`. Use `public void start(@NonNull Activity activity, @NonNull BaseCallback<Credentials, AuthenticationException> callback)` instead.
- `public Builder withResponseType(@ResponseType int type)`. There is no replacement; only Code + PKCE flow supported in v2.

#### UsersAPIClient
Expand Down
42 changes: 26 additions & 16 deletions auth0/src/main/java/com/auth0/android/provider/OAuthManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import java.util.*

internal class OAuthManager(
private val account: Auth0,
private val callback: AuthCallback,
private val callback: BaseCallback<Credentials, AuthenticationException>,
parameters: Map<String, String>,
ctOptions: CustomTabsOptions,
networkingClient: NetworkingClient?
Expand Down Expand Up @@ -99,24 +99,34 @@ internal class OAuthManager(
}

// response_type=code
pkce!!.getToken(values[KEY_CODE], object : SimpleAuthCallback(
callback
) {
override fun onSuccess(credentials: Credentials) {
assertValidIdToken(credentials.idToken, object : VoidCallback {
override fun onSuccess(ignored: Unit?) {
callback.onSuccess(credentials)
}
pkce!!.getToken(
values[KEY_CODE],
object : BaseCallback<Credentials, AuthenticationException> {
override fun onSuccess(credentials: Credentials?) {
assertValidIdToken(credentials!!.idToken, object : VoidCallback {
override fun onSuccess(payload: Unit?) {
callback.onSuccess(credentials)
}

override fun onFailure(error: Auth0Exception) {
val wrappedError = AuthenticationException(
ERROR_VALUE_ID_TOKEN_VALIDATION_FAILED, error
)
callback.onFailure(wrappedError)
}
})
}

override fun onFailure(error: Auth0Exception) {
val wrappedError = AuthenticationException(
ERROR_VALUE_ID_TOKEN_VALIDATION_FAILED, error
override fun onFailure(error: AuthenticationException) {
if ("Unauthorized" == error.getDescription()) {
Log.e(
PKCE.TAG,
"Unable to complete authentication with PKCE. PKCE support can be enabled by setting Application Type to 'Native' and Token Endpoint Authentication Method to 'None' for this app at 'https://manage.auth0.com/#/applications/" + apiClient.clientId + "/settings'."
)
callback.onFailure(wrappedError)
}
})
}
})
callback.onFailure(error)
}
})
return true
}

Expand Down
Loading