Skip to content

Commit cd55aa7

Browse files
committed
Review comments addressed
1 parent 58ed59b commit cd55aa7

File tree

13 files changed

+322
-288
lines changed

13 files changed

+322
-288
lines changed

EXAMPLES.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -545,14 +545,14 @@ authentication
545545

546546
This feature allows you to authenticate a user in a web session using the refresh token obtained from the native session without requiring the user to log in again.
547547

548-
Call the api to fetch a webSessionTransferToken in exchange for a refresh token. Use the obtained token to authenticate the user by calling the `/authorize` end point.
548+
Call the API to fetch a webSessionTransferToken in exchange for a refresh token. Use the obtained token to authenticate the user by calling the `/authorize` end point by passing as a query parameter or a cookie value.
549549

550550
```kotlin
551551
authentication
552-
.fetchSessionTransferToken("refresh_token")
553-
.start(object : Callback<SessionTransferCredentials, AuthenticationException> {
554-
override fun onSuccess(result: SessionTransferCredentials) {
555-
// Use the web_sso token to authenticate the user in a web session in your app
552+
.ssoExchange("refresh_token")
553+
.start(object : Callback<SSOCredentials, AuthenticationException> {
554+
override fun onSuccess(result: SSOCredentials) {
555+
// Use the sessionTransferToken token to authenticate the user in a web session in your app
556556
}
557557

558558
override fun onFailure(exception: AuthenticationException) {
@@ -567,8 +567,8 @@ Call the api to fetch a webSessionTransferToken in exchange for a refresh token.
567567

568568
``` kotlin
569569
try {
570-
val sessionTransferCredentials = authentication
571-
.fetchSessionTransferToken("refresh_token")
570+
val ssoCredentials = authentication
571+
.ssoExchange("refresh_token")
572572
.await()
573573
} catch (e: AuthenticationException) {
574574
e.printStacktrace()
@@ -581,10 +581,10 @@ try {
581581

582582
```java
583583
authentication
584-
.fetchSessionTransferToken("refresh_token")
585-
.start(new Callback<SessionTransferCredentials, AuthenticationException>() {
584+
.ssoExchange("refresh_token")
585+
.start(new Callback<SSOCredentials, AuthenticationException>() {
586586
@Override
587-
public void onSuccess(@Nullable SessionTransferCredentials result) {
587+
public void onSuccess(@Nullable SSOCredentials result) {
588588
// Handle success
589589
}
590590
@Override

auth0/src/main/java/com/auth0/android/authentication/AuthenticationAPIClient.kt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import com.auth0.android.result.Credentials
1414
import com.auth0.android.result.DatabaseUser
1515
import com.auth0.android.result.PasskeyChallenge
1616
import com.auth0.android.result.PasskeyRegistrationChallenge
17-
import com.auth0.android.result.SessionTransferCredentials
17+
import com.auth0.android.result.SSOCredentials
1818
import com.auth0.android.result.UserProfile
1919
import com.google.gson.Gson
2020
import okhttp3.HttpUrl.Companion.toHttpUrl
@@ -923,19 +923,25 @@ public class AuthenticationAPIClient @VisibleForTesting(otherwise = VisibleForTe
923923
}
924924

925925
/**
926-
* Creates a new request to fetch a web sso token in exchange for a refresh token.
926+
* Creates a new request to exchange a refresh token for a session transfer token that can be used to perform web single sign-on.
927+
*
928+
* When opening your website on any browser or web view, add the session transfer token to the URL as a query
929+
* parameter. Then your website can redirect the user to Auth0's `/authorize` endpoint, passing along the query
930+
* parameter with the session transfer token. For example,
931+
* `https://example.com/login?session_transfer_token=THE_TOKEN`.
932+
*
927933
*
928934
* @param refreshToken A valid refresh token obtained as part of Auth0 authentication
929-
* @return a request to fetch a web sso token
935+
* @return a request to fetch a session transfer token
930936
*
931937
*/
932-
public fun fetchSessionTransferToken(refreshToken: String): Request<SessionTransferCredentials, AuthenticationException> {
938+
public fun ssoExchange(refreshToken: String): Request<SSOCredentials, AuthenticationException> {
933939
val params = ParameterBuilder.newBuilder()
934940
.setGrantType(ParameterBuilder.REFRESH_TOKEN_KEY)
935941
.setAudience("urn:${auth0.domain}:session_transfer")
936942
.set(ParameterBuilder.REFRESH_TOKEN_KEY, refreshToken)
937943
.asDictionary()
938-
return loginWithTokenGeneric<SessionTransferCredentials>(params)
944+
return loginWithTokenGeneric<SSOCredentials>(params)
939945
}
940946

941947
/**

auth0/src/main/java/com/auth0/android/authentication/ParameterBuilder.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,6 @@ public class ParameterBuilder private constructor(parameters: Map<String, String
160160
public const val GRANT_TYPE_TOKEN_EXCHANGE: String =
161161
"urn:ietf:params:oauth:grant-type:token-exchange"
162162
public const val GRANT_TYPE_PASSKEY :String = "urn:okta:params:oauth:grant-type:webauthn"
163-
public const val TOKEN_TYPE_REFRESH_TOKEN :String = "urn:ietf:params:oauth:token-type:refresh_token"
164-
public const val TOKEN_TYPE_SESSION_TRANSFER_TOKEN :String = "urn:auth0:params:oauth:token-type:session_transfer_token"
165-
public const val SCOPE_OPENID: String = "openid"
166163
public const val SCOPE_OFFLINE_ACCESS: String = "openid offline_access"
167164
public const val SCOPE_KEY: String = "scope"
168165
public const val REFRESH_TOKEN_KEY: String = "refresh_token"

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import androidx.annotation.VisibleForTesting
44
import com.auth0.android.authentication.AuthenticationAPIClient
55
import com.auth0.android.callback.Callback
66
import com.auth0.android.result.Credentials
7-
import com.auth0.android.result.SessionTransferCredentials
7+
import com.auth0.android.result.SSOCredentials
88
import com.auth0.android.util.Clock
99
import java.util.*
1010

@@ -31,13 +31,13 @@ public abstract class BaseCredentialsManager internal constructor(
3131
@Throws(CredentialsManagerException::class)
3232
public abstract fun saveCredentials(credentials: Credentials)
3333
public abstract fun getCredentials(callback: Callback<Credentials, CredentialsManagerException>)
34-
public abstract fun getSessionTransferCredentials(
34+
public abstract fun getSsoCredentials(
3535
parameters: Map<String, String>,
36-
callback: Callback<SessionTransferCredentials, CredentialsManagerException>
36+
callback: Callback<SSOCredentials, CredentialsManagerException>
3737
)
3838

39-
public abstract fun getSessionTransferCredentials(
40-
callback: Callback<SessionTransferCredentials, CredentialsManagerException>
39+
public abstract fun getSsoCredentials(
40+
callback: Callback<SSOCredentials, CredentialsManagerException>
4141
)
4242

4343
public abstract fun getCredentials(
@@ -72,13 +72,13 @@ public abstract class BaseCredentialsManager internal constructor(
7272

7373
@JvmSynthetic
7474
@Throws(CredentialsManagerException::class)
75-
public abstract suspend fun awaitSessionTransferCredentials(parameters: Map<String, String>)
76-
: SessionTransferCredentials
75+
public abstract suspend fun awaitSsoCredentials(parameters: Map<String, String>)
76+
: SSOCredentials
7777

7878
@JvmSynthetic
7979
@Throws(CredentialsManagerException::class)
80-
public abstract suspend fun awaitSessionTransferCredentials()
81-
: SessionTransferCredentials
80+
public abstract suspend fun awaitSsoCredentials()
81+
: SSOCredentials
8282

8383
@JvmSynthetic
8484
@Throws(CredentialsManagerException::class)

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

Lines changed: 58 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import com.auth0.android.authentication.AuthenticationAPIClient
66
import com.auth0.android.authentication.AuthenticationException
77
import com.auth0.android.callback.Callback
88
import com.auth0.android.result.Credentials
9-
import com.auth0.android.result.SessionTransferCredentials
9+
import com.auth0.android.result.SSOCredentials
1010
import kotlinx.coroutines.suspendCancellableCoroutine
1111
import java.util.*
1212
import java.util.concurrent.Executor
@@ -55,22 +55,34 @@ public class CredentialsManager @VisibleForTesting(otherwise = VisibleForTesting
5555
}
5656

5757
/**
58-
* Fetches a new [SessionTransferCredentials] . It will fail with [CredentialsManagerException]
59-
* if the existing refresh_token is null or no longer valid. This method will handle saving the refresh_token,
60-
* if a new one is issued.
58+
* Creates a new request to exchange a refresh token for a session transfer token that can be used to perform web single sign-on.
59+
*
60+
* When opening your website on any browser or web view, add the session transfer token to the URL as a query
61+
* parameter. Then your website can redirect the user to Auth0's `/authorize` endpoint, passing along the query
62+
* parameter with the session transfer token. For example,
63+
* `https://example.com/login?session_transfer_token=THE_TOKEN`.
64+
*
65+
* It will fail with [CredentialsManagerException] if the existing refresh_token is null or no longer valid.
66+
* This method will handle saving the refresh_token, if a new one is issued.
6167
*/
62-
override fun getSessionTransferCredentials(callback: Callback<SessionTransferCredentials, CredentialsManagerException>) {
63-
getSessionTransferCredentials(emptyMap(), callback)
68+
override fun getSsoCredentials(callback: Callback<SSOCredentials, CredentialsManagerException>) {
69+
getSsoCredentials(emptyMap(), callback)
6470
}
6571

6672
/**
67-
* Fetches a new [SessionTransferCredentials] . It will fail with [CredentialsManagerException]
68-
* if the existing refresh_token is null or no longer valid. This method will handle saving the refresh_token,
69-
* if a new one is issued.
73+
* Creates a new request to exchange a refresh token for a session transfer token that can be used to perform web single sign-on.
74+
*
75+
* When opening your website on any browser or web view, add the session transfer token to the URL as a query
76+
* parameter. Then your website can redirect the user to Auth0's `/authorize` endpoint, passing along the query
77+
* parameter with the session transfer token. For example,
78+
* `https://example.com/login?session_transfer_token=THE_TOKEN`.
79+
*
80+
* It will fail with [CredentialsManagerException] if the existing refresh_token is null or no longer valid.
81+
* This method will handle saving the refresh_token, if a new one is issued.
7082
*/
71-
override fun getSessionTransferCredentials(
83+
override fun getSsoCredentials(
7284
parameters: Map<String, String>,
73-
callback: Callback<SessionTransferCredentials, CredentialsManagerException>
85+
callback: Callback<SSOCredentials, CredentialsManagerException>
7486
) {
7587
serialExecutor.execute {
7688
val refreshToken = storage.retrieveString(KEY_REFRESH_TOKEN)
@@ -79,21 +91,18 @@ public class CredentialsManager @VisibleForTesting(otherwise = VisibleForTesting
7991
return@execute
8092
}
8193

82-
val request = authenticationClient.fetchSessionTransferToken(refreshToken)
94+
val request = authenticationClient.ssoExchange(refreshToken)
8395
try {
8496
if (parameters.isNotEmpty()) {
8597
request.addParameters(parameters)
8698
}
8799
val sessionTransferCredentials = request.execute()
88-
saveSessionTransferCredentials(sessionTransferCredentials)
100+
saveSsoCredentials(sessionTransferCredentials)
89101
callback.onSuccess(sessionTransferCredentials)
90102
} catch (error: AuthenticationException) {
91103
val exception = when {
92-
error.isRefreshTokenDeleted ||
93-
error.isInvalidRefreshToken -> CredentialsManagerException.Code.RENEW_FAILED
94-
95104
error.isNetworkError -> CredentialsManagerException.Code.NO_NETWORK
96-
else -> CredentialsManagerException.Code.API_ERROR
105+
else -> CredentialsManagerException.Code.SSO_EXCHANGE_FAILED
97106
}
98107
callback.onFailure(
99108
CredentialsManagerException(
@@ -106,29 +115,41 @@ public class CredentialsManager @VisibleForTesting(otherwise = VisibleForTesting
106115
}
107116

108117
/**
109-
* Fetches a new [SessionTransferCredentials] . It will fail with [CredentialsManagerException]
110-
* if the existing refresh_token is null or no longer valid. This method will handle saving the refresh_token,
111-
* if a new one is issued.
118+
* Creates a new request to exchange a refresh token for a session transfer token that can be used to perform web single sign-on.
119+
*
120+
* When opening your website on any browser or web view, add the session transfer token to the URL as a query
121+
* parameter. Then your website can redirect the user to Auth0's `/authorize` endpoint, passing along the query
122+
* parameter with the session transfer token. For example,
123+
* `https://example.com/login?session_transfer_token=THE_TOKEN`.
124+
*
125+
* It will fail with [CredentialsManagerException] if the existing refresh_token is null or no longer valid.
126+
* This method will handle saving the refresh_token, if a new one is issued.
112127
*/
113128
@JvmSynthetic
114129
@Throws(CredentialsManagerException::class)
115-
override suspend fun awaitSessionTransferCredentials(): SessionTransferCredentials {
116-
return awaitSessionTransferCredentials(emptyMap())
130+
override suspend fun awaitSsoCredentials(): SSOCredentials {
131+
return awaitSsoCredentials(emptyMap())
117132
}
118133

119134
/**
120-
* Fetches a new [SessionTransferCredentials] . It will fail with [CredentialsManagerException]
121-
* if the existing refresh_token is null or no longer valid. This method will handle saving the refresh_token,
122-
* if a new one is issued.
135+
* Creates a new request to exchange a refresh token for a session transfer token that can be used to perform web single sign-on.
136+
*
137+
* When opening your website on any browser or web view, add the session transfer token to the URL as a query
138+
* parameter. Then your website can redirect the user to Auth0's `/authorize` endpoint, passing along the query
139+
* parameter with the session transfer token. For example,
140+
* `https://example.com/login?session_transfer_token=THE_TOKEN`.
141+
*
142+
* It will fail with [CredentialsManagerException] if the existing refresh_token is null or no longer valid.
143+
* This method will handle saving the refresh_token, if a new one is issued.
123144
*/
124145
@JvmSynthetic
125146
@Throws(CredentialsManagerException::class)
126-
override suspend fun awaitSessionTransferCredentials(parameters: Map<String, String>): SessionTransferCredentials {
147+
override suspend fun awaitSsoCredentials(parameters: Map<String, String>): SSOCredentials {
127148
return suspendCancellableCoroutine { continuation ->
128-
getSessionTransferCredentials(
149+
getSsoCredentials(
129150
parameters,
130-
object : Callback<SessionTransferCredentials, CredentialsManagerException> {
131-
override fun onSuccess(result: SessionTransferCredentials) {
151+
object : Callback<SSOCredentials, CredentialsManagerException> {
152+
override fun onSuccess(result: SSOCredentials) {
132153
continuation.resume(result)
133154
}
134155

@@ -466,21 +487,21 @@ public class CredentialsManager @VisibleForTesting(otherwise = VisibleForTesting
466487
}
467488

468489
/**
469-
* Helper method to store the given [SessionTransferCredentials] refresh token in the storage.
470-
* Method will silently return ,if the passed credentials has no refresh token.
490+
* Helper method to store the given [SSOCredentials] refresh token in the storage.
491+
* Method will silently return if the passed credentials have no refresh token.
471492
*
472-
* @param sessionTransferCredentials the credentials to save in the storage.
493+
* @param ssoCredentials the credentials to save in the storage.
473494
*/
474495
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
475-
internal fun saveSessionTransferCredentials(sessionTransferCredentials: SessionTransferCredentials) {
476-
storage.store(KEY_ID_TOKEN, sessionTransferCredentials.idToken)
496+
internal fun saveSsoCredentials(ssoCredentials: SSOCredentials) {
497+
storage.store(KEY_ID_TOKEN, ssoCredentials.idToken)
477498
val existingRefreshToken = storage.retrieveString(KEY_REFRESH_TOKEN)
478499
// Checking if the existing one needs to be replaced with the new one
479-
if (sessionTransferCredentials.refreshToken.isNullOrEmpty())
500+
if (ssoCredentials.refreshToken.isNullOrEmpty())
480501
return // No refresh token to save
481-
if (sessionTransferCredentials.refreshToken == existingRefreshToken)
502+
if (ssoCredentials.refreshToken == existingRefreshToken)
482503
return // Same refresh token, no need to save
483-
storage.store(KEY_REFRESH_TOKEN, sessionTransferCredentials.refreshToken)
504+
storage.store(KEY_REFRESH_TOKEN, ssoCredentials.refreshToken)
484505
}
485506

486507
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ public class CredentialsManagerException :
4444
BIOMETRICS_INVALID_USER,
4545
BIOMETRIC_AUTHENTICATION_FAILED,
4646
NO_NETWORK,
47-
API_ERROR
47+
API_ERROR,
48+
SSO_EXCHANGE_FAILED,
4849
}
4950

5051
private var code: Code?
@@ -142,6 +143,8 @@ public class CredentialsManagerException :
142143
CredentialsManagerException(Code.NO_NETWORK)
143144
public val API_ERROR: CredentialsManagerException =
144145
CredentialsManagerException(Code.API_ERROR)
146+
public val SSO_EXCHANGE_FAILED: CredentialsManagerException =
147+
CredentialsManagerException(Code.SSO_EXCHANGE_FAILED)
145148

146149

147150
private fun getMessage(code: Code): String {
@@ -187,6 +190,7 @@ public class CredentialsManagerException :
187190
Code.BIOMETRIC_AUTHENTICATION_FAILED -> "Biometric authentication failed."
188191
Code.NO_NETWORK -> "Failed to execute the network request."
189192
Code.API_ERROR -> "An error occurred while processing the request."
193+
Code.SSO_EXCHANGE_FAILED ->"The exchange of the refresh token for SSO credentials failed."
190194
}
191195
}
192196
}

0 commit comments

Comments
 (0)