-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OIDC test app & e2e tests (#1256)
- Loading branch information
Showing
22 changed files
with
303 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...src/TeachingRecordSystem.AuthorizeAccess/Infrastructure/Filters/NotFoundResourceFilter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
|
||
namespace TeachingRecordSystem.AuthorizeAccess.Infrastructure.Filters; | ||
|
||
public class NotFoundResourceFilter : IResourceFilter | ||
{ | ||
public void OnResourceExecuted(ResourceExecutedContext context) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public void OnResourceExecuting(ResourceExecutingContext context) | ||
{ | ||
context.Result = new NotFoundResult(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
TeachingRecordSystem/src/TeachingRecordSystem.AuthorizeAccess/Pages/OidcTest/SignOut.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
@page "/oidc-test/sign-out" | ||
@addTagHelper *, Joonasw.AspNetCore.SecurityHeaders | ||
@model TeachingRecordSystem.AuthorizeAccess.Pages.OidcTest.SignOutModel | ||
@{ | ||
} | ||
|
||
<form asp-page="SignOut" method="post"> | ||
<govuk-button type="submit">Sign out</govuk-button> | ||
</form> | ||
|
||
<script asp-add-nonce="true">document.forms[0].submit();</script> |
27 changes: 27 additions & 0 deletions
27
...ingRecordSystem/src/TeachingRecordSystem.AuthorizeAccess/Pages/OidcTest/SignOut.cshtml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.AspNetCore.Authentication.Cookies; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
|
||
namespace TeachingRecordSystem.AuthorizeAccess.Pages.OidcTest; | ||
|
||
[Authorize(AuthenticationSchemes = TestAppConfiguration.AuthenticationSchemeName)] | ||
public class SignOutModel : PageModel | ||
{ | ||
public void OnGet() | ||
{ | ||
} | ||
|
||
public async Task<IActionResult> OnPost() | ||
{ | ||
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); | ||
|
||
return SignOut( | ||
new AuthenticationProperties() | ||
{ | ||
RedirectUri = Url.Page("Start") | ||
}, | ||
TestAppConfiguration.AuthenticationSchemeName); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
TeachingRecordSystem/src/TeachingRecordSystem.AuthorizeAccess/Pages/OidcTest/SignedIn.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
@page "/oidc-test/signed-in" | ||
@using System.Text.Json | ||
@using TeachingRecordSystem.AuthorizeAccess.Pages.OidcTest | ||
@addTagHelper *, Joonasw.AspNetCore.SecurityHeaders | ||
@model SignedInModel | ||
@{ | ||
var claimsJson = JsonSerializer.Serialize( | ||
User.Claims.ToDictionary(c => c.Type, c => c.Value), | ||
new JsonSerializerOptions() { WriteIndented = true }); | ||
} | ||
|
||
@section Head { | ||
<style type="text/css" asp-add-nonce="true"> | ||
.claims-json { | ||
font-family: monospace !important; | ||
white-space: nowrap; | ||
height: 260px; | ||
} | ||
</style> | ||
} | ||
|
||
<p class="govuk-body"> | ||
<govuk-textarea name="Claims" textarea-class="claims-json"> | ||
<govuk-textarea-label>Claims</govuk-textarea-label> | ||
<govuk-textarea-value>@claimsJson</govuk-textarea-value> | ||
</govuk-textarea> | ||
</p> |
12 changes: 12 additions & 0 deletions
12
...ngRecordSystem/src/TeachingRecordSystem.AuthorizeAccess/Pages/OidcTest/SignedIn.cshtml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
|
||
namespace TeachingRecordSystem.AuthorizeAccess.Pages.OidcTest; | ||
|
||
[Authorize(AuthenticationSchemes = TestAppConfiguration.AuthenticationSchemeName)] | ||
public class SignedInModel : PageModel | ||
{ | ||
public void OnGet() | ||
{ | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
TeachingRecordSystem/src/TeachingRecordSystem.AuthorizeAccess/Pages/OidcTest/Start.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
@page "/oidc-test" | ||
@model TeachingRecordSystem.AuthorizeAccess.Pages.OidcTest.StartModel | ||
@{ | ||
} | ||
|
||
<form asp-page="OidcTest" method="post"> | ||
<govuk-button type="submit" is-start-button="true">Start</govuk-button> | ||
</form> |
19 changes: 19 additions & 0 deletions
19
TeachingRecordSystem/src/TeachingRecordSystem.AuthorizeAccess/Pages/OidcTest/Start.cshtml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
|
||
namespace TeachingRecordSystem.AuthorizeAccess.Pages.OidcTest; | ||
|
||
public class StartModel : PageModel | ||
{ | ||
public void OnGet() | ||
{ | ||
} | ||
|
||
public IActionResult OnPost() => Challenge( | ||
new AuthenticationProperties() | ||
{ | ||
RedirectUri = Url.Page("SignedIn") | ||
}, | ||
TestAppConfiguration.AuthenticationSchemeName); | ||
} |
29 changes: 29 additions & 0 deletions
29
TeachingRecordSystem/src/TeachingRecordSystem.AuthorizeAccess/Pages/OidcTest/_Layout.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
@{ | ||
Layout = "../Shared/_Layout"; | ||
|
||
ViewBag.ServiceName = "OIDC Sample"; | ||
} | ||
|
||
@section Head { | ||
@RenderSection("Head", required: false) | ||
} | ||
|
||
@if (User.Identity?.IsAuthenticated == true) | ||
{ | ||
@section Nav { | ||
<nav aria-label="Menu" class="govuk-header__navigation"> | ||
<button type="button" class="govuk-header__menu-button govuk-js-header-toggle" aria-controls="navigation" hidden> | ||
Menu | ||
</button> | ||
<ul id="navigation" class="govuk-header__navigation-list"> | ||
<li class="govuk-header__navigation-item govuk-header__navigation-item--active"> | ||
<a class="govuk-header__link" asp-page="SignOut"> | ||
Sign out | ||
</a> | ||
</li> | ||
</ul> | ||
</nav> | ||
} | ||
} | ||
|
||
@RenderBody() |
3 changes: 3 additions & 0 deletions
3
...ingRecordSystem/src/TeachingRecordSystem.AuthorizeAccess/Pages/OidcTest/_ViewStart.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@{ | ||
Layout = "./_Layout"; | ||
} |
6 changes: 6 additions & 0 deletions
6
TeachingRecordSystem/src/TeachingRecordSystem.AuthorizeAccess/Pages/Shared/_Layout.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
TeachingRecordSystem/src/TeachingRecordSystem.AuthorizeAccess/TestAppConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using Microsoft.AspNetCore.Authentication.Cookies; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
using TeachingRecordSystem.AuthorizeAccess.Infrastructure.Filters; | ||
using static IdentityModel.OidcConstants; | ||
|
||
namespace TeachingRecordSystem.AuthorizeAccess; | ||
|
||
public static class TestAppConfiguration | ||
{ | ||
public const string AuthenticationSchemeName = "OidcTest"; | ||
public const string ClientId = "test-app"; | ||
public const string ClientSecret = "Devel0pm3ntSecr4t"; | ||
public const string RedirectUriPath = "/test-app/callback"; | ||
public const string PostLogoutRedirectUriPath = "/test-app/logout-callback"; | ||
|
||
public static WebApplicationBuilder AddTestApp(this WebApplicationBuilder builder) | ||
{ | ||
if (builder.Environment.IsDevelopment() || builder.Environment.IsEndToEndTests()) | ||
{ | ||
builder.Services.AddAuthentication() | ||
.AddCookie() | ||
.AddOpenIdConnect(AuthenticationSchemeName, options => | ||
{ | ||
options.Authority = "https://localhost:7236"; | ||
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; | ||
options.ClientId = ClientId; | ||
options.ClientSecret = ClientSecret; | ||
options.CallbackPath = RedirectUriPath; | ||
options.SignedOutCallbackPath = PostLogoutRedirectUriPath; | ||
options.ResponseMode = ResponseModes.Query; | ||
options.ResponseType = ResponseTypes.Code; | ||
options.MapInboundClaims = false; | ||
options.SaveTokens = true; | ||
options.Scope.Clear(); | ||
options.Scope.Add("openid"); | ||
options.Scope.Add("email"); | ||
options.Scope.Add("profile"); | ||
}); | ||
} | ||
else | ||
{ | ||
builder.Services.Configure<RazorPagesOptions>(options => | ||
options.Conventions.AddFolderApplicationModelConvention( | ||
"/OidcTest", model => model.Filters.Add(new NotFoundResourceFilter()))); | ||
} | ||
|
||
return builder; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.