/aws-android-sdk-cognitoidentityprovider/src/main/java/com/amazonaws/mobileconnectors/cognitoidentityprovider/continuations/AuthenticationDetails.java

https://gitlab.com/github-cloud-corp/aws-sdk-android · Java · 185 lines · 89 code · 16 blank · 80 comment · 15 complexity · 4269080fa096313722025d6cc9415386 MD5 · raw file

  1. /*
  2. * Copyright 2013-2016 Amazon.com,
  3. * Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Licensed under the Amazon Software License (the "License").
  6. * You may not use this file except in compliance with the
  7. * License. A copy of the License is located at
  8. *
  9. * http://aws.amazon.com/asl/
  10. *
  11. * or in the "license" file accompanying this file. This file is
  12. * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, express or implied. See the License
  14. * for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations;
  18. import com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoParameterInvalidException;
  19. import com.amazonaws.services.cognitoidentityprovider.model.AttributeType;
  20. import java.util.ArrayList;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. /**
  25. * Encapsulates all data required to authenticate a user.
  26. * Pass an object of this type to the continuation object to continue with the authentication process.
  27. * This contents of this object are set when it is constructed and are immutable afterwards.
  28. */
  29. public class AuthenticationDetails {
  30. public final static String PASSWORD_AUTHENTICATION = "PASSWORD_VERIFIER";
  31. public final static String CUSTOM_AUTHENTICATION = "CUSTOM_CHALLENGE";
  32. private String authenticationType;
  33. private String userId;
  34. private String password;
  35. private List<AttributeType> validationData;
  36. private Map<String, String> authenticationParameters;
  37. /**
  38. * Constructs a new object with authentication details.
  39. *
  40. * @param userId REQUIRED: User ID, NOTE: This will over ride the current Used ID.
  41. * @param password REQUIRED: Users' password.
  42. * @param validationData REQUIRED: Validation data parameters for the pre-auth lambda.
  43. */
  44. public AuthenticationDetails(String userId, String password, Map<String, String> validationData){
  45. this.authenticationType = PASSWORD_AUTHENTICATION;
  46. this.userId = userId;
  47. this.password = password;
  48. setValidationData(validationData);
  49. }
  50. /**
  51. * Constructs a new object for custom authentication.
  52. *
  53. * @param userId REQUIRED: User ID, NOTE: This will over ride the current Used ID.
  54. * @param authenticationParameters REQUIRED: Authentication details to launch custom authentication process.
  55. */
  56. public AuthenticationDetails(String userId, Map<String, String> authenticationParameters, Map<String, String> validationData) {
  57. this.userId = userId;
  58. if (authenticationParameters != null) {
  59. this.authenticationType = CUSTOM_AUTHENTICATION;
  60. this.authenticationParameters = authenticationParameters;
  61. setValidationData(validationData);
  62. } else {
  63. this.authenticationType = null;
  64. }
  65. }
  66. public void setAuthenticationType(String authenticationType) {
  67. this.authenticationType = authenticationType;
  68. if (this.authenticationType.equals(PASSWORD_AUTHENTICATION)) {
  69. this.authenticationParameters = null;
  70. } else if (this.authenticationType.equals(CUSTOM_AUTHENTICATION)) {
  71. this.password = null;
  72. }
  73. }
  74. /**
  75. * This method returns the password.
  76. *
  77. * @return password set.
  78. */
  79. public String getPassword() {
  80. return password;
  81. }
  82. /**
  83. * This method returns the User Id.
  84. *
  85. * @return userId set in this object.
  86. */
  87. public String getUserId()
  88. {
  89. return userId;
  90. }
  91. /**
  92. * This method returns the validation data.
  93. *
  94. * @return validation data set in this object.
  95. */
  96. public List<AttributeType> getValidationData() {
  97. return validationData;
  98. }
  99. /**
  100. * This method returns the authentication type.
  101. *
  102. * @return the authentication type set for this object.
  103. */
  104. public String getAuthenticationType() {
  105. return authenticationType;
  106. }
  107. /**
  108. * The authentication parameters set for custom authentication process.
  109. *
  110. * @return Authentication details as a Map.
  111. */
  112. public Map<String, String> getAuthenticationParameters() {
  113. return authenticationParameters;
  114. }
  115. /**
  116. * Set the name of the custom challenge. This will override the existing authentication name.
  117. *
  118. * @param customChallenge REQUIRED: Custom challenge name.
  119. */
  120. public void setCustomChallenge(String customChallenge) {
  121. if (this.authenticationType.equals(PASSWORD_AUTHENTICATION)) {
  122. throw new CognitoParameterInvalidException(String.format("Cannot set custom challenge when the authentication type is %s.", PASSWORD_AUTHENTICATION));
  123. }
  124. this.authenticationType = CUSTOM_AUTHENTICATION;
  125. setAuthenticationParameter("CHALLENGE_NAME", customChallenge);
  126. }
  127. /**
  128. * Set the name of the authentication challenge.
  129. *
  130. * @param validationData
  131. */
  132. private void setValidationData(Map<String, String> validationData) {
  133. if (validationData != null){
  134. this.validationData = new ArrayList<AttributeType>();
  135. for (Map.Entry<String, String> data : validationData.entrySet()) {
  136. AttributeType validation = new AttributeType();
  137. validation.setName(data.getKey());
  138. validation.setValue(data.getValue());
  139. this.validationData.add(validation);
  140. }
  141. } else{
  142. this.validationData = null;
  143. }
  144. }
  145. /**
  146. * Sets new authentication details, will override the current values.
  147. *
  148. * @param authenticationParameters REQUIRED: Authentication details as a Map.
  149. */
  150. public void setAuthenticationParameters(Map<String, String> authenticationParameters) {
  151. this.authenticationParameters = authenticationParameters;
  152. }
  153. /**
  154. * Set an authentication detail, will override the current value.
  155. *
  156. * @param key REQUIRED: Authentication detail key.
  157. * @param value REQUIRED: Authentication detail value.
  158. */
  159. public void setAuthenticationParameter(String key, String value) {
  160. if (key != null) {
  161. if (this.authenticationParameters == null) {
  162. this.authenticationParameters = new HashMap<String, String>();
  163. }
  164. authenticationParameters.put(key, value);
  165. } else {
  166. throw new CognitoParameterInvalidException("A null key was used to add a new authentications parameter.");
  167. }
  168. }
  169. }