Add argument validation
This commit is contained in:
parent
6294badd97
commit
962a74c488
|
|
@ -82,6 +82,11 @@ namespace Microsoft.AspNetCore.Authentication.OpenIdConnect
|
||||||
throw new ArgumentNullException(nameof(htmlEncoder));
|
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)
|
if (!Options.CallbackPath.HasValue)
|
||||||
{
|
{
|
||||||
throw new ArgumentException("Options.CallbackPath must be provided.");
|
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);
|
Options.StringDataFormat = new SecureDataFormat<string>(new StringSerializer(), dataProtector);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (Options.Events == null)
|
if (Options.Events == null)
|
||||||
{
|
{
|
||||||
Options.Events = new OpenIdConnectEvents();
|
Options.Events = new OpenIdConnectEvents();
|
||||||
|
|
@ -164,6 +168,12 @@ namespace Microsoft.AspNetCore.Authentication.OpenIdConnect
|
||||||
new HttpDocumentRetriever(Backchannel) { RequireHttps = Options.RequireHttpsMetadata });
|
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; }
|
protected HttpClient Backchannel { get; private set; }
|
||||||
|
|
|
||||||
|
|
@ -161,6 +161,8 @@ namespace Microsoft.AspNetCore.Authentication.Tests.OpenIdConnect
|
||||||
return Task.FromResult(0);
|
return Task.FromResult(0);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
options.ClientId = "Test Id";
|
||||||
|
options.Configuration = TestUtilities.DefaultOpenIdConnectConfiguration;
|
||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -550,5 +552,66 @@ namespace Microsoft.AspNetCore.Authentication.Tests.OpenIdConnect
|
||||||
|
|
||||||
return nonceTime;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue