Skip to content

Commit 11a5681

Browse files
Extract OpenIdConnectEvent logic into own handler
1 parent 072e57f commit 11a5681

4 files changed

Lines changed: 77 additions & 12 deletions

File tree

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
using Microsoft.AspNetCore.Authentication;
12
using Microsoft.AspNetCore.Authentication.Cookies;
23
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
34
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
45
using SchoolAccount.SharedKernel;
6+
using SchoolAccount.Web.Mvc.Authentication.Handlers;
57
using SchoolAccount.Web.Mvc.Authentication.Models;
68
using static SchoolAccount.Web.Mvc.Authentication.ClaimConstants;
79

@@ -54,16 +56,7 @@ IConfigurationManager configuration
5456
await Task.CompletedTask;
5557
},
5658

57-
OnTicketReceived = context =>
58-
{
59-
var org = context.Principal?.GetOrganisation();
60-
if (org is null)
61-
{
62-
context.Response.Redirect("/error/403");
63-
context.HandleResponse();
64-
}
65-
return Task.CompletedTask;
66-
},
59+
OnTicketReceived = OpenIdConnectEventHandlers.OnTicketReceived,
6760

6861
// within ACA a container runs on http, though available as https publicly
6962
// this causes the OIDC redirect_url to have the http protocol, rather than https
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Microsoft.AspNetCore.Authentication;
2+
using SchoolAccount.Web.Mvc.Authentication.Extensions;
3+
4+
namespace SchoolAccount.Web.Mvc.Authentication.Handlers;
5+
6+
public static class OpenIdConnectEventHandlers
7+
{
8+
public static Task OnTicketReceived(TicketReceivedContext context)
9+
{
10+
var org = context.Principal?.GetOrganisation();
11+
if (org is null)
12+
{
13+
context.Response.Redirect("/error/403");
14+
context.HandleResponse();
15+
}
16+
17+
return Task.CompletedTask;
18+
}
19+
}

src/SchoolAccount.Web.Mvc/Authentication/UserContext.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Security.Principal;
1+
using System.Globalization;
2+
using System.Security.Principal;
23
using SchoolAccount.SharedKernel;
34
using SchoolAccount.Web.Mvc.Authentication.Extensions;
45

@@ -15,7 +16,10 @@ public UserContext(IHttpContextAccessor contextAccessor)
1516
GivenName = user?.FindFirst(ClaimConstants.GivenName)?.Value;
1617
Surname = user?.FindFirst(ClaimConstants.FamilyName)?.Value;
1718
EmailAddress = user?.FindFirst(ClaimConstants.Email)?.Value;
18-
OrganisationName = user?.GetOrganisation()?.Name;
19+
OrganisationName = OrganisationName =
20+
Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(
21+
(user?.GetOrganisation()?.Name ?? string.Empty).ToLower(CultureInfo.CurrentCulture)
22+
);
1923
}
2024

2125
public string? GivenName { get; }
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System.Security.Claims;
2+
using Microsoft.AspNetCore.Authentication;
3+
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
4+
using SchoolAccount.Web.Mvc.Authentication.Handlers;
5+
6+
namespace SchoolAccount.IntegrationTests.Authentication.Handlers;
7+
8+
public class OpenIdConnectEventHandlersTests
9+
{
10+
[Fact]
11+
public async Task OnTicketReceived_NoOrganisation_Redirects403()
12+
{
13+
var ticket = new AuthenticationTicket(new ClaimsPrincipal(new ClaimsIdentity()), "oidc");
14+
15+
var context = new TicketReceivedContext(
16+
new DefaultHttpContext(),
17+
new AuthenticationScheme("oidc", null, typeof(IAuthenticationHandler)),
18+
new OpenIdConnectOptions(),
19+
ticket
20+
);
21+
22+
await OpenIdConnectEventHandlers.OnTicketReceived(context);
23+
24+
Assert.Equal(302, context.HttpContext.Response.StatusCode);
25+
Assert.Equal("/error/403", context.HttpContext.Response.Headers.Location);
26+
Assert.True(context.Result?.Handled);
27+
}
28+
29+
[Fact]
30+
public async Task OnTicketReceived_WithOrganisation_DoesNotRedirect()
31+
{
32+
var identity = new ClaimsIdentity();
33+
identity.AddClaim(new Claim("organisation", """{"NAME":"Test School"}"""));
34+
35+
var ticket = new AuthenticationTicket(new ClaimsPrincipal(identity), "oidc");
36+
37+
var context = new TicketReceivedContext(
38+
new DefaultHttpContext(),
39+
new AuthenticationScheme("oidc", null, typeof(IAuthenticationHandler)),
40+
new OpenIdConnectOptions(),
41+
ticket
42+
);
43+
44+
await OpenIdConnectEventHandlers.OnTicketReceived(context);
45+
46+
Assert.NotEqual(302, context.HttpContext.Response.StatusCode);
47+
Assert.Null(context.Result);
48+
}
49+
}

0 commit comments

Comments
 (0)