Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Commit 0c815da

Browse files
committed
#903 Ensure redirect uris can be generated
1 parent 834718d commit 0c815da

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

src/Microsoft.AspNetCore.Authentication.OpenIdConnect/OpenIdConnectHandler.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,12 @@ protected override async Task HandleSignOutAsync(SignOutContext context)
208208

209209
message.State = Options.StateDataFormat.Protect(properties);
210210

211+
if (string.IsNullOrEmpty(message.IssuerAddress))
212+
{
213+
throw new InvalidOperationException(
214+
"Cannot redirect to the end session endpoint, the configuration may be missing or invalid.");
215+
}
216+
211217
if (Options.AuthenticationMethod == OpenIdConnectRedirectBehavior.RedirectGet)
212218
{
213219
var redirectUri = message.CreateLogoutRequestUrl();
@@ -356,6 +362,12 @@ protected override async Task<bool> HandleUnauthorizedAsync(ChallengeContext con
356362

357363
message.State = Options.StateDataFormat.Protect(properties);
358364

365+
if (string.IsNullOrEmpty(message.IssuerAddress))
366+
{
367+
throw new InvalidOperationException(
368+
"Cannot redirect to the authorization endpoint, the configuration may be missing or invalid.");
369+
}
370+
359371
if (Options.AuthenticationMethod == OpenIdConnectRedirectBehavior.RedirectGet)
360372
{
361373
var redirectUri = message.CreateAuthenticationRequestUrl();

test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/OpenIdConnectChallengeTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ public async Task OnRedirectToIdentityProviderEventCanReplaceMessage()
222222
{
223223
var newMessage = new MockOpenIdConnectMessage
224224
{
225+
IssuerAddress = "http://example.com/",
225226
TestAuthorizeEndpoint = $"http://example.com/{Guid.NewGuid()}/oauth2/signin"
226227
};
227228

@@ -322,5 +323,16 @@ public async Task ChallengeSetsNonceAndStateCookies()
322323
Assert.StartsWith(".AspNetCore.Correlation.OpenIdConnect.", secondCookie);
323324
Assert.Contains("expires", secondCookie);
324325
}
326+
327+
[Fact]
328+
public async Task Challenge_WithEmptyConfig_Fails()
329+
{
330+
var settings = new TestSettings(
331+
opt => opt.Configuration = new OpenIdConnectConfiguration());
332+
333+
var server = settings.CreateTestServer();
334+
var exception = await Assert.ThrowsAsync<InvalidOperationException>(() => server.SendAsync(ChallengeEndpoint));
335+
Assert.Equal("Cannot redirect to the authorization endpoint, the configuration may be missing or invalid.", exception.Message);
336+
}
325337
}
326338
}

test/Microsoft.AspNetCore.Authentication.Test/OpenIdConnect/OpenIdConnectMiddlewareTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,17 @@ public async Task SignOutWith_Specific_RedirectUri_From_Authentication_Properite
135135
Assert.Equal("http://www.example.com/specific_redirect_uri", properties.RedirectUri, true);
136136
}
137137

138+
[Fact]
139+
public async Task SignOut_WithMissingConfig_Throws()
140+
{
141+
var setting = new TestSettings(opt => opt.Configuration = new OpenIdConnectConfiguration());
142+
143+
var server = setting.CreateTestServer();
144+
145+
var exception = await Assert.ThrowsAsync<InvalidOperationException>(() => server.SendAsync(DefaultHost + TestServerBuilder.Signout));
146+
Assert.Equal("Cannot redirect to the end session endpoint, the configuration may be missing or invalid.", exception.Message);
147+
}
148+
138149
// Test Cases for calculating the expiration time of cookie from cookie name
139150
[Fact]
140151
public void NonceCookieExpirationTime()

0 commit comments

Comments
 (0)