From 29b79204e4ee94dc16d5089d7b48f009a9e79d28 Mon Sep 17 00:00:00 2001 From: Xiao Yijun Date: Thu, 5 Sep 2024 18:34:43 +0800 Subject: [PATCH] feat: support `direct_sign_in` param for sign-in uri --- .../android/auth/logto/LogtoAuthSession.kt | 1 + .../logto/sdk/android/type/SignInOptions.kt | 3 +++ .../src/main/kotlin/io/logto/sdk/core/Core.kt | 4 ++++ .../sdk/core/constant/DirectSignInMethod.kt | 6 +++++ .../sdk/core/type/DirectSignInOptions.kt | 6 +++++ .../sdk/core/type/GenerateSignInUriOptions.kt | 1 + .../test/kotlin/io/logto/sdk/core/CoreTest.kt | 24 +++++++++++++++++++ 7 files changed, 45 insertions(+) create mode 100644 kotlin-sdk/kotlin/src/main/kotlin/io/logto/sdk/core/constant/DirectSignInMethod.kt create mode 100644 kotlin-sdk/kotlin/src/main/kotlin/io/logto/sdk/core/type/DirectSignInOptions.kt diff --git a/android-sdk/android/src/main/kotlin/io/logto/sdk/android/auth/logto/LogtoAuthSession.kt b/android-sdk/android/src/main/kotlin/io/logto/sdk/android/auth/logto/LogtoAuthSession.kt index 20def4b..d7bb90c 100644 --- a/android-sdk/android/src/main/kotlin/io/logto/sdk/android/auth/logto/LogtoAuthSession.kt +++ b/android-sdk/android/src/main/kotlin/io/logto/sdk/android/auth/logto/LogtoAuthSession.kt @@ -45,6 +45,7 @@ class LogtoAuthSession( loginHint = signInOptions.loginHint, firstScreen = signInOptions.firstScreen, identifiers = signInOptions.identifiers, + directSignIn = signInOptions.directSignIn, extraParams = signInOptions.extraParams, includeReservedScopes = logtoConfig.includeReservedScopes, ), diff --git a/android-sdk/android/src/main/kotlin/io/logto/sdk/android/type/SignInOptions.kt b/android-sdk/android/src/main/kotlin/io/logto/sdk/android/type/SignInOptions.kt index 1eeb152..3710d30 100644 --- a/android-sdk/android/src/main/kotlin/io/logto/sdk/android/type/SignInOptions.kt +++ b/android-sdk/android/src/main/kotlin/io/logto/sdk/android/type/SignInOptions.kt @@ -1,10 +1,13 @@ package io.logto.sdk.android.type +import io.logto.sdk.core.type.DirectSignInOptions + class SignInOptions( val redirectUri: String, val prompt: String? = null, val firstScreen: String? = null, val identifiers: List? = null, + val directSignIn: DirectSignInOptions? = null, val loginHint: String? = null, val extraParams: Map? = null, ) diff --git a/kotlin-sdk/kotlin/src/main/kotlin/io/logto/sdk/core/Core.kt b/kotlin-sdk/kotlin/src/main/kotlin/io/logto/sdk/core/Core.kt index 77447a1..0b8be42 100644 --- a/kotlin-sdk/kotlin/src/main/kotlin/io/logto/sdk/core/Core.kt +++ b/kotlin-sdk/kotlin/src/main/kotlin/io/logto/sdk/core/Core.kt @@ -64,6 +64,10 @@ object Core { addQueryParameter(QueryKey.FIRST_SCREEN, it) } + options.directSignIn?.let { + addQueryParameter(QueryKey.DIRECT_SIGN_IN, "${it.method}:${it.target}") + } + options.identifiers?.let { addQueryParameter(QueryKey.IDENTIFIER, it.joinToString(" ")) } diff --git a/kotlin-sdk/kotlin/src/main/kotlin/io/logto/sdk/core/constant/DirectSignInMethod.kt b/kotlin-sdk/kotlin/src/main/kotlin/io/logto/sdk/core/constant/DirectSignInMethod.kt new file mode 100644 index 0000000..1efa337 --- /dev/null +++ b/kotlin-sdk/kotlin/src/main/kotlin/io/logto/sdk/core/constant/DirectSignInMethod.kt @@ -0,0 +1,6 @@ +package io.logto.sdk.core.constant + +object DirectSignInMethod { + const val SOCIAL = "social" + const val SSO = "sso" +} diff --git a/kotlin-sdk/kotlin/src/main/kotlin/io/logto/sdk/core/type/DirectSignInOptions.kt b/kotlin-sdk/kotlin/src/main/kotlin/io/logto/sdk/core/type/DirectSignInOptions.kt new file mode 100644 index 0000000..b290337 --- /dev/null +++ b/kotlin-sdk/kotlin/src/main/kotlin/io/logto/sdk/core/type/DirectSignInOptions.kt @@ -0,0 +1,6 @@ +package io.logto.sdk.core.type + +class DirectSignInOptions( + val method: String, + val target: String, +) diff --git a/kotlin-sdk/kotlin/src/main/kotlin/io/logto/sdk/core/type/GenerateSignInUriOptions.kt b/kotlin-sdk/kotlin/src/main/kotlin/io/logto/sdk/core/type/GenerateSignInUriOptions.kt index 7bca3f9..7e8de0f 100644 --- a/kotlin-sdk/kotlin/src/main/kotlin/io/logto/sdk/core/type/GenerateSignInUriOptions.kt +++ b/kotlin-sdk/kotlin/src/main/kotlin/io/logto/sdk/core/type/GenerateSignInUriOptions.kt @@ -12,6 +12,7 @@ class GenerateSignInUriOptions( val loginHint: String? = null, val firstScreen: String? = null, val identifiers: List? = null, + val directSignIn: DirectSignInOptions? = null, val extraParams: Map? = null, val includeReservedScopes: Boolean? = true, ) diff --git a/kotlin-sdk/kotlin/src/test/kotlin/io/logto/sdk/core/CoreTest.kt b/kotlin-sdk/kotlin/src/test/kotlin/io/logto/sdk/core/CoreTest.kt index 9afacb9..c0d8fa7 100644 --- a/kotlin-sdk/kotlin/src/test/kotlin/io/logto/sdk/core/CoreTest.kt +++ b/kotlin-sdk/kotlin/src/test/kotlin/io/logto/sdk/core/CoreTest.kt @@ -3,6 +3,7 @@ package io.logto.sdk.core import com.google.common.truth.Truth.assertThat import io.logto.sdk.core.constant.* import io.logto.sdk.core.exception.UriConstructionException +import io.logto.sdk.core.type.DirectSignInOptions import io.logto.sdk.core.type.GenerateSignInUriOptions import okhttp3.HttpUrl.Companion.toHttpUrl import org.junit.Assert @@ -317,6 +318,29 @@ class CoreTest { } } + @Test + fun `generateSignInUri should contain direct_sign_in if provided`() { + val directSignIn = DirectSignInOptions( + method = DirectSignInMethod.SOCIAL, + target = "google" + ) + + val signInUri = Core.generateSignInUri( + GenerateSignInUriOptions( + authorizationEndpoint = testAuthorizationEndpoint, + clientId = testClientId, + redirectUri = testRedirectUri, + codeChallenge = testCodeChallenge, + state = testState, + directSignIn = directSignIn, + ) + ) + + signInUri.toHttpUrl().apply { + assertThat(queryParameterValues(QueryKey.DIRECT_SIGN_IN)).contains("${directSignIn.method}:${directSignIn.target}") + } + } + @Test fun `generateSignInUri should contain extra params if provided`() { val extraParamKey = "tenant_id"