public virtual async Task <TokenRevocationResponse> RevokeAsync(
            TokenRevocationRequest request,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }
            if (string.IsNullOrWhiteSpace(request.Token))
            {
                throw new ArgumentNullException("token");
            }

            var form = new Dictionary <string, string>();

            form.Add("token", request.Token);

            if (!string.IsNullOrWhiteSpace(request.TokenTypeHint))
            {
                form.Add("token_type_hint", request.TokenTypeHint);
            }

            if (!string.IsNullOrWhiteSpace(request.ClientId))
            {
                form.Add("client_id", request.ClientId);
            }
            else if (!string.IsNullOrWhiteSpace(_clientId))
            {
                form.Add("client_id", _clientId);
            }

            if (!string.IsNullOrWhiteSpace(request.ClientSecret))
            {
                form.Add("client_secret", request.ClientSecret);
            }

            try
            {
                var response = await _client.PostAsync("", new FormUrlEncodedContent(form)).ConfigureAwait(false);

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    return(new TokenRevocationResponse());
                }
                else if (response.StatusCode == HttpStatusCode.BadRequest)
                {
                    var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                    return(new TokenRevocationResponse(content));
                }
                else
                {
                    return(new TokenRevocationResponse(response.StatusCode, response.ReasonPhrase));
                }
            }
            catch (Exception ex)
            {
                return(new TokenRevocationResponse(ex));
            }
        }
        /// <summary>
        /// Sends an OAuth token revocation request.
        /// </summary>
        /// <param name="client">The client.</param>
        /// <param name="request">The request.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns></returns>
        public static async Task <TokenRevocationResponse> RevokeTokenAsync(this HttpClient client, TokenRevocationRequest request, CancellationToken cancellationToken = default)
        {
            var httpRequest = new HttpRequestMessage(HttpMethod.Post, request.Address);

            httpRequest.Headers.Accept.Clear();
            httpRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            ClientCredentialsHelper.PopulateClientCredentials(request, httpRequest);

            request.Parameters.AddRequired(OidcConstants.TokenIntrospectionRequest.Token, request.Token);
            request.Parameters.AddOptional(OidcConstants.TokenIntrospectionRequest.TokenTypeHint, request.TokenTypeHint);

            httpRequest.Content = new FormUrlEncodedContent(request.Parameters);

            HttpResponseMessage response;

            try
            {
                response = await client.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    return(new TokenRevocationResponse());
                }
                else if (response.StatusCode == HttpStatusCode.BadRequest)
                {
                    var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                    return(new TokenRevocationResponse(content));
                }
                else
                {
                    return(new TokenRevocationResponse(response.StatusCode, response.ReasonPhrase));
                }
            }
            catch (Exception ex)
            {
                return(new TokenRevocationResponse(ex));
            }
        }
        /// <summary>
        /// Sends a token revocation request
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">
        /// request
        /// or
        /// Token
        /// </exception>
        public virtual async Task <TokenRevocationResponse> RevokeAsync(
            TokenRevocationRequest request,
            CancellationToken cancellationToken = default)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            if (request.Token.IsMissing())
            {
                throw new ArgumentNullException(nameof(request.Token));
            }

            IDictionary <string, string> form;

            if (request.Parameters == null)
            {
                form = new Dictionary <string, string>();
            }
            else
            {
                form = request.Parameters;
            }

            form.Add("token", request.Token);

            if (request.ClientId.IsPresent())
            {
                form.Add("client_id", request.ClientId);
            }
            else if (ClientId.IsPresent())
            {
                form.Add("client_id", ClientId);
            }

            form.AddIfPresent("token_type_hint", request.TokenTypeHint);
            form.AddIfPresent("client_secret", request.ClientSecret);

            try
            {
                var response = await Client.PostAsync("", new FormUrlEncodedContent(form), cancellationToken).ConfigureAwait(false);

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    return(new TokenRevocationResponse());
                }
                else if (response.StatusCode == HttpStatusCode.BadRequest)
                {
                    var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                    return(new TokenRevocationResponse(content));
                }
                else
                {
                    return(new TokenRevocationResponse(response.StatusCode, response.ReasonPhrase));
                }
            }
            catch (Exception ex)
            {
                return(new TokenRevocationResponse(ex));
            }
        }
Example #4
0
        /// <summary>
        /// Sends an OAuth token revocation request.
        /// </summary>
        /// <param name="client">The client.</param>
        /// <param name="request">The request.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns></returns>
        public static async Task <TokenRevocationResponse> RevokeTokenAsync(this HttpMessageInvoker client, TokenRevocationRequest request, CancellationToken cancellationToken = default)
        {
            var httpRequest = new HttpRequestMessage(HttpMethod.Post, request.Address);

            httpRequest.Headers.Accept.Clear();
            httpRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            var clone = request.Clone();

            ClientCredentialsHelper.PopulateClientCredentials(clone, httpRequest);

            clone.Parameters.AddRequired(OidcConstants.TokenIntrospectionRequest.Token, request.Token);
            clone.Parameters.AddOptional(OidcConstants.TokenIntrospectionRequest.TokenTypeHint, request.TokenTypeHint);

            httpRequest.Content = new FormUrlEncodedContent(clone.Parameters);

            HttpResponseMessage response;

            try
            {
                response = await client.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                return(ProtocolResponse.FromException <TokenRevocationResponse>(ex));
            }

            return(await ProtocolResponse.FromHttpResponseAsync <TokenRevocationResponse>(response).ConfigureAwait(false));
        }
Example #5
0
        /// <summary>
        /// Sends an OAuth token revocation request.
        /// </summary>
        /// <param name="client">The client.</param>
        /// <param name="request">The request.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns></returns>
        public static async Task <TokenRevocationResponse> RevokeTokenAsync(this HttpMessageInvoker client, TokenRevocationRequest request, CancellationToken cancellationToken = default)
        {
            var clone = request.Clone();

            clone.Method = HttpMethod.Post;
            clone.Parameters.AddRequired(OidcConstants.TokenIntrospectionRequest.Token, request.Token);
            clone.Parameters.AddOptional(OidcConstants.TokenIntrospectionRequest.TokenTypeHint, request.TokenTypeHint);
            clone.Prepare();

            HttpResponseMessage response;

            try
            {
                response = await client.SendAsync(clone, cancellationToken).ConfigureAwait();
            }
            catch (Exception ex)
            {
                return(ProtocolResponse.FromException <TokenRevocationResponse>(ex));
            }

            return(await ProtocolResponse.FromHttpResponseAsync <TokenRevocationResponse>(response).ConfigureAwait());
        }