From 7ffa0b48e701c8fef5dd2b09fe250f1e8ba2dadf Mon Sep 17 00:00:00 2001 From: Marcel Kesselring <2410681-Ingwersaft@users.noreply.gitlab.com> Date: Wed, 29 Nov 2023 10:28:53 +0100 Subject: [PATCH] feat: support ClientMetadata during SignUp and ForgotPassword --- build.gradle.kts | 6 ---- .../cognito/idp/IdentityProviderClient.kt | 14 +++++---- .../cognito/idp/core/IdentityProvider.kt | 4 +-- .../com/liftric/cognito/idp/core/Payload.kt | 8 +++-- .../idp/IdentityProviderClientTests.kt | 2 +- src/jsMain/kotlin/IdentityProviderJS.kt | 10 ++++--- .../idp/IdentityProviderClientJSTests.kt | 29 ++++++++++++++++++- 7 files changed, 51 insertions(+), 22 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index da9f4c9..667e775 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,4 @@ import com.android.build.gradle.LibraryExtension -import com.android.build.gradle.internal.tasks.factory.dependsOn import com.liftric.vault.GetVaultSecretTask import org.jetbrains.kotlin.gradle.targets.native.tasks.KotlinNativeSimulatorTest import org.jetbrains.kotlin.gradle.tasks.* @@ -297,11 +296,6 @@ publishing { } } developers { - developer { - id.set("gaebel") - name.set("Jan Gaebel") - email.set("gaebel@liftric.com") - } developer { id.set("benjohnde") name.set("Ben John") diff --git a/src/commonMain/kotlin/com/liftric/cognito/idp/IdentityProviderClient.kt b/src/commonMain/kotlin/com/liftric/cognito/idp/IdentityProviderClient.kt index cf7b675..a023a29 100644 --- a/src/commonMain/kotlin/com/liftric/cognito/idp/IdentityProviderClient.kt +++ b/src/commonMain/kotlin/com/liftric/cognito/idp/IdentityProviderClient.kt @@ -47,14 +47,16 @@ open class IdentityProviderClient(region: String, clientId: String, engine: Http override suspend fun signUp( username: String, password: String, - attributes: List? + attributes: List?, + clientMetadata: Map?, ): Result = request( Request.SignUp, SignUp( ClientId = configuration.clientId, Username = username, Password = password, - UserAttributes = attributes ?: listOf() + UserAttributes = attributes ?: listOf(), + ClientMetadata = clientMetadata, ) ) @@ -157,12 +159,14 @@ open class IdentityProviderClient(region: String, clientId: String, engine: Http ) override suspend fun forgotPassword( - username: String + username: String, + clientMetadata: Map? ): Result = request( Request.ForgotPassword, ForgotPassword( ClientId = configuration.clientId, - Username = username + Username = username, + ClientMetadata = clientMetadata, ) ) @@ -183,7 +187,7 @@ open class IdentityProviderClient(region: String, clientId: String, engine: Http override suspend fun getUserAttributeVerificationCode( accessToken: String, attributeName: String, - clientMetadata: Map? + clientMetadata: Map?, ): Result = request( Request.GetUserAttributeVerificationCode, GetUserAttributeVerificationCode( diff --git a/src/commonMain/kotlin/com/liftric/cognito/idp/core/IdentityProvider.kt b/src/commonMain/kotlin/com/liftric/cognito/idp/core/IdentityProvider.kt index e044e3e..9836fae 100644 --- a/src/commonMain/kotlin/com/liftric/cognito/idp/core/IdentityProvider.kt +++ b/src/commonMain/kotlin/com/liftric/cognito/idp/core/IdentityProvider.kt @@ -8,7 +8,7 @@ interface IdentityProvider { * @param attributes Optional account attributes e.g. email, phone number, ... * @return Result object containing SignUpResponse on success or an error on failure */ - suspend fun signUp(username: String, password: String, attributes: List? = null): Result + suspend fun signUp(username: String, password: String, attributes: List? = null, clientMetadata: Map? = null): Result /** * Confirms sign up of a new user @@ -91,7 +91,7 @@ interface IdentityProvider { * @param username The username * @return Result object containing CodeDeliveryDetails on success or an error on failure */ - suspend fun forgotPassword(username: String): Result + suspend fun forgotPassword(username: String, clientMetadata: Map? = null): Result /** * Confirms forgot password diff --git a/src/commonMain/kotlin/com/liftric/cognito/idp/core/Payload.kt b/src/commonMain/kotlin/com/liftric/cognito/idp/core/Payload.kt index d148e0a..cc01c4e 100644 --- a/src/commonMain/kotlin/com/liftric/cognito/idp/core/Payload.kt +++ b/src/commonMain/kotlin/com/liftric/cognito/idp/core/Payload.kt @@ -54,7 +54,8 @@ internal data class SignUp( val ClientId: String, val Password: String, val Username: String, - val UserAttributes: List + val UserAttributes: List, + val ClientMetadata: Map?, ) @Serializable @@ -73,7 +74,8 @@ internal data class ResendConfirmationCode( @Serializable internal data class ForgotPassword( val ClientId: String, - val Username: String + val Username: String, + val ClientMetadata: Map?, ) @Serializable @@ -101,7 +103,7 @@ internal data class UpdateUserAttributes( internal data class GetUserAttributeVerificationCode( val AccessToken: String, val AttributeName: String, - val ClientMetadata: Map? = null + val ClientMetadata: Map? ) @Serializable diff --git a/src/commonTest/kotlin/com/liftric/cognito/idp/IdentityProviderClientTests.kt b/src/commonTest/kotlin/com/liftric/cognito/idp/IdentityProviderClientTests.kt index 22e8eab..77899b6 100644 --- a/src/commonTest/kotlin/com/liftric/cognito/idp/IdentityProviderClientTests.kt +++ b/src/commonTest/kotlin/com/liftric/cognito/idp/IdentityProviderClientTests.kt @@ -68,7 +68,7 @@ abstract class AbstractIdentityProviderClientTests { val signUpResponse = provider.signUp( credentials.username, credentials.password, attributes = listOf( - UserAttribute(Name = "custom:target_group", Value = "ROLE_USER") + UserAttribute(Name = "custom:target_group", Value = "ROLE_PATIENT") ) ) diff --git a/src/jsMain/kotlin/IdentityProviderJS.kt b/src/jsMain/kotlin/IdentityProviderJS.kt index 792a8a2..48c9f17 100644 --- a/src/jsMain/kotlin/IdentityProviderJS.kt +++ b/src/jsMain/kotlin/IdentityProviderJS.kt @@ -15,13 +15,15 @@ class IdentityProviderClientJS(region: String, clientId: String) { fun signUp( username: String, password: String, - attributes: Array? = null + attributes: Array? = null, + clientMetadata: Array? = null, ): Promise = MainScope().promise { provider.signUp( username = username, password = password, - attributes = attributes?.toList() + attributes = attributes?.toList(), + clientMetadata = clientMetadata?.associate { it.key to it.value } ).getOrWrapThrowable() } @@ -105,9 +107,9 @@ class IdentityProviderClientJS(region: String, clientId: String) { ).getOrWrapThrowable() } - fun forgotPassword(username: String): Promise = + fun forgotPassword(username: String, clientMetadata: Array? = null): Promise = MainScope().promise { - provider.forgotPassword(username) + provider.forgotPassword(username, clientMetadata?.associate { it.key to it.value }) .getOrWrapThrowable() } diff --git a/src/jsTest/kotlin/com/liftric/cognito/idp/IdentityProviderClientJSTests.kt b/src/jsTest/kotlin/com/liftric/cognito/idp/IdentityProviderClientJSTests.kt index bcc0788..4048e25 100644 --- a/src/jsTest/kotlin/com/liftric/cognito/idp/IdentityProviderClientJSTests.kt +++ b/src/jsTest/kotlin/com/liftric/cognito/idp/IdentityProviderClientJSTests.kt @@ -5,6 +5,7 @@ import IdentityProviderExceptionJs import com.liftric.cognito.idp.core.UserAttribute import env import kotlinx.coroutines.await +import toMapEntries import kotlin.test.Test import kotlin.test.assertNotNull import kotlin.test.assertTrue @@ -30,7 +31,7 @@ class IdentityProviderClientJSTests { provider.signUp( username, password, attributes = arrayOf( - UserAttribute(Name = "custom:target_group", Value = "ROLE_USER") + UserAttribute(Name = "custom:target_group", Value = "ROLE_PATIENT") ) ).await().also { println("signUpResponse=$it") @@ -48,6 +49,32 @@ class IdentityProviderClientJSTests { } } + @JsName("SignUpSignInDeleteUserClientMetaDataTest") + @Test + fun `Sign up, sign in, delete user with clientMetadata should succeed`() = runTest { + println("Sign up, sign in, delete user should succeed") + provider.signUp( + username, password, + attributes = arrayOf( + UserAttribute(Name = "custom:target_group", Value = "ROLE_PATIENT") + ), + clientMetadata = mapOf("fallback_mode" to "true").toMapEntries(), + ).await().also { + println("signUpResponse=$it") + assertNotNull(it) + } + + provider.signIn(username, password).await().also { + println("signInResponse=$it") + assertNotNull(it) + + provider.deleteUser(it.AuthenticationResult!!.AccessToken!!).await().also { + println("deleteUser=$it") + assertNotNull(it) + } + } + } + @JsName("SignUpFailPasswordTooLongTest") @Test fun `Sign up should fail because password too long`() = runTest {