Skip to content

Commit 58c3d37

Browse files
committed
upgrade Gradle, add forgot password, require user pool on setup
1 parent b47c8fd commit 58c3d37

File tree

8 files changed

+197
-132
lines changed

8 files changed

+197
-132
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.jump.sdk.amplifyframework
22

33
enum class CognitoAction(val headerValue: String) {
4+
CONFIRM_FORGOT_PASSWORD("AWSCognitoIdentityProviderService.ConfirmForgotPassword"),
45
CONFIRM_SIGN_UP("AWSCognitoIdentityProviderService.ConfirmSignUp"),
5-
SIGN_UP("AWSCognitoIdentityProviderService.SignUp"),
6+
FORGOT_PASSWORD("AWSCognitoIdentityProviderService.ForgotPassword"),
67
INITIATE_AUTH("AWSCognitoIdentityProviderService.InitiateAuth"),
78
RESPOND_TO_AUTH_CHALLENGE("AWSCognitoIdentityProviderService.RespondToAuthChallenge"),
9+
SIGN_UP("AWSCognitoIdentityProviderService.SignUp"),
810
}

amplifyframework/src/commonMain/kotlin/com/jump/sdk/amplifyframework/CognitoException.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@ package com.jump.sdk.amplifyframework
33
sealed class CognitoException(override val message: String) : Exception(message) {
44
data object BadSrpB : CognitoException("Bad server public value 'B'")
55
data object HashOfAAndSrpBCannotBeZero : CognitoException("Hash of A and B cannot be zero")
6-
data object UserPoolNameNotSet : CognitoException("Must call setUserPoolParams() before this")
76
data object UserIdNotSet : CognitoException("Must call setUserPoolParams() before this")
87
}

