Esempio n. 1
0
        /*
         * A class representing when the browser is navigated by the system or user
         */
        private void webBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            // for debugging only, if logging set to DEBUG, display title on form
            log.Debug("Determining if the log mode is debug or not.");
            if (log.IsDebugEnabled)
            {
                // set the browser's title to the windows form title
                log.Debug("Setting the form's title to the actual title of the browser.");
                this.Text = "(title = " + this.webBrowser.DocumentTitle.ToString() + ", url = " + this.webBrowser.Url + ")";
            }

            // check to see if the URI is an approval from Google Accounts
            log.Debug("Checking to see if the URI, back from the sever, contains .../approval/");
            log.Debug("AbsoluteUri: " + e.Url.AbsoluteUri);
            log.Debug("Title: " + this.webBrowser.DocumentTitle.ToString());
            if (e.Url.AbsoluteUri.StartsWith("https://accounts.google.com/o/oauth2/approval"))
            {
                log.Debug("URL does contain .../approval/");

                // fetch the title from the web browser
                log.Debug("Fetching the title from the browser object.");
                string title = this.webBrowser.DocumentTitle.ToString();

                // immediately close the browser window
                log.Debug("Closing the browser window");
                this.Close();

                // decode the title of the browser and retrieve the token object
                log.Debug("Decoding the title string response from the server into a Token object.");
                log.Debug("Title: " + title);
                Extension.Auth.OAuth2Token token = Extension.Auth.OAuth2Utilities.decodeTitleResponse(ref log, title, isViewOnly);

                if (token != null)
                {
                    // raise authentication event, success
                    log.Debug("Raising an authentication event, Authorized.");
                    ext.publishRaiseAuthenticationStateChangeEvent(true, this.isViewOnly, token);
                }
                else
                {
                    // raise authentication event, failed
                    log.Debug("Raising an authentication event, Unauthorized.");
                    ext.publishRaiseAuthenticationStateChangeEvent(false, this.isViewOnly, null);
                }
            }
            else
            {
                // the title did not show /approval/, raise an unauthorized event
                log.Debug("Raising an authentication event, Unauthorized.");
                ext.publishRaiseAuthenticationStateChangeEvent(false, this.isViewOnly, null);
            }
        }