Esempio n. 1
0
        // Because we need to launch a browser and wait for authentications this needs to be a task so it can wait.
        async Task BeginoAuth2Authentication()
        {
            if (string.IsNullOrEmpty(XeroConfig.ClientID))
            {
                throw new ArgumentNullException("Missing Client ID");
            }
            // Raise event to the parent caller (your app)
            onStatusUpdates("Begin Authentication", XeroEventStatus.Success);

            XeroConfig.ReturnedAccessCode = null;               // Ensure the Return code cleared as we are authenticating and this propery will be monitored for the completion
            XeroConfig.AccessTokenSet     = new XeroTokenSet(); // Reset this token as we are authenticating so its all going to be replaced
            //start webserver to listen for the callback
            responseListener             = new LocalHttpListener();
            responseListener.Message    += MessageResponse;
            responseListener.callBackUri = XeroConfig.CallbackUri;
            responseListener.config      = XeroConfig;
            responseListener.StartWebServer();

            //open web browser with the link generated
            System.Diagnostics.Process.Start(XeroConfig.AuthURL);

            // Fire Event so the caller can monitor
            onStatusUpdates("Login URL Opened", XeroEventStatus.Log);

            // Basically wait for 60 Seconds (should be long enough, possibly not for first time if using 2FA)
            HasTimedout = false;
            int counter = 0;

            do
            {
                await Task.Delay(1000); // Wait 1 second - gives time for response back to listener

                counter++;
            } while (responseListener.config.ReturnedAccessCode == null && counter < Timeout); // Keep waiting until a code is returned or a timeout happens

            if (counter >= Timeout)
            {
                // Raise event to the parent caller (your app)
                onStatusUpdates("Timed Out Waiting for Authentication", XeroEventStatus.Timeout);
                HasTimedout = true;
            }
            else
            {
                // Test if access was not granted
                // ReturnedAccessCode will be either a valid code or "ACCESS DENIED"
                if (responseListener.config.ReturnedAccessCode != XeroConstants.XERO_AUTH_ACCESS_DENIED)
                {
                    // Raise event to the parent caller (your app)
                    onStatusUpdates("Success", XeroEventStatus.Success);

                    //       XeroConfig = responseListener.config;// update the config with the retrieved access code data
                    ExchangeCodeForToken();
                    // Raise event to the parent caller (your app)
                    onStatusUpdates("Authentication Completed", XeroEventStatus.Success);
                }
            }
            responseListener.StopWebServer();
            // Raise event to the parent caller (your app)
            onStatusUpdates("Authentication Failed", XeroEventStatus.Failed);
        }
Esempio n. 2
0
 public oAuth2()
 {
     // Setup the Listener client
     responseListener = new LocalHttpListener();
     if (!Timeout.HasValue)
     {
         Timeout = 60;
     }
     HasTimedout = false;
 }