-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): add updateUserAttributes (batch) (#601)
* feat: add auth.updateUserAttributes * Apply suggestions from code review Co-authored-by: Chris F <[email protected]> * address pr comments * refactor android user attributes * consolidate user attr logic on iOS * refactor deliveryDetails serialization * add missing newline * revert missing attribute changes * update serializeAuthUpdateAttributeResult for nil * move comment to to of file * bump amplify-android to 1.17.7 Co-authored-by: Chris F <[email protected]>
- Loading branch information
1 parent
4209c80
commit 8b0893c
Showing
39 changed files
with
1,105 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...in/com/amazonaws/amplify/amplify_auth_cognito/types/FlutterUpdateUserAttributesRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package com.amazonaws.amplify.amplify_auth_cognito.types | ||
|
||
import com.amazonaws.amplify.amplify_auth_cognito.utils.createAuthUserAttribute | ||
import com.amazonaws.amplify.amplify_auth_cognito.utils.validateUserAttribute | ||
import com.amazonaws.amplify.amplify_core.exception.ExceptionMessages | ||
import com.amazonaws.amplify.amplify_core.exception.InvalidRequestException | ||
import com.amplifyframework.auth.AuthUserAttribute | ||
|
||
data class FlutterUpdateUserAttributesRequest(val map: HashMap<String, *>) { | ||
|
||
val attributes: List<AuthUserAttribute> = (map["attributes"] as List<HashMap<*, *>>) | ||
.map { createAuthUserAttribute(it["userAttributeKey"] as String, it["value"] as String) } | ||
|
||
companion object { | ||
private const val validationErrorMessage: String = "UpdateUserAttributesRequest Request malformed." | ||
fun validate(req: HashMap<String, *>?) { | ||
if (req == null) { | ||
throw InvalidRequestException(validationErrorMessage, ExceptionMessages.missingAttribute.format("request map")) | ||
} else if (!req.containsKey("attributes") || req["attributes"] !is List<*>) { | ||
throw InvalidRequestException(validationErrorMessage, ExceptionMessages.missingAttribute.format("attributes")) | ||
} else { | ||
val attributes = req["attributes"] as List<HashMap<*, *>> | ||
if (attributes.isEmpty()) { | ||
throw InvalidRequestException(validationErrorMessage, "The request must have at least one attribute.") | ||
} else { | ||
attributes.forEach { validateUserAttribute(it, validationErrorMessage) } | ||
} | ||
} | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...lin/com/amazonaws/amplify/amplify_auth_cognito/types/FlutterUpdateUserAttributesResult.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package com.amazonaws.amplify.amplify_auth_cognito.types | ||
|
||
import com.amazonaws.amplify.amplify_auth_cognito.utils.serializeAuthUpdateAttributeResult | ||
import com.amplifyframework.auth.AuthUserAttributeKey | ||
import com.amplifyframework.auth.result.AuthUpdateAttributeResult | ||
|
||
data class FlutterUpdateUserAttributesResult(private val raw: Map<AuthUserAttributeKey, AuthUpdateAttributeResult>) { | ||
val attributes: Map<AuthUserAttributeKey, AuthUpdateAttributeResult> = raw; | ||
|
||
fun toValueMap(): Map<String, Any> { | ||
return attributes.entries.associate { | ||
it.key.keyString to serializeAuthUpdateAttributeResult(it.value) | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
.../com/amazonaws/amplify/amplify_auth_cognito/utils/AuthCodeDeliveryDetailsSerialization.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
|
||
package com.amazonaws.amplify.amplify_auth_cognito.utils | ||
|
||
import com.amplifyframework.auth.AuthCodeDeliveryDetails | ||
|
||
fun serializeAuthCodeDeliveryDetails(deliveryDetails: AuthCodeDeliveryDetails?): Map<String, Any> { | ||
return mapOf( | ||
"destination" to (deliveryDetails?.destination ?: ""), | ||
"deliveryMedium" to (deliveryDetails?.deliveryMedium?.name | ||
?: ""), | ||
"attributeName" to (deliveryDetails?.attributeName ?: "") | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...ain/kotlin/com/amazonaws/amplify/amplify_auth_cognito/utils/UserAttributeSerialization.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package com.amazonaws.amplify.amplify_auth_cognito.utils | ||
|
||
import com.amplifyframework.auth.result.AuthUpdateAttributeResult | ||
import com.amplifyframework.auth.result.step.AuthNextUpdateAttributeStep | ||
import com.google.gson.Gson | ||
|
||
fun serializeAuthUpdateAttributeResult(result: AuthUpdateAttributeResult): Map<String, Any> { | ||
return mapOf( | ||
"isUpdated" to result.isUpdated, | ||
"nextStep" to serializeAuthUpdateAttributeStep(result.nextStep) | ||
) | ||
} | ||
|
||
fun serializeAuthUpdateAttributeStep(nextStep: AuthNextUpdateAttributeStep): Map<String, Any> { | ||
return mapOf( | ||
"updateAttributeStep" to nextStep.updateAttributeStep.toString(), | ||
"additionalInfo" to Gson().toJson(nextStep.additionalInfo), | ||
"codeDeliveryDetails" to serializeAuthCodeDeliveryDetails(nextStep.codeDeliveryDetails) | ||
) | ||
} |
30 changes: 30 additions & 0 deletions
30
...c/main/kotlin/com/amazonaws/amplify/amplify_auth_cognito/utils/UserAttributeValidation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package com.amazonaws.amplify.amplify_auth_cognito.utils | ||
|
||
import com.amazonaws.amplify.amplify_core.exception.ExceptionMessages | ||
import com.amazonaws.amplify.amplify_core.exception.InvalidRequestException | ||
|
||
fun validateUserAttribute(attribute: HashMap<*, *>, validationErrorMessage: String) { | ||
if (!attribute.containsKey("value")) { | ||
throw InvalidRequestException(validationErrorMessage, ExceptionMessages.missingAttribute.format("value")) | ||
} else if (!attribute.containsKey("userAttributeKey")) { | ||
throw InvalidRequestException(validationErrorMessage, ExceptionMessages.missingAttribute.format("userAttributeKey")) | ||
} else if (attribute["value"] !is String) { | ||
// Android SDK expects a string for user attr values, regardless of the configuration in cognito | ||
throw InvalidRequestException(validationErrorMessage, "Attribute value is not a String.") | ||
} | ||
} |
Oops, something went wrong.