public BaseOpenIdConnectContext(HttpContext context, OpenIdConnectOptions options)
            : base(context)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            Options = options;
        }
        /// <summary>
        /// Adds the <see cref="OpenIdConnectMiddleware"/> middleware to the specified <see cref="IApplicationBuilder"/>, which enables OpenID Connect authentication capabilities.
        /// </summary>
        /// <param name="app">The <see cref="IApplicationBuilder"/> to add the middleware to.</param>
        /// <param name="options">An <see cref="OpenIdConnectOptions"/> that specifies options for the middleware.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IApplicationBuilder UseOpenIdConnectAuthentication(this IApplicationBuilder app, OpenIdConnectOptions options)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            return app.UseMiddleware<OpenIdConnectMiddleware>(options);
        }
 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;
 }
        /// <summary>
        /// Adds the <see cref="OpenIdConnectMiddleware"/> middleware to the specified <see cref="IApplicationBuilder"/>, which enables OpenID Connect authentication capabilities.
        /// </summary>
        /// <param name="app">The <see cref="IApplicationBuilder"/> to add the middleware to.</param>
        /// <param name="options">An action delegate to configure the provided <see cref="OpenIdConnectOptions"/>.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IApplicationBuilder UseOpenIdConnectAuthentication(this IApplicationBuilder app, Action<OpenIdConnectOptions> configureOptions)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }


            var options = new OpenIdConnectOptions();
            if (configureOptions != null)
            {
                configureOptions(options);
            }
            return app.UseOpenIdConnectAuthentication(options);
        }
 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;
 }
        // Setup an event to check for expected state.
        // The state gets set by the runtime after the 'MessageReceivedContext'
        private static void SetStateOptions(OpenIdConnectOptions options)
        {
            options.AuthenticationScheme = "OpenIdConnectHandlerTest";
            options.ConfigurationManager = TestUtilities.DefaultOpenIdConnectConfigurationManager;
            options.ClientId = Guid.NewGuid().ToString();
            options.StateDataFormat = new AuthenticationPropertiesFormaterKeyValue();
            options.SignInScheme = "Cookies";
            options.Events = new OpenIdConnectEvents()
            {
                OnTokenResponseReceived = context =>
                {
                    context.HandleResponse();
                    if (context.ProtocolMessage.State == null && !context.ProtocolMessage.Parameters.ContainsKey(ExpectedStateParameter))
                        return Task.FromResult<object>(null);

                    if (context.ProtocolMessage.State == null || !context.ProtocolMessage.Parameters.ContainsKey(ExpectedStateParameter))
                        Assert.True(false, "(context.ProtocolMessage.State=!= null || !context.ProtocolMessage.Parameters.ContainsKey(expectedState)");

                    Assert.Equal(context.ProtocolMessage.State, context.ProtocolMessage.Parameters[ExpectedStateParameter]);
                    return Task.FromResult<object>(null);
                }
            };
        }
 public MessageReceivedContext(HttpContext context, OpenIdConnectOptions options)
     : base(context, options)
 {
 }
 public AuthenticationValidatedContext(HttpContext context, OpenIdConnectOptions options)
     : base(context, options)
 {
 }
 public UserInformationReceivedContext(HttpContext context, OpenIdConnectOptions options)
     : base(context, options)
 {
 }
 private static void DefaultChallengeOptions(OpenIdConnectOptions options)
 {
     options.AuthenticationScheme = "OpenIdConnectHandlerTest";
     options.AutomaticChallenge = true;
     options.ClientId = Guid.NewGuid().ToString();
     options.ConfigurationManager = TestUtilities.DefaultOpenIdConnectConfigurationManager;
     options.StateDataFormat = new AuthenticationPropertiesFormaterKeyValue();
 }
        private void SetOptions(OpenIdConnectOptions options, List<string> parameters, ExpectedQueryValues queryValues, ISecureDataFormat<AuthenticationProperties> secureDataFormat = null)
        {
            foreach (var param in parameters)
            {
                if (param.Equals(OpenIdConnectParameterNames.ClientId))
                    options.ClientId = queryValues.ClientId;
                else if (param.Equals(OpenIdConnectParameterNames.Resource))
                    options.Resource = queryValues.Resource;
                else if (param.Equals(OpenIdConnectParameterNames.Scope)) {
                    options.Scope.Clear();

                    foreach (var scope in queryValues.Scope.Split(' ')) {
                        options.Scope.Add(scope);
                    }
                }
            }

            options.Authority = queryValues.Authority;
            options.Configuration = queryValues.Configuration;
            options.StateDataFormat = secureDataFormat ?? new AuthenticationPropertiesFormaterKeyValue();
        }
