Skip to content

Commit

Permalink
upgrade Gradle, add forgot password, require user pool on setup
Browse files Browse the repository at this point in the history
  • Loading branch information
hbmartin committed Oct 18, 2023
1 parent b47c8fd commit 58c3d37
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 132 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.jump.sdk.amplifyframework

enum class CognitoAction(val headerValue: String) {
CONFIRM_FORGOT_PASSWORD("AWSCognitoIdentityProviderService.ConfirmForgotPassword"),
CONFIRM_SIGN_UP("AWSCognitoIdentityProviderService.ConfirmSignUp"),
SIGN_UP("AWSCognitoIdentityProviderService.SignUp"),
FORGOT_PASSWORD("AWSCognitoIdentityProviderService.ForgotPassword"),
INITIATE_AUTH("AWSCognitoIdentityProviderService.InitiateAuth"),
RESPOND_TO_AUTH_CHALLENGE("AWSCognitoIdentityProviderService.RespondToAuthChallenge"),
SIGN_UP("AWSCognitoIdentityProviderService.SignUp"),
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ package com.jump.sdk.amplifyframework
sealed class CognitoException(override val message: String) : Exception(message) {
data object BadSrpB : CognitoException("Bad server public value 'B'")
data object HashOfAAndSrpBCannotBeZero : CognitoException("Hash of A and B cannot be zero")
data object UserPoolNameNotSet : CognitoException("Must call setUserPoolParams() before this")
data object UserIdNotSet : CognitoException("Must call setUserPoolParams() before this")
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ object CognitoKeys {
const val PASSWORD_CLAIM_SECRET_BLOCK = "PASSWORD_CLAIM_SECRET_BLOCK"
const val PASSWORD_CLAIM_SIGNATURE = "PASSWORD_CLAIM_SIGNATURE"
const val PASSWORD_VERIFIER = "PASSWORD_VERIFIER"
const val REFRESH_TOKEN_AUTH = "REFRESH_TOKEN_AUTH"
const val REFRESH_TOKEN = "REFRESH_TOKEN"
const val SALT = "SALT"
const val SECRET_BLOCK = "SECRET_BLOCK"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import io.ktor.utils.io.core.toByteArray
import org.kotlincrypto.SecureRandom
import org.kotlincrypto.hash.sha2.SHA256
import org.kotlincrypto.macs.hmac.sha2.HmacSHA256
import kotlin.coroutines.cancellation.CancellationException
import kotlin.io.encoding.Base64
import kotlin.io.encoding.ExperimentalEncodingApi

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

@OptIn(ExperimentalEncodingApi::class)
@Suppress("TooManyFunctions")
class SRPHelper(private val password: String) {
class SRPHelper(private val password: String, userPoolName: String) {
@Suppress("VariableNaming")
private val N = BigInteger.parseString(HEX_N, 16)

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

private val digest = SHA256()
var userIdForSrp: String? = null
private val userPoolName: String

init {
if (userPoolName.contains("_")) {
this.userPoolName = userPoolName.split(Regex("_"), 2)[1]
} else {
this.userPoolName = userPoolName
}

// Generate client private 'a' and public 'A' values
do {
privateA = BigInteger.fromByteArray(random.nextBytesOf(EPHEMERAL_KEY_LENGTH), Sign.POSITIVE).mod(N)
Expand All @@ -76,17 +85,6 @@ class SRPHelper(private val password: String) {
k = BigInteger.fromByteArray(digest.digest(g.toByteArray()), Sign.POSITIVE)
}

private var userId: String? = null
private var userPoolName: String? = null

fun setUserPoolParams(userIdForSrp: String, userPoolName: String) {
this.userId = userIdForSrp
this.userPoolName = userPoolName
if (userPoolName.contains("_")) {
this.userPoolName = userPoolName.split(Regex("_"), 2)[1]
}
}

// @TestOnly
internal fun modN(value: BigInteger): BigInteger = value.mod(N)

Expand All @@ -109,8 +107,8 @@ class SRPHelper(private val password: String) {
@Throws(CognitoException::class)
internal fun computeX(salt: BigInteger): BigInteger {
digest.reset()
digest.update(userPoolName?.toByteArray() ?: throw CognitoException.UserPoolNameNotSet)
digest.update(userId?.toByteArray() ?: throw CognitoException.UserIdNotSet)
digest.update(userPoolName.toByteArray())
digest.update(userIdForSrp?.toByteArray() ?: throw CognitoException.UserIdNotSet)
digest.update(":".toByteArray())
val userIdPasswordHash = digest.digest(password.toByteArray())

Expand Down Expand Up @@ -155,8 +153,8 @@ class SRPHelper(private val password: String) {
@Throws(CognitoException::class)
internal fun generateM1Signature(key: ByteArray, secretBlock: String): ByteArray {
val mac = HmacSHA256(key)
mac.update(userPoolName?.toByteArray() ?: throw CognitoException.UserPoolNameNotSet)
mac.update(userId?.toByteArray() ?: throw CognitoException.UserIdNotSet)
mac.update(userPoolName.toByteArray())
mac.update(userIdForSrp?.toByteArray() ?: throw CognitoException.UserIdNotSet)
mac.update(Base64.decode(secretBlock))
return mac.doFinal(timestamp.toByteArray())
}
Expand All @@ -178,8 +176,8 @@ class SRPHelper(private val password: String) {
* for the subsequent call to AWSCognitoIdentityProviderService.RespondToAuthChallenge
* @return A string representing the PASSWORD_CLAIM_SIGNATURE for authentication.
*/
@Throws(CognitoException::class)
fun getSignature(salt: String, srpB: String, secretBlock: String): String {
@Throws(CognitoException::class, CancellationException::class)
suspend fun getSignature(salt: String, srpB: String, secretBlock: String): String {
val bigIntSRPB = BigInteger.parseString(srpB, HEX)
val bigIntSalt = BigInteger.parseString(salt, HEX)

Expand Down
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
plugins {
id("com.android.library").version("8.2.0-beta06").apply(false)
id("com.android.library").version("8.2.0-rc01").apply(false)
kotlin("multiplatform").version("1.9.10").apply(false)
id("io.gitlab.arturbosch.detekt") version "1.23.1"
id("com.github.ben-manes.versions") version "0.49.0"
}

tasks.register("clean", Delete::class) {
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
5 changes: 3 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#Sat Oct 14 10:22:57 PDT 2023
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2.1-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit 58c3d37

Please sign in to comment.