// Retrieves credentials for the roles defined on the identity pool
        private async System.Threading.Tasks.Task <CredentialsRefreshState> GetPoolCredentialsAsync()
        {
            CredentialsRefreshState credentialsState;

            var identity = await GetIdentityIdAsync(RefreshIdentityOptions.Refresh).ConfigureAwait(false);

            var getCredentialsRequest = new GetCredentialsForIdentityRequest {
                IdentityId = identity
            };

            if (Logins.Count > 0)
            {
                getCredentialsRequest.Logins = Logins;
            }
            if (_identityState != null && !string.IsNullOrEmpty(_identityState.LoginToken))
            {
                getCredentialsRequest.Logins = new Dictionary <string, string>();
                getCredentialsRequest.Logins.Add("cognito-identity.amazonaws.com", _identityState.LoginToken);
            }

            bool retry = false;
            GetCredentialsForIdentityResponse response = null;

            try
            {
                response = (await cib.GetCredentialsForIdentityAsync(getCredentialsRequest).ConfigureAwait(false));
                // IdentityId may have changed, save the new value
                UpdateIdentity(response.IdentityId);
            }
            catch (AmazonCognitoIdentityException e)
            {
                if (ShouldRetry(e))
                {
                    retry = true;
                }
                else
                {
                    throw;
                }
            }

            if (retry)
            {
                return(await GetPoolCredentialsAsync());
            }


            var credentials = response.Credentials;

            credentialsState = new CredentialsRefreshState(credentials.GetCredentials(), credentials.Expiration);
            return(credentialsState);
        }
        // Retrieves credentials for the roles defined on the identity pool
        private async System.Threading.Tasks.Task <CredentialsRefreshState> GetPoolCredentialsAsync()
        {
            CredentialsRefreshState credentialsState;
            var identity = await GetIdentityIdWithCachingAsync().ConfigureAwait(false);

            var getCredentialsRequest = new GetCredentialsForIdentityRequest {
                IdentityId = identity.IdentityId
            };

            if (Logins.Count > 0)
            {
                getCredentialsRequest.Logins = Logins;
            }

            bool retry = false;
            GetCredentialsForIdentityResponse response = null;

            try
            {
                response = (await cib.GetCredentialsForIdentityAsync(getCredentialsRequest).ConfigureAwait(false));
                // IdentityId may have changed, save the new value
                UpdateIdentity(response.IdentityId, true);
            }
            catch (AmazonCognitoIdentityException e)
            {
                if (ShouldRetry(e, identity))
                {
                    retry = true;
                }
                else
                {
                    throw;
                }
            }

            if (retry)
            {
                return(await GetPoolCredentialsAsync());
            }


            var credentials = response.Credentials;

            credentialsState = new CredentialsRefreshState(credentials.GetCredentials(), credentials.Expiration);
            return(credentialsState);
        }
Example #3
0
        public async Task <HttpResponseMessage> PutMetricsAsync(IDictionary <string, object> data)
        {
            HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Put, TELEMETRIC_SERVICE_URI + "metrics");

            message.Content = HttpClientExtensions.GetStringContent(data);

            //Get credential if no credential or withing 5 minute of expiration
            if (_cognitoCredentials == null || _cognitoCredentials.Expiration - AWSSDKUtils.CorrectedUtcNow < TimeSpan.FromMinutes(5))
            {
                var getCredentialsForIdentityResponse = await _cognitoIdentityClient.GetCredentialsForIdentityAsync(ClientId);

                _cognitoCredentials = getCredentialsForIdentityResponse.Credentials;
            }

            await AWSV4SignerExtensions.SignRequestAsync(message, REGION, SERVICE_NAME, _cognitoCredentials);

            return(await _httpClient.SendAsync(message));
        }
        public async Task PutMetricsAsync(IDictionary <string, object> data, CancellationToken cancellationToken)
        {
            var clientId = await GetClientIdAsync(cancellationToken);

            using var message = new HttpRequestMessage(HttpMethod.Put, TELEMETRIC_SERVICE_URI + "metrics");
            message.Content   = HttpClientExtensions.GetStringContent(data);

            //Get credential if no credential or withing 5 minute of expiration
#pragma warning disable CS0618 // Type or member is obsolete
            if (_cognitoCredentials == null || _cognitoCredentials.Expiration - AWSSDKUtils.CorrectedUtcNow < TimeSpan.FromMinutes(5))
#pragma warning restore CS0618 // Type or member is obsolete
            {
                var getCredentialsForIdentityResponse = await _cognitoIdentityClient.GetCredentialsForIdentityAsync(clientId);

                _cognitoCredentials = getCredentialsForIdentityResponse.Credentials;
            }

            await AWSV4SignerExtensions.SignRequestAsync(message, REGION, SERVICE_NAME, _cognitoCredentials);

            using var response = await _httpClient.SendAsync(message, cancellationToken);

            response.EnsureSuccessStatusCode();
        }
Example #5
0
        // Retrieves credentials for the roles defined on the identity pool
        private async System.Threading.Tasks.Task <CredentialsRefreshState> GetPoolCredentialsAsync()
        {
            CredentialsRefreshState credentialsState;
            var identityId = await GetIdentityIdAsync().ConfigureAwait(false);

            var getCredentialsRequest = new GetCredentialsForIdentityRequest {
                IdentityId = identityId
            };

            if (Logins.Count > 0)
            {
                getCredentialsRequest.Logins = Logins;
            }
            var response = (await cib.GetCredentialsForIdentityAsync(getCredentialsRequest).ConfigureAwait(false));

            // IdentityId may have changed, save the new value
            UpdateIdentity(response.IdentityId, true);

            var credentials = response.Credentials;

            credentialsState = new CredentialsRefreshState(credentials.GetCredentials(), credentials.Expiration);
            return(credentialsState);
        }