/// <summary> /// Get a working provider (so, an access token) from Dropbox. /// </summary> /// <param name="savedState"></param> /// <param name="silent"></param> public async Task <string> Register(string savedState = null, bool silent = false) { var result = new TaskCompletionSource <string>(); // Check if the saved token is still usable if (await ClientFactory(savedState)) { result.SetResult(savedState); return(await result.Task); } // If the saved token was not usable and silent is true, return with failure if (silent) { result.SetResult(null); return(await result.Task); } // If it is not, try to get a new one var url = DropboxOAuth2Helper.GetAuthorizeUri( OAuthResponseType.Code, Obscure.CaesarDecode(Config.AppKey), (string)null); var form = new CodeForm(url.ToString(), 43); var success = false; form.OnResult += async code => { success = true; form.Close(); var response = await DropboxOAuth2Helper.ProcessCodeFlowAsync( code, Obscure.CaesarDecode(Config.AppKey), Obscure.CaesarDecode(Config.AppSecret)); if (response?.AccessToken != null && await ClientFactory(response.AccessToken)) { result.SetResult(response.AccessToken); } else { result.SetResult(null); } }; form.FormClosed += (sender, args) => { if (!success) { result.SetResult(null); } }; Process.Start(url.ToString()); form.Show(); return(await result.Task); }
/// <summary> /// Get an access token from OneDrive - or try to use an existing one. /// </summary> /// <param name="savedState">Previous serialized TokenResponse from OneDrive.</param> /// <param name="showForm"></param> /// <returns></returns> public async Task <string> Register(string savedState = null, bool showForm = true) { var result = new TaskCompletionSource <string>(); var provider = new AuthProvider(); // Check if there is a saved token and try to use it if (!string.IsNullOrEmpty(savedState)) { provider.Session = JsonConvert.DeserializeObject <TokenResponse>(Obscure.Base64Decode(savedState)); if (await ClientFactory(provider)) { result.SetResult(savedState); return(await result.Task); } } // If the saved token was not usable and showForm is false, return with failure if (!showForm) { result.SetResult(null); return(await result.Task); } // Create a CodeForm and open OneDrive token request link to obtain a new token var form = new CodeForm(provider.Url, 37); var success = false; form.OnResult += async code => { success = true; form.Close(); provider.ProcessCode(code); if (await ClientFactory(provider)) { result.SetResult(Obscure.Base64Encode(JsonConvert.SerializeObject(provider.Session))); } else { result.SetResult(null); } }; form.FormClosed += (sender, e) => { if (!success) { result.SetResult(null); } }; System.Diagnostics.Process.Start(provider.Url); form.Show(); return(await result.Task); }
/// <summary> /// Register an FTP connection from saved or new credentials. /// </summary> /// <param name="savedState">The login informations of the FTP.</param> /// <param name="showForm">Show the registration form, if the saved state did not work.</param> /// <returns></returns> public Task <string> Register(string savedState = null, bool showForm = true) { var result = new TaskCompletionSource <string>(); // Try to use the old credentials try { var oldCreds = JsonConvert.DeserializeObject <LoginCredentials>(Obscure.Base64Decode(savedState)); result.SetResult(savedState); creds = oldCreds; return(result.Task); } catch { // Ignored } // If the saved creds were not usable and showForm is false, return with failure if (!showForm) { result.SetResult(null); return(result.Task); } // If the old ones failed, try to get new ones var form = new LoginForm(); var success = false; form.OnResult += newCreds => { creds = newCreds; success = true; result.SetResult(Obscure.Base64Encode(JsonConvert.SerializeObject(newCreds))); form.Close(); }; form.FormClosed += (sender, e) => { if (!success) { result.SetResult(null); } }; form.Show(); return(result.Task); }
/// <summary> /// Get a working Provider for Google Drive. /// </summary> /// <param name="savedState">Serialized TokenResponse from the Google Api (and folderId).</param> /// <param name="showForm">Show registration form, if the saved state did not work.</param> /// <returns></returns> public async Task <string> Register(string savedState = null, bool showForm = true) { var result = new TaskCompletionSource <string>(); try { state = new MemoryStore(Obscure.Base64Decode(savedState)); // Try to use the previously saved TokenResponse and if it fails, show the registration form (if the showForm parameter allows it) var secret = new ClientSecrets() { ClientId = Obscure.CaesarDecode(Constants.AppKey), ClientSecret = Obscure.CaesarDecode(Constants.AppSecret) }; var receiver = new CodeReceiver(showForm); var credentials = await GoogleWebAuthorizationBroker.AuthorizeAsync(secret, Constants.Scopes, "token", CancellationToken.None, state, receiver); service = new DriveService(new BaseClientService.Initializer() { HttpClientInitializer = credentials, ApplicationName = "SharpCrop", }); // Try to use the saved application folder or get a new one // If the access token was expired or corrupted, it is safer to get the folder id again if (receiver.Executed || await state.GetAsync <string>("folderId") == null) { var folder = await GetFolder(); await state.DeleteAsync <string>("folderId"); await state.StoreAsync("folderId", folder?.Id); } // Export the serialized TokenResponse which gonna be saved into the config result.SetResult(Obscure.Base64Encode(state.Export())); } catch { result.SetResult(null); } return(await result.Task); }
/// <summary> /// Process a code and obtain a TokenResponse. /// </summary> /// <param name="code"></param> public void ProcessCode(string code) { // The redirect_uri redirects the user to the Github Page of the project where the OneDrive.html // writes the API code to the document.body from the URL. This is needed, because OneDrive // does not support a token request without a callback URL - like Google Drive and Dropbox. var request = WebRequest.Create("https://login.live.com/oauth20_token.srf"); var array = Encoding.UTF8.GetBytes(string.Format( "client_id={0}&client_secret={1}&redirect_uri={2}&code={3}&grant_type=authorization_code", Obscure.CaesarDecode(Constants.AppKey), Obscure.CaesarDecode(Constants.AppSecret), Constants.RedirectUrl, code)); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = array.Length; request.Method = "POST"; var stream = request.GetRequestStream(); stream.Write(array, 0, array.Length); stream.Close(); var response = request.GetResponse(); stream = response.GetResponseStream(); if (stream != null) { var reader = new StreamReader(stream); var responseData = reader.ReadToEnd(); Session = JsonConvert.DeserializeObject <TokenResponse>(responseData); reader.Close(); stream.Close(); } response.Close(); }