Skip to content

Commit d98240c

Browse files
committed
Changes to align the api with the new server contract
1 parent bd87beb commit d98240c

File tree

11 files changed

+313
-331
lines changed

11 files changed

+313
-331
lines changed

EXAMPLES.md

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -541,20 +541,17 @@ authentication
541541
</details>
542542

543543

544-
## Native to Web SSO login (Experimental)
545-
> **Warning**
546-
>
547-
> Native to Web SSO login support in Auth0.Android is still experimental and can change in the future.
544+
## Native to Web SSO login
548545

549546
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.
550547

551-
Call the api to fetch a webSsoToken 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.
552549

553550
```kotlin
554551
authentication
555-
.fetchWebSsoToken("refresh_token")
556-
.start(object : Callback<SSOCredentials, AuthenticationException> {
557-
override fun onSuccess(result: SSOCredentials) {
552+
.fetchSessionTransferToken("refresh_token")
553+
.start(object : Callback<SessionTransferCredentials, AuthenticationException> {
554+
override fun onSuccess(result: SessionTransferCredentials) {
558555
// Use the web_sso token to authenticate the user in a web session in your app
559556
}
560557

@@ -570,8 +567,8 @@ Call the api to fetch a webSsoToken in exchange for a refresh token. Use the obt
570567

571568
``` kotlin
572569
try {
573-
val ssoCredentials = authentication
574-
.fetchWebSsoToken("refresh_token")
570+
val sessionTransferCredentials = authentication
571+
.fetchSessionTransferToken("refresh_token")
575572
.await()
576573
} catch (e: AuthenticationException) {
577574
e.printStacktrace()
@@ -584,10 +581,10 @@ try {
584581

585582
```java
586583
authentication
587-
.fetchWebSsoToken("refresh_token")
588-
.start(new Callback<SSOCredentials, AuthenticationException>() {
584+
.fetchSessionTransferToken("refresh_token")
585+
.start(new Callback<SessionTransferCredentials, AuthenticationException>() {
589586
@Override
590-
public void onSuccess(@Nullable SSOCredentials payload) {
587+
public void onSuccess(@Nullable SessionTransferCredentials result) {
591588
// Handle success
592589
}
593590
@Override

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import androidx.annotation.VisibleForTesting
44
import com.auth0.android.Auth0
55
import com.auth0.android.Auth0Exception
66
import com.auth0.android.NetworkErrorException
7-
import com.auth0.android.annotation.ExperimentalAuth0Api
87
import com.auth0.android.request.*
98
import com.auth0.android.request.internal.*
109
import com.auth0.android.request.internal.GsonAdapter.Companion.forMap
@@ -15,7 +14,7 @@ import com.auth0.android.result.Credentials
1514
import com.auth0.android.result.DatabaseUser
1615
import com.auth0.android.result.PasskeyChallenge
1716
import com.auth0.android.result.PasskeyRegistrationChallenge
18-
import com.auth0.android.result.SSOCredentials
17+
import com.auth0.android.result.SessionTransferCredentials
1918
import com.auth0.android.result.UserProfile
2019
import com.google.gson.Gson
2120
import okhttp3.HttpUrl.Companion.toHttpUrl
@@ -925,28 +924,24 @@ public class AuthenticationAPIClient @VisibleForTesting(otherwise = VisibleForTe
925924

926925
/**
927926
* Creates a new request to fetch a web sso token in exchange for a refresh token.
928-
* This is still an experimental feature, test it thoroughly in the targeted devices and OS variants and let us know your feedback.
929927
*
930928
* @param refreshToken A valid refresh token obtained as part of Auth0 authentication
931929
* @return a request to fetch a web sso token
932930
*
933931
*/
934-
@ExperimentalAuth0Api
935-
public fun fetchWebSsoToken(refreshToken: String): Request<SSOCredentials, AuthenticationException> {
932+
public fun fetchSessionTransferToken(refreshToken: String): Request<SessionTransferCredentials, AuthenticationException> {
936933
val params = ParameterBuilder.newBuilder()
937-
.setClientId(clientId)
938-
.setGrantType(ParameterBuilder.GRANT_TYPE_TOKEN_EXCHANGE)
939-
.set(SUBJECT_TOKEN_KEY, refreshToken)
940-
.set(SUBJECT_TOKEN_TYPE_KEY, ParameterBuilder.TOKEN_TYPE_REFRESH_TOKEN)
941-
.set(REQUESTED_TOKEN_TYPE_KEY, ParameterBuilder.TOKEN_TYPE_SESSION_TRANSFER_TOKEN)
934+
.setGrantType(ParameterBuilder.REFRESH_TOKEN_KEY)
935+
.setAudience("urn:${auth0.domain}:session_transfer")
936+
.set(ParameterBuilder.REFRESH_TOKEN_KEY, refreshToken)
942937
.asDictionary()
943-
return loginWithTokenGeneric<SSOCredentials>(params)
938+
return loginWithTokenGeneric<SessionTransferCredentials>(params)
944939
}
945940

946941
/**
947942
* Helper function to make a request to the /oauth/token endpoint with a custom response type.
948943
*/
949-
private inline fun <reified T> loginWithTokenGeneric(parameters: Map<String, String>): Request<T,AuthenticationException> {
944+
private inline fun <reified T> loginWithTokenGeneric(parameters: Map<String, String>): Request<T, AuthenticationException> {
950945
val url = auth0.getDomainUrl().toHttpUrl().newBuilder()
951946
.addPathSegment(OAUTH_PATH)
952947
.addPathSegment(TOKEN_PATH)

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.SSOCredentials
7+
import com.auth0.android.result.SessionTransferCredentials
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 getSsoCredentials(
34+
public abstract fun getSessionTransferCredentials(
3535
parameters: Map<String, String>,
36-
callback: Callback<SSOCredentials, CredentialsManagerException>
36+
callback: Callback<SessionTransferCredentials, CredentialsManagerException>
3737
)
3838

39-
public abstract fun getSsoCredentials(
40-
callback: Callback<SSOCredentials, CredentialsManagerException>
39+
public abstract fun getSessionTransferCredentials(
40+
callback: Callback<SessionTransferCredentials, 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 awaitSsoCredentials(parameters: Map<String, String>)
76-
: SSOCredentials
75+
public abstract suspend fun awaitSessionTransferCredentials(parameters: Map<String, String>)
76+
: SessionTransferCredentials
7777

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

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

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

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ package com.auth0.android.authentication.storage
22

33
import android.text.TextUtils
44
import androidx.annotation.VisibleForTesting
5-
import com.auth0.android.annotation.ExperimentalAuth0Api
65
import com.auth0.android.authentication.AuthenticationAPIClient
76
import com.auth0.android.authentication.AuthenticationException
87
import com.auth0.android.callback.Callback
98
import com.auth0.android.result.Credentials
10-
import com.auth0.android.result.SSOCredentials
9+
import com.auth0.android.result.SessionTransferCredentials
1110
import kotlinx.coroutines.suspendCancellableCoroutine
1211
import java.util.*
1312
import java.util.concurrent.Executor
@@ -56,26 +55,22 @@ public class CredentialsManager @VisibleForTesting(otherwise = VisibleForTesting
5655
}
5756

5857
/**
59-
* Fetches a new [SSOCredentials] . It will fail with [CredentialsManagerException]
58+
* Fetches a new [SessionTransferCredentials] . It will fail with [CredentialsManagerException]
6059
* if the existing refresh_token is null or no longer valid. This method will handle saving the refresh_token,
6160
* if a new one is issued.
62-
* This is still an experimental feature, test it thoroughly and let us know your feedback.
6361
*/
64-
@ExperimentalAuth0Api
65-
override fun getSsoCredentials(callback: Callback<SSOCredentials, CredentialsManagerException>) {
66-
getSsoCredentials(emptyMap(), callback)
62+
override fun getSessionTransferCredentials(callback: Callback<SessionTransferCredentials, CredentialsManagerException>) {
63+
getSessionTransferCredentials(emptyMap(), callback)
6764
}
6865

6966
/**
70-
* Fetches a new [SSOCredentials] . It will fail with [CredentialsManagerException]
67+
* Fetches a new [SessionTransferCredentials] . It will fail with [CredentialsManagerException]
7168
* if the existing refresh_token is null or no longer valid. This method will handle saving the refresh_token,
7269
* if a new one is issued.
73-
* This is still an experimental feature, test it thoroughly and let us know your feedback.
7470
*/
75-
@ExperimentalAuth0Api
76-
override fun getSsoCredentials(
71+
override fun getSessionTransferCredentials(
7772
parameters: Map<String, String>,
78-
callback: Callback<SSOCredentials, CredentialsManagerException>
73+
callback: Callback<SessionTransferCredentials, CredentialsManagerException>
7974
) {
8075
serialExecutor.execute {
8176
val refreshToken = storage.retrieveString(KEY_REFRESH_TOKEN)
@@ -84,14 +79,14 @@ public class CredentialsManager @VisibleForTesting(otherwise = VisibleForTesting
8479
return@execute
8580
}
8681

87-
val request = authenticationClient.fetchWebSsoToken(refreshToken)
82+
val request = authenticationClient.fetchSessionTransferToken(refreshToken)
8883
try {
8984
if (parameters.isNotEmpty()) {
9085
request.addParameters(parameters)
9186
}
92-
val sessionCredentials = request.execute()
93-
saveSsoCredentials(sessionCredentials)
94-
callback.onSuccess(sessionCredentials)
87+
val sessionTransferCredentials = request.execute()
88+
saveSessionTransferCredentials(sessionTransferCredentials)
89+
callback.onSuccess(sessionTransferCredentials)
9590
} catch (error: AuthenticationException) {
9691
val exception = when {
9792
error.isRefreshTokenDeleted ||
@@ -111,32 +106,29 @@ public class CredentialsManager @VisibleForTesting(otherwise = VisibleForTesting
111106
}
112107

113108
/**
114-
* Fetches a new [SSOCredentials] . It will fail with [CredentialsManagerException]
109+
* Fetches a new [SessionTransferCredentials] . It will fail with [CredentialsManagerException]
115110
* if the existing refresh_token is null or no longer valid. This method will handle saving the refresh_token,
116111
* if a new one is issued.
117-
* This is still an experimental feature, test it thoroughly and OS variants and let us know your feedback.
118112
*/
119113
@JvmSynthetic
120114
@Throws(CredentialsManagerException::class)
121-
@ExperimentalAuth0Api
122-
override suspend fun awaitSsoCredentials(): SSOCredentials {
123-
return awaitSsoCredentials(emptyMap())
115+
override suspend fun awaitSessionTransferCredentials(): SessionTransferCredentials {
116+
return awaitSessionTransferCredentials(emptyMap())
124117
}
125118

126119
/**
127-
* Fetches a new [SSOCredentials] . It will fail with [CredentialsManagerException]
120+
* Fetches a new [SessionTransferCredentials] . It will fail with [CredentialsManagerException]
128121
* if the existing refresh_token is null or no longer valid. This method will handle saving the refresh_token,
129122
* if a new one is issued.
130-
* This is still an experimental feature, test it thoroughly and OS variants and let us know your feedback.
131123
*/
132124
@JvmSynthetic
133125
@Throws(CredentialsManagerException::class)
134-
@ExperimentalAuth0Api
135-
override suspend fun awaitSsoCredentials(parameters: Map<String, String>): SSOCredentials {
126+
override suspend fun awaitSessionTransferCredentials(parameters: Map<String, String>): SessionTransferCredentials {
136127
return suspendCancellableCoroutine { continuation ->
137-
getSsoCredentials(parameters,
138-
object : Callback<SSOCredentials, CredentialsManagerException> {
139-
override fun onSuccess(result: SSOCredentials) {
128+
getSessionTransferCredentials(
129+
parameters,
130+
object : Callback<SessionTransferCredentials, CredentialsManagerException> {
131+
override fun onSuccess(result: SessionTransferCredentials) {
140132
continuation.resume(result)
141133
}
142134

@@ -238,7 +230,8 @@ public class CredentialsManager @VisibleForTesting(otherwise = VisibleForTesting
238230
forceRefresh: Boolean
239231
): Credentials {
240232
return suspendCancellableCoroutine { continuation ->
241-
getCredentials(scope,
233+
getCredentials(
234+
scope,
242235
minTtl,
243236
parameters,
244237
headers,
@@ -473,20 +466,21 @@ public class CredentialsManager @VisibleForTesting(otherwise = VisibleForTesting
473466
}
474467

475468
/**
476-
* Helper method to store the given [SSOCredentials] refresh token in the storage.
469+
* Helper method to store the given [SessionTransferCredentials] refresh token in the storage.
477470
* Method will silently return ,if the passed credentials has no refresh token.
478471
*
479-
* @param ssoCredentials the credentials to save in the storage.
472+
* @param sessionTransferCredentials the credentials to save in the storage.
480473
*/
481474
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
482-
internal fun saveSsoCredentials(ssoCredentials: SSOCredentials) {
483-
if (ssoCredentials.refreshToken.isNullOrEmpty())
484-
return // No refresh token to save
475+
internal fun saveSessionTransferCredentials(sessionTransferCredentials: SessionTransferCredentials) {
476+
storage.store(KEY_ID_TOKEN, sessionTransferCredentials.idToken)
485477
val existingRefreshToken = storage.retrieveString(KEY_REFRESH_TOKEN)
486478
// Checking if the existing one needs to be replaced with the new one
487-
if (ssoCredentials.refreshToken == existingRefreshToken)
479+
if (sessionTransferCredentials.refreshToken.isNullOrEmpty())
480+
return // No refresh token to save
481+
if (sessionTransferCredentials.refreshToken == existingRefreshToken)
488482
return // Same refresh token, no need to save
489-
storage.store(KEY_REFRESH_TOKEN, ssoCredentials.refreshToken)
483+
storage.store(KEY_REFRESH_TOKEN, sessionTransferCredentials.refreshToken)
490484
}
491485

492486
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)

0 commit comments

Comments
 (0)