Exemplo n.º 1
0
        private static string ScopeToString(CloudDriveScope scope)
        {
            var result = new List <string>();
            var values = Enum.GetValues(typeof(CloudDriveScope));

            foreach (CloudDriveScope value in values)
            {
                if (scope.HasFlag(value))
                {
                    result.Add(scopeToStringMap[value]);
                }
            }
            return(string.Join(" ", result));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Opens Amazon Cloud Drive authentication in default browser. Then it starts listener for port 45674 if portSelector is null
        /// </summary>
        /// <param name="scope">Your App scope to access cloud</param>
        /// <param name="timeout">How long lister will wait for redirect before throw TimeoutException</param>
        /// <param name="portSelector">Func to select port for redirect listener.
        /// portSelector(int lastPort, int time) where lastPost is port used last time and
        /// time is number of times selector was called before. To abort port selection throw exception other than HttpListenerException</param>
        /// <param name="cancelToken">Cancellation for auth. Can be null.</param>
        /// <returns>True if authenticated</returns>
        /// <exception cref="InvalidOperationException">if any selected port could not be opened by default selector</exception>
        public async Task <bool> SafeAuthenticationAsync(CloudDriveScope scope, TimeSpan timeout, CancellationToken?cancelToken = null, Func <int, int, int> portSelector = null)
        {
            CreateListener(portSelector);

            redirectListener.Start();
            using (var tabProcess = Process.Start(BuildLoginUrl(redirectUrl, scope)))
            {
                try
                {
                    var task        = redirectListener.GetContextAsync();
                    var timeoutTask = (cancelToken != null) ? Task.Delay(timeout, cancelToken.Value) : Task.Delay(timeout);
                    var anytask     = await Task.WhenAny(task, timeoutTask).ConfigureAwait(false);

                    if (anytask == task)
                    {
                        await ProcessRedirect(await task, clientId, clientSecret, redirectUrl).ConfigureAwait(false);

                        this.scope = scope;
                    }
                    else
                    {
                        if (timeoutTask.IsCanceled)
                        {
                            return(false);
                        }
                        throw new TimeoutException("No redirection detected");
                    }
                }
                finally
                {
                    redirectListener.Close();
                    redirectListener = null;
                    //tabProcess.Kill();
                }
            }

            return(Token != null);
        }
Exemplo n.º 3
0
        public string BuildLoginUrl(string redirectUrl, CloudDriveScope scope)
        {
            Contract.Assert(redirectUrl != null);

            return($"{loginUrlBase}?client_id={clientId}&scope={ScopeToString(scope)}&response_type=code&redirect_uri={redirectUrl}");
        }
Exemplo n.º 4
0
        public string BuildLoginUrl(string redirectUrl, CloudDriveScope scope)
        {
            Contract.Assert(redirectUrl != null);

            return $"{loginUrlBase}?client_id={clientId}&scope={ScopeToString(scope)}&response_type=code&redirect_uri={redirectUrl}";

        }
Exemplo n.º 5
0
 private static string ScopeToString(CloudDriveScope scope)
 {
     var result = new List<string>();
     var values = Enum.GetValues(typeof(CloudDriveScope));
     foreach (CloudDriveScope value in values)
         if (scope.HasFlag(value))
             result.Add(scopeToStringMap[value]);
     return string.Join(" ", result);
 }
Exemplo n.º 6
0
        /// <summary>
        /// Opens Amazon Cloud Drive authentication in default browser. Then it starts listener for port 45674 if portSelector is null
        /// </summary>
        /// <param name="scope">Your App scope to access cloud</param>
        /// <param name="timeout">How long lister will wait for redirect before throw TimeoutException</param>
        /// <param name="portSelector">Func to select port for redirect listener. 
        /// portSelector(int lastPort, int time) where lastPost is port used last time and 
        /// time is number of times selector was called before. To abort port selection throw exception other than HttpListenerException</param>
        /// <param name="cancelToken">Cancellation for auth. Can be null.</param>
        /// <returns>True if authenticated</returns>
        /// <exception cref="InvalidOperationException">if any selected port could not be opened by default selector</exception>
        public async Task<bool> SafeAuthenticationAsync(CloudDriveScope scope, TimeSpan timeout, CancellationToken? cancelToken = null, Func<int, int, int> portSelector = null)
        {
            CreateListener(portSelector);

            redirectListener.Start();
            using (var tabProcess = Process.Start(BuildLoginUrl(redirectUrl, scope)))
            {
                try
                {
                    var task = redirectListener.GetContextAsync();
                    var timeoutTask = (cancelToken != null) ? Task.Delay(timeout, cancelToken.Value) : Task.Delay(timeout);
                    var anytask = await Task.WhenAny(task, timeoutTask).ConfigureAwait(false);
                    if (anytask == task)
                    {
                        await ProcessRedirect(await task, clientId, clientSecret, redirectUrl).ConfigureAwait(false);

                        this.scope = scope;
                    }
                    else
                    {
                        if (timeoutTask.IsCanceled) return false;
                        throw new TimeoutException("No redirection detected");
                    }
                }
                finally
                {
                    redirectListener.Close();
                    redirectListener = null;
                    //tabProcess.Kill();
                }
            }

            return Token != null;
        }