Esempio n. 12
0
 public RedirectContext(HttpContext context, OpenIdConnectOptions options)
     : base(context, options)
 {
 }
 public AuthenticationValidatedContext(HttpContext context, OpenIdConnectOptions options)
     : base(context, options)
 {
 }
 private static void SetProtocolMessageOptions(OpenIdConnectOptions options)
 {
     var mockOpenIdConnectMessage = new Mock<OpenIdConnectMessage>();
     mockOpenIdConnectMessage.Setup(m => m.CreateAuthenticationRequestUrl()).Returns(ExpectedAuthorizeRequest);
     mockOpenIdConnectMessage.Setup(m => m.CreateLogoutRequestUrl()).Returns(ExpectedLogoutRequest);
     options.AutomaticAuthentication = true;
     options.Events = new OpenIdConnectEvents()
     {
         OnRedirectToAuthenticationEndpoint = (context) =>
         {
             context.ProtocolMessage = mockOpenIdConnectMessage.Object;
             return Task.FromResult<object>(null);
         },
         OnRedirectToEndSessionEndpoint = (context) =>
         {
             context.ProtocolMessage = mockOpenIdConnectMessage.Object;
             return Task.FromResult<object>(null);
         }
     };
 }
 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();
         }
     );
 }
 public UserInformationReceivedContext(HttpContext context, OpenIdConnectOptions options)
     : base(context, options)
 {
 }
 public AuthorizationResponseReceivedContext(HttpContext context, OpenIdConnectOptions options)
     : base(context, options)
 {
 }
 /// <summary>
 /// Creates a <see cref="TokenResponseReceivedContext"/>
 /// </summary>
 public TokenResponseReceivedContext(HttpContext context, OpenIdConnectOptions options)
     : base(context, options)
 {
 }
 private static void SetProtocolMessageOptions(OpenIdConnectOptions options)
 {
     var fakeOpenIdRequestMessage = new FakeOpenIdConnectMessage(ExpectedAuthorizeRequest, ExpectedLogoutRequest);
     options.AutomaticChallenge = true;
     options.Events = new OpenIdConnectEvents()
     {
         OnRedirectToAuthenticationEndpoint = (context) =>
         {
             context.ProtocolMessage = fakeOpenIdRequestMessage;
             return Task.FromResult(0);
         },
         OnRedirectToEndSessionEndpoint = (context) =>
         {
             context.ProtocolMessage = fakeOpenIdRequestMessage;
             return Task.FromResult(0);
         }
     };
 }
        private static void GenerateCorrelationId(HttpContext context, OpenIdConnectOptions options, AuthenticationProperties properties)
        {
            if (properties == null)
            {
                throw new ArgumentNullException(nameof(properties));
            }

            var correlationKey = OpenIdConnectDefaults.CookieStatePrefix;

            var nonceBytes = new byte[32];
            CryptoRandom.GetBytes(nonceBytes);
            var correlationId = Base64UrlTextEncoder.Encode(nonceBytes);

            var cookieOptions = new CookieOptions
            {
                HttpOnly = true,
                Secure = context.Request.IsHttps,
                Expires = DateTime.UtcNow + options.ProtocolValidator.NonceLifetime
            };

            properties.Items[correlationKey] = correlationId;

            context.Response.Cookies.Append(correlationKey + correlationId, NonceProperty, cookieOptions);
        }
Esempio n. 21
0
 public MessageReceivedContext(HttpContext context, OpenIdConnectOptions options)
     : base(context, options)
 {
 }
 /// <summary>
 /// Creates a <see cref="AuthorizationCodeReceivedContext"/>
 /// </summary>
 public AuthorizationCodeReceivedContext(HttpContext context, OpenIdConnectOptions options)
     : base(context, options)
 { 
 }
 /// <summary>
 /// Creates a <see cref="TokenResponseReceivedContext"/>
 /// </summary>
 public TokenResponseReceivedContext(HttpContext context, OpenIdConnectOptions options)
     : base(context, options)
 {
 }