Beispiel #1
0
        /// <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);
        }
Beispiel #2
0
        /// <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);
        }
Beispiel #3
0
        public Task <AuthorizationCodeResponseUrl> ReceiveCodeAsync(AuthorizationCodeRequestUrl authUrl, CancellationToken taskCancellationToken)
        {
            var result = new TaskCompletionSource <AuthorizationCodeResponseUrl>();

            if (showForm == false)
            {
                result.SetResult(null);
                return(result.Task);
            }

            var url     = authUrl.Build().ToString();
            var form    = new CodeForm(url, 45);
            var success = false;

            form.OnResult += code =>
            {
                success = true;

                form.Close();
                result.SetResult(new AuthorizationCodeResponseUrl()
                {
                    Code = code
                });
            };

            form.FormClosed += (sender, e) =>
            {
                if (!success)
                {
                    result.SetResult(null);
                }
            };

            System.Diagnostics.Process.Start(url);
            form.Show();

            executed = true;

            return(result.Task);
        }