Skip to content

Commit e69cf08

Browse files
chore: add options for user attribute methods (#1436)
Co-authored-by: Raphael Kim <[email protected]>
1 parent 60e7330 commit e69cf08

File tree

16 files changed

+1171
-56
lines changed

16 files changed

+1171
-56
lines changed

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

Lines changed: 88 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@
3737
import com.amplifyframework.auth.cognito.options.AWSCognitoAuthConfirmSignInOptions;
3838
import com.amplifyframework.auth.cognito.options.AWSCognitoAuthConfirmSignUpOptions;
3939
import com.amplifyframework.auth.cognito.options.AWSCognitoAuthResendSignUpCodeOptions;
40+
import com.amplifyframework.auth.cognito.options.AWSCognitoAuthResendUserAttributeConfirmationCodeOptions;
4041
import com.amplifyframework.auth.cognito.options.AWSCognitoAuthResetPasswordOptions;
4142
import com.amplifyframework.auth.cognito.options.AWSCognitoAuthSignInOptions;
4243
import com.amplifyframework.auth.cognito.options.AWSCognitoAuthSignOutOptions;
4344
import com.amplifyframework.auth.cognito.options.AWSCognitoAuthSignUpOptions;
45+
import com.amplifyframework.auth.cognito.options.AWSCognitoAuthUpdateUserAttributeOptions;
46+
import com.amplifyframework.auth.cognito.options.AWSCognitoAuthUpdateUserAttributesOptions;
4447
import com.amplifyframework.auth.cognito.options.AWSCognitoAuthWebUISignInOptions;
4548
import com.amplifyframework.auth.cognito.util.AuthProviderConverter;
4649
import com.amplifyframework.auth.cognito.util.CognitoAuthExceptionConverter;
@@ -49,10 +52,13 @@
4952
import com.amplifyframework.auth.options.AuthConfirmSignInOptions;
5053
import com.amplifyframework.auth.options.AuthConfirmSignUpOptions;
5154
import com.amplifyframework.auth.options.AuthResendSignUpCodeOptions;
55+
import com.amplifyframework.auth.options.AuthResendUserAttributeConfirmationCodeOptions;
5256
import com.amplifyframework.auth.options.AuthResetPasswordOptions;
5357
import com.amplifyframework.auth.options.AuthSignInOptions;
5458
import com.amplifyframework.auth.options.AuthSignOutOptions;
5559
import com.amplifyframework.auth.options.AuthSignUpOptions;
60+
import com.amplifyframework.auth.options.AuthUpdateUserAttributeOptions;
61+
import com.amplifyframework.auth.options.AuthUpdateUserAttributesOptions;
5662
import com.amplifyframework.auth.options.AuthWebUISignInOptions;
5763
import com.amplifyframework.auth.result.AuthResetPasswordResult;
5864
import com.amplifyframework.auth.result.AuthSessionResult;
@@ -828,12 +834,21 @@ public void onError(Exception error) {
828834
@Override
829835
public void updateUserAttribute(
830836
@NonNull AuthUserAttribute attribute,
837+
@NonNull AuthUpdateUserAttributeOptions options,
831838
@NonNull Consumer<AuthUpdateAttributeResult> onSuccess,
832839
@NonNull Consumer<AuthException> onError
833840
) {
834841

842+
final Map<String, String> clientMetadata = new HashMap<>();
843+
if (options instanceof AWSCognitoAuthUpdateUserAttributeOptions) {
844+
AWSCognitoAuthUpdateUserAttributeOptions cognitoOptions =
845+
(AWSCognitoAuthUpdateUserAttributeOptions) options;
846+
clientMetadata.putAll(cognitoOptions.getMetadata());
847+
}
848+
835849
awsMobileClient.updateUserAttributes(
836850
Collections.singletonMap(attribute.getKey().getKeyString(), attribute.getValue()),
851+
clientMetadata,
837852
new Callback<List<UserCodeDeliveryDetails>>() {
838853
@Override
839854
public void onResult(List<UserCodeDeliveryDetails> result) {
@@ -868,19 +883,38 @@ public void onError(Exception error) {
868883
);
869884
}
870885

886+
@Override
887+
public void updateUserAttribute(
888+
@NonNull AuthUserAttribute attribute,
889+
@NonNull Consumer<AuthUpdateAttributeResult> onSuccess,
890+
@NonNull Consumer<AuthException> onError
891+
) {
892+
updateUserAttribute(attribute, AuthUpdateUserAttributeOptions.defaults(), onSuccess, onError);
893+
}
894+
871895
@Override
872896
public void updateUserAttributes(
873897
@NonNull List<AuthUserAttribute> attributes,
898+
@NonNull AuthUpdateUserAttributesOptions options,
874899
@NonNull Consumer<Map<AuthUserAttributeKey, AuthUpdateAttributeResult>> onSuccess,
875900
@NonNull Consumer<AuthException> onError
876901
) {
902+
903+
final Map<String, String> clientMetadata = new HashMap<>();
904+
if (options instanceof AWSCognitoAuthUpdateUserAttributesOptions) {
905+
AWSCognitoAuthUpdateUserAttributesOptions cognitoOptions =
906+
(AWSCognitoAuthUpdateUserAttributesOptions) options;
907+
clientMetadata.putAll(cognitoOptions.getMetadata());
908+
}
909+
877910
Map<String, String> attributesMap = new HashMap<>();
878911
for (AuthUserAttribute attribute : attributes) {
879912
attributesMap.put(attribute.getKey().getKeyString(), attribute.getValue());
880913
}
881914

882915
awsMobileClient.updateUserAttributes(
883916
attributesMap,
917+
clientMetadata,
884918
new Callback<List<UserCodeDeliveryDetails>>() {
885919
@Override
886920
public void onResult(List<UserCodeDeliveryDetails> result) {
@@ -928,28 +962,68 @@ public void onError(Exception error) {
928962
});
929963
}
930964

965+
@Override
966+
public void updateUserAttributes(
967+
@NonNull List<AuthUserAttribute> attributes,
968+
@NonNull Consumer<Map<AuthUserAttributeKey, AuthUpdateAttributeResult>> onSuccess,
969+
@NonNull Consumer<AuthException> onError
970+
) {
971+
updateUserAttributes(
972+
attributes,
973+
AuthUpdateUserAttributesOptions.defaults(),
974+
onSuccess,
975+
onError
976+
);
977+
}
978+
931979
@Override
932980
public void resendUserAttributeConfirmationCode(
933981
@NonNull AuthUserAttributeKey attributeKey,
982+
@NonNull AuthResendUserAttributeConfirmationCodeOptions options,
934983
@NonNull Consumer<AuthCodeDeliveryDetails> onSuccess,
935984
@NonNull Consumer<AuthException> onError
936985
) {
986+
987+
final Map<String, String> clientMetadata = new HashMap<>();
988+
if (options instanceof AWSCognitoAuthResendUserAttributeConfirmationCodeOptions) {
989+
AWSCognitoAuthResendUserAttributeConfirmationCodeOptions cognitoOptions =
990+
(AWSCognitoAuthResendUserAttributeConfirmationCodeOptions) options;
991+
clientMetadata.putAll(cognitoOptions.getMetadata());
992+
}
993+
937994
String attributeName = attributeKey.getKeyString();
938-
awsMobileClient.verifyUserAttribute(attributeName, new Callback<UserCodeDeliveryDetails>() {
939-
@Override
940-
public void onResult(UserCodeDeliveryDetails result) {
941-
onSuccess.accept(convertCodeDeliveryDetails(result));
942-
}
995+
awsMobileClient.verifyUserAttribute(
996+
attributeName,
997+
clientMetadata,
998+
new Callback<UserCodeDeliveryDetails>() {
999+
@Override
1000+
public void onResult(UserCodeDeliveryDetails result) {
1001+
onSuccess.accept(convertCodeDeliveryDetails(result));
1002+
}
9431003

944-
@Override
945-
public void onError(Exception error) {
946-
onError.accept(new AuthException(
947-
"Failed to resend user attribute confirmation code",
948-
error,
949-
"See attached exception for more details"
950-
));
951-
}
952-
});
1004+
@Override
1005+
public void onError(Exception error) {
1006+
onError.accept(new AuthException(
1007+
"Failed to resend user attribute confirmation code",
1008+
error,
1009+
"See attached exception for more details"
1010+
));
1011+
}
1012+
});
1013+
}
1014+
1015+
@Override
1016+
public void resendUserAttributeConfirmationCode(
1017+
@NonNull AuthUserAttributeKey attributeKey,
1018+
@NonNull Consumer<AuthCodeDeliveryDetails> onSuccess,
1019+
@NonNull Consumer<AuthException> onError
1020+
) {
1021+
resendUserAttributeConfirmationCode(
1022+
attributeKey,
1023+
AuthResendUserAttributeConfirmationCodeOptions.defaults(),
1024+
onSuccess,
1025+
onError
1026+
);
9531027
}
9541028

9551029
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package com.amplifyframework.auth.cognito.options;
17+
18+
import androidx.annotation.NonNull;
19+
import androidx.core.util.ObjectsCompat;
20+
21+
import com.amplifyframework.auth.options.AuthResendUserAttributeConfirmationCodeOptions;
22+
import com.amplifyframework.util.Immutable;
23+
24+
import java.util.HashMap;
25+
import java.util.Map;
26+
import java.util.Objects;
27+
28+
/**
29+
* Cognito extension of resend user attribute confirmation code options to add the platform specific fields.
30+
*/
31+
public final class AWSCognitoAuthResendUserAttributeConfirmationCodeOptions
32+
extends AuthResendUserAttributeConfirmationCodeOptions {
33+
private final Map<String, String> metadata;
34+
35+
/**
36+
* Advanced options for resend user attribute confirmation code.
37+
* @param metadata Additional custom attributes to be sent to the service such as information about the client
38+
*/
39+
protected AWSCognitoAuthResendUserAttributeConfirmationCodeOptions(
40+
Map<String, String> metadata
41+
) {
42+
this.metadata = metadata;
43+
}
44+
45+
/**
46+
* Get custom attributes to be sent to the service such as information about the client.
47+
* @return custom attributes to be sent to the service such as information about the client
48+
*/
49+
@NonNull
50+
public Map<String, String> getMetadata() {
51+
return metadata;
52+
}
53+
54+
/**
55+
* Get a builder object.
56+
* @return a builder object.
57+
*/
58+
@NonNull
59+
public static CognitoBuilder builder() {
60+
return new CognitoBuilder();
61+
}
62+
63+
@Override
64+
public int hashCode() {
65+
return ObjectsCompat.hash(
66+
getMetadata()
67+
);
68+
}
69+
70+
@Override
71+
public boolean equals(Object obj) {
72+
if (this == obj) {
73+
return true;
74+
} else if (obj == null || getClass() != obj.getClass()) {
75+
return false;
76+
} else {
77+
AWSCognitoAuthResendUserAttributeConfirmationCodeOptions authResendUserAttributeConfirmationCodeOptions =
78+
(AWSCognitoAuthResendUserAttributeConfirmationCodeOptions) obj;
79+
return ObjectsCompat.equals(getMetadata(), authResendUserAttributeConfirmationCodeOptions.getMetadata());
80+
}
81+
}
82+
83+
@Override
84+
public String toString() {
85+
return "AWSCognitoAuthResendUserAttributeConfirmationCodeOptions{" +
86+
"metadata=" + metadata +
87+
'}';
88+
}
89+
90+
/**
91+
* The builder for this class.
92+
*/
93+
public static final class CognitoBuilder extends Builder<CognitoBuilder> {
94+
private Map<String, String> metadata;
95+
96+
/**
97+
* Constructor for the builder.
98+
*/
99+
public CognitoBuilder() {
100+
super();
101+
this.metadata = new HashMap<>();
102+
}
103+
104+
/**
105+
* Returns the type of builder this is to support proper flow with it being an extended class.
106+
* @return the type of builder this is to support proper flow with it being an extended class.
107+
*/
108+
@Override
109+
public CognitoBuilder getThis() {
110+
return this;
111+
}
112+
113+
/**
114+
* Set the metadata field for the object being built.
115+
* @param metadata Custom user metadata to be sent with the resend user attribute confirmation code request.
116+
* @return The builder object to continue building.
117+
*/
118+
@NonNull
119+
public CognitoBuilder metadata(@NonNull Map<String, String> metadata) {
120+
Objects.requireNonNull(metadata);
121+
this.metadata.clear();
122+
this.metadata.putAll(metadata);
123+
return getThis();
124+
}
125+
126+
/**
127+
* Construct and return the object with the values set in the builder.
128+
* @return a new instance of AWSCognitoAuthResendUserAttributeConfirmationCodeOptions
129+
* with the values specified in the builder.
130+
*/
131+
@NonNull
132+
public AWSCognitoAuthResendUserAttributeConfirmationCodeOptions build() {
133+
return new AWSCognitoAuthResendUserAttributeConfirmationCodeOptions(
134+
Immutable.of(metadata));
135+
}
136+
}
137+
}

0 commit comments

Comments
 (0)