Пример #1
0
        internal async Task <bool> AuthorizeCompleted(AblyAuthUpdatedEventArgs args)
        {
            if (AuthUpdated == null)
            {
                return(true);
            }

            bool?completed = null;

            void OnTimerElapsed()
            {
                if (args?.CompletedTask != null && completed.HasValue == false)
                {
                    args.CompletedTask.TrySetException(
                        new AblyException($"Timeout waiting for Authorize to complete. A CONNECTED or ERROR ProtocolMessage was expected before the timeout ({Options.RealtimeRequestTimeout.TotalMilliseconds}ms) elapsed.", 40140));
                }
            }

            var timer = new Timer(state => OnTimerElapsed(), null, (int)Options.RealtimeRequestTimeout.TotalMilliseconds, Timeout.Infinite);

            completed = await args.CompletedTask.Task;
            timer.Dispose();

            return(completed.Value);
        }
Пример #2
0
        /// <summary>
        /// Ensure valid auth credentials are present. This may rely in an already-known
        /// and valid token, and will obtain a new token if necessary or explicitly
        /// requested.
        /// Authorisation will use the parameters supplied on construction except
        /// where overridden with the options supplied in the call.
        /// </summary>
        /// <param name="tokenParams"><see cref="TokenParams"/> custom parameter. Pass null and default token request options will be generated used the options passed when creating the client</param>
        /// <param name="authOptions"><see cref="AuthOptions"/> custom options.</param>
        /// <returns>Returns a valid token</returns>
        /// <exception cref="AblyException">Throws an ably exception representing the server response</exception>
        public async Task <TokenDetails> AuthorizeAsync(TokenParams tokenParams = null, AuthOptions authOptions = null)
        {
            // RSA10j - TokenParams and AuthOptions supersede any previously client library configured TokenParams and AuthOptions
            authOptions = authOptions ?? CurrentAuthOptions ?? Options;
            SetCurrentAuthOptions(authOptions);

            tokenParams = tokenParams ?? CurrentTokenParams ?? TokenParams.WithDefaultsApplied();
            SetCurrentTokenParams(tokenParams);

            CurrentToken = await RequestTokenAsync(tokenParams, authOptions);

            AuthMethod = AuthMethod.Token;
            var eventArgs = new AblyAuthUpdatedEventArgs(CurrentToken);

            AuthUpdated?.Invoke(this, eventArgs);

            // RTC8a3
            await AuthorizeCompleted(eventArgs);

            return(CurrentToken);
        }