public class CognitoUser extends Object
This class encapsulates all operations possible on a user and all tokens
belonging to the user. The user tokens, as CognitoUserSession
, are
stored in SharedPreferences. Only the tokens belonging to the last
successfully authenticated user are stored.
Modifier | Constructor and Description |
---|---|
protected |
CognitoUser(CognitoUserPool pool,
String userId,
String clientId,
String clientSecret,
String secretHash,
AmazonCognitoIdentityProvider client,
android.content.Context context)
Constructs a new Cognito User from a Cognito user identity pool
CognitoUserPool and userId. |
Modifier and Type | Method and Description |
---|---|
void |
changePassword(String oldUserPassword,
String newUserPassword,
GenericHandler callback)
Request to change password for this user, in current thread.
|
void |
changePasswordInBackground(String oldUserPassword,
String newUserPassword,
GenericHandler callback)
Request to change password for this user, in background.
|
void |
confirmPassword(String verificationCode,
String newPassword,
ForgotPasswordHandler callback)
Sends the new password and the verification code to Cognito Identity
Provider service, in background.
|
void |
confirmPasswordInBackground(String verificationCode,
String newPassword,
ForgotPasswordHandler callback)
Set new password and send verification code to Cognito Identity Provider
service, in background.
|
void |
confirmSignUp(String confirmationCode,
boolean forcedAliasCreation,
GenericHandler callback)
Confirms user registration in current thread.
|
void |
confirmSignUpInBackground(String confirmationCode,
boolean forcedAliasCreation,
GenericHandler callback)
Confirms user registration in background.
|
void |
deleteAttributes(List<String> attributeNamesToDelete,
GenericHandler callback)
Deletes user attributes, in current thread.
|
void |
deleteAttributesInBackground(List<String> attributeNamesToDelete,
GenericHandler callback)
Deletes user attributes, in background.
|
void |
deleteUser(GenericHandler callback)
Deletes this user, in current thread.
|
void |
deleteUserInBackground(GenericHandler callback)
Deletes this user, in background.
|
RespondToAuthChallengeRequest |
deviceSrpAuthRequest(RespondToAuthChallengeResult challenge,
String deviceSecret,
String deviceGroupKey,
com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUser.AuthenticationHelper authenticationHelper)
Creates request for device SRP verification.
|
void |
forgotPassword(ForgotPasswordHandler callback)
Starts the process to set a new new password for forgotten password case,
in current thread.
|
void |
forgotPasswordInBackground(ForgotPasswordHandler callback)
Starts the process to set a new password for forgotten password case, in
background.
|
void |
getAttributeVerificationCode(String attributeName,
VerificationHandler callback)
Requests code to verify a user attribute, in current thread.
|
void |
getAttributeVerificationCodeInBackground(String attributeName,
VerificationHandler callback)
Requests code to verify a user attribute, in background.
|
protected CognitoUserSession |
getCachedSession()
Call this method for valid, cached tokens for this user.
|
protected AmazonCognitoIdentityProvider |
getCognitoIdentityProviderClient()
Method low-level client for Amazon Cognito Identity Provider.
|
void |
getDetails(GetDetailsHandler callback)
Retrieves the current user attributes.
|
void |
getDetailsInBackground(GetDetailsHandler callback)
Retrieves the current user attributes.
|
void |
getSession(AuthenticationHandler callback)
Returns a valid tokens for a user through the callback method.
|
void |
getSessionInBackground(AuthenticationHandler callback)
Returns a valid tokens for a user through the callback method.
|
String |
getUserId()
Returns the userId of this user.
|
String |
getUserPoolId()
Returns the pool Id of this user.
|
void |
globalSignOut(GenericHandler callback)
Sign-out from all devices associated with this user, in current thread.
|
void |
globalSignOutInBackground(GenericHandler callback)
Sign-out from all devices associated with this user, in background.
|
Runnable |
initiateUserAuthentication(AuthenticationDetails authenticationDetails,
AuthenticationHandler callback,
boolean runInBackground)
Initiates user authentication through the generic auth flow (also called
as Enhanced or Custom authentication).
|
void |
listDevices(int limit,
String paginationToken,
DevicesHandler callback)
Fetches the list of all remembered devices for this user, runs in current
thread.
|
void |
listDevicesInBackground(int limit,
String paginationToken,
DevicesHandler callback)
Fetches the list of all remembered devices for this user.
|
void |
resendConfirmationCode(VerificationHandler callback)
Request to resend registration confirmation code for a user, in current
thread.
|
void |
resendConfirmationCodeInBackground(VerificationHandler callback)
Request to resend registration confirmation code for a user, in
background.
|
Runnable |
respondToChallenge(RespondToAuthChallengeRequest challengeResponse,
AuthenticationHandler callback,
boolean runInBackground)
This method sends the challenge response to the Cognito IDP service.
|
Runnable |
respondToMfaChallenge(String mfaCode,
RespondToAuthChallengeResult challenge,
AuthenticationHandler callback,
boolean runInBackground)
Responds to an MFA challenge.
|
void |
setUserSettings(CognitoUserSettings cognitoUserSettings,
GenericHandler callback)
Set's user settings, in current thread.
|
void |
setUserSettingsInBackground(CognitoUserSettings cognitoUserSettings,
GenericHandler callback)
Set's user settings, in background.
|
void |
signOut()
Sign-Out this user by removing all cached tokens.
|
CognitoDevice |
thisDevice()
Returns the current device, if users in this pool can remember devices.
|
void |
updateAttributes(CognitoUserAttributes attributes,
UpdateAttributesHandler callback)
Updates attributes for a user.
|
void |
updateAttributesInBackground(CognitoUserAttributes attributes,
UpdateAttributesHandler callback)
Updates attributes for a user.
|
void |
verifyAttribute(String attributeName,
String verificationCode,
GenericHandler callback)
Verify an attribute with the verification code, in current thread.
|
void |
verifyAttributeInBackground(String attributeName,
String verificationCode,
GenericHandler callback)
Verify an attribute with the verification code, in background.
|
protected CognitoUser(CognitoUserPool pool, String userId, String clientId, String clientSecret, String secretHash, AmazonCognitoIdentityProvider client, android.content.Context context)
CognitoUserPool
and userId.pool
- REQUIRED: Reference to CognitoUserPool
, to which this
user belongs.userId
- REQUIRED: userId of this user.clientId
- REQUIRED: Client-Id of the android app.clientSecret
- REQUIRED: Client secret assigned for this Client-Id.secretHash
- REQUIRED: Secret-Hash, calculated for this android app.client
- REQUIRED: Low level android client.context
- REQUIRED: Application context.public String getUserId()
public String getUserPoolId()
protected AmazonCognitoIdentityProvider getCognitoIdentityProviderClient()
public void confirmSignUpInBackground(String confirmationCode, boolean forcedAliasCreation, GenericHandler callback)
Confirming a user is required to complete the user's registration. Any other operations on a user. are possible only after registration confirmation.
confirmationCode
- REQUIRED: Code sent to the phone-number or email
used to register the user.forcedAliasCreation
- REQUIRED: This flag indicates if the
confirmation should go-through in case of parameter
contentions.callback
- REQUIRED: This is a reference to GenericHandler
callback handler.public void confirmSignUp(String confirmationCode, boolean forcedAliasCreation, GenericHandler callback)
Confirming a user is required to complete the user's registration. Any other operations on a user are possible only after registration confirmation. Note: This method will perform network operations. Calling this method in applications' main thread will cause Android to throw NetworkOnMainThreadException.
confirmationCode
- REQUIRED: Code sent to the phone-number or email
used to register the userforcedAliasCreation
- REQUIRED: This flag indicates if the
confirmation should go-through in case of parameter
contentions.callback
- REQUIRED: This is a reference to GenericHandler
callback handlerpublic void resendConfirmationCodeInBackground(VerificationHandler callback)
callback
- REQUIRED: VerificationHandler
callback handler.public void resendConfirmationCode(VerificationHandler callback)
Note: This method will perform network operations. Calling this method in applications' main thread will cause Android to throw NetworkOnMainThreadException.
callback
- REQUIRED: VerificationHandler
callback handler.public void forgotPasswordInBackground(ForgotPasswordHandler callback)
This will initiate the process to set a new password when the current
password is forgotten. The new password will be successfully set only
after the verification code, sent to the registered email or phone number
of the user, successfully verified by Cognito Identity Provider service.
This method will pass a continuation object to the callback. Use setters
in the Continuation object ForgotPasswordContinuation
to set the
new password and verification code and call continue on the continuation
object, CognitoIdentityProviderContinuation.continueTask()
.
callback
- REQUIRED: ForgotPasswordHandler
callbackpublic void forgotPassword(ForgotPasswordHandler callback)
This will initiate the process to set a new password when the current
password is forgotten. The new password will be successfully set only
after the verification code, sent to the registered email or phone number
of the user, successfully verified by Cognito Identity Provider service.
This method will pass a continuation object to the callback. Use setters
in the Continuation object ForgotPasswordContinuation
to set the
new password and verification code and call continue on the continuation
object, CognitoIdentityProviderContinuation.continueTask()
.
Note: This method will perform network operations. Calling this
method in applications' main thread will cause Android to throw
NetworkOnMainThreadException.
callback
- REQUIRED: ForgotPasswordHandler
callbackpublic void confirmPasswordInBackground(String verificationCode, String newPassword, ForgotPasswordHandler callback)
This method will be called by ForgotPasswordContinuation
continuation object.
verificationCode
- REQUIRED: Code sent from Cognito Identity
Provider Service.newPassword
- REQUIRED: New password. On successful verification of
verificationCode
, this will be the new password for
this user.callback
- REQUIRED: ForgotPasswordHandler
callback.public void confirmPassword(String verificationCode, String newPassword, ForgotPasswordHandler callback)
This method will be called by ForgotPasswordContinuation
continuation object. Note: This method will perform network
operations. Calling this method in applications' main thread will cause
Android to throw NetworkOnMainThreadException.
verificationCode
- REQUIRED: Code sent from Cognito Identity
Provider Service.newPassword
- REQUIRED: New password. On successful verification of
verificationCode
, this will be the new password for
this user.callback
- REQUIRED: ForgotPasswordHandler
callback.public void getSessionInBackground(AuthenticationHandler callback)
AuthenticationHandler.onSuccess(CognitoUserSession, CognitoDevice)
.
Tokens are passed as instance of CognitoUserSession
. Call this
method to get valid tokens for a user. This method returns any valid
cached tokens for the user. If no valid cached tokens are available this
method initiates the process to authenticate the user and get tokens from
Cognito Identity Provider service. Implement the interface
AuthenticationHandler
and pass it as callback to this method.
This method uses the callback to interact with application at different
stages of the authentication process. Continuation objects are used when
the authentication process requires more data to continue. See
CognitoIdentityProviderContinuation
for details on continuation objects.
callback
- REQUIRED: AuthenticationHandler
callbackpublic void getSession(AuthenticationHandler callback)
AuthenticationHandler.onSuccess(CognitoUserSession, CognitoDevice)
.
Tokens are passed as instance of CognitoUserSession
. Call this
method to get valid tokens for a user. This method returns any valid
cached tokens for the user. If no valid cached tokens are available this
method initiates the process to authenticate the user and get tokens from
Cognito Identity Provider service. Implement the interface
AuthenticationHandler
and pass it as callback to this method.
This method uses the callback to interact with application at different
stages of the authentication process. Continuation objects are used when
the authentication process requires more data to continue. See
CognitoIdentityProviderContinuation
for details on continuation objects. Note: This method will
perform network operations. Calling this method in applications' main
thread will cause Android to throw NetworkOnMainThreadException.
callback
- REQUIRED: AuthenticationHandler
callbackpublic Runnable initiateUserAuthentication(AuthenticationDetails authenticationDetails, AuthenticationHandler callback, boolean runInBackground)
authenticationDetails
- REQUIRED: Contains details about the user
authentication.runInBackground
- flag indicating if the operation has to run in
backgroundcallback
- REQUIRED: AuthenticationHandler
callback.Runnable
for the next step in user authentication.public Runnable respondToMfaChallenge(String mfaCode, RespondToAuthChallengeResult challenge, AuthenticationHandler callback, boolean runInBackground)
mfaCode
- REQUIRED: The MFA code received by the user.challenge
- REQUIRED: Current challenge
RespondToAuthChallengeResult
.runInBackground
- flag indicating if the operation has to run in
background.callback
- REQUIRED: AuthenticationHandler
callback.Runnable
for the next step in user authentication.protected CognitoUserSession getCachedSession()
CognitoUserSession
. null
otherwise.public void changePasswordInBackground(String oldUserPassword, String newUserPassword, GenericHandler callback)
This operation requires a valid accessToken
CognitoUserSession.accessToken
. Un-authenticated users will have
to be authenticated before calling this method.
oldUserPassword
- REQUIRED: Current password of this user.newUserPassword
- REQUIRED: New password for this user.callback
- REQUIRED: GenericHandler
callback handler.public void changePassword(String oldUserPassword, String newUserPassword, GenericHandler callback)
This operation requires a valid accessToken
CognitoUserSession.accessToken
. Unauthenticated users will need
to be authenticated before calling this method. Note: This method
will perform network operations. Calling this method in applications'
main thread will cause Android to throw NetworkOnMainThreadException.
oldUserPassword
- REQUIRED: Current password of this user.newUserPassword
- REQUIRED: New password for this user.callback
- REQUIRED: GenericHandler
callback handler.public void getDetailsInBackground(GetDetailsHandler callback)
All attributes, which are set for this user, are fetched. This method requires valid accessToken.
callback
- REQUIRED: GetDetailsHandler
callbackpublic void getDetails(GetDetailsHandler callback)
All attributes, which are set for this user, are fetched. This method requires valid accessToken. Note: This method will perform network operations. Calling this method in applications' main thread will cause Android to throw NetworkOnMainThreadException.
callback
- REQUIRED: GetDetailsHandler
callbackpublic void getAttributeVerificationCodeInBackground(String attributeName, VerificationHandler callback)
The user attributes that can be verified are those attributes that can be used to communicate with the user, e.g. phone_number and email. The verification code is sent to the medium that is represented by the attribute. Attribute verification is required to enable the attribute to be used an attribute as alias for the user. Aliases attributes can be used in lieu of the userId to authenticate the user. If an attribute was used in the confirm the user after sign-up, then that alias is already verified and does not require re-verification.
attributeName
- REQUIRED: Name of the attribute that requires
verification.callback
- REQUIRED: callback.public void getAttributeVerificationCode(String attributeName, VerificationHandler callback)
The user attributes that can be verified are those attributes that can be used to communicate with the user, e.g. phone_number and email. The verification code is sent to the medium that is represented by the attribute. Attribute verification is required to enable the attribute to be used an attribute as alias for the user. Aliases attributes can be used in lieu of the userId to authenticate the user. If an attribute was used in the confirm the user after sign-up, then that alias is already verified and does not require re-verification. Note: This method will perform network operations. Calling this method in applications' main thread will cause Android to throw NetworkOnMainThreadException.
attributeName
- REQUIRED: Name of the attribute that requires
verification.callback
- REQUIRED: callback.public void verifyAttributeInBackground(String attributeName, String verificationCode, GenericHandler callback)
Call this method to verify an attribute with the "verification code". To
request for a "verification code" call the method
getAttributeVerificationCodeInBackground(String, VerificationHandler)
.
attributeName
- REQUIRED: The attribute that is being verified.verificationCode
- REQUIRED: The code for verification.callback
- REQUIRED: Callbackpublic void verifyAttribute(String attributeName, String verificationCode, GenericHandler callback)
Call this method to verify an attribute with the "verification code". To
request for a "verification code" call the method
getAttributeVerificationCodeInBackground(String, VerificationHandler)
. Note: This method will perform network operations. Calling this
method in applications' main thread will cause Android to throw
NetworkOnMainThreadException.
attributeName
- REQUIRED: The attribute that is being verified.verificationCode
- REQUIRED: The code for verification.callback
- REQUIRED: Callbackpublic void updateAttributesInBackground(CognitoUserAttributes attributes, UpdateAttributesHandler callback)
Requires valid accessToken.
attributes
- REQUIRED: All attributes and values that need to be
updated for this user.callback
- REQUIRED: UpdateAttributesHandler
callback.public void updateAttributes(CognitoUserAttributes attributes, UpdateAttributesHandler callback)
Requires valid accessToken. Note: This method will perform network operations. Calling this method in applications' main thread will cause Android to throw NetworkOnMainThreadException.
attributes
- REQUIRED: All attributes and values that need to be
updated for this user.callback
- REQUIRED: UpdateAttributesHandler
callback.public void deleteAttributesInBackground(List<String> attributeNamesToDelete, GenericHandler callback)
attributeNamesToDelete
- REQUIRED: List of user attributes that have
to be deleted.callback
- REQUIRED: GenericHandler
callbackpublic void deleteAttributes(List<String> attributeNamesToDelete, GenericHandler callback)
Note: This method will perform network operations. Calling this method in applications' main thread will cause Android to throw NetworkOnMainThreadException.
attributeNamesToDelete
- REQUIRED: List of user attributes that have
to be deleted.callback
- REQUIRED: GenericHandler
callbackpublic void signOut()
public void globalSignOutInBackground(GenericHandler callback)
callback
- REQUIRED: GenericHandler
callback.public void globalSignOut(GenericHandler callback)
Note: This method will perform network operations. Calling this method in applications' main thread will cause Android to throw NetworkOnMainThreadException.
callback
- REQUIRED: GenericHandler
callback.public void deleteUserInBackground(GenericHandler callback)
callback
- REQUIRED: @link GenericHandler} callback.public void deleteUser(GenericHandler callback)
Note: This method will perform network operations. Calling this method in applications' main thread will cause Android to throw NetworkOnMainThreadException.
callback
- REQUIRED: @link GenericHandler} callback.public void setUserSettingsInBackground(CognitoUserSettings cognitoUserSettings, GenericHandler callback)
cognitoUserSettings
- REQUIRED: User settings as
CognitoUserSettings
.callback
- REQUIRED: GenericHandler
callback.public void setUserSettings(CognitoUserSettings cognitoUserSettings, GenericHandler callback)
Note: This method will perform network operations. Calling this method in applications' main thread will cause Android to throw NetworkOnMainThreadException.
cognitoUserSettings
- REQUIRED: User settings as
CognitoUserSettings
.callback
- REQUIRED: GenericHandler
callback.public Runnable respondToChallenge(RespondToAuthChallengeRequest challengeResponse, AuthenticationHandler callback, boolean runInBackground)
challengeResponse
- REQUIRED: RespondToAuthChallengeRequest
contains response for the current challenge.callback
- REQUIRED: AuthenticationHandler
callback.runInBackground
- REQUIRED: Boolean to indicate the current
threading.Runnable
for the next step in user authentication.public RespondToAuthChallengeRequest deviceSrpAuthRequest(RespondToAuthChallengeResult challenge, String deviceSecret, String deviceGroupKey, com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUser.AuthenticationHelper authenticationHelper)
challenge
- REQUIRED: RespondToAuthChallengeResult
contains
next challenge.deviceSecret
- REQUIRED: Device secret verifier.authenticationHelper
- REQUIRED: Internal helper class for SRP
calculations.deviceGroupKey
- the device group keyRespondToAuthChallengeRequest
.public void listDevicesInBackground(int limit, String paginationToken, DevicesHandler callback)
limit
- REQUIRED: Maximum number of devices to be returned in this
call, defaults to 10.paginationToken
- REQUIRED: Token to continue an earlier search.callback
- REQUIRED: DevicesHandler
callback.public void listDevices(int limit, String paginationToken, DevicesHandler callback)
limit
- REQUIRED: Maximum number of devices to be returned in this
call, defaults to 10.paginationToken
- REQUIRED: Token to continue an earlier search.callback
- REQUIRED: DevicesHandler
callback.public CognitoDevice thisDevice()
CognitoDevice
if the device is available, null otherwise.Copyright © 2017. All rights reserved.