internal static async Task <IdentityModel.Client.IdentityModelExtensions.TokenResponse> RequestTokenAsync(this HttpMessageInvoker client, ProtocolRequest request, CancellationToken?cancellationToken)
        {
            request.Prepare();
            request.Method = HttpMethod.Post;

            HttpResponseMessage response;

            try
            {
                response = await client.SendAsync(request, cancellationToken.HasValue?cancellationToken.Value : new CancellationToken()).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                return(ProtocolResponse.FromException <IdentityModel.Client.IdentityModelExtensions.TokenResponse>(ex));
            }

            return(await ProtocolResponse.FromHttpResponseAsync <IdentityModel.Client.IdentityModelExtensions.TokenResponse>(response).ConfigureAwait(false));
        }
        internal static async Task <TokenResponse> RequestTokenAsync(this HttpMessageInvoker client, ProtocolRequest request, CancellationToken cancellationToken = default)
        {
            request.Prepare();
            request.Method = HttpMethod.Post;

            HttpResponseMessage response;

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

            return(await ProtocolResponse.FromHttpResponseAsync <TokenResponse>(response).ConfigureAwait());
        }
예제 #3
0
        /// <summary>
        /// Sends a userinfo 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 <DeviceAuthorizationResponse> RequestDeviceAuthorizationAsync(this HttpMessageInvoker client, DeviceAuthorizationRequest request, CancellationToken cancellationToken = default)
        {
            var clone = request.Clone();

            clone.Parameters.AddOptional(OidcConstants.AuthorizeRequest.Scope, request.Scope);
            clone.Method = HttpMethod.Post;
            clone.Prepare();

            HttpResponseMessage response;

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

            return(await ProtocolResponse.FromHttpResponseAsync <DeviceAuthorizationResponse>(response).ConfigureAwait());
        }
        /// <summary>
        /// Sends an OAuth token introspection 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 <TokenIntrospectionResponse> IntrospectTokenAsync(this HttpMessageInvoker client, TokenIntrospectionRequest 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(false);
            }
            catch (Exception ex)
            {
                return(ProtocolResponse.FromException <TokenIntrospectionResponse>(ex));
            }

            return(await ProtocolResponse.FromHttpResponseAsync <TokenIntrospectionResponse>(response).ConfigureAwait(false));
        }
        /// <summary>
        /// Sends a discovery document 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 <DiscoveryDocumentResponse> GetDiscoveryDocumentAsync(this HttpMessageInvoker client, DiscoveryDocumentRequest request, CancellationToken cancellationToken = default)
        {
            string address;

            if (request.Address.IsPresent())
            {
                address = request.Address;
            }
            else if (client is HttpClient)
            {
                address = ((HttpClient)client).BaseAddress.AbsoluteUri;
            }
            else
            {
                throw new ArgumentException("An address is required.");
            }

            var parsed    = DiscoveryEndpoint.ParseUrl(address);
            var authority = parsed.Authority;
            var url       = parsed.Url;

            if (request.Policy.Authority.IsMissing())
            {
                request.Policy.Authority = authority;
            }

            string jwkUrl = "";

            if (!DiscoveryEndpoint.IsSecureScheme(new Uri(url), request.Policy))
            {
                return(ProtocolResponse.FromException <DiscoveryDocumentResponse>(new InvalidOperationException("HTTPS required"), $"Error connecting to {url}. HTTPS required."));
            }

            try
            {
                var clone = request.Clone();

                clone.Method = HttpMethod.Get;
                clone.Prepare();

                clone.RequestUri = new Uri(url);

                var response = await client.SendAsync(clone, cancellationToken).ConfigureAwait(false);

                string responseContent = null;

                if (response.Content != null)
                {
                    responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
                }

                if (!response.IsSuccessStatusCode)
                {
                    return(await ProtocolResponse.FromHttpResponseAsync <DiscoveryDocumentResponse>(response, $"Error connecting to {url}: {response.ReasonPhrase}"));
                }

                var disco = await ProtocolResponse.FromHttpResponseAsync <DiscoveryDocumentResponse>(response, request.Policy).ConfigureAwait(false);

                if (disco.IsError)
                {
                    return(disco);
                }

                try
                {
                    jwkUrl = disco.JwksUri;
                    if (jwkUrl != null)
                    {
                        var jwkClone = request.Clone <JsonWebKeySetRequest>();
                        jwkClone.Method  = HttpMethod.Get;
                        jwkClone.Address = jwkUrl;
                        jwkClone.Prepare();

                        var jwkResponse = await client.GetJsonWebKeySetAsync(jwkClone, cancellationToken).ConfigureAwait(false);

                        if (jwkResponse.IsError)
                        {
                            return(await ProtocolResponse.FromHttpResponseAsync <DiscoveryDocumentResponse>(jwkResponse.HttpResponse, $"Error connecting to {jwkUrl}: {jwkResponse.HttpErrorReason}").ConfigureAwait(false));
                        }

                        disco.KeySet = jwkResponse.KeySet;
                    }

                    return(disco);
                }
                catch (Exception ex)
                {
                    return(ProtocolResponse.FromException <DiscoveryDocumentResponse>(ex, $"Error connecting to {jwkUrl}. {ex.Message}."));
                }
            }
            catch (Exception ex)
            {
                return(ProtocolResponse.FromException <DiscoveryDocumentResponse>(ex, $"Error connecting to {url}. {ex.Message}."));
            }
        }