Skip to content

Commit 252bb44

Browse files
User errors when signing out on the dev environment
Add string replacement for the sign out redirect to ensure that it it is https. The headers will be forwarded correctly within this ticket https://dfedigital.atlassian.net/browse/SAB-345.
1 parent 1c6dfaf commit 252bb44

2 files changed

Lines changed: 102 additions & 14 deletions

File tree

src/SchoolAccount.Web.Mvc/Authentication/Extensions/ServiceCollectionExtensions.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using Microsoft.AspNetCore.Authentication;
21
using Microsoft.AspNetCore.Authentication.Cookies;
32
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
43
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
@@ -50,25 +49,26 @@ IConfigurationManager configuration
5049

5150
options.Events = new OpenIdConnectEvents
5251
{
53-
OnRedirectToIdentityProviderForSignOut = async context =>
54-
{
55-
context.HttpContext.Session.Clear();
56-
await Task.CompletedTask;
57-
},
58-
59-
OnTicketReceived = OpenIdConnectEventHandlers.OnTicketReceived,
60-
6152
// within ACA a container runs on http, though available as https publicly
6253
// this causes the OIDC redirect_url to have the http protocol, rather than https
6354
// DSI does not allow http redirect URLS. The following corrects the URL
64-
OnRedirectToIdentityProvider = async n =>
55+
OnRedirectToIdentityProvider = async context =>
6556
{
66-
n.ProtocolMessage.RedirectUri = n.ProtocolMessage.RedirectUri.Replace(
67-
"http://",
68-
"https://"
69-
);
57+
context.ProtocolMessage.RedirectUri =
58+
context.ProtocolMessage.RedirectUri.Replace("http://", "https://");
7059
await Task.CompletedTask;
7160
},
61+
OnRedirectToIdentityProviderForSignOut = async context =>
62+
{
63+
context.HttpContext.Session.Clear();
64+
context.ProtocolMessage.PostLogoutRedirectUri =
65+
context.ProtocolMessage.PostLogoutRedirectUri.Replace(
66+
"http://",
67+
"https://"
68+
);
69+
await Task.CompletedTask;
70+
},
71+
OnTicketReceived = OpenIdConnectEventHandlers.OnTicketReceived,
7272
};
7373
});
7474

tests/SchoolAccount.Web.Mvc.UnitTests/Extensions/ServiceCollection/ServiceCollectionAddDsiAuthenticationExtensionTests.cs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,100 @@ public async Task Ensure_that_OnRedirectToIdentityProviderForSignOut_clears_sess
105105
scheme,
106106
oidcOptions,
107107
new AuthenticationProperties()
108+
)
109+
{
110+
ProtocolMessage = new OpenIdConnectMessage
111+
{
112+
PostLogoutRedirectUri = "https://test-oidc.signin",
113+
},
114+
};
115+
116+
// Act
117+
await oidcOptions.Events.OnRedirectToIdentityProviderForSignOut(redirectContext);
118+
119+
// Assert
120+
mockSession.Received(1).Clear();
121+
}
122+
123+
[Fact]
124+
public async Task Ensure_that_OnRedirectToIdentityProviderForSignOut_logout_redirect_uri_is_https()
125+
{
126+
// Arrange
127+
var services = new Microsoft.Extensions.DependencyInjection.ServiceCollection();
128+
using var configuration = BuildConfiguration();
129+
services.AddDsiAuthentication(configuration);
130+
var provider = services.BuildServiceProvider();
131+
132+
var oidcOptions = provider
133+
.GetRequiredService<IOptionsMonitor<OpenIdConnectOptions>>()
134+
.Get(OpenIdConnectDefaults.AuthenticationScheme);
135+
136+
var mockSession = Substitute.For<ISession>();
137+
var httpContext = new DefaultHttpContext { Session = mockSession };
138+
139+
var scheme = new AuthenticationScheme(
140+
OpenIdConnectDefaults.AuthenticationScheme,
141+
OpenIdConnectDefaults.AuthenticationScheme,
142+
typeof(OpenIdConnectHandler)
108143
);
109144

145+
var redirectContext = new RedirectContext(
146+
httpContext,
147+
scheme,
148+
oidcOptions,
149+
new AuthenticationProperties()
150+
)
151+
{
152+
ProtocolMessage = new OpenIdConnectMessage
153+
{
154+
PostLogoutRedirectUri = "http://test-oidc.signin",
155+
},
156+
};
157+
110158
// Act
111159
await oidcOptions.Events.OnRedirectToIdentityProviderForSignOut(redirectContext);
112160

113161
// Assert
114162
mockSession.Received(1).Clear();
163+
redirectContext.ProtocolMessage.PostLogoutRedirectUri.ShouldBe("https://test-oidc.signin");
164+
}
165+
166+
[Fact]
167+
public async Task Ensure_that_OnRedirectToIdentityProvider_redirect_uri_is_https()
168+
{
169+
// Arrange
170+
var services = new Microsoft.Extensions.DependencyInjection.ServiceCollection();
171+
using var configuration = BuildConfiguration();
172+
services.AddDsiAuthentication(configuration);
173+
var provider = services.BuildServiceProvider();
174+
175+
var oidcOptions = provider
176+
.GetRequiredService<IOptionsMonitor<OpenIdConnectOptions>>()
177+
.Get(OpenIdConnectDefaults.AuthenticationScheme);
178+
179+
var mockSession = Substitute.For<ISession>();
180+
var httpContext = new DefaultHttpContext { Session = mockSession };
181+
182+
var scheme = new AuthenticationScheme(
183+
OpenIdConnectDefaults.AuthenticationScheme,
184+
OpenIdConnectDefaults.AuthenticationScheme,
185+
typeof(OpenIdConnectHandler)
186+
);
187+
188+
var redirectContext = new RedirectContext(
189+
httpContext,
190+
scheme,
191+
oidcOptions,
192+
new AuthenticationProperties()
193+
)
194+
{
195+
ProtocolMessage = new OpenIdConnectMessage { RedirectUri = "http://test-oidc.signin" },
196+
};
197+
198+
// Act
199+
await oidcOptions.Events.OnRedirectToIdentityProvider(redirectContext);
200+
201+
// Assert
202+
redirectContext.ProtocolMessage.RedirectUri.ShouldBe("https://test-oidc.signin");
115203
}
116204
}

0 commit comments

Comments
 (0)