Skip to content

Commit aab6508

Browse files
committed
review comments addressed
1 parent 3320784 commit aab6508

File tree

6 files changed

+28
-54
lines changed

6 files changed

+28
-54
lines changed

auth0/src/main/java/com/auth0/android/authentication/storage/CredentialsManager.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,10 @@ public class CredentialsManager @VisibleForTesting(otherwise = VisibleForTesting
325325

326326
/**
327327
* Retrieves API credentials from storage and automatically renews them using the refresh token if the access
328-
* token is expired. Otherwise, the retrieved API credentials will be returned via the success case as they are still valid.
328+
* token is expired. Otherwise, the retrieved API credentials will be returned as they are still valid.
329329
*
330330
* If there are no stored API credentials, the refresh token will be exchanged for a new set of API credentials.
331-
* New or renewed API credentials will be automatically stored in storage.
332-
* This is a Coroutine that is exposed only for Kotlin.
331+
* New or renewed API credentials will be automatically persisted in storage.
333332
*
334333
* @param audience Identifier of the API that your application is requesting access to.
335334
* @param scope the scope to request for the access token. If null is passed, the previous scope will be kept.
@@ -556,10 +555,10 @@ public class CredentialsManager @VisibleForTesting(otherwise = VisibleForTesting
556555

557556
/**
558557
* Retrieves API credentials from storage and automatically renews them using the refresh token if the access
559-
* token is expired. Otherwise, the retrieved API credentials will be returned via the success case as they are still valid.
558+
* token is expired. Otherwise, the retrieved API credentials will be returned via the success callback as they are still valid.
560559
*
561560
* If there are no stored API credentials, the refresh token will be exchanged for a new set of API credentials.
562-
* New or renewed API credentials will be automatically stored in storage.
561+
* New or renewed API credentials will be automatically persisted in storage.
563562
*
564563
* @param audience Identifier of the API that your application is requesting access to.
565564
* @param scope the scope to request for the access token. If null is passed, the previous scope will be kept.
@@ -691,6 +690,7 @@ public class CredentialsManager @VisibleForTesting(otherwise = VisibleForTesting
691690
*/
692691
override fun clearApiCredentials(audience: String) {
693692
storage.remove(audience)
693+
Log.d(TAG, "API Credentials for $audience were just removed from the storage")
694694
}
695695

696696
/**

auth0/src/main/java/com/auth0/android/authentication/storage/SecureCredentialsManager.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -439,11 +439,10 @@ public class SecureCredentialsManager @VisibleForTesting(otherwise = VisibleForT
439439

440440
/**
441441
* Retrieves API credentials from storage and automatically renews them using the refresh token if the access
442-
* token is expired. Otherwise, the retrieved API credentials will be returned via the success case as they are still valid.
442+
* token is expired. Otherwise, the retrieved API credentials will be returned as they are still valid.
443443
*
444444
* If there are no stored API credentials, the refresh token will be exchanged for a new set of API credentials.
445-
* New or renewed API credentials will be automatically stored in storage.
446-
* This is a Coroutine that is exposed only for Kotlin.
445+
* New or renewed API credentials will be automatically persisted in storage.
447446
*
448447
* @param audience Identifier of the API that your application is requesting access to.
449448
* @param scope the scope to request for the access token. If null is passed, the previous scope will be kept.
@@ -627,10 +626,10 @@ public class SecureCredentialsManager @VisibleForTesting(otherwise = VisibleForT
627626

628627
/**
629628
* Retrieves API credentials from storage and automatically renews them using the refresh token if the access
630-
* token is expired. Otherwise, the retrieved API credentials will be returned via the success case as they are still valid.
629+
* token is expired. Otherwise, the retrieved API credentials will be returned via the success callback as they are still valid.
631630
*
632631
* If there are no stored API credentials, the refresh token will be exchanged for a new set of API credentials.
633-
* New or renewed API credentials will be automatically stored in storage.
632+
* New or renewed API credentials will be automatically persisted in storage.
634633
*
635634
* @param audience Identifier of the API that your application is requesting access to.
636635
* @param scope the scope to request for the access token. If null is passed, the previous scope will be kept.
@@ -873,7 +872,7 @@ public class SecureCredentialsManager @VisibleForTesting(otherwise = VisibleForT
873872
internal fun continueGetApiCredentials(
874873
audience: String,
875874
scope: String?,
876-
minTtl: Int = 0,
875+
minTtl: Int,
877876
parameters: Map<String, String>,
878877
headers: Map<String, String>,
879878
callback: Callback<APICredentials, CredentialsManagerException>

auth0/src/main/java/com/auth0/android/result/APICredentials.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public data class APICredentials(
3636
@field:SerializedName("expires_at")
3737
val expiresAt: Date,
3838
/**
39-
* Getter for the access token's granted scope. Only available if the requested scope differs from the granted one.
39+
* Getter for the access token's granted scope.
4040
*
4141
* @return the granted scope.
4242
*/
@@ -53,6 +53,6 @@ public data class APICredentials(
5353
* Converts a Credentials instance to an APICredentials instance.
5454
*/
5555
internal fun Credentials.toAPICredentials(): APICredentials {
56-
val newScope = scope ?: "openid"
56+
val newScope = scope ?: ""
5757
return APICredentials(accessToken, type, expiresAt, newScope)
5858
}

auth0/src/test/java/com/auth0/android/authentication/AuthenticationAPIClientTest.kt

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2561,7 +2561,7 @@ public class AuthenticationAPIClientTest {
25612561
val auth0 = auth0
25622562
val client = AuthenticationAPIClient(auth0)
25632563
mockAPI.willReturnSuccessfulLogin()
2564-
val credentials = client.renewAuth(refreshToken = "refreshToken", scope = "read:data")
2564+
val credentials = client.renewAuth(refreshToken = "refreshToken", scope = "openid read:data")
25652565
.execute()
25662566
val request = mockAPI.takeRequest()
25672567
assertThat(
@@ -2574,7 +2574,7 @@ public class AuthenticationAPIClientTest {
25742574
assertThat(body, Matchers.hasEntry("client_id", CLIENT_ID))
25752575
assertThat(body, Matchers.hasEntry("refresh_token", "refreshToken"))
25762576
assertThat(body, Matchers.hasEntry("grant_type", "refresh_token"))
2577-
assertThat(body, Matchers.hasEntry("scope", "read:data openid"))
2577+
assertThat(body, Matchers.hasEntry("scope", "openid read:data"))
25782578
assertThat(body, Matchers.not(Matchers.hasKey("audience")))
25792579
assertThat(credentials, Matchers.`is`(Matchers.notNullValue()))
25802580
}
@@ -2602,30 +2602,6 @@ public class AuthenticationAPIClientTest {
26022602
assertThat(credentials, Matchers.`is`(Matchers.notNullValue()))
26032603
}
26042604

2605-
@Test
2606-
public fun shouldRenewAuthWithOAuthAudienceAndScopeNotEnforcingOpendId() {
2607-
val auth0 = auth0
2608-
val client = AuthenticationAPIClient(auth0)
2609-
mockAPI.willReturnSuccessfulLogin()
2610-
val credentials =
2611-
client.renewAuth("refreshToken", "_audience", "openid read:data write:data")
2612-
.execute()
2613-
val request = mockAPI.takeRequest()
2614-
assertThat(
2615-
request.getHeader("Accept-Language"), Matchers.`is`(
2616-
defaultLocale
2617-
)
2618-
)
2619-
assertThat(request.path, Matchers.equalTo("/oauth/token"))
2620-
val body = bodyFromRequest<String>(request)
2621-
assertThat(body, Matchers.hasEntry("client_id", CLIENT_ID))
2622-
assertThat(body, Matchers.hasEntry("refresh_token", "refreshToken"))
2623-
assertThat(body, Matchers.hasEntry("grant_type", "refresh_token"))
2624-
assertThat(body, Matchers.hasEntry("audience", "_audience"))
2625-
assertThat(body, Matchers.hasEntry("scope", "openid read:data write:data"))
2626-
assertThat(credentials, Matchers.`is`(Matchers.notNullValue()))
2627-
}
2628-
26292605
@Test
26302606
public fun shouldFetchProfileSyncAfterLoginRequest() {
26312607
mockAPI.willReturnSuccessfulLogin()

auth0/src/test/java/com/auth0/android/authentication/storage/CredentialsManagerTest.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ public class CredentialsManagerTest {
502502
Mockito.`when`(storage.retrieveString("audience")).thenReturn(gson.toJson(apiCredentials))
503503
Mockito.`when`(storage.retrieveString("com.auth0.refresh_token")).thenReturn("refreshToken")
504504
Mockito.`when`(
505-
client.renewAuth("refreshToken", "audience", "newScope")
505+
client.renewAuth("refreshToken", "audience", "scope")
506506
).thenReturn(request)
507507
val newDate = Date(CredentialsMock.ONE_HOUR_AHEAD_MS + ONE_HOUR_SECONDS * 1000)
508508
val jwtMock = mock<Jwt>()
@@ -512,9 +512,9 @@ public class CredentialsManagerTest {
512512
// Trigger success
513513
val newRefresh: String? = null
514514
val renewedCredentials =
515-
Credentials("newId", "newAccess", "newType", newRefresh, newDate, "newScope")
515+
Credentials("newId", "newAccess", "newType", newRefresh, newDate, "scope")
516516
Mockito.`when`(request.execute()).thenReturn(renewedCredentials)
517-
manager.getApiCredentials("audience", "newScope", callback = apiCredentialsCallback)
517+
manager.getApiCredentials("audience", "scope", callback = apiCredentialsCallback)
518518
verify(apiCredentialsCallback).onSuccess(
519519
apiCredentialsCaptor.capture()
520520
)
@@ -530,7 +530,7 @@ public class CredentialsManagerTest {
530530
MatcherAssert.assertThat(newAPiCredentials.accessToken, Is.`is`("newAccess"))
531531
MatcherAssert.assertThat(newAPiCredentials.type, Is.`is`("newType"))
532532
MatcherAssert.assertThat(newAPiCredentials.expiresAt, Is.`is`(newDate))
533-
MatcherAssert.assertThat(newAPiCredentials.scope, Is.`is`("newScope"))
533+
MatcherAssert.assertThat(newAPiCredentials.scope, Is.`is`("scope"))
534534
}
535535

536536
@Test
@@ -544,7 +544,7 @@ public class CredentialsManagerTest {
544544
Mockito.`when`(storage.retrieveString("audience")).thenReturn(gson.toJson(apiCredentials))
545545
Mockito.`when`(storage.retrieveString("com.auth0.refresh_token")).thenReturn("refreshToken")
546546
Mockito.`when`(
547-
client.renewAuth("refreshToken", "audience", "newScope")
547+
client.renewAuth("refreshToken", "audience", "scope")
548548
).thenReturn(request)
549549
val newDate = Date(CredentialsMock.ONE_HOUR_AHEAD_MS + ONE_HOUR_SECONDS * 1000)
550550
val jwtMock = mock<Jwt>()
@@ -554,9 +554,9 @@ public class CredentialsManagerTest {
554554
// Trigger success
555555
val newRefresh: String? = null
556556
val renewedCredentials =
557-
Credentials("newId", "newAccess", "newType", newRefresh, newDate, "newScope")
557+
Credentials("newId", "newAccess", "newType", newRefresh, newDate, "scope")
558558
Mockito.`when`(request.execute()).thenReturn(renewedCredentials)
559-
manager.getApiCredentials("audience", "newScope", minTtl = 10, callback = apiCredentialsCallback)
559+
manager.getApiCredentials("audience", "scope", minTtl = 10, callback = apiCredentialsCallback)
560560
verify(apiCredentialsCallback).onSuccess(
561561
apiCredentialsCaptor.capture()
562562
)
@@ -572,7 +572,7 @@ public class CredentialsManagerTest {
572572
MatcherAssert.assertThat(newAPiCredentials.accessToken, Is.`is`("newAccess"))
573573
MatcherAssert.assertThat(newAPiCredentials.type, Is.`is`("newType"))
574574
MatcherAssert.assertThat(newAPiCredentials.expiresAt, Is.`is`(newDate))
575-
MatcherAssert.assertThat(newAPiCredentials.scope, Is.`is`("newScope"))
575+
MatcherAssert.assertThat(newAPiCredentials.scope, Is.`is`("scope"))
576576
}
577577

578578
@Test
@@ -641,7 +641,7 @@ public class CredentialsManagerTest {
641641

642642
// Verify the credentials are property stored
643643
verify(storage).store("com.auth0.id_token", renewedCredentials.idToken)
644-
// RefreshToken should not be replaced
644+
// RefreshToken should be replaced
645645
verify(storage).store("com.auth0.refresh_token", "newRefreshToken")
646646
verify(storage).store("audience", gson.toJson(renewedCredentials.toAPICredentials()))
647647
// Verify the returned credentials are the latest

auth0/src/test/java/com/auth0/android/authentication/storage/SecureCredentialsManagerTest.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import com.google.gson.Gson
2424
import com.nhaarman.mockitokotlin2.KArgumentCaptor
2525
import com.nhaarman.mockitokotlin2.any
2626
import com.nhaarman.mockitokotlin2.argumentCaptor
27+
import com.nhaarman.mockitokotlin2.doNothing
2728
import com.nhaarman.mockitokotlin2.eq
2829
import com.nhaarman.mockitokotlin2.mock
2930
import com.nhaarman.mockitokotlin2.never
@@ -2176,18 +2177,20 @@ public class SecureCredentialsManagerTest {
21762177
val renewedCredentials =
21772178
Credentials("newId", "newAccess", "newType", newRefresh, newDate, "newScope")
21782179
Mockito.`when`(request.execute()).thenReturn(renewedCredentials)
2179-
val expectedJson = gson.toJson(renewedCredentials)
2180+
val updatedCredentials = renewedCredentials.copy(refreshToken = "refreshToken")
2181+
val expectedJson = gson.toJson(updatedCredentials)
21802182
Mockito.`when`(crypto.encrypt(any()))
21812183
.thenReturn(expectedJson.toByteArray())
21822184
manager.getApiCredentials("audience", "newScope", callback = apiCredentialsCallback)
21832185
verify(apiCredentialsCallback).onSuccess(
21842186
apiCredentialsCaptor.capture()
21852187
)
21862188

2187-
21882189
// Verify the credentials are property stored
21892190
verify(storage).store(eq("com.auth0.credentials"), stringCaptor.capture())
21902191
MatcherAssert.assertThat(stringCaptor.firstValue, Is.`is`(Matchers.notNullValue()))
2192+
val credentials = gson.fromJson(expectedJson,Credentials::class.java)
2193+
Assert.assertEquals("refreshToken",credentials.refreshToken)
21912194
// Verify the returned credentials are the latest
21922195
val newAPiCredentials = apiCredentialsCaptor.firstValue
21932196
MatcherAssert.assertThat(newAPiCredentials, Is.`is`(Matchers.notNullValue()))
@@ -2233,7 +2236,6 @@ public class SecureCredentialsManagerTest {
22332236
apiCredentialsCaptor.capture()
22342237
)
22352238

2236-
// Verify the returned credentials are the latest
22372239
val newAPiCredentials = apiCredentialsCaptor.firstValue
22382240
MatcherAssert.assertThat(newAPiCredentials, Is.`is`(Matchers.notNullValue()))
22392241
MatcherAssert.assertThat(newAPiCredentials.accessToken, Is.`is`("newAccess"))
@@ -2283,7 +2285,6 @@ public class SecureCredentialsManagerTest {
22832285
apiCredentialsCaptor.capture()
22842286
)
22852287

2286-
// Verify the returned credentials are the latest
22872288
val newAPiCredentials = apiCredentialsCaptor.firstValue
22882289
MatcherAssert.assertThat(newAPiCredentials, Is.`is`(Matchers.notNullValue()))
22892290
MatcherAssert.assertThat(newAPiCredentials.accessToken, Is.`is`("newAccess"))
@@ -2328,7 +2329,6 @@ public class SecureCredentialsManagerTest {
23282329
apiCredentialsCaptor.capture()
23292330
)
23302331

2331-
// Verify the returned credentials are the latest
23322332
val newAPiCredentials = apiCredentialsCaptor.firstValue
23332333
MatcherAssert.assertThat(newAPiCredentials, Is.`is`(Matchers.notNullValue()))
23342334
MatcherAssert.assertThat(newAPiCredentials.accessToken, Is.`is`("newAccess"))
@@ -2425,7 +2425,6 @@ public class SecureCredentialsManagerTest {
24252425
exceptionCaptor.capture()
24262426
)
24272427

2428-
// Verify the returned credentials are the latest
24292428
val exception = exceptionCaptor.firstValue
24302429
MatcherAssert.assertThat(exception, Is.`is`(Matchers.notNullValue()))
24312430
}

0 commit comments

Comments
 (0)