Skip to content

Commit

Permalink
add expires_in field to the Credentials class
Browse files Browse the repository at this point in the history
  • Loading branch information
lbalmaceda committed Feb 20, 2017
1 parent 62568ac commit 3dccb80
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 15 deletions.
10 changes: 8 additions & 2 deletions auth0/src/main/java/com/auth0/android/provider/OAuthManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class OAuthManager {
private static final String KEY_ACCESS_TOKEN = "access_token";
private static final String KEY_TOKEN_TYPE = "token_type";
private static final String KEY_REFRESH_TOKEN = "refresh_token";
private static final String KEY_EXPIRES_IN = "expires_in";
private static final String KEY_CODE = "code";

private final Auth0 account;
Expand Down Expand Up @@ -119,7 +120,11 @@ public boolean resumeAuthorization(AuthorizeResult data) {
}

Log.d(TAG, "Authenticated using web flow");
final Credentials urlCredentials = new Credentials(values.get(KEY_ID_TOKEN), values.get(KEY_ACCESS_TOKEN), values.get(KEY_TOKEN_TYPE), values.get(KEY_REFRESH_TOKEN));
Long expiresIn = null;
if (values.containsKey(KEY_EXPIRES_IN)) {
expiresIn = Long.valueOf(values.get(KEY_EXPIRES_IN));
}
final Credentials urlCredentials = new Credentials(values.get(KEY_ID_TOKEN), values.get(KEY_ACCESS_TOKEN), values.get(KEY_TOKEN_TYPE), values.get(KEY_REFRESH_TOKEN), expiresIn);
if (!shouldUsePKCE()) {
callback.onSuccess(urlCredentials);
} else {
Expand Down Expand Up @@ -255,8 +260,9 @@ static Credentials mergeCredentials(Credentials urlCredentials, Credentials code
final String accessToken = codeCredentials.getAccessToken() != null ? codeCredentials.getAccessToken() : urlCredentials.getAccessToken();
final String type = codeCredentials.getType() != null ? codeCredentials.getType() : urlCredentials.getType();
final String refreshToken = codeCredentials.getRefreshToken() != null ? codeCredentials.getRefreshToken() : urlCredentials.getRefreshToken();
final Long expiresIn = codeCredentials.getExpiresIn() != null ? codeCredentials.getExpiresIn() : urlCredentials.getExpiresIn();

return new Credentials(idToken, accessToken, type, refreshToken);
return new Credentials(idToken, accessToken, type, refreshToken, expiresIn);
}

@VisibleForTesting
Expand Down
10 changes: 9 additions & 1 deletion auth0/src/main/java/com/auth0/android/result/Credentials.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,15 @@ public class Credentials {
@SerializedName("refresh_token")
private String refreshToken;

public Credentials(String idToken, String accessToken, String type, String refreshToken) {
@SerializedName("expires_in")
private Long expiresIn;

public Credentials(String idToken, String accessToken, String type, String refreshToken, Long expiresIn) {
this.idToken = idToken;
this.accessToken = accessToken;
this.type = type;
this.refreshToken = refreshToken;
this.expiresIn = expiresIn;
}

/**
Expand Down Expand Up @@ -98,4 +102,8 @@ public String getType() {
public String getRefreshToken() {
return refreshToken;
}

public Long getExpiresIn() {
return expiresIn;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,28 @@ public void shouldUseFullScreen() throws Exception {

@Test
public void shouldMergeCredentials() throws Exception {
Credentials urlCredentials = new Credentials("urlId", "urlAccess", "urlType", "urlRefresh");
Credentials codeCredentials = new Credentials("codeId", "codeAccess", "codeType", "codeRefresh");
Credentials urlCredentials = new Credentials("urlId", "urlAccess", "urlType", "urlRefresh", 9999L);
Credentials codeCredentials = new Credentials("codeId", "codeAccess", "codeType", "codeRefresh", 9999L);
Credentials merged = OAuthManager.mergeCredentials(urlCredentials, codeCredentials);

assertThat(merged.getIdToken(), is(codeCredentials.getIdToken()));
assertThat(merged.getAccessToken(), is(codeCredentials.getAccessToken()));
assertThat(merged.getType(), is(codeCredentials.getType()));
assertThat(merged.getRefreshToken(), is(codeCredentials.getRefreshToken()));
assertThat(merged.getExpiresIn(), is(codeCredentials.getExpiresIn()));
}

@Test
public void shouldPreferNonNullValuesWhenMergingCredentials() throws Exception {
Credentials urlCredentials = new Credentials("urlId", "urlAccess", "urlType", "urlRefresh");
Credentials codeCredentials = new Credentials(null, null, null, null);
Credentials urlCredentials = new Credentials("urlId", "urlAccess", "urlType", "urlRefresh", 9999L);
Credentials codeCredentials = new Credentials(null, null, null, null, null);
Credentials merged = OAuthManager.mergeCredentials(urlCredentials, codeCredentials);

assertThat(merged.getIdToken(), is(urlCredentials.getIdToken()));
assertThat(merged.getAccessToken(), is(urlCredentials.getAccessToken()));
assertThat(merged.getType(), is(urlCredentials.getType()));
assertThat(merged.getRefreshToken(), is(urlCredentials.getRefreshToken()));
assertThat(merged.getExpiresIn(), is(urlCredentials.getExpiresIn()));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
@SuppressWarnings("deprecation")
@Test
public void shouldResumeWithIntentWithCodeGrant() throws Exception {
final Credentials codeCredentials = new Credentials("codeId", "codeAccess", "codeType", "codeRefresh");
final Credentials codeCredentials = new Credentials("codeId", "codeAccess", "codeType", "codeRefresh", 9999L);
PKCE pkce = Mockito.mock(PKCE.class);
Mockito.doAnswer(new Answer() {
@Override
Expand Down Expand Up @@ -1127,12 +1127,13 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
assertThat(credentialsCaptor.getValue().getAccessToken(), is("codeAccess"));
assertThat(credentialsCaptor.getValue().getRefreshToken(), is("codeRefresh"));
assertThat(credentialsCaptor.getValue().getType(), is("codeType"));
assertThat(credentialsCaptor.getValue().getExpiresIn(), is(9999L));
}

@SuppressWarnings("deprecation")
@Test
public void shouldResumeWithRequestCodeWithCodeGrant() throws Exception {
final Credentials codeCredentials = new Credentials("codeId", "codeAccess", "codeType", "codeRefresh");
final Credentials codeCredentials = new Credentials("codeId", "codeAccess", "codeType", "codeRefresh", 9999L);
PKCE pkce = Mockito.mock(PKCE.class);
Mockito.doAnswer(new Answer() {
@Override
Expand Down Expand Up @@ -1163,6 +1164,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
assertThat(credentialsCaptor.getValue().getAccessToken(), is("codeAccess"));
assertThat(credentialsCaptor.getValue().getRefreshToken(), is("codeRefresh"));
assertThat(credentialsCaptor.getValue().getType(), is("codeType"));
assertThat(credentialsCaptor.getValue().getExpiresIn(), is(9999L));
}

@SuppressWarnings("deprecation")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public void shouldReturnBasic() throws Exception {
assertThat(credentials.getIdToken(), is(nullValue()));
assertThat(credentials.getType(), equalTo("bearer"));
assertThat(credentials.getRefreshToken(), is(nullValue()));
assertThat(credentials.getExpiresIn(), is(86000L));
}

@Test
Expand All @@ -70,6 +71,7 @@ public void shouldReturnWithIdToken() throws Exception {
assertThat(credentials.getIdToken(), is(notNullValue()));
assertThat(credentials.getType(), equalTo("bearer"));
assertThat(credentials.getRefreshToken(), is(nullValue()));
assertThat(credentials.getExpiresIn(), is(86000L));
}

@Test
Expand All @@ -80,6 +82,7 @@ public void shouldReturnWithRefreshToken() throws Exception {
assertThat(credentials.getIdToken(), is(notNullValue()));
assertThat(credentials.getType(), equalTo("bearer"));
assertThat(credentials.getRefreshToken(), is(notNullValue()));
assertThat(credentials.getExpiresIn(), is(86000L));
}

private Credentials buildCredentialsFrom(Reader json) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class CredentialsTest {

@Before
public void setUp() throws Exception {
credentials = new Credentials("idToken", "accessToken", "type", "refreshToken");
credentials = new Credentials("idToken", "accessToken", "type", "refreshToken", 999999L);
}

@Test
Expand All @@ -35,4 +35,9 @@ public void getRefreshToken() throws Exception {
assertThat(credentials.getRefreshToken(), is("refreshToken"));
}

@Test
public void getExpiresIn() throws Exception {
assertThat(credentials.getExpiresIn(), is(999999L));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class DelegationTest {

@Before
public void setUp() throws Exception {
delegation = new Delegation("idToken", "type", 1234567890l);
delegation = new Delegation("idToken", "type", 1234567890L);
}

@Test
Expand All @@ -27,6 +27,6 @@ public void getType() throws Exception {

@Test
public void getExpiresIn() throws Exception {
assertThat(delegation.getExpiresIn(), is(1234567890l));
assertThat(delegation.getExpiresIn(), is(1234567890L));
}
}
3 changes: 2 additions & 1 deletion auth0/src/test/resources/credentials.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"access_token": "s6GS5FGJN2jfd4l6",
"token_type": "bearer"
"token_type": "bearer",
"expires_in": 86000
}
3 changes: 2 additions & 1 deletion auth0/src/test/resources/credentials_openid.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"id_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL3NhbXBsZXMuYXV0aDAuY29tLyIsInN1YiI6ImF1dGgwfDUzYjk5NWY4YmNlNjhkOWZjOTAwMDk5YyIsImF1ZCI6Ikk5bWhVcmZrVEdGVldqbEVxWlNUQ0JVRkFCTGJKRkdMMyIsImV4cCI6MTQ2NTEwOTAzMywiaWF0IjoxNDY1MDczMDMzfQ.TdRc-lnVcX0LT7ZySzVysjVcYzAUIRnCPufTO8VV6g8",
"access_token": "s6GS5FGJN2jfd4l6",
"token_type": "bearer"
"token_type": "bearer",
"expires_in": 86000
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"refresh_token": "qOsimOhQ2wLoSeymtGeQ4Vumx04t9IbrfaywOVYFVvxZt",
"id_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL3NhbXBsZXMuYXV0aDAuY29tLyIsInN1YiI6ImF1dGgwfDUzYjk5NWY4YmNlNjhkOWZjOTAwMDk5YyIsImF1ZCI6Ikk5bWhVcmZrVEdGVldqbEVxWlNUQ0JVRkFCTGJKRkdMMyIsImV4cCI6MTQ2NTEwOTAzMywiaWF0IjoxNDY1MDczMDMzfQ.TdRc-lnVcX0LT7ZySzVysjVcYzAUIRnCPufTO8VV6g8",
"access_token": "s6GS5FGJN2jfd4l6",
"token_type": "bearer"
"token_type": "bearer",
"expires_in": 86000
}

0 comments on commit 3dccb80

Please sign in to comment.