Exemplo n.º 1
0
        /// <summary>
        /// Creates the introspection client.
        /// </summary>
        /// <param name="innerHttpMessageHandler">The inner HTTP message handler.</param>
        /// <param name="headerStyle">The header style.</param>
        /// <returns>The introspection client.</returns>
        public async Task <IntrospectionClient> CreateIntrospectionClient(
            HttpMessageHandler innerHttpMessageHandler,
            BasicAuthenticationHeaderStyle headerStyle = BasicAuthenticationHeaderStyle.Rfc6749)
        {
            var disco = await this.GetDiscovery();

            return(new IntrospectionClient(
                       endpoint: disco.IntrospectionEndpoint,
                       clientId: this.ClientId,
                       clientSecret: this.ClientSecret,
                       innerHttpMessageHandler: innerHttpMessageHandler,
                       headerStyle: headerStyle));
        }
 internal static void PopulateClientCredentials(
     string clientId,
     string clientSecret,
     ClientCredentialStyle credentialStyle,
     BasicAuthenticationHeaderStyle headerStyle,
     HttpRequestMessage httpRequest,
     IDictionary <string, string> parameters)
 {
     if (clientId.IsPresent())
     {
         if (credentialStyle == ClientCredentialStyle.AuthorizationHeader)
         {
             if (headerStyle == BasicAuthenticationHeaderStyle.Rfc6749)
             {
                 httpRequest.SetBasicAuthenticationOAuth(clientId, clientSecret ?? "");
             }
             else if (headerStyle == BasicAuthenticationHeaderStyle.Rfc2617)
             {
                 httpRequest.SetBasicAuthentication(clientId, clientSecret ?? "");
             }
             else
             {
                 throw new InvalidOperationException("Unsupported basic authentication header style");
             }
         }
         else if (credentialStyle == ClientCredentialStyle.PostBody)
         {
             parameters.Add(OidcConstants.TokenRequest.ClientId, clientId);
             parameters.AddOptional(OidcConstants.TokenRequest.ClientSecret, clientSecret);
         }
         else
         {
             throw new InvalidOperationException("Unsupported client credential style");
         }
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="IntrospectionClient" /> class.
        /// </summary>
        /// <param name="endpoint">The endpoint.</param>
        /// <param name="clientId">The client identifier.</param>
        /// <param name="clientSecret">The client secret.</param>
        /// <param name="innerHttpMessageHandler">The inner HTTP message handler.</param>
        /// <param name="headerStyle">The header style.</param>
        /// <exception cref="ArgumentNullException">endpoint</exception>
        /// <exception cref="ArgumentException">Invalid header style - headerStyle</exception>
        public IntrospectionClient(string endpoint, string clientId = "", string clientSecret = "", HttpMessageHandler innerHttpMessageHandler = null, BasicAuthenticationHeaderStyle headerStyle = BasicAuthenticationHeaderStyle.Rfc6749)
        {
            if (string.IsNullOrWhiteSpace(endpoint))
            {
                throw new ArgumentNullException(nameof(endpoint));
            }
            if (innerHttpMessageHandler == null)
            {
                innerHttpMessageHandler = new HttpClientHandler();
            }

            Client = new HttpClient(innerHttpMessageHandler)
            {
                BaseAddress = new Uri(endpoint)
            };

            Client.DefaultRequestHeaders.Accept.Clear();
            Client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));

            if (clientId.IsPresent() && clientSecret.IsPresent())
            {
                if (headerStyle == BasicAuthenticationHeaderStyle.Rfc6749)
                {
                    Client.SetBasicAuthenticationOAuth(clientId, clientSecret);
                }
                else if (headerStyle == BasicAuthenticationHeaderStyle.Rfc2617)
                {
                    Client.SetBasicAuthentication(clientId, clientSecret);
                }
                else
                {
                    throw new ArgumentException("Invalid header style", nameof(headerStyle));
                }
            }
            else if (!string.IsNullOrWhiteSpace(clientId))
            {
                ClientId = clientId;
            }
        }