amplifyframework/src/commonMain/kotlin/com/jump/sdk/amplifyframework/CognitoKeys.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ object CognitoKeys {
2121
const val PASSWORD_CLAIM_SECRET_BLOCK = "PASSWORD_CLAIM_SECRET_BLOCK"
2222
const val PASSWORD_CLAIM_SIGNATURE = "PASSWORD_CLAIM_SIGNATURE"
2323
const val PASSWORD_VERIFIER = "PASSWORD_VERIFIER"
24+
const val REFRESH_TOKEN_AUTH = "REFRESH_TOKEN_AUTH"
2425
const val REFRESH_TOKEN = "REFRESH_TOKEN"
2526
const val SALT = "SALT"
2627
const val SECRET_BLOCK = "SECRET_BLOCK"

amplifyframework/src/commonMain/kotlin/com/jump/sdk/amplifyframework/SRPHelper.kt

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import io.ktor.utils.io.core.toByteArray
2323
import org.kotlincrypto.SecureRandom
2424
import org.kotlincrypto.hash.sha2.SHA256
2525
import org.kotlincrypto.macs.hmac.sha2.HmacSHA256
26+
import kotlin.coroutines.cancellation.CancellationException
2627
import kotlin.io.encoding.Base64
2728
import kotlin.io.encoding.ExperimentalEncodingApi
2829

@@ -45,7 +46,7 @@ private const val HEX_N =
4546

4647
@OptIn(ExperimentalEncodingApi::class)
4748
@Suppress("TooManyFunctions")
48-
class SRPHelper(private val password: String) {
49+
class SRPHelper(private val password: String, userPoolName: String) {
4950
@Suppress("VariableNaming")
5051
private val N = BigInteger.parseString(HEX_N, 16)
5152

@@ -61,8 +62,16 @@ class SRPHelper(private val password: String) {
6162
internal set
6263

6364
private val digest = SHA256()
65+
var userIdForSrp: String? = null
66+
private val userPoolName: String
6467

6568
init {
69+
if (userPoolName.contains("_")) {
70+
this.userPoolName = userPoolName.split(Regex("_"), 2)[1]
71+
} else {
72+
this.userPoolName = userPoolName
73+
}
74+
6675
// Generate client private 'a' and public 'A' values
6776
do {
6877
privateA = BigInteger.fromByteArray(random.nextBytesOf(EPHEMERAL_KEY_LENGTH), Sign.POSITIVE).mod(N)
@@ -76,17 +85,6 @@ class SRPHelper(private val password: String) {
7685
k = BigInteger.fromByteArray(digest.digest(g.toByteArray()), Sign.POSITIVE)
7786
}
7887

79-
private var userId: String? = null
80-
private var userPoolName: String? = null
81-
82-
fun setUserPoolParams(userIdForSrp: String, userPoolName: String) {
83-
this.userId = userIdForSrp
84-
this.userPoolName = userPoolName
85-
if (userPoolName.contains("_")) {
86-
this.userPoolName = userPoolName.split(Regex("_"), 2)[1]
87-
}
88-
}
89-
9088
// @TestOnly
9189
internal fun modN(value: BigInteger): BigInteger = value.mod(N)
9290

@@ -109,8 +107,8 @@ class SRPHelper(private val password: String) {
109107
@Throws(CognitoException::class)
110108
internal fun computeX(salt: BigInteger): BigInteger {
111109
digest.reset()
112-
digest.update(userPoolName?.toByteArray() ?: throw CognitoException.UserPoolNameNotSet)
113-
digest.update(userId?.toByteArray() ?: throw CognitoException.UserIdNotSet)
110+
digest.update(userPoolName.toByteArray())
111+
digest.update(userIdForSrp?.toByteArray() ?: throw CognitoException.UserIdNotSet)
114112
digest.update(":".toByteArray())
115113
val userIdPasswordHash = digest.digest(password.toByteArray())
116114

@@ -155,8 +153,8 @@ class SRPHelper(private val password: String) {
155153
@Throws(CognitoException::class)
156154
internal fun generateM1Signature(key: ByteArray, secretBlock: String): ByteArray {
157155
val mac = HmacSHA256(key)
158-
mac.update(userPoolName?.toByteArray() ?: throw CognitoException.UserPoolNameNotSet)
159-
mac.update(userId?.toByteArray() ?: throw CognitoException.UserIdNotSet)
156+
mac.update(userPoolName.toByteArray())
157+
mac.update(userIdForSrp?.toByteArray() ?: throw CognitoException.UserIdNotSet)
160158
mac.update(Base64.decode(secretBlock))
161159
return mac.doFinal(timestamp.toByteArray())
162160
}
@@ -178,8 +176,8 @@ class SRPHelper(private val password: String) {
178176
* for the subsequent call to AWSCognitoIdentityProviderService.RespondToAuthChallenge
179177
* @return A string representing the PASSWORD_CLAIM_SIGNATURE for authentication.
180178
*/
181-
@Throws(CognitoException::class)
182-
fun getSignature(salt: String, srpB: String, secretBlock: String): String {
179+
@Throws(CognitoException::class, CancellationException::class)
180+
suspend fun getSignature(salt: String, srpB: String, secretBlock: String): String {
183181
val bigIntSRPB = BigInteger.parseString(srpB, HEX)
184182
val bigIntSalt = BigInteger.parseString(salt, HEX)
185183

build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
plugins {
2-
id("com.android.library").version("8.2.0-beta06").apply(false)
2+
id("com.android.library").version("8.2.0-rc01").apply(false)
33
kotlin("multiplatform").version("1.9.10").apply(false)
44
id("io.gitlab.arturbosch.detekt") version "1.23.1"
5+
id("com.github.ben-manes.versions") version "0.49.0"
56
}
67

78
tasks.register("clean", Delete::class) {

gradle/wrapper/gradle-wrapper.jar

4.07 KB
Binary file not shown.
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
#Sat Oct 14 10:22:57 PDT 2023
21
distributionBase=GRADLE_USER_HOME
32
distributionPath=wrapper/dists
4-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2.1-bin.zip
4+
networkTimeout=10000
5+
validateDistributionUrl=true
56
zipStoreBase=GRADLE_USER_HOME
67
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)