Пример #1
0
        private static bool Authorize()
        {
            string authorizeUrl = string.Format("{0}?client_id={1}&response_type=code&state=dropboxplugin&redirect_uri={2}", AuthorizeUri, BoxCredentials.ClientId, RedirectUri);

            OAuthLoginForm loginForm = new OAuthLoginForm("Box Authorize", new Size(1060, 600), authorizeUrl, RedirectUri);

            loginForm.ShowDialog();
            if (!loginForm.isOk)
            {
                return(false);
            }
            var callbackParameters = loginForm.CallbackParameters;

            if (callbackParameters == null || !callbackParameters.ContainsKey("code"))
            {
                return(false);
            }

            string authorizationResponse = PostAndReturn(new Uri(TokenUri), string.Format("grant_type=authorization_code&code={0}&client_id={1}&client_secret={2}", callbackParameters["code"], BoxCredentials.ClientId, BoxCredentials.ClientSecret));
            var    authorization         = JsonSerializer.Deserialize <Authorization>(authorizationResponse);

            Config.BoxToken = authorization.AccessToken;
            IniConfig.Save();
            return(true);
        }
Пример #2
0
        /// <summary>
        ///     Receive the code from an OAuth server
        /// </summary>
        /// <param name="authorizeMode">AuthorizeModes</param>
        /// <param name="codeReceiverSettings">ICodeReceiverSettings</param>
        /// <param name="cancellationToken">CancellationToken</param>
        /// <returns>IDictionary with information</returns>
        public Task <IDictionary <string, string> > ReceiveCodeAsync(AuthorizeModes authorizeMode, ICodeReceiverSettings codeReceiverSettings,
                                                                     CancellationToken cancellationToken = default(CancellationToken))
        {
            if (codeReceiverSettings.RedirectUrl == null)
            {
                throw new ArgumentNullException(nameof(codeReceiverSettings.RedirectUrl), "The EmbeddedBrowserCodeReceiver needs a redirect url.");
            }
            // while the listener is beging starter in the "background", here we prepare opening the browser
            var uriBuilder = new UriBuilder(codeReceiverSettings.AuthorizationUri)
            {
                Query = codeReceiverSettings.AuthorizationUri.QueryToKeyValuePairs()
                        .Select(x => new KeyValuePair <string, string>(x.Key, x.Value.FormatWith(codeReceiverSettings)))
                        .ToQueryString()
            };

            Log.Verbose().WriteLine("Opening Uri {0}", uriBuilder.Uri.AbsoluteUri);

            // Needs to run on th UI thread.
            return(Dispatcher.CurrentDispatcher.Invoke(async() => await Task.Run(() =>
            {
                var oAuthLoginForm = new OAuthLoginForm(codeReceiverSettings.CloudServiceName,
                                                        new Size(codeReceiverSettings.EmbeddedBrowserWidth, codeReceiverSettings.EmbeddedBrowserHeight), uriBuilder.Uri, codeReceiverSettings.RedirectUrl);

                if (oAuthLoginForm.ShowDialog() == DialogResult.OK)
                {
                    return oAuthLoginForm.CallbackParameters;
                }
                return null;
            }, cancellationToken)));
        }
Пример #3
0
        public OperationResult CheckAuthenticationSession(OAuthLoginForm loginForm)
        {
            OperationResult result = new OperationResult();
            IFindAuthenticationSessionRepository sessionRepo = (IFindAuthenticationSessionRepository)RepositoryFactory.Create("Find.Tools.OAuthServerManager.AuthenticationSession");
            List <AuthenticationSession>         sessions    = sessionRepo.FindByAuthenticationSessionString(loginForm.AuthorizationSessionID);

            if (sessions.Count > 0)
            {
                return(new OperationResult(true, sessions[0], ""));
            }
            else
            {
                return(new OperationResult(false, null));
            }
        }
Пример #4
0
        private static bool Authorize()
        {
            string         ticketUrl    = string.Format("https://www.box.com/api/1.0/rest?action=get_ticket&api_key={0}", BoxCredentials.API_KEY);
            string         ticketXML    = NetworkHelper.GetAsString(new Uri(ticketUrl));
            string         ticket       = ParseTicket(ticketXML);
            string         authorizeUrl = string.Format("https://www.box.com/api/1.0/auth/{0}", ticket);
            OAuthLoginForm loginForm    = new OAuthLoginForm("Box Authorize", new Size(1060, 600), authorizeUrl, "http://getgreenshot.org");

            loginForm.ShowDialog();
            if (loginForm.isOk)
            {
                if (loginForm.CallbackParameters != null && loginForm.CallbackParameters.ContainsKey("auth_token"))
                {
                    config.BoxToken = loginForm.CallbackParameters["auth_token"];
                    IniConfig.Save();
                    return(true);
                }
            }
            return(false);
        }
