Skip to content

Commit

Permalink
Merge pull request #23 from auth0/credentials-from-resources
Browse files Browse the repository at this point in the history
Create APIClient without passing Account
  • Loading branch information
hzalaz authored Aug 12, 2016
2 parents aebf70b + 8b667b5 commit cf3b79a
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 11 deletions.
30 changes: 23 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,32 @@ First create an instance of `Auth0` with your client information
Auth0 account = new Auth0("{YOUR_CLIENT_ID}", "{YOUR_DOMAIN}");
```

Alternatively, you can save your client information in the `strings.xml` file using the following names:

```xml
<resources>
<string name="com_auth0_client_id">YOUR_CLIENT_ID</string>
<string name="com_auth0_domain">YOUR_DOMAIN</string>
</resources>

```

And then create a new Auth0 instance by passing an Android Context:

```java
AuthenticationAPIClient authentication = new AuthenticationAPIClient(context);
```


### Authentication API

The client provides methods to authenticate the user against Auth0 server.

Create a new instance by passing the account:
Create a new instance by passing the account:

```java
```java
AuthenticationAPIClient authentication = new AuthenticationAPIClient(account);
```
```

#### Login with database connection

Expand Down Expand Up @@ -154,14 +171,13 @@ authentication

The client provides methods to link and unlink users account.

Create a new instance by passing the account:
Create a new instance by passing the account and the token:

```java
Auth0 account = new Auth0("client id", "domain");
UsersAPIClient apiClient = new UsersAPIClient(account, "user token");
UsersAPIClient apiClient = new UsersAPIClient(account, "api token");
```



#### Link users

```java
Expand Down
21 changes: 21 additions & 0 deletions auth0/src/main/java/com/auth0/android/Auth0.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package com.auth0.android;


import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

