예제 #1
0
 private static DropboxClientConfig GetConfig()
 {
     return(new DropboxClientConfig
     {
         HttpClient = ProxyTools.CreateHttpClient()
     });
 }
예제 #2
0
        private static HttpClient GetHttpClient(string accessToken)
        {
            var httpClient = ProxyTools.CreateHttpClient();

            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
            httpClient.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json"));
            httpClient.DefaultRequestHeaders.AcceptCharset.Add(new StringWithQualityHeaderValue("utf-8"));
            return(httpClient);
        }
예제 #3
0
        private HttpClient GetClient()
        {
            var client = ProxyTools.CreateHttpClient();

            client.DefaultRequestHeaders.Add("X-Auth-Token", _credentials.Token);
            client.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json"));
            client.DefaultRequestHeaders.AcceptCharset.Add(new StringWithQualityHeaderValue("utf-8"));

            return(client);
        }
예제 #4
0
        public static DropboxClient GetApi(string accessToken)
        {
            var config = new DropboxClientConfig
            {
                HttpClient = ProxyTools.CreateHttpClient()
            };

            var api = new DropboxClient(accessToken, config);

            return(api);
        }
예제 #5
0
        public static async Task <OAuth2Token> ProcessCodeFlowAsync(string code)
        {
            if (string.IsNullOrEmpty(code))
            {
                throw new ArgumentNullException("code");
            }

            var httpClient = ProxyTools.CreateHttpClient();

            try
            {
                var parameters = new Dictionary <string, string>
                {
                    { "code", code },
                    { "grant_type", "authorization_code" },
                    { "client_id", HubiCClientId },
                    { "client_secret", HubiCClientSecret }
                };

                if (!string.IsNullOrEmpty(RedirectUri))
                {
                    parameters["redirect_uri"] = RedirectUri;
                }

                var content  = new FormUrlEncodedContent(parameters);
                var response = await httpClient.PostAsync(TokenUri, content);

                var raw = await response.Content.ReadAsStringAsync();

                var json = JObject.Parse(raw);

                return(new OAuth2Token(
                           json["access_token"].ToString(),
                           json["token_type"].ToString(),
                           json["expires_in"].Value <int>(),
                           json["refresh_token"].ToString()));
            }
            finally
            {
                httpClient.Dispose();
            }
        }
예제 #6
0
        //public async Task<AccountConfiguration> CreateAccount()
        //{
        //    var type = _isAccessRestricted ? StorageType.DropboxRestricted : StorageType.Dropbox;
        //    var clientId = _isAccessRestricted ? DropboxHelper.DropboxAppFolderOnlyClientId : DropboxHelper.DropboxFullAccessClientId;
        //    var clientSecret = _isAccessRestricted ? DropboxHelper.DropboxAppFolderOnlyClientSecret : DropboxHelper.DropboxFullAccessClientSecret;
        //    string[] scopes =
        //    {
        //        "account_info.read",
        //        "files.metadata.write",
        //        "files.metadata.read",
        //        "files.content.write",
        //        "files.content.read"
        //    };

        //    var flow = new OidcFlow(type, clientId, clientSecret, scopes, 50001, 50005);
        //    return await flow.AuthorizeOauth2Async("https://www.dropbox.com/oauth2/authorize", "https://api.dropboxapi.com/oauth2/token");

        //}

        public async Task <AccountConfiguration> CreateAccount()
        {
            var f = new OidcWaitForm();

            f.InitEx(_isAccessRestricted ? StorageType.DropboxRestricted : StorageType.Dropbox);
            f.Show();

            var clientId     = _isAccessRestricted ? DropboxHelper.DropboxAppFolderOnlyClientId : DropboxHelper.DropboxFullAccessClientId;
            var clientSecret = _isAccessRestricted ? DropboxHelper.DropboxAppFolderOnlyClientSecret : DropboxHelper.DropboxFullAccessClientSecret;

            string[] scopes =
            {
                "account_info.read",
                "files.metadata.write",
                "files.metadata.read",
                "files.content.write",
                "files.content.read"
            };

            var ports = Enumerable.Range(49306, 10)
                        .Concat(Enumerable.Range(49996, 10))
                        .Concat(Enumerable.Range(63900, 10));

            var browser = new OidcSystemBrowser(ports);

            var redirectUri = browser.RedirectUri;

            var state         = Guid.NewGuid().ToString("N");
            var codeVerifier  = db.DropboxOAuth2Helper.GeneratePKCECodeVerifier();
            var codeChallenge = db.DropboxOAuth2Helper.GeneratePKCECodeChallenge(codeVerifier);
            var uri           = db.DropboxOAuth2Helper.GetAuthorizeUri(db.OAuthResponseType.Code, clientId, redirectUri, state, false, false, null, false, db.TokenAccessType.Offline, scopes, db.IncludeGrantedScopes.None, codeChallenge);

            var query = await browser.GetQueryStringAsync(uri.ToString(), f.CancellationToken);


            var resultState = query["state"];

            if (state != resultState)
            {
                throw new Exception("MiM-Attack?");
            }

            var code = query["code"];

            var response = await db.DropboxOAuth2Helper.ProcessCodeFlowAsync(code, clientId, null, redirectUri, ProxyTools.CreateHttpClient(), codeVerifier);

            var api   = DropboxHelper.GetApi(response.AccessToken);
            var owner = await api.Users.GetCurrentAccountAsync();

            var account = new AccountConfiguration()
            {
                Id     = owner.AccountId,
                Name   = owner.Name.DisplayName,
                Type   = _isAccessRestricted ? StorageType.DropboxRestricted : StorageType.Dropbox,
                Secret = response.RefreshToken,
            };

            f.Close();

            return(account);
        }