Beispiel #1
0
            public void Run()
            {
                AuthenticationDomain authDomain = null;
                var browser = new OAuthAuthenticationForm();

                browser.AddUrlEventHandler(Resources.RedirectURL, uri =>
                {
                    try
                    {
                        // return is <redirect_url>/oauth/authorize#access_token=...&subdomain=...&apicp=...&appcp=...
                        var query        = new Dictionary <string, string>();
                        OAuthToken token = null;
                        if (!string.IsNullOrEmpty(uri.Query))
                        {
                            foreach (var kvp in uri.Query.Substring(1).Split('&'))
                            {
                                var kvpSplit = kvp.Split('=');
                                if (kvpSplit.Length == 2)
                                {
                                    query.Add(kvpSplit[0], kvpSplit[1]);
                                }
                            }
                            var subdomain = query["subdomain"];
                            var apiCP     = query["apicp"];
                            var appCP     = query["appcp"];

                            token = _psClient.GetTokenResponse(
                                string.Format("https://{0}.{1}/oauth/token", subdomain, appCP),
                                string.Format("grant_type=authorization_code&code={0}&client_id={1}&client_secret={2}&requirev3=true", query["code"],
                                              Resources.ClientId, Resources.ClientSecret));

                            authDomain                   = new AuthenticationDomain();
                            authDomain.OAuthToken        = token.AccessToken;
                            authDomain.OAuthRefreshToken = token.RefreshToken;
                            authDomain.Account           = token.Subdomain;
                            authDomain.Provider          = Resources.ShareFileProvider;
                            authDomain.Domain            = token.ApiCP;
                            authDomain.ApiVersion        = Resources.DefaultApiVersion;
                            return(true);
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    catch (Exception)
                    {
                        return(true);
                    }
                });
                browser.Navigate(new Uri(string.Format("https://{0}.{1}/oauth/authorize?response_type=code&client_id={2}&redirect_uri={3}&autoredirect=true",
                                                       _requestDomain.Account != null ? _requestDomain.Account : "secure",
                                                       _requestDomain.Domain,
                                                       Resources.ClientId,
                                                       Uri.EscapeUriString(Resources.RedirectURL))));
                _result = authDomain;
                _waitHandle.Set();
            }
            public void Run()
            {
                AuthenticationDomain authDomain = null;
                var browser    = new OAuthAuthenticationForm();
                Uri requestUri = null;

                if (_formUri == null)
                {
                    requestUri = new Uri(string.Format("https://{0}.{1}/oauth/authorize?response_type=code&client_id={2}{3}{4}&redirect_uri={5}",
                                                       _requestDomain.Account ?? "secure",
                                                       _requestDomain.Domain,
                                                       Resources.ClientId,
                                                       _requestDomain.Account != null && !_requestDomain.Account.Equals("secure") ? "&subdomain=" + _requestDomain.Account : "",
                                                       _requestDomain.Username != null ? "&username="******"",
                                                       Uri.EscapeUriString(Resources.RedirectURL)));

                    browser.AddUrlEventHandler(Resources.RedirectURL, uri =>
                    {
                        try
                        {
                            // return is <redirect_url>/oauth/authorize#access_token=...&subdomain=...&apicp=...&appcp=...
                            var query        = new Dictionary <string, string>();
                            OAuthToken token = null;
                            if (!string.IsNullOrEmpty(uri.Query))
                            {
                                foreach (var kvp in uri.Query.Substring(1).Split('&'))
                                {
                                    var kvpSplit = kvp.Split('=');
                                    if (kvpSplit.Length == 2)
                                    {
                                        query.Add(kvpSplit[0], kvpSplit[1]);
                                    }
                                }
                                var subdomain = query["subdomain"];
                                var apiCP     = query["apicp"];
                                var appCP     = query["appcp"];

                                token = _psClient.GetTokenResponse(
                                    "POST",
                                    string.Format("https://{0}.{1}/oauth/token", subdomain, appCP),
                                    string.Format("grant_type=authorization_code&code={0}&client_id={1}&client_secret={2}&requirev3=true", query["code"],
                                                  Resources.ClientId, Resources.ClientSecret));

                                authDomain                   = new AuthenticationDomain();
                                authDomain.OAuthToken        = token.AccessToken;
                                authDomain.OAuthRefreshToken = token.RefreshToken;
                                authDomain.Account           = token.Subdomain;
                                authDomain.Provider          = Resources.ShareFileProvider;
                                authDomain.Domain            = token.ApiCP;
                                authDomain.ApiVersion        = Resources.DefaultApiVersion;
                                return(true);
                            }
                            else
                            {
                                return(false);
                            }
                        }
                        catch (Exception)
                        {
                            return(true);
                        }
                    });
                }
                else
                {
                    requestUri = _formUri;
                    browser.AddUrlEventHandler(Resources.RedirectURL, uri =>
                    {
                        var query = new Dictionary <string, string>();
                        if (!string.IsNullOrEmpty(uri.Query))
                        {
                            foreach (var kvp in uri.Query.Substring(1).Split('&'))
                            {
                                var kvpSplit = kvp.Split('=');
                                if (kvpSplit.Length == 2)
                                {
                                    query.Add(kvpSplit[0], kvpSplit[1]);
                                }
                            }
                            var request     = HttpWebRequest.CreateHttp(_tokenUri.ToString() + string.Format("?root={0}&code={1}", _root, query["code"]));
                            var response    = (HttpWebResponse)request.GetResponse();
                            Session session = null;
                            using (var reader = new StreamReader(response.GetResponseStream()))
                            {
                                session = JsonConvert.DeserializeObject <Session>(reader.ReadToEnd());
                            }
                            authDomain        = new AuthenticationDomain();
                            authDomain.AuthID = session.Id;
                            authDomain.Uri    = uri.ToString();
                            return(true);
                        }
                        else
                        {
                            return(false);
                        }
                    });
                }
                browser.Navigate(requestUri);
                _result = authDomain;
                _waitHandle.Set();
            }