From f01b6436186be1d258a680b0d5a31807bba60996 Mon Sep 17 00:00:00 2001 From: Dillon Nys <24740863+dnys1@users.noreply.github.com> Date: Fri, 10 Sep 2021 09:24:48 -0700 Subject: [PATCH] Revert "Merge branch 'release-candidate' into main" (#871) This reverts commit a88a10947b0b5030b06ec01135f71b1c3561de8e, reversing changes made to 324ebad1904ebebc46e26432a93642b2776698ba. --- .../amplify_auth_cognito/AuthCognito.kt | 49 +++---------------- .../AmplifyAuthCognitoPluginTest.kt | 37 ++++++-------- .../types/model/FlutterModelFieldType.kt | 4 +- .../amplify/amplify_datastore/SchemaData.kt | 4 +- 4 files changed, 28 insertions(+), 66 deletions(-) diff --git a/packages/amplify_auth_cognito/android/src/main/kotlin/com/amazonaws/amplify/amplify_auth_cognito/AuthCognito.kt b/packages/amplify_auth_cognito/android/src/main/kotlin/com/amazonaws/amplify/amplify_auth_cognito/AuthCognito.kt index c7da98d32b..0ee5714097 100644 --- a/packages/amplify_auth_cognito/android/src/main/kotlin/com/amazonaws/amplify/amplify_auth_cognito/AuthCognito.kt +++ b/packages/amplify_auth_cognito/android/src/main/kotlin/com/amazonaws/amplify/amplify_auth_cognito/AuthCognito.kt @@ -52,7 +52,6 @@ import com.amazonaws.amplify.amplify_auth_cognito.types.FlutterResendUserAttribu import com.amazonaws.amplify.amplify_auth_cognito.types.FlutterSignOutRequest import com.amazonaws.amplify.amplify_core.exception.ExceptionUtil.Companion.handleAddPluginException import com.amazonaws.amplify.amplify_auth_cognito.utils.isRedirectActivityDeclared -import com.amazonaws.mobile.client.AWSMobileClient import com.amplifyframework.auth.AuthException import com.amplifyframework.auth.AuthProvider import com.amplifyframework.auth.AuthSession @@ -392,37 +391,13 @@ public class AuthCognito : FlutterPlugin, ActivityAware, MethodCallHandler, Plug private fun onGetCurrentUser(@NonNull flutterResult: Result) { try { - /* - Because Android does not preserve the user after session expiration but iOS does so, - we need to use the mobileclient to get the old username to enforce platform parity. - */ - Amplify.Auth.fetchAuthSession( - { result -> - val cognitoAuthSession = result as AWSCognitoAuthSession - when (cognitoAuthSession.userSub.type) { - // If the user sub accessor successful the user is present and the session should be valid - AuthSessionResult.Type.SUCCESS -> { - val awsMobileClient = AWSMobileClient.getInstance() - val username = awsMobileClient.username - prepareUserResult(flutterResult, AuthUser(cognitoAuthSession.userSub.toString(), username)); - } - // If the user sub accessor failed, check the signIn state - AuthSessionResult.Type.FAILURE -> { - // if signedIn flag still true, we know session is expired so get the old user - if (result.isSignedIn) { - val awsMobileClient = AWSMobileClient.getInstance() - val username = awsMobileClient.username - val userid = awsMobileClient.userSub - prepareUserResult(flutterResult, AuthUser(userid, username)); - // if signIn flag is false, we assume user is signed out so throw exception - } else { - errorHandler.handleAuthError(flutterResult, AuthException.SignedOutException()) - } - } - } - }, - { error -> errorHandler.handleAuthError(flutterResult, error) } - ) + var user: AuthUser? = Amplify.Auth.currentUser; + if (user is AuthUser) { + prepareUserResult(flutterResult, user); + } else { + // TODO: Mechanism to check guest access status + throw AuthException.SignedOutException(AuthException.GuestAccess.GUEST_ACCESS_DISABLED) + } } catch (e: AuthException) { errorHandler.handleAuthError(flutterResult, e) } catch (e: Exception) { @@ -607,15 +582,7 @@ public class AuthCognito : FlutterPlugin, ActivityAware, MethodCallHandler, Plug } fun prepareCognitoSessionFailure(@NonNull flutterResult: Result, @NonNull result: AWSCognitoAuthSession) { - // If a User Pool token's error is a SignedOutException, we send SignedOutException as - // method call response because this indicates that the problem is not expired tokens, - // but total lack of authentication (i.e. the user is signed out) - var sessionException: AuthException = if (result.userPoolTokens.error is AuthException.SignedOutException) { - AuthException.SignedOutException() - } else { - AuthException.SessionExpiredException() - } - errorHandler.handleAuthError(flutterResult, sessionException) + errorHandler.handleAuthError(flutterResult, AuthException.SessionExpiredException()) } fun prepareSessionResult(@NonNull flutterResult: Result, @NonNull result: AuthSession) { diff --git a/packages/amplify_auth_cognito/android/src/test/kotlin/com/amazonaws/amplify/amplify_auth_cognito/AmplifyAuthCognitoPluginTest.kt b/packages/amplify_auth_cognito/android/src/test/kotlin/com/amazonaws/amplify/amplify_auth_cognito/AmplifyAuthCognitoPluginTest.kt index 3d6d817ca1..5daed49fd5 100644 --- a/packages/amplify_auth_cognito/android/src/test/kotlin/com/amazonaws/amplify/amplify_auth_cognito/AmplifyAuthCognitoPluginTest.kt +++ b/packages/amplify_auth_cognito/android/src/test/kotlin/com/amazonaws/amplify/amplify_auth_cognito/AmplifyAuthCognitoPluginTest.kt @@ -82,20 +82,8 @@ class AmplifyAuthCognitoPluginTest { private val mockSignInResult = AuthSignInResult(false, signInStep) private val mockResetPasswordResult = AuthResetPasswordResult(false, resetStep) private val mockUpdateUserAttributeResult = AuthUpdateAttributeResult(true, updateAttributeStep) - private val id = AuthSessionResult.success("id") - private val awsCredentials: AuthSessionResult = AuthSessionResult.success(BasicAWSCredentials("access", "secret")) - private val userSub = AuthSessionResult.success("sub") - private val tokens = AuthSessionResult.success(AWSCognitoUserPoolTokens("access", "id", "refresh")) - private val mockSession = AWSCognitoAuthSession( - true, - id, - awsCredentials, - userSub, - tokens - ) private var mockAuth = mock(AuthCategory::class.java) - @Before fun setup() { plugin = AuthCognito(AuthCognitoHubEventStreamHandler(), mock(Activity::class.java)) @@ -729,18 +717,13 @@ class AmplifyAuthCognitoPluginTest { fun getCurrentUser_returnsSuccess() { // Arrange doAnswer { invocation: InvocationOnMock -> - plugin.prepareUserResult(mockResult, AuthUser("username", "userSub")) - null as Void? - }.`when`(mockAuth).currentUser - - doAnswer { invocation: InvocationOnMock -> - plugin.prepareCognitoSessionResult(mockResult, mockSession) + plugin.prepareUpdatePasswordResult(mockResult) null as Void? - }.`when`(mockAuth).fetchAuthSession(any(), any()) + }.`when`(mockAuth).getCurrentUser() val data: HashMap<*, *> = hashMapOf( - "username" to "username", - "userSub" to "userSub" + "username" to "username", + "userSub" to "userSub" ) val arguments: HashMap = hashMapOf("data" to data) val call = MethodCall("getCurrentUser", arguments) @@ -781,6 +764,18 @@ class AmplifyAuthCognitoPluginTest { @Test fun fetchSession_returnsSuccess() { + val id = AuthSessionResult.success("id") + val awsCredentials: AuthSessionResult = AuthSessionResult.success(BasicAWSCredentials("access", "secret")) + val userSub = AuthSessionResult.success("sub") + val tokens = AuthSessionResult.success(AWSCognitoUserPoolTokens("access", "id", "refresh")) + val mockSession = AWSCognitoAuthSession( + true, + id, + awsCredentials, + userSub, + tokens + ) + // Arrange doAnswer { invocation: InvocationOnMock -> plugin.prepareCognitoSessionResult(mockResult, mockSession) diff --git a/packages/amplify_datastore/android/src/main/kotlin/com/amazonaws/amplify/amplify_datastore/types/model/FlutterModelFieldType.kt b/packages/amplify_datastore/android/src/main/kotlin/com/amazonaws/amplify/amplify_datastore/types/model/FlutterModelFieldType.kt index a464abe89a..35f61379a1 100644 --- a/packages/amplify_datastore/android/src/main/kotlin/com/amazonaws/amplify/amplify_datastore/types/model/FlutterModelFieldType.kt +++ b/packages/amplify_datastore/android/src/main/kotlin/com/amazonaws/amplify/amplify_datastore/types/model/FlutterModelFieldType.kt @@ -59,12 +59,12 @@ data class FlutterModelFieldType(val map: Map) { return when (fieldType) { "string" -> String::class.java "int" -> Integer::class.java - "double" -> Double::class.javaObjectType + "double" -> Double::class.java "date" -> Temporal.Date::class.java "dateTime" -> Temporal.DateTime::class.java "time" -> Temporal.Time::class.java "timestamp" -> Temporal.Timestamp::class.java - "bool" -> Boolean::class.javaObjectType + "bool" -> Boolean::class.java "enumeration" -> String::class.java "model" -> Model::class.java "collection" -> List::class.java diff --git a/packages/amplify_datastore/android/src/test/kotlin/com/amazonaws/amplify/amplify_datastore/SchemaData.kt b/packages/amplify_datastore/android/src/test/kotlin/com/amazonaws/amplify/amplify_datastore/SchemaData.kt index 55b768615c..25434ccee3 100644 --- a/packages/amplify_datastore/android/src/test/kotlin/com/amazonaws/amplify/amplify_datastore/SchemaData.kt +++ b/packages/amplify_datastore/android/src/test/kotlin/com/amazonaws/amplify/amplify_datastore/SchemaData.kt @@ -266,7 +266,7 @@ val allTypeModelSchema = ModelSchema.builder() "floatType" to ModelField.builder() .name("floatType") - .javaClassForValue(Double::class.javaObjectType) + .javaClassForValue(Double::class.java) .targetType("Double") .isRequired(true) .isArray(false) @@ -274,7 +274,7 @@ val allTypeModelSchema = ModelSchema.builder() "boolType" to ModelField.builder() .name("boolType") - .javaClassForValue(Boolean::class.javaObjectType) + .javaClassForValue(Boolean::class.java) .targetType("Boolean") .isRequired(true) .isArray(false)