Skip to content

Commit 7f4e639

Browse files
authored
chore(auth): Move Attribute management into usecase classes (#2984)
1 parent 416c971 commit 7f4e639

16 files changed

+804
-902
lines changed

aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/AWSCognitoAuthPlugin.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -348,53 +348,53 @@ class AWSCognitoAuthPlugin : AuthPlugin<AWSCognitoAuthService>() {
348348
) = enqueue(onSuccess, onError) { queueFacade.updatePassword(oldPassword, newPassword) }
349349

350350
override fun fetchUserAttributes(onSuccess: Consumer<List<AuthUserAttribute>>, onError: Consumer<AuthException>) =
351-
enqueue(onSuccess, onError) { queueFacade.fetchUserAttributes() }
351+
enqueue(onSuccess, onError) { useCaseFactory.fetchUserAttributes().execute() }
352352

353353
override fun updateUserAttribute(
354354
attribute: AuthUserAttribute,
355355
options: AuthUpdateUserAttributeOptions,
356356
onSuccess: Consumer<AuthUpdateAttributeResult>,
357357
onError: Consumer<AuthException>
358-
) = enqueue(onSuccess, onError) { queueFacade.updateUserAttribute(attribute, options) }
358+
) = enqueue(onSuccess, onError) { useCaseFactory.updateUserAttributes().execute(attribute, options) }
359359

360360
override fun updateUserAttribute(
361361
attribute: AuthUserAttribute,
362362
onSuccess: Consumer<AuthUpdateAttributeResult>,
363363
onError: Consumer<AuthException>
364-
) = enqueue(onSuccess, onError) { queueFacade.updateUserAttribute(attribute) }
364+
) = enqueue(onSuccess, onError) { useCaseFactory.updateUserAttributes().execute(attribute) }
365365

366366
override fun updateUserAttributes(
367367
attributes: List<AuthUserAttribute>,
368368
options: AuthUpdateUserAttributesOptions,
369369
onSuccess: Consumer<Map<AuthUserAttributeKey, AuthUpdateAttributeResult>>,
370370
onError: Consumer<AuthException>
371-
) = enqueue(onSuccess, onError) { queueFacade.updateUserAttributes(attributes, options) }
371+
) = enqueue(onSuccess, onError) { useCaseFactory.updateUserAttributes().execute(attributes, options) }
372372

373373
override fun updateUserAttributes(
374374
attributes: List<AuthUserAttribute>,
375375
onSuccess: Consumer<Map<AuthUserAttributeKey, AuthUpdateAttributeResult>>,
376376
onError: Consumer<AuthException>
377-
) = enqueue(onSuccess, onError) { queueFacade.updateUserAttributes(attributes) }
377+
) = enqueue(onSuccess, onError) { useCaseFactory.updateUserAttributes().execute(attributes) }
378378

379379
override fun resendUserAttributeConfirmationCode(
380380
attributeKey: AuthUserAttributeKey,
381381
options: AuthResendUserAttributeConfirmationCodeOptions,
382382
onSuccess: Consumer<AuthCodeDeliveryDetails>,
383383
onError: Consumer<AuthException>
384-
) = enqueue(onSuccess, onError) { queueFacade.resendUserAttributeConfirmationCode(attributeKey, options) }
384+
) = enqueue(onSuccess, onError) { useCaseFactory.resendUserAttributeConfirmation().execute(attributeKey, options) }
385385

386386
override fun resendUserAttributeConfirmationCode(
387387
attributeKey: AuthUserAttributeKey,
388388
onSuccess: Consumer<AuthCodeDeliveryDetails>,
389389
onError: Consumer<AuthException>
390-
) = enqueue(onSuccess, onError) { queueFacade.resendUserAttributeConfirmationCode(attributeKey) }
390+
) = enqueue(onSuccess, onError) { useCaseFactory.resendUserAttributeConfirmation().execute(attributeKey) }
391391

392392
override fun confirmUserAttribute(
393393
attributeKey: AuthUserAttributeKey,
394394
confirmationCode: String,
395395
onSuccess: Action,
396396
onError: Consumer<AuthException>
397-
) = enqueue(onSuccess, onError) { queueFacade.confirmUserAttribute(attributeKey, confirmationCode) }
397+
) = enqueue(onSuccess, onError) { useCaseFactory.confirmUserAttribute().execute(attributeKey, confirmationCode) }
398398

399399
override fun getCurrentUser(onSuccess: Consumer<AuthUser>, onError: Consumer<AuthException>) =
400400
enqueue(onSuccess, onError) { useCaseFactory.getCurrentUser().execute() }

aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/AWSCognitoAuthSession.kt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import com.amplifyframework.auth.AWSAuthSessionBehavior
1919
import com.amplifyframework.auth.AWSCognitoUserPoolTokens
2020
import com.amplifyframework.auth.AWSCredentials
2121
import com.amplifyframework.auth.AuthException
22+
import com.amplifyframework.auth.cognito.exceptions.configuration.InvalidUserPoolConfigurationException
2223
import com.amplifyframework.auth.cognito.helpers.SessionHelper
2324
import com.amplifyframework.auth.exceptions.ConfigurationException
2425
import com.amplifyframework.auth.exceptions.InvalidStateException
@@ -54,20 +55,20 @@ data class AWSCognitoAuthSession internal constructor(
5455
override val accessToken = userPoolTokensResult.value?.accessToken
5556
}
5657

57-
internal fun AmplifyCredential.isValid(): Boolean {
58-
return when (this) {
59-
is AmplifyCredential.UserPool -> SessionHelper.isValidTokens(signedInData.cognitoUserPoolTokens)
60-
is AmplifyCredential.UserAndIdentityPool ->
61-
SessionHelper.isValidTokens(signedInData.cognitoUserPoolTokens) && SessionHelper.isValidSession(credentials)
62-
is AmplifyCredential.IdentityPoolTypeCredential -> SessionHelper.isValidSession(credentials)
63-
else -> false
64-
}
58+
internal fun AWSCognitoAuthSession.requireAccessToken(): String =
59+
accessToken ?: throw InvalidUserPoolConfigurationException()
60+
61+
internal fun AmplifyCredential.isValid(): Boolean = when (this) {
62+
is AmplifyCredential.UserPool -> SessionHelper.isValidTokens(signedInData.cognitoUserPoolTokens)
63+
is AmplifyCredential.UserAndIdentityPool ->
64+
SessionHelper.isValidTokens(signedInData.cognitoUserPoolTokens) && SessionHelper.isValidSession(credentials)
65+
is AmplifyCredential.IdentityPoolTypeCredential -> SessionHelper.isValidSession(credentials)
66+
else -> false
6567
}
6668

6769
internal fun AmplifyCredential.getCognitoSession(
6870
exception: AuthException? = null
6971
): AWSAuthSessionBehavior<AWSCognitoUserPoolTokens> {
70-
7172
fun getCredentialsResult(
7273
awsCredentials: CognitoCredentials,
7374
exception: AuthException?
@@ -83,15 +84,14 @@ internal fun AmplifyCredential.getCognitoSession(
8384
} ?: AuthSessionResult.failure(UnknownException("Failed to fetch AWS credentials."))
8485
}
8586

86-
fun getIdentityIdResult(identityId: String, exception: AuthException?): AuthSessionResult<String> {
87-
return if (exception != null && exception !is SignedOutException) {
87+
fun getIdentityIdResult(identityId: String, exception: AuthException?): AuthSessionResult<String> =
88+
if (exception != null && exception !is SignedOutException) {
8889
AuthSessionResult.failure(exception)
8990
} else if (identityId.isNotEmpty()) {
9091
AuthSessionResult.success(identityId)
9192
} else {
9293
AuthSessionResult.failure(UnknownException("Failed to fetch identity id."))
9394
}
94-
}
9595

9696
fun getUserSubResult(userPoolTokens: CognitoUserPoolTokens?, exception: AuthException?): AuthSessionResult<String> {
9797
if (exception != null && exception !is SignedOutException) {

aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/KotlinAuthFacadeInternal.kt

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import android.content.Intent
2020
import com.amplifyframework.auth.AuthCodeDeliveryDetails
2121
import com.amplifyframework.auth.AuthProvider
2222
import com.amplifyframework.auth.AuthSession
23-
import com.amplifyframework.auth.AuthUserAttribute
24-
import com.amplifyframework.auth.AuthUserAttributeKey
2523
import com.amplifyframework.auth.TOTPSetupDetails
2624
import com.amplifyframework.auth.cognito.options.FederateToIdentityPoolOptions
2725
import com.amplifyframework.auth.cognito.result.FederateToIdentityPoolResult
@@ -30,20 +28,16 @@ import com.amplifyframework.auth.options.AuthConfirmSignInOptions
3028
import com.amplifyframework.auth.options.AuthConfirmSignUpOptions
3129
import com.amplifyframework.auth.options.AuthFetchSessionOptions
3230
import com.amplifyframework.auth.options.AuthResendSignUpCodeOptions
33-
import com.amplifyframework.auth.options.AuthResendUserAttributeConfirmationCodeOptions
3431
import com.amplifyframework.auth.options.AuthResetPasswordOptions
3532
import com.amplifyframework.auth.options.AuthSignInOptions
3633
import com.amplifyframework.auth.options.AuthSignOutOptions
3734
import com.amplifyframework.auth.options.AuthSignUpOptions
38-
import com.amplifyframework.auth.options.AuthUpdateUserAttributeOptions
39-
import com.amplifyframework.auth.options.AuthUpdateUserAttributesOptions
4035
import com.amplifyframework.auth.options.AuthVerifyTOTPSetupOptions
4136
import com.amplifyframework.auth.options.AuthWebUISignInOptions
4237
import com.amplifyframework.auth.result.AuthResetPasswordResult
4338
import com.amplifyframework.auth.result.AuthSignInResult
4439
import com.amplifyframework.auth.result.AuthSignOutResult
4540
import com.amplifyframework.auth.result.AuthSignUpResult
46-
import com.amplifyframework.auth.result.AuthUpdateAttributeResult
4741
import kotlin.coroutines.resume
4842
import kotlin.coroutines.resumeWithException
4943
import kotlin.coroutines.suspendCoroutine
@@ -256,87 +250,6 @@ internal class KotlinAuthFacadeInternal(private val delegate: RealAWSCognitoAuth
256250
)
257251
}
258252

259-
suspend fun fetchUserAttributes(): List<AuthUserAttribute> = suspendCoroutine { continuation ->
260-
delegate.fetchUserAttributes(
261-
{ continuation.resume(it) },
262-
{ continuation.resumeWithException(it) }
263-
)
264-
}
265-
266-
suspend fun updateUserAttribute(attribute: AuthUserAttribute): AuthUpdateAttributeResult =
267-
suspendCoroutine { continuation ->
268-
delegate.updateUserAttribute(
269-
attribute,
270-
{ continuation.resume(it) },
271-
{ continuation.resumeWithException(it) }
272-
)
273-
}
274-
275-
suspend fun updateUserAttribute(
276-
attribute: AuthUserAttribute,
277-
options: AuthUpdateUserAttributeOptions
278-
): AuthUpdateAttributeResult = suspendCoroutine { continuation ->
279-
delegate.updateUserAttribute(
280-
attribute,
281-
options,
282-
{ continuation.resume(it) },
283-
{ continuation.resumeWithException(it) }
284-
)
285-
}
286-
287-
suspend fun updateUserAttributes(
288-
attributes: List<AuthUserAttribute>
289-
): Map<AuthUserAttributeKey, AuthUpdateAttributeResult> = suspendCoroutine { continuation ->
290-
delegate.updateUserAttributes(
291-
attributes,
292-
{ continuation.resume(it) },
293-
{ continuation.resumeWithException(it) }
294-
)
295-
}
296-
297-
suspend fun updateUserAttributes(
298-
attributes: List<AuthUserAttribute>,
299-
options: AuthUpdateUserAttributesOptions
300-
): Map<AuthUserAttributeKey, AuthUpdateAttributeResult> = suspendCoroutine { continuation ->
301-
delegate.updateUserAttributes(
302-
attributes,
303-
options,
304-
{ continuation.resume(it) },
305-
{ continuation.resumeWithException(it) }
306-
)
307-
}
308-
309-
suspend fun resendUserAttributeConfirmationCode(attributeKey: AuthUserAttributeKey): AuthCodeDeliveryDetails =
310-
suspendCoroutine { continuation ->
311-
delegate.resendUserAttributeConfirmationCode(
312-
attributeKey,
313-
{ continuation.resume(it) },
314-
{ continuation.resumeWithException(it) }
315-
)
316-
}
317-
318-
suspend fun resendUserAttributeConfirmationCode(
319-
attributeKey: AuthUserAttributeKey,
320-
options: AuthResendUserAttributeConfirmationCodeOptions
321-
): AuthCodeDeliveryDetails = suspendCoroutine { continuation ->
322-
delegate.resendUserAttributeConfirmationCode(
323-
attributeKey,
324-
options,
325-
{ continuation.resume(it) },
326-
{ continuation.resumeWithException(it) }
327-
)
328-
}
329-
330-
suspend fun confirmUserAttribute(attributeKey: AuthUserAttributeKey, confirmationCode: String) =
331-
suspendCoroutine { continuation ->
332-
delegate.confirmUserAttribute(
333-
attributeKey,
334-
confirmationCode,
335-
{ continuation.resume(Unit) },
336-
{ continuation.resumeWithException(it) }
337-
)
338-
}
339-
340253
suspend fun signOut(): AuthSignOutResult = suspendCoroutine { continuation ->
341254
delegate.signOut { continuation.resume(it) }
342255
}

0 commit comments

Comments
 (0)