@@ -23,6 +23,7 @@ import io.ktor.utils.io.core.toByteArray
2323import org.kotlincrypto.SecureRandom
2424import org.kotlincrypto.hash.sha2.SHA256
2525import org.kotlincrypto.macs.hmac.sha2.HmacSHA256
26+ import kotlin.coroutines.cancellation.CancellationException
2627import kotlin.io.encoding.Base64
2728import 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
0 commit comments