using Amazon.CognitoIdentity; using Amazon.CognitoSync.SyncManager.Internal; using System; namespace Amazon.CognitoSync.SyncManager { /// /// The SQLiteCognitoAWSCredentials extends from /// and adds support for caching of identity id using SQLite /// public class SQLiteCognitoAWSCredentials : CognitoAWSCredentials { /// /// Constructs a new SQLiteCognitoAWSCredentials instance, which will use the /// specified Amazon Cognito identity pool to get short lived session credentials. /// /// The Amazon Cogntio identity pool to use /// Region to use when accessing Amazon Cognito and AWS Security Token Service. public SQLiteCognitoAWSCredentials(string identityPoolId, RegionEndpoint region) : base(identityPoolId, region) { } /// /// Constructs a new SQLiteCognitoAWSCredentials instance, which will use the /// specified Amazon Cognito identity pool to make a requests to the /// AWS Security Token Service (STS) to request short lived session credentials. /// /// The AWS accountId for the account with Amazon Cognito /// The Amazon Cogntio identity pool to use /// The ARN of the IAM Role that will be assumed when unauthenticated /// The ARN of the IAM Role that will be assumed when authenticated /// Region to use when accessing Amazon Cognito and AWS Security Token Service. public SQLiteCognitoAWSCredentials(string accountId, string identityPoolId, string unAuthRoleArn, string authRoleArn, RegionEndpoint region) : base(accountId, identityPoolId, unAuthRoleArn, authRoleArn, region) { } private const String IDENTITY_ID_CACHE_KEY = "CognitoIdentity:IdentityId"; /// /// Caches the identity id retrieved from Cognito. /// /// The Cognito identity id to cache [System.Security.SecuritySafeCritical] public override void CacheIdentityId(string identityId) { base.CacheIdentityId(identityId); using (var kvStore = new SQLiteLocalStorage()) { kvStore.CacheIdentity(GetNamespacedKey(IDENTITY_ID_CACHE_KEY), identityId); } } /// /// Clears the currently identity id from the cache. /// [System.Security.SecuritySafeCritical] public override void ClearIdentityCache() { base.ClearIdentityCache(); using (var kvStore = new SQLiteLocalStorage()) { kvStore.DeleteCachedIdentity(GetNamespacedKey(IDENTITY_ID_CACHE_KEY)); } } /// /// Gets the previously cached the identity id retrieved from Cognito. /// /// The previously cached identity id [System.Security.SecuritySafeCritical] public override string GetCachedIdentityId() { string identityId = null; using (var kvStore = new SQLiteLocalStorage()) { identityId = kvStore.GetIdentity(GetNamespacedKey(IDENTITY_ID_CACHE_KEY)); } return identityId; } } }