-
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.
- Loading branch information
Showing
10 changed files
with
136 additions
and
5 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
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
25 changes: 25 additions & 0 deletions
25
TeachingRecordSystem/src/TeachingRecordSystem.AuthorizeAccess/Pages/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,25 @@ | ||
@page "/connect/logout" | ||
@using Microsoft.Extensions.Primitives | ||
@addTagHelper *, Joonasw.AspNetCore.SecurityHeaders | ||
@model TeachingRecordSystem.AuthorizeAccess.Pages.SignOutModel | ||
@{ | ||
ViewBag.Title = "Sign out" + (Model.ServiceName is not null ? $" of {Model.ServiceName}" : ""); | ||
ViewBag.ServiceName = Model.ServiceName; | ||
} | ||
|
||
<form asp-page="SignOut" method="post"> | ||
<div class="govuk-grid-row"> | ||
<div class="govuk-grid-column-two-thirds-from-desktop"> | ||
<h1 class="govuk-heading-l">@ViewBag.Title</h1> | ||
|
||
@foreach (var parameter in HttpContext.Request.HasFormContentType ? (IEnumerable<KeyValuePair<string, StringValues>>)HttpContext.Request.Form : HttpContext.Request.Query) | ||
{ | ||
<input type="hidden" name="@parameter.Key" value="@parameter.Value" /> | ||
} | ||
|
||
<govuk-button type="submit">Sign out</govuk-button> | ||
</div> | ||
</div> | ||
</form> | ||
|
||
<script asp-add-nonce="true">document.forms[0].submit();</script> |
72 changes: 72 additions & 0 deletions
72
TeachingRecordSystem/src/TeachingRecordSystem.AuthorizeAccess/Pages/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,72 @@ | ||
using System.Security.Claims; | ||
using Microsoft.AspNetCore; | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
using Microsoft.IdentityModel.Protocols.OpenIdConnect; | ||
using OpenIddict.Abstractions; | ||
using OpenIddict.Server.AspNetCore; | ||
using TeachingRecordSystem.Core.DataStore.Postgres; | ||
using TeachingRecordSystem.Core.DataStore.Postgres.Models; | ||
|
||
namespace TeachingRecordSystem.AuthorizeAccess.Pages; | ||
|
||
public class SignOutModel(TrsDbContext dbContext) : PageModel | ||
{ | ||
private OpenIddictRequest? _request; | ||
private AuthenticateResult? _authenticateResult; | ||
private ApplicationUser? _client; | ||
|
||
public string? ServiceName => _client?.Name; | ||
|
||
public void OnGet() | ||
{ | ||
} | ||
|
||
public async Task<IActionResult> OnPost() | ||
{ | ||
// We need to sign out with One Login and then complete the OIDC sign out request. | ||
// We do it by calling SignOutAsync with OpenIddict first, capturing the Location header from its redirect | ||
// then redirecting to OneLogin with that URL as the RedirectUri. | ||
|
||
await HttpContext.SignOutAsync( | ||
OpenIddictServerAspNetCoreDefaults.AuthenticationScheme, | ||
new AuthenticationProperties() | ||
{ | ||
RedirectUri = "/" | ||
}); | ||
|
||
var authenticationProperties = new AuthenticationProperties() | ||
{ | ||
RedirectUri = HttpContext.Response.Headers.Location | ||
}; | ||
var oneLoginIdToken = _authenticateResult!.Principal!.FindFirstValue(ClaimTypes.OneLoginIdToken)!; | ||
authenticationProperties.SetParameter(OpenIdConnectParameterNames.IdToken, oneLoginIdToken); | ||
|
||
return SignOut(authenticationProperties, _client!.OneLoginAuthenticationSchemeName!); | ||
} | ||
|
||
public async override Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next) | ||
{ | ||
// Although the spec allows for logout requests without an id_token_hint, we require one so we can | ||
// a) extract the One Login ID token and; | ||
// b) know which authentication scheme to sign out with. | ||
|
||
_request = HttpContext.GetOpenIddictServerRequest() ?? | ||
throw new InvalidOperationException("The OpenID Connect request cannot be retrieved."); | ||
|
||
if (_request.IdTokenHint is null) | ||
{ | ||
context.Result = BadRequest(); | ||
return; | ||
} | ||
|
||
_authenticateResult = await HttpContext.AuthenticateAsync(OpenIddictServerAspNetCoreDefaults.AuthenticationScheme); | ||
|
||
string clientId = _authenticateResult.Principal!.GetAudiences().Single(); | ||
_client = await dbContext.ApplicationUsers.SingleAsync(u => u.ClientId == clientId); | ||
|
||
await base.OnPageHandlerExecutionAsync(context, next); | ||
} | ||
} |
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