Skip to content

Commit

Permalink
Revert "Merge branch 'release-candidate' into main" (aws-amplify#871)
Browse files Browse the repository at this point in the history
This reverts commit a88a109, reversing
changes made to 324ebad.
  • Loading branch information
dnys1 authored and Noyes committed Oct 8, 2021
1 parent 393e39b commit f01b643
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<AWSCredentials> = 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))
Expand Down Expand Up @@ -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<String, Any> = hashMapOf("data" to data)
val call = MethodCall("getCurrentUser", arguments)
Expand Down Expand Up @@ -781,6 +764,18 @@ class AmplifyAuthCognitoPluginTest {

@Test
fun fetchSession_returnsSuccess() {
val id = AuthSessionResult.success("id")
val awsCredentials: AuthSessionResult<AWSCredentials> = 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ data class FlutterModelFieldType(val map: Map<String, Any>) {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,15 @@ val allTypeModelSchema = ModelSchema.builder()
"floatType" to
ModelField.builder()
.name("floatType")
.javaClassForValue(Double::class.javaObjectType)
.javaClassForValue(Double::class.java)
.targetType("Double")
.isRequired(true)
.isArray(false)
.build(),
"boolType" to
ModelField.builder()
.name("boolType")
.javaClassForValue(Boolean::class.javaObjectType)
.javaClassForValue(Boolean::class.java)
.targetType("Boolean")
.isRequired(true)
.isArray(false)
Expand Down

0 comments on commit f01b643

Please sign in to comment.