public void Configure_NoOpsWhenConfigurationIsEmpty()
    {
        var expectedKeyPath = Path.Combine(Directory.GetCurrentDirectory(), "./testkey.json");

        try
        {
            // Arrange
            var configuration = new ConfigurationBuilder()
                                .AddInMemoryCollection(new Dictionary <string, string>()
            {
            }).Build();

            var configureSigningCredentials = new ConfigureSigningCredentials(
                configuration,
                new TestLogger <ConfigureSigningCredentials>());

            var options = new ApiAuthorizationOptions();

            // Act
            configureSigningCredentials.Configure(options);

            // Assert
            Assert.NotNull(options);
            Assert.False(File.Exists(expectedKeyPath));
            Assert.Null(options.SigningCredential);
        }
        finally
        {
            if (File.Exists(expectedKeyPath))
            {
                File.Delete(expectedKeyPath);
            }
        }
    }
    public void Configure_LoadsPfxCertificateCredentialFromConfiguration()
    {
        // Arrange
        var configuration = new ConfigurationBuilder()
                            .AddInMemoryCollection(new Dictionary <string, string>()
        {
            ["Type"]     = "File",
            ["FilePath"] = "test.pfx",
            ["Password"] = "******"
        }).Build();

        var configureSigningCredentials = new ConfigureSigningCredentials(
            configuration,
            new TestLogger <ConfigureSigningCredentials>());

        var options = new ApiAuthorizationOptions();

        // Act
        configureSigningCredentials.Configure(options);

        // Assert
        Assert.NotNull(options);
        Assert.NotNull(options.SigningCredential);
        var key = Assert.IsType <X509SecurityKey>(options.SigningCredential.Key);

        Assert.NotNull(key.Certificate);
        Assert.Equal("AC8FDF4BD4C10841BD24DC88D983225D10B43BB2", key.Certificate.Thumbprint);
    }
        public void Configure_should_throw_when_store_location_invalid()
        {
            var configuration = new ConfigurationBuilder().AddInMemoryCollection(new Dictionary <string, string>
            {
                ["Type"] = "Store",
            })
                                .Build();
            var loggerMock = new Mock <ILogger <ConfigureSigningCredentials> >();

            var sut = new ConfigureSigningCredentials(configuration, loggerMock.Object);

            Assert.Throws <InvalidOperationException>(() => sut.Configure(new Options.CredentialsOptions()));
        }
        public void Configure_should_throw_when_credential_file_not_exists()
        {
            var configuration = new ConfigurationBuilder().AddInMemoryCollection(new Dictionary <string, string>
            {
                ["Type"]     = "File",
                ["FilePath"] = "cred.pfx"
            })
                                .Build();
            var loggerMock = new Mock <ILogger <ConfigureSigningCredentials> >();

            var sut = new ConfigureSigningCredentials(configuration, loggerMock.Object);

            Assert.Throws <InvalidOperationException>(() => sut.Configure(new Options.CredentialsOptions()));
        }
    public void Configure_AddsDevelopmentKeyFromConfiguration()
    {
        var expectedKeyPath = Path.Combine(Directory.GetCurrentDirectory(), "./testkey.json");

        try
        {
            // Arrange
            var configuration = new ConfigurationBuilder()
                                .AddInMemoryCollection(new Dictionary <string, string>()
            {
                ["Type"]     = "Development",
                ["FilePath"] = "testkey.json"
            }).Build();

            var configureSigningCredentials = new ConfigureSigningCredentials(
                configuration,
                new TestLogger <ConfigureSigningCredentials>());

            var options = new ApiAuthorizationOptions();

            // Act
            configureSigningCredentials.Configure(options);

            // Assert
            Assert.NotNull(options);
            Assert.True(File.Exists(expectedKeyPath));
            Assert.NotNull(options.SigningCredential);
            Assert.Equal("Development", options.SigningCredential.Kid);
            Assert.IsType <RsaSecurityKey>(options.SigningCredential.Key);
        }
        finally
        {
            if (File.Exists(expectedKeyPath))
            {
                File.Delete(expectedKeyPath);
            }
        }
    }
    public void Configure_LoadsCertificateStoreCertificateCredentialFromConfiguration()
    {
        try
        {
            // Arrange
            var x509Certificate = new X509Certificate2("test.pfx", "aspnetcore", DefaultFlags);
            SetupTestCertificate(x509Certificate);

            var configuration = new ConfigurationBuilder()
                                .AddInMemoryCollection(new Dictionary <string, string>()
            {
                ["Type"]          = "Store",
                ["StoreLocation"] = "CurrentUser",
                ["StoreName"]     = "My",
                ["Name"]          = "CN=Test"
            }).Build();

            var configureSigningCredentials = new ConfigureSigningCredentials(
                configuration,
                new TestLogger <ConfigureSigningCredentials>());

            var options = new ApiAuthorizationOptions();

            // Act
            configureSigningCredentials.Configure(options);

            // Assert
            Assert.NotNull(options);
            Assert.NotNull(options.SigningCredential);
            var key = Assert.IsType <X509SecurityKey>(options.SigningCredential.Key);
            Assert.NotNull(key.Certificate);
            Assert.Equal("AC8FDF4BD4C10841BD24DC88D983225D10B43BB2", key.Certificate.Thumbprint);
        }
        finally
        {
            CleanupTestCertificate();
        }
    }