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

Use Auth0.Android v2 [SDK-2429] #610

Merged
merged 11 commits into from
Apr 19, 2021
Merged

Use Auth0.Android v2 [SDK-2429] #610

merged 11 commits into from
Apr 19, 2021

Conversation

lbalmaceda
Copy link
Contributor

Changes

Targetting the newer Auth0 SDK imposes two new constraints/requirements:

Once these settings have been adjusted, the migration guide was followed to fix the compiling errors. These mostly were:

  • Authentication parameters type Map<String, Object> --> Map<String, String>
  • Callback types AuthCallback --> Callback
  • Removed classes: VoidCallback and Telemetry
  • Removed methods: OIDC conformant

Note: The Auth0Parcelable class, used to communicate the Auth0 class settings between Lock instances, is not completely migrated. As of this PR, it's lacking support for customizing the networking client; a feature that was recently made available in Auth0 Android v2. A comment was added to re-evaluate this in the short term.

References

See SDK-2429.

Testing

Compile errors in the tests have not been completely fixed yet (wip). Once they are fixed, this PR will be removed from DRAFT.

@lbalmaceda lbalmaceda added the large Large review label Apr 13, 2021
@lbalmaceda lbalmaceda added this to the Major - v3 milestone Apr 13, 2021
@@ -228,8 +228,8 @@ private void showPasswordlessLock() {

private Auth0 getAccount() {
Auth0 account = new Auth0(getString(R.string.com_auth0_client_id), getString(R.string.com_auth0_domain));
account.setOIDCConformant(true);
account.setLoggingEnabled(true);
DefaultClient netClient = new DefaultClient(10, 10, Collections.<String, String>emptyMap(), true);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Default values from Kotlin are not available for Java. I had to hardcode the timeout values. This is for the sample app that we ship with this repository, not included in the library package itself.

@@ -76,8 +79,9 @@ private void parseAuthentication(Intent data) {
String accessToken = data.getStringExtra(Constants.ACCESS_TOKEN_EXTRA);
String tokenType = data.getStringExtra(Constants.TOKEN_TYPE_EXTRA);
String refreshToken = data.getStringExtra(Constants.REFRESH_TOKEN_EXTRA);
long expiresIn = data.getLongExtra(Constants.EXPIRES_IN_EXTRA, 0);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

we no longer save the expires in (seconds) but rather the calculated expires at (date)

.login(event.getUsername(), event.getPassword(), connection)
.addAuthenticationParameters(options.getAuthenticationParameters());
.login(event.getUsername(), event.getPassword(), connection);
request.addParameters(options.getAuthenticationParameters());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this method doesn't return the type "AuthenticationRequest", but "Request". So the chain had to be split.

if (options.getScope() != null) {
request.setScope(options.getScope());
}
if (options.getAudience() != null && options.getAccount().isOIDCConformant()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This method no longer exists. Moving on, everything is OIDC conformant

}
Log.d(TAG, "Couldn't find an specific provider, using the default: " + WebAuthProvider.class.getSimpleName());
webProvider.start(this, connection, extraAuthParameters, authProviderCallback, WEB_AUTH_REQUEST_CODE);
webProvider.start(this, connection, extraAuthParameters, new WebCallbackWrapper(authProviderCallback));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Lock works with the concept of AuthProvider. These are implementations from users (or our library) that know different strategies to sign in. We provide one for the web auth flow, using our WebAuthProvider.

Because the AuthProvider interface takes an AuthCallback to return the results, and given that we've changed the callback type in the last SDK release, we have to wrap it in this new class WebCallbackWrapper (internal)

* @param intent the intent received in the onActivityResult method.
* @return true if a result was expected and has a valid format, or false if not
*/
public boolean resume(int requestCode, int resultCode, Intent intent) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Deprecated, removed. This class is internal anyway.


client.newCall(req).enqueue(new Callback() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We will no longer use the OkHttp client directly, but through the interface exposed by the Auth0 class. We still have to do the error handling, as we're doing here.

The logic in this class has been moved to a "background task" AsyncTask where the network request will be safely executed.

@@ -22,9 +21,13 @@
private PasswordComplexity passwordComplexity;

private Connection(@NonNull String strategy, Map<String, Object> values) {
checkArgument(values != null && values.size() > 0, "Must have at least one value");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This method went away. We're doing it here now instead.

@@ -17,6 +17,7 @@ allprojects {
group = 'com.auth0.android'

repositories {
mavenLocal()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

temporary until we make a new release of the sdk

} else {
request = apiClient.signUp(getEmail(), getPassword(), connection);
}
SignUpRequest request = apiClient.signUp(getEmail(), getPassword(), getUsername(), connection, userMetadata);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

user metadata will be passed as an extra param of this method call, rather than later in the request parameters. The method accepts a nullable username, so we no longer need the if

}

@Test
public void shouldStartWebViewWithOptions() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

"webview" (instead of an external browser) is no longer supported

Intent intent = mock(Intent.class);
Options options = mock(Options.class);
WebProvider webProvider = new WebProvider(options);
assertThat(webProvider.resume(1, Activity.RESULT_CANCELED, intent), is(false));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

launching with a result code is no longer valid. This was only used for the "webview" flow.

@@ -27,7 +27,7 @@

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
MockitoAnnotations.openMocks(this);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the previous one is marked as deprecated. In some classes, I had to change it to this new way for the tests to run successfully.

@@ -35,11 +35,8 @@

private MockWebServer server;

public ApplicationAPI() throws IOException {
// FIXME: Remove this if migrating to Robolectric 4.4
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this PR migrates to robolectric 4.4; And this code is no longer needed.

/**
* Utility object for executing tests that use the networking client over HTTPS on localhost.
*/
public class SSLTestUtils {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is a test helper class. Not included as part of the packaged library

@@ -106,13 +118,15 @@ public void setUp() {
when(client.login(anyString(), anyString(), anyString())).thenReturn(authRequest);
when(client.loginWithOTP(anyString(), anyString())).thenReturn(authRequest);
when(client.createUser(anyString(), anyString(), anyString())).thenReturn(dbRequest);
when(client.createUser(anyString(), anyString(), anyString(), anyString())).thenReturn(dbRequest);
when(client.createUser(anyString(), anyString(), any(), anyString())).thenReturn(dbRequest);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

any() required for the username parameter, as it could be null or a valid string

implementation 'com.squareup:otto:1.3.8'
api 'com.auth0.android:auth0:1.29.0'
api 'com.auth0.android:auth0:2.1.0-SNAPSHOT'
Copy link
Contributor

Choose a reason for hiding this comment

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

also temporarily using a snapshot build built locally; makes sense for now

dest.writeString(auth0.getTelemetry().getVersion());
dest.writeString(auth0.getTelemetry().getLibraryVersion());
}
//FIXME: Find a way to pass the NetworkingClient implementation
Copy link
Contributor

Choose a reason for hiding this comment

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

Mostly just for my own education, but what does this comment mean?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This relates to the "note" I added to the PR description. Customizing the networking client was made available in the latest release of the SDK. But the way we accept that client is with a setter in the Auth0 class.

This library has an implementation that makes Auth0 "Parcelable" (similar to Serializable), so we could pass it across instances/classes to set up the widget. But because the NetworkingClient implementation cannot be serialized, it's not straightforward to update the Auth0 class to support it. I added the comment to come back to this later and evaluate if this is something we need to backport today or not.

Choose a reason for hiding this comment

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

Hello there! I realize this is a very old PR, but I was wondering if this was ever floated to be fixed, as I noticed it's still in the current version...being able to pass in a custom networking client would be super useful for my team right now as we have to do weird proxy stuff but would prefer not to do a full migration to the raw SDK at the moment 😅

My understanding is lock is not really receiving any new feature requests, but wondering if this would qualify as a bugfix and if we can contribute to a PR somehow!

Thanks so much!

Copy link
Contributor

@jimmyjames jimmyjames left a comment

Choose a reason for hiding this comment

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

Left a few minor comments; may be good to walk through the changes together as it gets closer to being out of draft.

@lbalmaceda lbalmaceda marked this pull request as ready for review April 19, 2021 09:47
@lbalmaceda lbalmaceda requested a review from a team as a code owner April 19, 2021 09:47
@lbalmaceda lbalmaceda merged commit 30269a8 into v3 Apr 19, 2021
@lbalmaceda lbalmaceda deleted the use-new-sdk branch April 19, 2021 17:22
@lbalmaceda lbalmaceda linked an issue May 4, 2021 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support Auth0 v2.0.0
3 participants