Skip to content

Instantly share code, notes, and snippets.

@bveenvliet
Created August 7, 2019 18:52
Show Gist options
  • Save bveenvliet/e421c0173c634f465f1c33d1740115c9 to your computer and use it in GitHub Desktop.
Save bveenvliet/e421c0173c634f465f1c33d1740115c9 to your computer and use it in GitHub Desktop.
CognitoUserAttributes with mfaOptions
package com.amazonaws.mobileconnectors.cognitoidentityprovider
import com.amazonaws.services.cognitoidentityprovider.model.AttributeType
import java.util.*
class CognitoUserAttributes
/**
* Constructor for internal use.
*
* @param userAttributes REQUIRED: Cognito user attributes as a list.
*/
protected constructor(userAttributes: List<AttributeType>?) {
/**
* All attributes set for a user
*/
private val userAttributes: MutableMap<String, String>?
// Multi-factor authentication options set for a user
private var mfaOptions: Map<String, String>? = null
/**
* Returns the user attributes as a key, value pairs.
*
* @return User attributes as key, value pairs
*/
val attributes: Map<String, String>?
get() = userAttributes
/**
* Returns the user attributes as a [AttributeType] list.
*
* @return [AttributeType] Cognito user attributes as a list.
*/
protected val attributesList: List<AttributeType>
get() {
val attributesList = ArrayList<AttributeType>()
if (userAttributes != null) {
for ((key, value) in userAttributes) {
val attribute = AttributeType()
attribute.name = key
attribute.value = value
attributesList.add(attribute)
}
}
return attributesList
}
/**
* Public constructor, creates an "empty container".
* Use [CognitoUserAttributes.addAttribute] method to add user attributes.
*/
constructor() : this(null) {}
init {
this.userAttributes = HashMap()
if (userAttributes != null) {
for (attribute in userAttributes) {
this.userAttributes[attribute.name] = attribute.value
}
}
}
/**
* Adds an attribute to this object.
*
*
* Will add the attribute and its value. Overrides an earlier value set for an attribute
* which was already added to this object.
*
*
* @param attributeName REQUIRED: The attribute name.
* @param value REQUIRED: Value for the attribute.
*/
fun addAttribute(attributeName: String, value: String) {
userAttributes!![attributeName] = value
}
/**
* Add the SMS MFA attributes to mfaOptions.
*/
fun enableMfa() {
mfaOptions = HashMap()
(mfaOptions as HashMap<String, String>)["phone_number"] = "SMS"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment