using System; using System.Collections.Generic; using System.Linq; using System.Text; using Amazon.Runtime; using Amazon.SecurityToken; using Amazon.SecurityToken.Model; using Amazon.CognitoIdentity.Model; using Amazon.Util.Storage; using Amazon.Util.Storage.Internal; using Amazon.Util.Internal.PlatformServices; using System.Threading; namespace Amazon.CognitoIdentity { /// /// Temporary, short-lived session credentials that are automatically retrieved from /// Amazon Cognito Identity Service and AWS Security Token Service. /// Depending on configured Logins, credentials may be authenticated or unauthenticated. /// public partial class CognitoAWSCredentials : RefreshingAWSCredentials { private static readonly String IDENTITY_ID_CACHE_KEY = "CognitoIdentity:IdentityId"; private static object _lock = new object(); #region private methods private Amazon.SecurityToken.Model.Credentials GetStsCredentials(AssumeRoleWithWebIdentityRequest assumeRequest) { var ars = new AutoResetEvent(false); Amazon.SecurityToken.Model.Credentials credentials = null; Exception exception = null; sts.AssumeRoleWithWebIdentityAsync(assumeRequest, (assumeResult) => { if (assumeResult.Exception != null) exception = assumeResult.Exception; else credentials = assumeResult.Response.Credentials; ars.Set(); }); ars.WaitOne(); if (exception != null) throw exception; return credentials; } private GetCredentialsForIdentityResponse GetCredentialsForIdentity(GetCredentialsForIdentityRequest getCredentialsRequest) { var result = cib.GetCredentialsForIdentity(getCredentialsRequest); return result; } private GetOpenIdTokenResponse GetOpenId(GetOpenIdTokenRequest getTokenRequest) { var getTokenResult = cib.GetOpenIdToken(getTokenRequest); return getTokenResult; } #endregion #region Overrides /// /// Gets the previously cached the identity id retrieved from Cognito. /// For Unity, the Identity id is stored in PlayerPrefs /// /// The previously cached identity id public virtual string GetCachedIdentityId() { var settings = ServiceFactory.Instance.GetService(); return settings.GetValue(GetNamespacedKey(IDENTITY_ID_CACHE_KEY), ApplicationSettingsMode.Local); } /// /// Caches the identity id retrieved from Cognito. /// /// For Unity, the Identity id is stored in PlayerPrefs /// property. /// /// /// The Cognito identity id to cache public virtual void CacheIdentityId(string identityId) { var settings = ServiceFactory.Instance.GetService(); settings.SetValue(GetNamespacedKey(IDENTITY_ID_CACHE_KEY), identityId, ApplicationSettingsMode.Local); } /// /// Clears the currently identity id from the cache. /// public virtual void ClearIdentityCache() { var settings = ServiceFactory.Instance.GetService(); settings.RemoveValue(GetNamespacedKey(IDENTITY_ID_CACHE_KEY), ApplicationSettingsMode.Local); } /// /// Caches the credentials to player pref's /// internal void CacheCredentials(CredentialsRefreshState credentialsState) { //TODO: add support for caching } /// /// Gets the cached credentials state /// internal CredentialsRefreshState GetCachedCredentials() { return null; //TODO: add support for caching } #endregion #region GetIdentityId /// /// Gets the Identity Id corresponding to the credentials retrieved from Cognito. /// Note: this setting may change during execution. To be notified of its /// new value, attach a listener to IdentityChangedEvent /// /// The callback which is executed when the asynchronous operations is completed /// Options for executing asynchronous operation public void GetIdentityIdAsync(AmazonCognitoIdentityCallback callback, AsyncOptions options = null) { options = options == null ? new AsyncOptions() : options; CognitoIdentityAsyncExecutor.ExecuteAsync(() => { return GetIdentityId(); }, options, callback); } #endregion #region GetCredentials /// /// Returns an instance of ImmutableCredentials for this instance /// /// The callback which is executed when the asynchronous operations is completed /// Options for executing asynchronous operation public void GetCredentialsAsync(AmazonCognitoIdentityCallback callback, AsyncOptions options = null) { options = options == null ? new AsyncOptions() : options; CognitoIdentityAsyncExecutor.ExecuteAsync(() => { return GetCredentials(); }, options, callback); } #endregion } }