public class CognitoCachingCredentialsProvider extends CognitoCredentialsProvider
SharedPreferences
.
Furthermore, it caches session credentials so as to reduce the number of
network requests. This is the provider to use with a custom identity
provider, which should be an extension of AWSAbstractCognitoIdentityProvider.
This will consume an identity provider, as well. If one is passed in to a
constructor, then that one is the one that is consumed, but if not/a
constructor that doesn't take an identity provider is used, then the Cognito
identity provider is used by default.
Note: if you haven't yet associated your IAM roles with your identity pool, please do so via the Cognito console before using this constructor. You will get an InvalidIdentityPoolConfigurationException if you use it and have not.
// initiate a credentials provider CognitoCachingCredentialsProvider provider = new CognitoCachingCredentialsProvider( context, "identityPoolId", Regions.US_EAST_1); // use the provider to instantiate an AWS client AmazonSNS snsClient = new AmazonSNSClient(provider); // If the user is authenticated through login with Amazon, you can set the map // of token to the provider Map<String, String> logins = new HashMap<String, String>(); logins.put(""www.amazon.com", "login with Amazon token"); provider.setLogins(logins); // Note: Please reuse the provider when possible. //The existing constructor will work without doing so, but will not use the enhanced flow: CognitoCachingCredentialsProvider provider = new CognitoCachingCredentialsProvider( context, "awsAccountId", "identityPoolId", "unauthRoleArn", "authRoleArn", Regions.US_EAST_1);
DEFAULT_DURATION_SECONDS, DEFAULT_THRESHOLD_SECONDS
Constructor and Description |
---|
CognitoCachingCredentialsProvider(android.content.Context context,
AWSCognitoIdentityProvider provider,
Regions region)
Constructs a new
CognitoCachingCredentialsProvider , which will
set up a link to the provider passed in using the enhanced authentication
flow to get short-lived credentials from Amazon Cognito, which can be
retrieved from getCredentials() |
CognitoCachingCredentialsProvider(android.content.Context context,
AWSCognitoIdentityProvider provider,
Regions region,
ClientConfiguration clientConfiguration)
Constructs a new
CognitoCachingCredentialsProvider , which will
set up a link to the provider passed in using the enhanced authentication
flow to get short-lived credentials from Amazon Cognito, which can be
retrieved from getCredentials() |
CognitoCachingCredentialsProvider(android.content.Context context,
AWSCognitoIdentityProvider provider,
java.lang.String unauthArn,
java.lang.String authArn)
Constructs a new
CognitoCachingCredentialsProvider , which will
set up a link to the provider passed in using the basic authentication
flow to get get short-lived credentials from STS, which can be retrieved
from getCredentials() |
CognitoCachingCredentialsProvider(android.content.Context context,
AWSCognitoIdentityProvider provider,
java.lang.String unauthArn,
java.lang.String authArn,
com.amazonaws.services.securitytoken.AWSSecurityTokenService stsClient)
Constructs a new
CognitoCachingCredentialsProvider , which will
set up a link to the provider passed in to use the basic authentication
flow to get short-lived credentials from STS, which can be retrieved from
getCredentials() |
CognitoCachingCredentialsProvider(android.content.Context context,
AWSConfiguration awsConfiguration)
Constructs a new
CognitoCachingCredentialsProvider , which will
use the specified Amazon Cognito identity pool to make a request to
Cognito, using the enhanced flow, to get short lived session credentials,
which will then be returned by this class's getCredentials()
method. |
CognitoCachingCredentialsProvider(android.content.Context context,
java.lang.String identityPoolId,
Regions region)
Constructs a new
CognitoCachingCredentialsProvider , which will
use the specified Amazon Cognito identity pool to make a request to
Cognito, using the enhanced flow, to get short lived session credentials,
which will then be returned by this class's getCredentials()
method. |
CognitoCachingCredentialsProvider(android.content.Context context,
java.lang.String identityPoolId,
Regions region,
ClientConfiguration clientConfiguration)
Constructs a new
CognitoCachingCredentialsProvider , which will
use the specified Amazon Cognito identity pool to make a request to
Cognito, using the enhanced flow, to get short lived session credentials,
which will then be returned by this class's getCredentials()
method. |
CognitoCachingCredentialsProvider(android.content.Context context,
java.lang.String accountId,
java.lang.String identityPoolId,
java.lang.String unauthArn,
java.lang.String authArn,
com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClient cibClient,
com.amazonaws.services.securitytoken.AWSSecurityTokenService stsClient)
Constructs a new
CognitoCachingCredentialsProvider , which will
use the specified Amazon Cognito identity pool to make a request to the
AWS Security Token Service (STS) to get short-lived session credentials,
which will then be returned by this class's getCredentials()
method. |
CognitoCachingCredentialsProvider(android.content.Context context,
java.lang.String accountId,
java.lang.String identityPoolId,
java.lang.String unauthRoleArn,
java.lang.String authRoleArn,
Regions region)
Constructs a new
CognitoCachingCredentialsProvider , which will
use the specified Amazon Cognito identity pool to make a request, using
the basic authentication flow, to the AWS Security Token Service (STS) to
request short-lived session credentials, which will then be returned by
this class's getCredentials() method. |
CognitoCachingCredentialsProvider(android.content.Context context,
java.lang.String accountId,
java.lang.String identityPoolId,
java.lang.String unauthRoleArn,
java.lang.String authRoleArn,
Regions region,
ClientConfiguration clientConfiguration)
Constructs a new
CognitoCachingCredentialsProvider , which will
use the specified Amazon Cognito identity pool to make a request, using
the basic authentication flow, to the AWS Security Token Service (STS) to
request short-lived session credentials, which will then be returned by
this class's getCredentials() method. |
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clear all in-memory and saved state for the credentials provider.
|
void |
clearCredentials()
Clear credentials.
|
java.lang.String |
getCachedIdentityId()
Gets the cached identity id without making a network request.
|
AWSSessionCredentials |
getCredentials()
If the current session has expired/credentials are invalid, a new session
is started, establishing the credentials.
|
java.lang.String |
getIdentityId()
Gets the Cognito identity id of the user.
|
void |
refresh()
Forces this credentials provider to refresh its credentials.
|
void |
setLogins(java.util.Map<java.lang.String,java.lang.String> logins)
Set the logins map used to authenticated with Amazon Cognito.
|
void |
setPersistenceEnabled(boolean isPersistenceEnabled)
Set the flag that determines if the credentials and identityId
will be stored in memory or SharedPreferences.
|
void |
setUserAgentOverride(java.lang.String userAgentOverride)
The user agent string is sometimes combined with other strings.
|
getCustomRoleArn, getIdentityPoolId, getIdentityProvider, getLogins, getRefreshThreshold, getSessionCredentialsExpiration, getSessionCredentitalsExpiration, getSessionDuration, getToken, registerIdentityChangedListener, setCustomRoleArn, setRefreshThreshold, setSessionCredentialsExpiration, setSessionDuration, unregisterIdentityChangedListener, withLogins, withRefreshThreshold, withSessionDuration
public CognitoCachingCredentialsProvider(android.content.Context context, java.lang.String accountId, java.lang.String identityPoolId, java.lang.String unauthRoleArn, java.lang.String authRoleArn, Regions region)
CognitoCachingCredentialsProvider
, which will
use the specified Amazon Cognito identity pool to make a request, using
the basic authentication flow, to the AWS Security Token Service (STS) to
request short-lived session credentials, which will then be returned by
this class's getCredentials()
method.context
- The Android context to be used for the cachingaccountId
- The AWS accountId for the account with Amazon CognitoidentityPoolId
- The Amazon Cogntio identity pool to useunauthRoleArn
- The ARN of the IAM Role that will be assumed when
unauthenticatedauthRoleArn
- The ARN of the IAM Role that will be assumed when
authenticatedregion
- The region to use when contacting Cognito Identitypublic CognitoCachingCredentialsProvider(android.content.Context context, java.lang.String accountId, java.lang.String identityPoolId, java.lang.String unauthRoleArn, java.lang.String authRoleArn, Regions region, ClientConfiguration clientConfiguration)
CognitoCachingCredentialsProvider
, which will
use the specified Amazon Cognito identity pool to make a request, using
the basic authentication flow, to the AWS Security Token Service (STS) to
request short-lived session credentials, which will then be returned by
this class's getCredentials()
method.
This version of the constructor allows you to specify a client configuration for the Amazon Cognito and STS clients.
context
- The Android context to be used for the cachingaccountId
- The AWS accountId for the account with Amazon CognitoidentityPoolId
- The Amazon Cognito identity pool to useunauthRoleArn
- The ARN of the IAM Role that will be assumed when
unauthenticatedauthRoleArn
- The ARN of the IAM Role that will be assumed when
authenticatedregion
- The region to use when contacting Cognito IdentityclientConfiguration
- Configuration to apply to service clients
createdpublic CognitoCachingCredentialsProvider(android.content.Context context, java.lang.String identityPoolId, Regions region)
CognitoCachingCredentialsProvider
, which will
use the specified Amazon Cognito identity pool to make a request to
Cognito, using the enhanced flow, to get short lived session credentials,
which will then be returned by this class's getCredentials()
method.
Note: if you haven't yet associated your IAM roles with your identity pool, please do so via the Cognito console before using this constructor. You will get an InvalidIdentityPoolConfigurationException if you use it and have not. The existing constructor (mirroring this one but with roles and an account id) will work without doing so, but will not use the enhanced flow.
context
- The Android context to be used for the cachingidentityPoolId
- The Amazon Cognito identity pool to useregion
- The region to use when contacting Cognito Identitypublic CognitoCachingCredentialsProvider(android.content.Context context, AWSConfiguration awsConfiguration)
CognitoCachingCredentialsProvider
, which will
use the specified Amazon Cognito identity pool to make a request to
Cognito, using the enhanced flow, to get short lived session credentials,
which will then be returned by this class's getCredentials()
method.
Note: if you haven't yet associated your IAM roles with your identity pool, please do so via the Cognito console before using this constructor. You will get an InvalidIdentityPoolConfigurationException if you use it and have not. The existing constructor (mirroring this one but with roles and an account id) will work without doing so, but will not use the enhanced flow.
Example json file: { "CredentialsProvider": { "CognitoIdentity": { "Default": { "PoolId": "us-east-1:example-pool-id1234", "Region": "us-east-1" } } } }context
- The Android context to be used for the cachingawsConfiguration
- The configuration holding you identity pool id
and the region to use when contacting
Cognito Identitypublic CognitoCachingCredentialsProvider(android.content.Context context, java.lang.String identityPoolId, Regions region, ClientConfiguration clientConfiguration)
CognitoCachingCredentialsProvider
, which will
use the specified Amazon Cognito identity pool to make a request to
Cognito, using the enhanced flow, to get short lived session credentials,
which will then be returned by this class's getCredentials()
method.
This version of the constructor allows you to specify a client configuration for the Amazon Cognito client.
Note: if you haven't yet associated your IAM roles with your identity pool, please do so via the Cognito console before using this constructor. You will get an InvalidIdentityPoolConfigurationException if you use it and have not. The existing constructor (mirroring this one but with roles and an account id) will work without doing so, but will not use the enhanced flow.
context
- The Android context to be used for the cachingidentityPoolId
- The Amazon Cognito identity pool to useregion
- The region to use when contacting Cognito IdentityclientConfiguration
- Configuration to apply to service clients
createdpublic CognitoCachingCredentialsProvider(android.content.Context context, java.lang.String accountId, java.lang.String identityPoolId, java.lang.String unauthArn, java.lang.String authArn, com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClient cibClient, com.amazonaws.services.securitytoken.AWSSecurityTokenService stsClient)
CognitoCachingCredentialsProvider
, which will
use the specified Amazon Cognito identity pool to make a request to the
AWS Security Token Service (STS) to get short-lived session credentials,
which will then be returned by this class's getCredentials()
method.
This version of the constructor allows you to specify the Amazon Cognito and STS client to use.
Set the roles and stsClient to null to use the enhanced authentication flow, not contacting STS. Otherwise the basic flow will be used.
context
- The Android context to be used for the cachingaccountId
- The AWS accountId for the account with Amazon CognitoidentityPoolId
- The Amazon Cogntio identity pool to useunauthArn
- The ARN of the IAM Role that will be assumed when
unauthenticatedauthArn
- The ARN of the IAM Role that will be assumed when
authenticatedcibClient
- Preconfigured CognitoIdentity client to make requests
withstsClient
- Preconfigured STS client to make requests withpublic CognitoCachingCredentialsProvider(android.content.Context context, AWSCognitoIdentityProvider provider, java.lang.String unauthArn, java.lang.String authArn)
CognitoCachingCredentialsProvider
, which will
set up a link to the provider passed in using the basic authentication
flow to get get short-lived credentials from STS, which can be retrieved
from getCredentials()
This version of the constructor allows you to specify your own Identity Provider class.
context
- The Android context to be used for the cachingprovider
- a reference to the provider in question, including what's
needed to interact with it to later connect with STSunauthArn
- the unauthArn, for use with the STS callauthArn
- the authArn, for use with the STS callpublic CognitoCachingCredentialsProvider(android.content.Context context, AWSCognitoIdentityProvider provider, java.lang.String unauthArn, java.lang.String authArn, com.amazonaws.services.securitytoken.AWSSecurityTokenService stsClient)
CognitoCachingCredentialsProvider
, which will
set up a link to the provider passed in to use the basic authentication
flow to get short-lived credentials from STS, which can be retrieved from
getCredentials()
This version of the constructor allows you to specify your own Identity Provider class, and the STS client to use.
context
- The Android context to be used for the cachingprovider
- a reference to the provider in question, including what's
needed to interact with it to later connect with STSunauthArn
- the unauthArn, for use with the STS callauthArn
- the authArn, for use with the STS callstsClient
- the sts endpoint to get session credentials frompublic CognitoCachingCredentialsProvider(android.content.Context context, AWSCognitoIdentityProvider provider, Regions region)
CognitoCachingCredentialsProvider
, which will
set up a link to the provider passed in using the enhanced authentication
flow to get short-lived credentials from Amazon Cognito, which can be
retrieved from getCredentials()
This version of the constructor allows you to specify your own Identity Provider class.
Note: if you haven't yet associated your IAM roles with your identity pool, please do so via the Cognito console before using this constructor. You will get an InvalidIdentityPoolConfigurationException if you use it and have not. The existing constructor (mirroring this one but with roles) will work without doing so, but will not use the enhanced flow.
context
- The Android context to be used for the cachingprovider
- a reference to the provider in question, including what's
needed to interact with it to later connect with Amazon
Cognitoregion
- The region to use when contacting Cognitopublic CognitoCachingCredentialsProvider(android.content.Context context, AWSCognitoIdentityProvider provider, Regions region, ClientConfiguration clientConfiguration)
CognitoCachingCredentialsProvider
, which will
set up a link to the provider passed in using the enhanced authentication
flow to get short-lived credentials from Amazon Cognito, which can be
retrieved from getCredentials()
This version of the constructor allows you to specify your own Identity Provider class and the configuration for the Amazon Cognito client.
Note: if you haven't yet associated your IAM roles with your identity pool, please do so via the Cognito console before using this constructor. You will get an InvalidIdentityPoolConfigurationException if you use it and have not. The existing constructor (mirroring this one but with roles) will work without doing so, but will not use the enhanced flow.
context
- The Android context to be used for the cachingprovider
- a reference to the provider in question, including what's
needed to interact with it to later connect with Amazon
CognitoclientConfiguration
- Configuration to apply to service clients
createdregion
- The region to use when contacting Cognito Identitypublic java.lang.String getIdentityId()
SharedPreferences
. Please don't call it
in the main thread.getIdentityId
in class CognitoCredentialsProvider
public AWSSessionCredentials getCredentials()
CognitoCredentialsProvider
getCredentials
in interface AWSCredentialsProvider
getCredentials
in class CognitoCredentialsProvider
public void refresh()
AWSCredentialsProvider
refresh
in interface AWSCredentialsProvider
refresh
in class CognitoCredentialsProvider
public void setLogins(java.util.Map<java.lang.String,java.lang.String> logins)
CognitoCredentialsProvider
setLogins
in class CognitoCredentialsProvider
logins
- The new logins map (providerName, providerToken) to use to
communicate with Amazon Cognitopublic void clear()
CognitoCredentialsProvider
clear
in class CognitoCredentialsProvider
public void clearCredentials()
CognitoCredentialsProvider
clearCredentials
in class CognitoCredentialsProvider
public java.lang.String getCachedIdentityId()
public void setPersistenceEnabled(boolean isPersistenceEnabled)
isPersistenceEnabled
- flag that indicates if the
credentials need to be persisted or notpublic void setUserAgentOverride(java.lang.String userAgentOverride)
userAgentOverride
- The string to use as the full user agent when sending requests.Copyright © 2018 Amazon Web Services, Inc. All Rights Reserved.