private static DropboxClientConfig GetConfig() { return(new DropboxClientConfig { HttpClient = ProxyTools.CreateHttpClient() }); }
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); }
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); }
public static DropboxClient GetApi(string accessToken) { var config = new DropboxClientConfig { HttpClient = ProxyTools.CreateHttpClient() }; var api = new DropboxClient(accessToken, config); return(api); }
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(); } }
//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); }