Exemplo n.º 1
0
        private static async Task <CreateTokenResponse> PollForSsoTokenAsync(IAmazonSSOOIDC client,
                                                                             CreateTokenRequest createTokenRequest,
                                                                             int pollingIntervalSeconds,
                                                                             DateTime deviceCodeExpiration,
                                                                             IGetSsoTokenContext context)
        {
            var logger = Logger.GetLogger(typeof(CoreAmazonSSOOIDC));

            // Spec: If the Interval value is not returned as part of the StartDeviceAuthorization response,
            // a default Interval value of 5 seconds should be used.
            var intervalSec = pollingIntervalSeconds > 0 ? pollingIntervalSeconds : DefaultPollingIntervalSeconds;

            // Poll for Token until success, failure, or an error condition arises.
            while (true)
            {
                try
                {
                    var response = await client.CreateTokenAsync(createTokenRequest).ConfigureAwait(false);

                    // If we reach here, the user has completed the SSO Login authorization.
                    return(response);
                }
                catch (AuthorizationPendingException e)
                {
                    // Service is still waiting for user to complete authorization.
                    // Repeat the loop after an interval.
                }
                catch (SlowDownException e)
                {
                    // Spec: Add 5 seconds to the polling interval
                    intervalSec += PollingSlowdownIncrementSeconds;
                }
                catch (ExpiredTokenException e)
                {
                    // Spec: An exception must be raised, indicating that the SSO login window expired
                    // and the SSO login flow must be re-initiated.
                    throw new AmazonSSOOIDCException("Device code has expired while polling for SSO token, login flow must be re-initiated.", e);
                }
                catch (TimeoutException e)
                {
                    // Spec: If the call times out then the tool should double its polling interval and then retry.
                    intervalSec *= 2;
                }
                catch (Exception e)
                {
                    logger.Error(e, "Unexpected exception while polling for SSO Token.");
                    throw;
                }

                if (DateTime.UtcNow.AddSeconds(intervalSec) > deviceCodeExpiration)
                {
                    throw new AmazonSSOOIDCException("Device code has expired while polling for SSO token, login flow must be re-initiated.");
                }

                context.Sleep(intervalSec * 1000);
            } // while(polling)
        }
Exemplo n.º 2
0
 private Amazon.SSOOIDC.Model.CreateTokenResponse CallAWSServiceOperation(IAmazonSSOOIDC client, Amazon.SSOOIDC.Model.CreateTokenRequest request)
 {
     Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "AWS Single Sign-On OIDC", "CreateToken");
     try
     {
         #if DESKTOP
         return(client.CreateToken(request));
         #elif CORECLR
         return(client.CreateTokenAsync(request).GetAwaiter().GetResult());
         #else
                 #error "Unknown build edition"
         #endif
     }
     catch (AmazonServiceException exc)
     {
         var webException = exc.InnerException as System.Net.WebException;
         if (webException != null)
         {
             throw new Exception(Utils.Common.FormatNameResolutionFailureMessage(client.Config, webException.Message), webException);
         }
         throw;
     }
 }