public BasicAuthMiddleware(
     RequestDelegate next,
     IOptions <BasicAuthOptions> basicAuthOptions)
 {
     _next             = next;
     _basicAuthOptions = basicAuthOptions.Value;
 }
 public BasicAuthenticationHandler(
     IOptionsMonitor <AuthenticationSchemeOptions> options,
     ILoggerFactory logger,
     UrlEncoder encoder,
     ISystemClock clock,
     IOptions <BasicAuthOptions> basicAuthOptions)
     : base(options, logger, encoder, clock)
 {
     _basicAuthOptions = basicAuthOptions.Value;
 }
        public BaseBasicAuthContext(HttpContext context, BasicAuthOptions options)
            : base(context)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            this.Options = options;
        }
        public static IApplicationBuilder UseBasicAuthentication(
            this IApplicationBuilder app,
            Action <BasicAuthOptions> configureBasicAuthOpts)
        {
            var opts = new BasicAuthOptions();

            configureBasicAuthOpts(opts);

            return(app.UseMiddleware <BasicAuthMiddleware>(opts));
        }
Example #5
0
        /// <summary>
        /// Extension method to use the Basic auth middleware with custom configuration
        /// </summary>
        /// <param name="app"><see cref="IApplicationBuilder"/> that is supplied by Asp.Net</param>
        /// <param name="options">Action to configure the Basic auth options</param>
        /// <returns><see cref="IApplicationBuilder"/> that includes the Basic auth middleware</returns>
        public static IApplicationBuilder UseBasicAuth(this IApplicationBuilder app, Action<BasicAuthOptions> options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            BasicAuthOptions basicAuthOptions = new BasicAuthOptions();
            options(basicAuthOptions);

            return app.UseMiddleware<BasicAuthMiddleware>(Options.Create(basicAuthOptions));
        }
Example #6
0
        /// <summary>
        /// Configures http client to use Basic auth.
        /// </summary>
        /// <param name="client">The http client.</param>
        /// <param name="options">The basic auth options.</param>
        /// <returns>Configured http client with a Basic authentication header.</returns>
        /// <exception cref="ArgumentNullException">client or options is null.</exception>
        public static HttpClient WithBasicAuth(this HttpClient client, BasicAuthOptions options)
        {
            if (client is null)
            {
                throw new ArgumentNullException(nameof(client));
            }

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

            return(client.WithBasicAuth(options.Username, options.Password));
        }
Example #7
0
        /// <summary>
        /// Configures http client to use Basic auth.
        /// </summary>
        /// <param name="client">The http client.</param>
        /// <param name="username">Basic auth username.</param>
        /// <param name="password">Basic auth password.</param>
        /// <returns>Configured http client with a Basic authentication header.</returns>
        /// <exception cref="ArgumentNullException">client or options is null.</exception>
        /// <exception cref="ArgumentException">username or password token is null or whitespace.</exception>
        public static HttpClient WithBasicAuth(this HttpClient client, string username, string password)
        {
            if (client is null)
            {
                throw new ArgumentNullException(nameof(client));
            }

            if (string.IsNullOrWhiteSpace(username))
            {
                throw new ArgumentException(FormatStrings.ValueCanNotBeNull, nameof(username));
            }

            if (string.IsNullOrWhiteSpace(password))
            {
                throw new ArgumentException(FormatStrings.ValueCanNotBeNull, nameof(password));
            }

            var basicAuthCredentials = BasicAuthOptions.GetHeaderValue(username, password);

            return(client.WithFormattedHeader(FormatStrings.Authorization_Basic, basicAuthCredentials));
        }
 public BasicAuthDetectedContext(HttpContext context, BasicAuthOptions options)
     : base(context, options)
 {
 }
Example #9
0
        public BasicFlowAuthorizationFilter(BasicAuthOptions basicAuthOptions)
        {
            var plainTextBytes = Encoding.UTF8.GetBytes($"{basicAuthOptions.Username}:{basicAuthOptions.Password}");

            _encodedUserNamePassword = Convert.ToBase64String(plainTextBytes);
        }
 public BasicAuthChallengeContext(HttpContext context, BasicAuthOptions options)
     : base(context, options)
 {
 }
 public BasicAuthValidatedContext(HttpContext context, BasicAuthOptions options)
     : base(context, options)
 {
 }
Example #12
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure <AzureAdOptions>(options => Configuration.Bind("AzureAd", options));
            services.Configure <BasicAuthOptions>(options => Configuration.Bind("BasicAuth", options));
            ServiceProvider serviceProvider = services.BuildServiceProvider();
            AzureAdOptions  azureAdOptions  = serviceProvider.GetRequiredService <IOptions <AzureAdOptions> >().Value;

            BasicAuthOptions basicAuthOptions = serviceProvider.GetRequiredService <IOptions <BasicAuthOptions> >().Value;
            var authFlow = new AuthFlow();

            authFlow.BasicAuthOptions = basicAuthOptions;
            services.AddSingleton(typeof(AuthFlow), authFlow);

            if (authFlow.UseAdalAuthFlow)
            {
                services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddJwtBearer(options =>
                {
                    options.Audience  = azureAdOptions.ApplicationId;
                    options.Authority = azureAdOptions.PassThroughAuthority;
                    options.TokenValidationParameters = new TokenValidationParameters
                    {
                        ValidateIssuer = false
                    };
                });
            }

            services.AddMvc(config =>
            {
                if (authFlow.UseAdalAuthFlow)
                {
                    // Adding ADAL authentication
                    var policy = new AuthorizationPolicyBuilder()
                                 .RequireAuthenticatedUser()
                                 .Build();
                    config.Filters.Add(new AuthorizeFilter(policy));
                }
                else
                {
                    config.Filters.Add(new BasicFlowAuthorizationFilter(basicAuthOptions));
                }
            }).SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
            .AddJsonOptions(o =>
            {
                o.SerializerSettings.DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Ignore;
                o.SerializerSettings.NullValueHandling    = Newtonsoft.Json.NullValueHandling.Ignore;
            });

            services.AddSwaggerGen(c =>
            {
                //The generated Swagger JSON file will have these properties.
                c.SwaggerDoc("v1", new Info
                {
                    Title   = "SmartHotel.Services.FacilityManagement",
                    Version = "v1",
                });

                //Locate the XML file being generated by ASP.NET...
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);

                //... and tell Swagger to use those XML comments.
                c.IncludeXmlComments(xmlPath);

                if (!authFlow.UseAdalAuthFlow)
                {
                    const string securityDefinitionName = "ApiKeyAuth";
                    c.AddSecurityDefinition(securityDefinitionName, new BasicAuthScheme());
                    c.AddSecurityRequirement(new Dictionary <string, IEnumerable <string> >
                    {
                        { securityDefinitionName, Enumerable.Empty <string>() }
                    });
                    c.OperationFilter <AddRequiredHeaderParameter>();
                }
            });
            services.AddCors();
            services.AddHttpClient();
            services.AddScoped <ITopologyClient, TopologyClient>();
        }