コード例 #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));
        }
コード例 #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);
        }
コード例 #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}");
        }
コード例 #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}";

        }
コード例 #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);
 }
コード例 #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;
        }