Expand All @@ -49,6 +50,18 @@ public class Auth0 {
private final HttpUrl configurationUrl;
private Telemetry telemetry;


/**
* Creates a new Auth0 instance with the 'com_auth0_client_id' and 'com_auth0_domain' values
* defined in the project String resources file.
* If the values are not found, IllegalArgumentException will raise.
*
* @param context a valid context
*/
public Auth0(@NonNull Context context) {
this(getResourceFromContext(context, "com_auth0_client_id"), getResourceFromContext(context, "com_auth0_domain"));
}

/**
* Creates a new object using clientId & domain
*
Expand Down Expand Up @@ -158,4 +171,12 @@ private HttpUrl ensureValidUrl(String url) {
String safeUrl = url.startsWith("http") ? url : "https://" + url;
return HttpUrl.parse(safeUrl);
}

private static String getResourceFromContext(@NonNull Context context, String resName) {
final int stringRes = context.getResources().getIdentifier(resName, "string", context.getPackageName());
if (stringRes == 0) {
throw new IllegalArgumentException(String.format("The 'R.string.%s' value it's not defined in your project's resources file.", resName));
}
return context.getString(stringRes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package com.auth0.android.authentication;

import android.content.Context;
import android.support.annotation.NonNull;

import com.auth0.android.Auth0;
Expand Down Expand Up @@ -100,6 +101,16 @@ public AuthenticationAPIClient(@NonNull Auth0 auth0) {
this(auth0, new OkHttpClient(), GsonProvider.buildGson());
}

/**
* Creates a new API client instance using the 'com_auth0_client_id' and 'com_auth0_domain' values
* defined in the project String resources file.
*
* @param context a valid Context
*/
public AuthenticationAPIClient(Context context) {
this(new Auth0(context));
}

private AuthenticationAPIClient(Auth0 auth0, OkHttpClient client, Gson gson) {
this.auth0 = auth0;
this.client = client;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
package com.auth0.android.management;


import android.content.Context;

import com.auth0.android.Auth0;
import com.auth0.android.authentication.ParameterBuilder;
import com.auth0.android.request.ErrorBuilder;
Expand All @@ -45,7 +47,7 @@

/**
* API client for Auth0 Management API.
* <p>
* <p/>
* <pre><code>
* Auth0 auth0 = new Auth0("your_client_id", "your_domain");
* UsersAPIClient client = new UsersAPIClient(auth0);
Expand All @@ -72,11 +74,23 @@ public class UsersAPIClient {
* Creates a new API client instance providing Auth0 account info.
*
* @param auth0 account information
* @param token of the primary identity
*/
public UsersAPIClient(Auth0 auth0, String token) {
this(auth0, token, new OkHttpClient(), GsonProvider.buildGson());
}

/**
* Creates a new API client instance using the 'com_auth0_client_id' and 'com_auth0_domain' values
* defined in the project String resources file.
*
* @param context a valid Context
* @param token of the primary identity
*/
public UsersAPIClient(Context context, String token) {
this(new Auth0(context), token);
}

private UsersAPIClient(Auth0 auth0, String token, OkHttpClient client, Gson gson) {
this.auth0 = auth0;
this.client = client;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package com.auth0.android.provider;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
Expand Down Expand Up @@ -238,6 +239,18 @@ public static Builder init(@NonNull Auth0 account) {
return new Builder(account);
}


/**
* Initialize the WebAuthProvider instance with an Android Context. Additional settings can be configured
* in the Builder, like setting the connection name or authentication parameters.
*
* @param context a valid context.
* @return a new Builder instance to customize.
*/
public static Builder init(@NonNull Context context) {
return new Builder(new Auth0(context));
}

/**
* Finishes the authentication flow by passing the data received in the activity's onActivityResult() callback.
* The final authentication result will be delivered to the callback specified when calling start().
Expand Down
56 changes: 56 additions & 0 deletions auth0/src/test/java/com/auth0/android/Auth0Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,28 @@

package com.auth0.android;

import android.content.Context;
import android.content.res.Resources;

import com.auth0.android.util.Telemetry;
import com.squareup.okhttp.HttpUrl;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mockito;

import static com.auth0.android.util.HttpUrlMatcher.hasHost;
import static com.auth0.android.util.HttpUrlMatcher.hasPath;
import static com.auth0.android.util.HttpUrlMatcher.hasScheme;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when;

public class Auth0Test {

Expand All @@ -51,6 +59,54 @@ public class Auth0Test {
private static final String AU_DOMAIN = "samples.au.auth0.com";
private static final String OTHER_DOMAIN = "samples-test.other-subdomain.other.auth0.com";


@Test
public void shouldBuildFromResources() throws Exception {
Context context = Mockito.mock(Context.class);
Resources resources = Mockito.mock(Resources.class);
when(context.getResources()).thenReturn(resources);
when(resources.getIdentifier(eq("com_auth0_client_id"), eq("string"), anyString())).thenReturn(222);
when(resources.getIdentifier(eq("com_auth0_domain"), eq("string"), anyString())).thenReturn(333);

when(context.getString(eq(222))).thenReturn(CLIENT_ID);
when(context.getString(eq(333))).thenReturn(DOMAIN);

Auth0 auth0 = new Auth0(context);

assertThat(auth0, notNullValue());
assertThat(auth0.getClientId(), equalTo(CLIENT_ID));
assertThat(auth0.getDomainUrl(), equalTo("https://samples.auth0.com/"));
assertThat(auth0.getConfigurationUrl(), equalTo("https://cdn.auth0.com/"));
}

@Test
public void shouldFailToBuildFromResourcesWithoutClientID() throws Exception {
Context context = Mockito.mock(Context.class);
Resources resources = Mockito.mock(Resources.class);
when(context.getResources()).thenReturn(resources);
when(resources.getIdentifier(eq("com_auth0_client_id"), eq("string"), anyString())).thenReturn(0);
when(resources.getIdentifier(eq("com_auth0_domain"), eq("string"), anyString())).thenReturn(333);

expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("The 'R.string.com_auth0_client_id' value it's not defined in your project's resources file.");

new Auth0(context);
}

@Test
public void shouldFailToBuildFromResourcesWithoutDomain() throws Exception {
Context context = Mockito.mock(Context.class);
Resources resources = Mockito.mock(Resources.class);
when(context.getResources()).thenReturn(resources);
when(resources.getIdentifier(eq("com_auth0_client_id"), eq("string"), anyString())).thenReturn(222);
when(resources.getIdentifier(eq("com_auth0_domain"), eq("string"), anyString())).thenReturn(0);

expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("The 'R.string.com_auth0_domain' value it's not defined in your project's resources file.");

new Auth0(context);
}

@Test
public void shouldBuildWithClientIdAndDomain() throws Exception {
Auth0 auth0 = new Auth0(CLIENT_ID, DOMAIN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
package com.auth0.android.authentication;


import android.content.Context;
import android.content.res.Resources;

import com.auth0.android.Auth0;
import com.auth0.android.result.Authentication;
import com.auth0.android.result.Credentials;
Expand All @@ -42,6 +45,7 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import java.lang.reflect.Type;
import java.util.HashMap;
Expand All @@ -62,6 +66,9 @@
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when;

public class AuthenticationAPIClientTest {

Expand Down Expand Up @@ -107,6 +114,24 @@ public void shouldCreateClientWithAccountInfo() throws Exception {
assertThat(HttpUrl.parse(client.getBaseURL()).encodedPath(), is("/"));
}

@Test
public void shouldCreateClientWithContextInfo() throws Exception {
Context context = Mockito.mock(Context.class);
Resources resources = Mockito.mock(Resources.class);
when(context.getResources()).thenReturn(resources);
when(resources.getIdentifier(eq("com_auth0_client_id"), eq("string"), anyString())).thenReturn(222);
when(resources.getIdentifier(eq("com_auth0_domain"), eq("string"), anyString())).thenReturn(333);

when(context.getString(eq(222))).thenReturn(CLIENT_ID);
when(context.getString(eq(333))).thenReturn(DOMAIN);

AuthenticationAPIClient client = new AuthenticationAPIClient(context);

assertThat(client, is(notNullValue()));
assertThat(client.getClientId(), is(CLIENT_ID));
assertThat(client.getBaseURL(), equalTo("https://" + DOMAIN + "/"));
}

@Test
public void shouldLoginWithUserAndPassword() throws Exception {
mockAPI.willReturnSuccessfulLogin();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
package com.auth0.android.management;


import android.content.Context;
import android.content.res.Resources;

import com.auth0.android.Auth0;
import com.auth0.android.result.UserIdentity;
import com.auth0.android.result.UserProfile;
Expand All @@ -39,6 +42,7 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import java.lang.reflect.Type;
import java.util.Arrays;
Expand All @@ -55,6 +59,9 @@
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when;

public class UsersAPIClientTest {

Expand Down Expand Up @@ -100,6 +107,24 @@ public void shouldCreateClientWithAccountInfo() throws Exception {
assertThat(client.getBaseURL(), equalTo("https://" + DOMAIN + "/"));
}

@Test
public void shouldCreateClientWithContextInfo() throws Exception {
Context context = Mockito.mock(Context.class);
Resources resources = Mockito.mock(Resources.class);
when(context.getResources()).thenReturn(resources);
when(resources.getIdentifier(eq("com_auth0_client_id"), eq("string"), anyString())).thenReturn(222);
when(resources.getIdentifier(eq("com_auth0_domain"), eq("string"), anyString())).thenReturn(333);

when(context.getString(eq(222))).thenReturn(CLIENT_ID);
when(context.getString(eq(333))).thenReturn(DOMAIN);

UsersAPIClient client = new UsersAPIClient(context, TOKEN_PRIMARY);

assertThat(client, is(notNullValue()));
assertThat(client.getClientId(), is(CLIENT_ID));
assertThat(client.getBaseURL(), equalTo("https://" + DOMAIN + "/"));
}

@Test
public void shouldLinkAccount() throws Exception {
mockAPI.willReturnSuccessfulLink();
Expand Down
Loading

0 comments on commit cf3b79a

Please sign in to comment.