Exemple #1
0
        /// <summary>
        /// Introspects a token
        /// </summary>
        /// <param name="token"></param>
        /// <param name="tokenTypeHint"></param>
        /// <param name="parameters"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task <TokenIntrospectionResponse> Introspect(string token, string tokenTypeHint = null, IDictionary <string, string> parameters = null, CancellationToken cancellationToken = default)
        {
            var request = new TokenIntrospectionRequest
            {
                Token         = token,
                TokenTypeHint = tokenTypeHint
            };

            ApplyRequestParameters(request, parameters);

            return(_client().IntrospectTokenAsync(request, cancellationToken));
        }
Exemple #2
0
        /// <summary>
        /// Sets request parameters from the options.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="parameters">The parameters.</param>
        internal void ApplyRequestParameters(TokenIntrospectionRequest request, IDictionary <string, string> parameters)
        {
            request.Address                  = _options.Address;
            request.ClientId                 = _options.ClientId;
            request.ClientSecret             = _options.ClientSecret;
            request.ClientAssertion          = _options.ClientAssertion;
            request.ClientCredentialStyle    = _options.ClientCredentialStyle;
            request.AuthorizationHeaderStyle = _options.AuthorizationHeaderStyle;
            request.Parameters               = _options.Parameters;

            if (parameters != null)
            {
                foreach (var parameter in parameters)
                {
                    request.Parameters.Add(parameter);
                }
            }
        }
Exemple #3
0
        /// <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 <IntrospectionResponse> IntrospectTokenAsync(this HttpMessageInvoker client, TokenIntrospectionRequest 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(new IntrospectionResponse(ex));
            }
            if (response.IsSuccessStatusCode)
            {
                return(new IntrospectionResponse(await response.Content.ReadAsStringAsync().ConfigureAwait(false)));
            }
            else
            {
                return(new IntrospectionResponse(response.StatusCode, response.ReasonPhrase));
            }
        }
        /// <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));
        }