public void ClientRemovedOnNullSecret()
        {
            var store = new SecretStore();

            store.Assign("client", "A");

            var secret = store.ClientSecret("client");

            Assert.That(secret, Is.EqualTo("A"));

            store.Assign("client", null);

            secret = store.ClientSecret("client");

            Assert.That(secret, Is.Null);
        }
Ejemplo n.º 2
0
        protected virtual WebApplicationFactory <Startup> CreateFactory()
        {
            var f = new WebApplicationFactory <Startup>()
                    .WithWebHostBuilder(builder => builder.UseContentRoot("."));
            //{
            //    Environment = TestEnvironment
            //};

            var secretRepository = new SecretStore();

            secretRepository.Assign("1234", "ABCD");

            var mrb        = new HmacMessageRepresentationBuilder();
            var calculator = new HmacSignatureCalculator();

            HmacClient = new HmacClient
            {
                ClientId = "1234"
            };
            var hmacHandler = new HmacClientHandler(HmacClient);
            var requestContentMd5Handler = new RequestContentMd5Handler();
            var hmacSigningHandler       = new HmacSigningHandler(secretRepository, mrb, calculator);

            // Inject all the handlers in the correct order
            Client = f.CreateDefaultClient(hmacHandler, requestContentMd5Handler, hmacSigningHandler);

            //Startup = Program.Startup;

            return(f);
        }
        public void ClientPresent()
        {
            var store = new SecretStore();

            store.Assign("client", "A");

            var secret = store.ClientSecret("client");

            Assert.That(secret, Is.EqualTo("A"));
        }
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // See https://stackoverflow.com/questions/45695382/how-do-i-setup-multiple-auth-schemes-in-asp-net-core-2-0
            // and https://github.com/aspnet/Security/issues/1469
            services.AddAuthentication(sharedOptions =>
            {
                sharedOptions.DefaultScheme          = "smart";
                sharedOptions.DefaultChallengeScheme = "smart";
            })
            .AddPolicyScheme("smart", "JWT or HMAC", options =>
            {
                options.ForwardDefaultSelector = context =>
                {
                    var authHeader = context.Request.Headers["Authorization"].ToString();
                    if (authHeader?.StartsWith("Bearer") == true)
                    {
                        return(JwtBearerDefaults.AuthenticationScheme);
                    }

                    return(HmacAuthentication.AuthenticationScheme);
                };
            })
            .AddJwtBearer(options =>
            {
                options.Authority = "https://foo.com/";
                options.Audience  = "aud";
            })
            .AddHmacAuthentication(options =>
            {
            });

            services.AddHmacAuthenticator();

            services.AddDistributedMemoryCache();
            services.AddSingleton <ISignatureCache, DistributedSignatureCache>();

            var secretRepository = new SecretStore();

            secretRepository.Assign("1234", "ABCD");
            services.AddSingleton <ISecretRepository>(secretRepository);
            services.AddTransient <IRequestClaimsProvider>(x => new ClientIdRequestClaimsProvider("name"));

            services.AddMvc()
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        }