Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(aws-auth-cognito): Adds clientMetadata to AWSCognitoAuthSignUpOptions #1407

Merged
merged 3 commits into from
Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ public void signUp(
) {
Map<String, String> userAttributes = new HashMap<>();
Map<String, String> validationData = new HashMap<>();
Map<String, String> clientMetadata = new HashMap<>();

if (options.getUserAttributes() != null) {
for (AuthUserAttribute attribute : options.getUserAttributes()) {
Expand All @@ -281,13 +282,15 @@ public void signUp(

if (options instanceof AWSCognitoAuthSignUpOptions) {
validationData = ((AWSCognitoAuthSignUpOptions) options).getValidationData();
clientMetadata = ((AWSCognitoAuthSignUpOptions) options).getClientMetadata();
}

awsMobileClient.signUp(
username,
password,
userAttributes,
validationData,
clientMetadata,
new Callback<SignUpResult>() {
@Override
public void onResult(SignUpResult result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,23 @@
* Cognito extension of sign up options to add the platform specific fields.
*/
public final class AWSCognitoAuthSignUpOptions extends AuthSignUpOptions {
private final Map<String, String> clientMetadata;
private final Map<String, String> validationData;

/**
* Advanced options for signing in.
* @param userAttributes Additional user attributes which should be associated with this user on registration
* @param validationData A map of custom key/values to be sent as part of the sign up process
* @param clientMetadata Additional custom attributes to be sent to the service such as information about the client
*/
protected AWSCognitoAuthSignUpOptions(
List<AuthUserAttribute> userAttributes,
Map<String, String> validationData
Map<String, String> validationData,
Map<String, String> clientMetadata
) {
super(userAttributes);
this.validationData = validationData;
this.clientMetadata = clientMetadata;
}

/**
Expand All @@ -55,6 +59,15 @@ public Map<String, String> getValidationData() {
return validationData;
}

/**
* Get additional custom attributes to be sent to the service such as information about the client.
* @return a map of additional custom attributes to be sent to the service such as information about the client
*/
@NonNull
public Map<String, String> getClientMetadata() {
return clientMetadata;
}

/**
* Returns a builder for this object.
* @return a builder for this object.
Expand All @@ -68,7 +81,8 @@ public static CognitoBuilder builder() {
public int hashCode() {
return ObjectsCompat.hash(
getUserAttributes(),
getValidationData()
getValidationData(),
getClientMetadata()
);
}

Expand All @@ -81,7 +95,8 @@ public boolean equals(Object obj) {
} else {
AWSCognitoAuthSignUpOptions authSignUpOptions = (AWSCognitoAuthSignUpOptions) obj;
return ObjectsCompat.equals(getUserAttributes(), authSignUpOptions.getUserAttributes()) &&
ObjectsCompat.equals(getValidationData(), authSignUpOptions.getValidationData());
ObjectsCompat.equals(getValidationData(), authSignUpOptions.getValidationData()) &&
ObjectsCompat.equals(getClientMetadata(), authSignUpOptions.getClientMetadata());
}
}

Expand All @@ -90,6 +105,7 @@ public String toString() {
return "AWSCognitoAuthSignUpOptions{" +
"userAttributes=" + getUserAttributes() +
", validationData=" + getValidationData() +
", clientMetadata=" + getClientMetadata() +
'}';
}

Expand All @@ -98,13 +114,15 @@ public String toString() {
*/
public static final class CognitoBuilder extends Builder<CognitoBuilder> {
private Map<String, String> validationData;
private Map<String, String> clientMetadata;

/**
* Constructs the builder.
*/
public CognitoBuilder() {
super();
this.validationData = new HashMap<>();
this.clientMetadata = new HashMap<>();
}

/**
Expand All @@ -129,6 +147,20 @@ public CognitoBuilder validationData(@NonNull Map<String, String> validationData
return getThis();
}

/**
* A map of additional custom attributes to be sent to the service such as information about the client.
* @param clientMetadata A map of additional custom attributes to be sent to the service such as information
* about the client.
* @return the instance of the builder.
*/
@NonNull
public CognitoBuilder clientMetadata(@NonNull Map<String, String> clientMetadata) {
Objects.requireNonNull(clientMetadata);
this.clientMetadata.clear();
this.clientMetadata.putAll(clientMetadata);
return getThis();
}

/**
* Build the object.
* @return a new instance of AWSCognitoAuthSignUpOptions.
Expand All @@ -137,7 +169,8 @@ public CognitoBuilder validationData(@NonNull Map<String, String> validationData
public AWSCognitoAuthSignUpOptions build() {
return new AWSCognitoAuthSignUpOptions(
Immutable.of(super.getUserAttributes()),
Immutable.of(validationData));
Immutable.of(validationData),
Immutable.of(clientMetadata));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,16 @@ public void signUp() throws AuthException {
);

doAnswer(invocation -> {
Callback<SignUpResult> callback = invocation.getArgument(4);
Callback<SignUpResult> callback = invocation.getArgument(5);
callback.onResult(amcResult);
return null;
}).when(mobileClient)
.signUp(any(), any(), any(), any(), Mockito.<Callback<SignUpResult>>any());
.signUp(any(), any(), any(), any(), any(), Mockito.<Callback<SignUpResult>>any());

AWSCognitoAuthSignUpOptions options = AWSCognitoAuthSignUpOptions.builder()
.userAttribute(AuthUserAttributeKey.email(), ATTRIBUTE_VAL)
.validationData(METADATA)
.clientMetadata(METADATA)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use a different value for clientMetadata just to be on the safe side?

.build();

AuthSignUpResult result = synchronousAuth.signUp(
Expand All @@ -220,6 +221,7 @@ public void signUp() throws AuthException {
eq(PASSWORD),
eq(expectedAttributeMap),
eq(METADATA),
eq(METADATA),
Mockito.<Callback<SignUpResult>>any()
);
}
Expand Down