Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
@file:OptIn(ExperimentalWasmDsl::class)

import com.android.build.gradle.LibraryExtension
import com.liftric.vault.GetVaultSecretTask
import com.vanniktech.maven.publish.KotlinMultiplatform
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
import org.jetbrains.kotlin.gradle.targets.native.tasks.KotlinNativeSimulatorTest
Expand Down Expand Up @@ -108,6 +111,10 @@ kotlin {
}
}
}
wasmJs {
browser()
binaries.library()
}

sourceSets {
val commonMain by getting {
Expand Down Expand Up @@ -164,6 +171,10 @@ kotlin {
implementation(kotlin("test-js"))
}
}
wasmJsMain.dependencies {
api(libs.ktor.client.js)
api(libs.kotlinx.browser)
}
all {
languageSettings {
optIn("kotlinx.serialization.ExperimentalSerializationApi")
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"ktor" = "3.3.1"
"otp-java" = "2.1.0"
"robolectric" = "4.16"
"browser" = "0.5.0"

[libraries]
"androidx-test-core" = { module = "androidx.test:core", version.ref = "androidTestCore" }
Expand All @@ -25,6 +26,7 @@
"ktor-client-js" = { module = "io.ktor:ktor-client-js", version.ref = "ktor" }
"otp-java" = { module = "com.github.bastiaanjansen:otp-java", version.ref = "otp-java" }
"robolectric" = { module = "org.robolectric:robolectric", version.ref = "robolectric" }
"kotlinx-browser" = { group = "org.jetbrains.kotlinx", name = "kotlinx-browser", version.ref = "browser" }

[plugins]
"kotlin-serialization" = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
Expand Down
2 changes: 0 additions & 2 deletions src/commonMain/kotlin/com/liftric/cognito/idp/core/Payload.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ internal data class ConfirmForgotPassword(
val Password: String
)

@JsExport
@Serializable
data class UserAttribute(
val Name: String,
Expand Down Expand Up @@ -134,7 +133,6 @@ internal data class SetUserMFAPreference(
val SoftwareTokenMfaSettings: MfaSettings?
)

@JsExport
@Serializable
data class MfaSettings(
val Enabled: Boolean,
Expand Down
10 changes: 0 additions & 10 deletions src/commonMain/kotlin/com/liftric/cognito/idp/core/Response.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ data class SignInResponse(
val Session: String?
)

@JsExport
@Serializable
data class AuthenticationResult(
val AccessToken: String?,
Expand All @@ -33,28 +32,24 @@ data class AuthenticationResult(
val NewDeviceMetadata: NewDeviceMetadata?,
)

@JsExport
@Serializable
data class NewDeviceMetadata(
val DeviceGroupKey: String?,
val DeviceKey: String?,
)

@JsExport
@Serializable
data class SignUpResponse(
val CodeDeliveryDetails: CodeDeliveryDetails?,
val UserConfirmed: Boolean,
val UserSub: String
)

@JsExport
@Serializable
data class ResendConfirmationCodeResponse(
val CodeDeliveryDetails: CodeDeliveryDetails
)

@JsExport
@Serializable
data class CodeDeliveryDetails(
val AttributeName: String?,
Expand All @@ -71,7 +66,6 @@ data class GetUserResponse(
val Username: String
)

@JsExport
@Serializable
data class MFAOptions(
val AttributeName: String?,
Expand All @@ -83,26 +77,22 @@ data class UpdateUserAttributesResponse(
val CodeDeliveryDetailsList: List<CodeDeliveryDetails> = listOf()
)

@JsExport
@Serializable
data class GetAttributeVerificationCodeResponse(
val CodeDeliveryDetails: CodeDeliveryDetails
)

@JsExport
@Serializable
data class ForgotPasswordResponse(
val CodeDeliveryDetails: CodeDeliveryDetails
)

@JsExport
@Serializable
data class AssociateSoftwareTokenResponse(
val SecretCode: String,
val Session: String?
)

@JsExport
@Serializable
data class VerifySoftwareTokenResponse(
val Session: String?,
Expand Down
60 changes: 30 additions & 30 deletions src/jsMain/kotlin/IdentityProviderJS.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ class IdentityProviderClientJS(region: String, clientId: String) {
fun signUp(
username: String,
password: String,
attributes: Array<UserAttribute>? = null,
attributes: Array<UserAttributeJS>? = null,
clientMetadata: Array<MapEntry>? = null,
): Promise<SignUpResponse> =
): Promise<SignUpResponseJS> =
MainScope().promise {
provider.signUp(
username = username,
password = password,
attributes = attributes?.toList(),
attributes = attributes?.map(UserAttributeJS::toUserAttribute)?.toList(),
clientMetadata = clientMetadata?.associate { it.key to it.value }
).getOrWrapThrowable()
).getOrWrapThrowable().toSignUpResponseJS()
}

fun confirmSignUp(username: String, confirmationCode: String): Promise<Unit> =
Expand All @@ -35,18 +35,18 @@ class IdentityProviderClientJS(region: String, clientId: String) {
).getOrWrapThrowable()
}

fun resendConfirmationCode(username: String): Promise<ResendConfirmationCodeResponse> =
fun resendConfirmationCode(username: String): Promise<ResendConfirmationCodeResponseJS> =
MainScope().promise {
provider.resendConfirmationCode(username)
.getOrWrapThrowable()
.getOrWrapThrowable().toResendConfirmationCodeResponseJS()
}

fun signIn(username: String, password: String): Promise<SignInResponseJS> =
MainScope().promise {
provider.signIn(username, password)
.getOrWrapThrowable().let {
SignInResponseJS(
AuthenticationResult = it.AuthenticationResult,
AuthenticationResult = it.AuthenticationResult?.toAuthenticationResultJS(),
ChallengeParameters = it.ChallengeParameters.toMapEntries(),
ChallengeName = it.ChallengeName,
Session = it.Session,
Expand All @@ -59,7 +59,7 @@ class IdentityProviderClientJS(region: String, clientId: String) {
provider.refresh(refreshToken)
.getOrWrapThrowable().let {
SignInResponseJS(
AuthenticationResult = it.AuthenticationResult,
AuthenticationResult = it.AuthenticationResult?.toAuthenticationResultJS(),
ChallengeParameters = it.ChallengeParameters.toMapEntries(),
ChallengeName = it.ChallengeName,
Session = it.Session
Expand All @@ -72,9 +72,9 @@ class IdentityProviderClientJS(region: String, clientId: String) {
provider.getUser(accessToken)
.getOrWrapThrowable().let {
GetUserResponseJS(
MFAOptions = it.MFAOptions,
MFAOptions = it.MFAOptions?.toMFAOptionsJS(),
PreferredMfaSetting = it.PreferredMfaSetting,
UserAttributes = it.UserAttributes.toTypedArray(),
UserAttributes = it.UserAttributes.map(UserAttribute::toUserAttributeJS).toTypedArray(),
UserMFASettingList = it.UserMFASettingList.toTypedArray(),
Username = it.Username
)
Expand All @@ -83,14 +83,14 @@ class IdentityProviderClientJS(region: String, clientId: String) {

fun updateUserAttributes(
accessToken: String,
attributes: Array<UserAttribute>
attributes: Array<UserAttributeJS>
): Promise<UpdateUserAttributesResponseJS> =
MainScope().promise {
provider.updateUserAttributes(
accessToken = accessToken,
attributes = attributes.toList()
attributes = attributes.map(UserAttributeJS::toUserAttribute).toList()
).getOrWrapThrowable().let {
UpdateUserAttributesResponseJS(it.CodeDeliveryDetailsList.toTypedArray())
UpdateUserAttributesResponseJS(it.CodeDeliveryDetailsList.map(CodeDeliveryDetails::toCodeDeliveryDetailsJS).toTypedArray())
}
}

Expand All @@ -107,10 +107,10 @@ class IdentityProviderClientJS(region: String, clientId: String) {
).getOrWrapThrowable()
}

fun forgotPassword(username: String, clientMetadata: Array<MapEntry>? = null): Promise<ForgotPasswordResponse> =
fun forgotPassword(username: String, clientMetadata: Array<MapEntry>? = null): Promise<ForgotPasswordResponseJS> =
MainScope().promise {
provider.forgotPassword(username, clientMetadata?.associate { it.key to it.value })
.getOrWrapThrowable()
.getOrWrapThrowable().toForgotPasswordResponseJS()
}

fun confirmForgotPassword(
Expand All @@ -130,13 +130,13 @@ class IdentityProviderClientJS(region: String, clientId: String) {
accessToken: String,
attributeName: String,
clientMetadata: Array<MapEntry>? = null
): Promise<GetAttributeVerificationCodeResponse> =
): Promise<GetAttributeVerificationCodeResponseJS> =
MainScope().promise {
provider.getUserAttributeVerificationCode(
accessToken = accessToken,
attributeName = attributeName,
clientMetadata = clientMetadata?.associate { it.key to it.value }
).getOrWrapThrowable()
).getOrWrapThrowable().toGetAttributeVerificationCodeResponseJS()
}

fun verifyUserAttribute(
Expand Down Expand Up @@ -172,13 +172,13 @@ class IdentityProviderClientJS(region: String, clientId: String) {

fun setUserMFAPreference(
accessToken: String,
smsMfaSettings: MfaSettings?,
softwareTokenMfaSettings: MfaSettings?
smsMfaSettings: MfaSettingsJS?,
softwareTokenMfaSettings: MfaSettingsJS?
): Promise<Unit> = MainScope().promise {
provider.setUserMFAPreference(
accessToken = accessToken,
smsMfaSettings = smsMfaSettings,
softwareTokenMfaSettings = softwareTokenMfaSettings
smsMfaSettings = smsMfaSettings?.toMfaSettings(),
softwareTokenMfaSettings = softwareTokenMfaSettings?.toMfaSettings()
).getOrWrapThrowable()
}

Expand All @@ -193,7 +193,7 @@ class IdentityProviderClientJS(region: String, clientId: String) {
session
).getOrWrapThrowable().let {
SignInResponseJS(
AuthenticationResult = it.AuthenticationResult,
AuthenticationResult = it.AuthenticationResult?.toAuthenticationResultJS(),
ChallengeParameters = it.ChallengeParameters.toMapEntries(),
ChallengeName = it.ChallengeName,
Session = it.Session
Expand All @@ -203,42 +203,42 @@ class IdentityProviderClientJS(region: String, clientId: String) {

fun associateSoftwareToken(
accessToken: String
): Promise<AssociateSoftwareTokenResponse> = MainScope().promise {
): Promise<AssociateSoftwareTokenResponseJS> = MainScope().promise {
provider.associateSoftwareToken(
accessToken = accessToken
).getOrWrapThrowable()
).getOrWrapThrowable().toAssociateSoftwareTokenResponseJS()
}

fun associateSoftwareTokenBySession(
session: String
): Promise<AssociateSoftwareTokenResponse> = MainScope().promise {
): Promise<AssociateSoftwareTokenResponseJS> = MainScope().promise {
provider.associateSoftwareTokenBySession(
session = session
).getOrWrapThrowable()
).getOrWrapThrowable().toAssociateSoftwareTokenResponseJS()
}

fun verifySoftwareToken(
accessToken: String,
friendlyDeviceName: String?,
userCode: String
): Promise<VerifySoftwareTokenResponse> = MainScope().promise {
): Promise<VerifySoftwareTokenResponseJS> = MainScope().promise {
provider.verifySoftwareToken(
accessToken = accessToken,
friendlyDeviceName = friendlyDeviceName,
userCode = userCode
).getOrWrapThrowable()
).getOrWrapThrowable().toVerifySoftwareTokenResponseJS()
}

fun verifySoftwareTokenBySession(
session: String,
friendlyDeviceName: String?,
userCode: String
): Promise<VerifySoftwareTokenResponse> = MainScope().promise {
): Promise<VerifySoftwareTokenResponseJS> = MainScope().promise {
provider.verifySoftwareTokenBySession(
friendlyDeviceName = friendlyDeviceName,
session = session,
userCode = userCode
).getOrWrapThrowable()
).getOrWrapThrowable().toVerifySoftwareTokenResponseJS()
}

private fun <T> Result<T>.getOrWrapThrowable(): T = when (value) {
Expand Down
31 changes: 31 additions & 0 deletions src/jsMain/kotlin/PayloadJS.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import com.liftric.cognito.idp.core.MfaSettings
import com.liftric.cognito.idp.core.UserAttribute
import kotlinx.serialization.Serializable

@JsExport
@Serializable
data class UserAttributeJS(
val Name: String, val Value: String
)

fun UserAttributeJS.toUserAttribute(): UserAttribute {
return UserAttribute(Name, Value)
}

fun UserAttribute.toUserAttributeJS(): UserAttributeJS {
return UserAttributeJS(Name, Value)
}

@JsExport
@Serializable
data class MfaSettingsJS(
val Enabled: Boolean, val PreferredMfa: Boolean
)

fun MfaSettingsJS.toMfaSettings(): MfaSettings {
return MfaSettings(Enabled, PreferredMfa)
}

fun MfaSettings.toMfaSettingsJS(): MfaSettingsJS {
return MfaSettingsJS(Enabled, PreferredMfa)
}
Loading
Loading