Exemplo n.º 1
0
        /// <summary>
        /// Applies protocol parameters to HTTP request
        /// </summary>
        public void Prepare()
        {
            if (ClientId.IsPresent())
            {
                if (ClientCredentialStyle == ClientCredentialStyle.AuthorizationHeader)
                {
                    if (AuthorizationHeaderStyle == BasicAuthenticationHeaderStyle.Rfc6749)
                    {
                        this.SetBasicAuthenticationOAuth(ClientId, ClientSecret ?? "");
                    }
                    else if (AuthorizationHeaderStyle == BasicAuthenticationHeaderStyle.Rfc2617)
                    {
                        this.SetBasicAuthentication(ClientId, ClientSecret ?? "");
                    }
                    else
                    {
                        throw new InvalidOperationException("Unsupported basic authentication header style");
                    }
                }
                else if (ClientCredentialStyle == ClientCredentialStyle.PostBody)
                {
                    Parameters.AddRequired(OidcConstants.TokenRequest.ClientId, ClientId);
                    Parameters.AddOptional(OidcConstants.TokenRequest.ClientSecret, ClientSecret);
                }
                else
                {
                    throw new InvalidOperationException("Unsupported client credential style");
                }
            }

            if (ClientAssertion != null)
            {
                if (ClientAssertion.Type != null && (ClientAssertion.Value != null || ClientAssertion.ValueProvider != null))
                {
                    Parameters.AddOptional(OidcConstants.TokenRequest.ClientAssertionType, ClientAssertion.Type);

                    var assertionValue = ClientAssertion.ValueProvider != null
                        ? ClientAssertion.ValueProvider()
                        : ClientAssertion.Value;

                    Parameters.AddOptional(OidcConstants.TokenRequest.ClientAssertion, assertionValue);
                }
            }

            if (Address.IsPresent())
            {
                RequestUri = new Uri(Address);
            }

            if (Parameters.Any())
            {
                Content = new FormUrlEncodedContent(Parameters);
            }
        }
Exemplo n.º 2
0
    /// <summary>
    /// Applies protocol parameters to HTTP request
    /// </summary>
    public void Prepare()
    {
        if (ClientId.IsPresent())
        {
            if (ClientCredentialStyle == ClientCredentialStyle.AuthorizationHeader)
            {
                if (AuthorizationHeaderStyle == BasicAuthenticationHeaderStyle.Rfc6749)
                {
                    this.SetBasicAuthenticationOAuth(ClientId, ClientSecret ?? "");
                }
                else if (AuthorizationHeaderStyle == BasicAuthenticationHeaderStyle.Rfc2617)
                {
                    this.SetBasicAuthentication(ClientId, ClientSecret ?? "");
                }
                else
                {
                    throw new InvalidOperationException("Unsupported basic authentication header style");
                }
            }
            else if (ClientCredentialStyle == ClientCredentialStyle.PostBody)
            {
                Parameters.AddRequired(OidcConstants.TokenRequest.ClientId, ClientId);
                Parameters.AddOptional(OidcConstants.TokenRequest.ClientSecret, ClientSecret);
            }
            else
            {
                throw new InvalidOperationException("Unsupported client credential style");
            }
        }

        if (ClientAssertion?.Type != null && ClientAssertion.Value != null)
        {
            if (ClientCredentialStyle == ClientCredentialStyle.AuthorizationHeader && !string.IsNullOrEmpty(ClientId))
            {
                throw new InvalidOperationException(
                          "CredentialStyle.AuthorizationHeader and client assertions are not compatible");
            }

            Parameters.AddOptional(OidcConstants.TokenRequest.ClientAssertionType, ClientAssertion.Type);
            Parameters.AddOptional(OidcConstants.TokenRequest.ClientAssertion, ClientAssertion.Value);
        }

        if (Address.IsPresent())
        {
            RequestUri = new Uri(Address, UriKind.RelativeOrAbsolute);
        }

        if (Parameters.Any())
        {
            Content = new FormUrlEncodedContent(Parameters);
        }
    }
        /// <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));
            }
        }