コード例 #1
0
        protected override async Task <OpenIdConnectMessage> RedeemAuthorizationCodeAsync(OpenIdConnectMessage tokenEndpointRequest)
        {
            //Logger.RedeemingCodeForTokens();

            OpenIdConnectHandler       idConnectHandler   = this;
            OpenIdConnectConfiguration configurationAsync = await idConnectHandler.Options.ConfigurationManager.GetConfigurationAsync(CancellationToken.None);

            var requestMessage = new HttpRequestMessage(HttpMethod.Post, configurationAsync.TokenEndpoint);

            //add header ipv body
            var basicAuthHeader = "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes($"{Options.ClientId}:{Options.ClientSecret}"));

            requestMessage.Headers.Add("Authorization", basicAuthHeader);

            var parameters = tokenEndpointRequest.Parameters;

            parameters.Remove("client_id");
            parameters.Remove("client_secret");

            requestMessage.Content = new FormUrlEncodedContent(parameters);

            var responseMessage = await Backchannel.SendAsync(requestMessage);

            var contentMediaType = responseMessage.Content.Headers.ContentType?.MediaType;

            if (string.IsNullOrEmpty(contentMediaType))
            {
                Logger.LogDebug($"Unexpected token response format. Status Code: {(int)responseMessage.StatusCode}. Content-Type header is missing.");
            }
            else if (!string.Equals(contentMediaType, "application/json", StringComparison.OrdinalIgnoreCase))
            {
                Logger.LogDebug($"Unexpected token response format. Status Code: {(int)responseMessage.StatusCode}. Content-Type {responseMessage.Content.Headers.ContentType}.");
            }

            // Error handling:
            // 1. If the response body can't be parsed as json, throws.
            // 2. If the response's status code is not in 2XX range, throw OpenIdConnectProtocolException. If the body is correct parsed,
            //    pass the error information from body to the exception.
            OpenIdConnectMessage message;

            try
            {
                var responseContent = await responseMessage.Content.ReadAsStringAsync();

                message = new OpenIdConnectMessage(responseContent);
            }
            catch (Exception ex)
            {
                throw new OpenIdConnectProtocolException($"Failed to parse token response body as JSON. Status Code: {(int)responseMessage.StatusCode}. Content-Type: {responseMessage.Content.Headers.ContentType}", ex);
            }

            if (!responseMessage.IsSuccessStatusCode)
            {
                //throw CreateOpenIdConnectProtocolException(message, responseMessage);
            }

            return(message);
        }
 public OpenIdConnectMiddlewareForTestingAuthenticate(
     RequestDelegate next,
     IDataProtectionProvider dataProtectionProvider,
     ILoggerFactory loggerFactory,
     IUrlEncoder encoder,
     IServiceProvider services,
     IOptions<SharedAuthenticationOptions> sharedOptions,
     OpenIdConnectOptions options,
     IHtmlEncoder htmlEncoder,
     OpenIdConnectHandler handler = null
     )
 : base(next, dataProtectionProvider, loggerFactory, encoder, services, sharedOptions, options, htmlEncoder)
 {
     _handler = handler;
 }
コード例 #3
0
 public OpenIdConnectMiddlewareForTestingAuthenticate(
     RequestDelegate next,
     IDataProtectionProvider dataProtectionProvider,
     ILoggerFactory loggerFactory,
     IUrlEncoder encoder,
     IServiceProvider services,
     IOptions <SharedAuthenticationOptions> sharedOptions,
     OpenIdConnectOptions options,
     IHtmlEncoder htmlEncoder,
     OpenIdConnectHandler handler = null
     )
     : base(next, dataProtectionProvider, loggerFactory, encoder, services, sharedOptions, options, htmlEncoder)
 {
     _handler = handler;
 }
コード例 #4
0
        private async Task <bool> EnableOpenIdIdentityProvider(
            AuthenticationProviderResponse authProvider,
            HttpContext context)
        {
            HttpClient httpClient;
            var        option = ExtractOpenIdConfiguration(authProvider, _dataProtectionProvider, out httpClient);

            if (option == null)
            {
                return(false);
            }

            var handler = new OpenIdConnectHandler(httpClient, _htmlEncoder);
            await handler.InitializeAsync(option, context, _logger, UrlEncoder.Default);

            return(await handler.HandleRequestAsync());
        }
 public OpenIdConnectMiddlewareForTestingAuthenticate(
     RequestDelegate next,            
     IDataProtectionProvider dataProtectionProvider,
     ILoggerFactory loggerFactory,
     IUrlEncoder encoder,
     IServiceProvider services,
     IOptions<SharedAuthenticationOptions> sharedOptions,
     OpenIdConnectOptions options,
     OpenIdConnectHandler handler = null
     )
 : base(next, dataProtectionProvider, loggerFactory, encoder, services, sharedOptions, options)
 {
     _handler = handler;
     var customFactory = loggerFactory as InMemoryLoggerFactory;
     if (customFactory != null)
         Logger = customFactory.Logger;
 }
コード例 #6
0
        public OpenIdConnectMiddlewareForTestingAuthenticate(
            RequestDelegate next,
            IDataProtectionProvider dataProtectionProvider,
            ILoggerFactory loggerFactory,
            IUrlEncoder encoder,
            IServiceProvider services,
            IOptions <SharedAuthenticationOptions> sharedOptions,
            OpenIdConnectOptions options,
            OpenIdConnectHandler handler = null
            )
            : base(next, dataProtectionProvider, loggerFactory, encoder, services, sharedOptions, options)
        {
            _handler = handler;
            var customFactory = loggerFactory as InMemoryLoggerFactory;

            if (customFactory != null)
            {
                Logger = customFactory.Logger;
            }
        }
コード例 #7
0
 public IdentityController(OpenIdConnectHandler handler)
 {
     this.handler = handler;
 }
コード例 #8
0
 private static TestServer CreateServer(Action <OpenIdConnectOptions> configureOptions, IUrlEncoder encoder, OpenIdConnectHandler handler = null)
 {
     return(TestServer.Create(
                app =>
     {
         var options = new OpenIdConnectOptions();
         configureOptions(options);
         app.UseMiddleware <OpenIdConnectMiddlewareForTestingAuthenticate>(options, encoder, handler);
         app.Use(async(context, next) =>
         {
             await next();
         });
     },
                services =>
     {
         services.AddWebEncoders();
         services.AddDataProtection();
     }
                ));
 }
コード例 #9
0
 private static TestServer CreateServer(Action<OpenIdConnectOptions> configureOptions, IUrlEncoder encoder, OpenIdConnectHandler handler = null)
 {
     return TestServer.Create(
         app =>
         {
             var options = new OpenIdConnectOptions();
             configureOptions(options);
             app.UseMiddleware<OpenIdConnectMiddlewareForTestingAuthenticate>(options, encoder, handler);
             app.Use(async (context, next) =>
             {
                 await next();
             });
         },
         services =>
         {
             services.AddWebEncoders();
             services.AddDataProtection();
         }
     );
 }