Add argument validation

This commit is contained in:
BrennanConroy 2016-05-16 08:09:00 -07:00
parent 6294badd97
commit 962a74c488
2 changed files with 74 additions and 1 deletions

View File

@ -82,6 +82,11 @@ namespace Microsoft.AspNetCore.Authentication.OpenIdConnect
throw new ArgumentNullException(nameof(htmlEncoder));
}
if (string.IsNullOrEmpty(Options.ClientId))
{
throw new ArgumentException("Options.ClientId must be provided", nameof(Options.ClientId));
}
if (!Options.CallbackPath.HasValue)
{
throw new ArgumentException("Options.CallbackPath must be provided.");
@ -120,7 +125,6 @@ namespace Microsoft.AspNetCore.Authentication.OpenIdConnect
Options.StringDataFormat = new SecureDataFormat<string>(new StringSerializer(), dataProtector);
}
if (Options.Events == null)
{
Options.Events = new OpenIdConnectEvents();
@ -164,6 +168,12 @@ namespace Microsoft.AspNetCore.Authentication.OpenIdConnect
new HttpDocumentRetriever(Backchannel) { RequireHttps = Options.RequireHttpsMetadata });
}
}
if (Options.ConfigurationManager == null)
{
throw new InvalidOperationException($"Provide {nameof(Options.Authority)}, {nameof(Options.MetadataAddress)}, "
+ $"{nameof(Options.Configuration)}, or {nameof(Options.ConfigurationManager)} to {nameof(OpenIdConnectOptions)}");
}
}
protected HttpClient Backchannel { get; private set; }

View File

@ -161,6 +161,8 @@ namespace Microsoft.AspNetCore.Authentication.Tests.OpenIdConnect
return Task.FromResult(0);
}
};
options.ClientId = "Test Id";
options.Configuration = TestUtilities.DefaultOpenIdConnectConfiguration;
return options;
}
@ -550,5 +552,66 @@ namespace Microsoft.AspNetCore.Authentication.Tests.OpenIdConnect
return nonceTime;
}
[Fact]
public void ThrowsWithNoClientId()
{
var builder = new WebHostBuilder()
.Configure(app =>
{
app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
SignInScheme = "TestScheme",
Authority = DefaultAuthority,
Configuration = TestUtilities.DefaultOpenIdConnectConfiguration,
AuthenticationMethod = OpenIdConnectRedirectBehavior.FormPost
});
}).ConfigureServices(services =>
{
services.AddAuthentication();
});
try
{
var server = new TestServer(builder);
}
catch (ArgumentException e)
{
Assert.Equal("ClientId", e.ParamName);
return;
}
Assert.True(false);
}
[Fact]
public void ThrowsWithNoConfigurationValues()
{
var builder = new WebHostBuilder()
.Configure(app =>
{
app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
SignInScheme = "TestScheme",
ClientId = "Test Id",
AuthenticationMethod = OpenIdConnectRedirectBehavior.FormPost
});
}).ConfigureServices(services =>
{
services.AddAuthentication();
});
try
{
var server = new TestServer(builder);
}
catch (InvalidOperationException e)
{
Assert.Equal("Provide Authority, MetadataAddress, Configuration, or ConfigurationManager to OpenIdConnectOptions", e.Message);
return;
}
Assert.True(false);
}
}
}