Пример #5
0
        /// <summary>
        /// Authorize the token by showing the dialog
        /// </summary>
        /// <returns>The request token.</returns>
        private String getAuthorizeToken()
        {
            if (string.IsNullOrEmpty(Token))
            {
                Exception e = new Exception("The request token is not set");
                throw e;
            }
            LOG.DebugFormat("Opening AuthorizationLink: {0}", authorizationLink);
            OAuthLoginForm oAuthLoginForm = new OAuthLoginForm(LoginTitle, BrowserSize, authorizationLink, CallbackUrl);

            oAuthLoginForm.ShowDialog();
            if (oAuthLoginForm.isOk)
            {
                if (oAuthLoginForm.CallbackParameters != null)
                {
                    if (oAuthLoginForm.CallbackParameters.ContainsKey(OAUTH_TOKEN_KEY))
                    {
                        Token = oAuthLoginForm.CallbackParameters[OAUTH_TOKEN_KEY];
                    }
                    if (oAuthLoginForm.CallbackParameters.ContainsKey(OAUTH_VERIFIER_KEY))
                    {
                        Verifier = oAuthLoginForm.CallbackParameters[OAUTH_VERIFIER_KEY];
                    }
                }
            }
            if (CheckVerifier)
            {
                if (!string.IsNullOrEmpty(Verifier))
                {
                    return(Token);
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                return(Token);
            }
        }
Пример #6
0
        public Stream HandleOAuthLogin(OAuthLoginForm loginForm)
        {
            IOAuthLoginBusinessService businessService = (IOAuthLoginBusinessService)BusinessFactory.Create("Tools.OAuthServerManager.OAuthLogin");
            OperationResult            result          = businessService.CheckAuthenticationSession(loginForm);

            if (result.Result)
            {
                result = businessService.GetAuthenticationServer(loginForm.ClientID);
                if (result.Result)
                {
                    //RegisteredApplication app = (RegisteredApplication)result.Data;
                    result = businessService.QueryAuthenticationServer((string)result.Data, loginForm.Username, loginForm.Password);
                    if (result.Result)
                    {
                        result = businessService.GenerateAuthorizationCode(result, loginForm.Username, loginForm.ClientID, loginForm.AuthorizationSessionID);
                        businessService.SaveAuthorizationCode((AuthenticationCode)result.Data);
                        result = businessService.CallCallbackUrlWithAuthorizationResult(loginForm.ClientID, ((AuthenticationCode)result.Data).AuthenticationCodeString);
                    }
                }
            }

            return(result.ToJsonStream());
        }
Пример #7
0
        /// <summary>
        /// Authorize the token by showing the dialog
        /// </summary>
        /// <returns>The request token.</returns>
        private String GetAuthorizeToken()
        {
            if (string.IsNullOrEmpty(Token))
            {
                Exception e = new Exception("The request token is not set");
                throw e;
            }
            LOG.DebugFormat("Opening AuthorizationLink: {0}", AuthorizationLink);
            OAuthLoginForm oAuthLoginForm = new OAuthLoginForm(LoginTitle, BrowserSize, AuthorizationLink, CallbackUrl);

            oAuthLoginForm.ShowDialog();
            if (oAuthLoginForm.isOk)
            {
                if (oAuthLoginForm.CallbackParameters != null)
                {
                    string tokenValue;
                    if (oAuthLoginForm.CallbackParameters.TryGetValue(OAUTH_TOKEN_KEY, out tokenValue))
                    {
                        Token = tokenValue;
                    }
                    string verifierValue;
                    if (oAuthLoginForm.CallbackParameters.TryGetValue(OAUTH_VERIFIER_KEY, out verifierValue))
                    {
                        Verifier = verifierValue;
                    }
                }
            }
            if (CheckVerifier)
            {
                if (!string.IsNullOrEmpty(Verifier))
                {
                    return(Token);
                }
                return(null);
            }
            return(Token);
        }