-
Notifications
You must be signed in to change notification settings - Fork 0
feat(session): serve session expiry from VIPER 2, not the legacy CFM #304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rlorenzo
wants to merge
1
commit into
fix/session-refresh-pathbase
from
feature/session-timeout-endpoint
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or 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,33 @@ | ||
| using System.Reflection; | ||
| using Viper.Classes; | ||
| using Viper.Controllers; | ||
|
|
||
| namespace Viper.test.Controllers | ||
| { | ||
| /// <summary> | ||
| /// The session expiry poll must never extend the session, or it would never time out. Both | ||
| /// controller bases in this app write a fresh expiry on every action, so the guarantee rests | ||
| /// entirely on this controller not inheriting either of them. Pin that. | ||
| /// </summary> | ||
| public class SessionTimeoutControllerTests | ||
| { | ||
| [Fact] | ||
| public void Controller_DoesNotInheritASessionExtendingBase() | ||
| { | ||
| Assert.False(typeof(ApiController).IsAssignableFrom(typeof(SessionTimeoutController))); | ||
| Assert.False(typeof(AreaController).IsAssignableFrom(typeof(SessionTimeoutController))); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Controller_DoesNotCarryTheSessionUpdateFilter() | ||
| { | ||
| Assert.Empty(typeof(SessionTimeoutController) | ||
| .GetCustomAttributes(typeof(ApiSessionUpdateFilterAttribute), inherit: true)); | ||
|
|
||
| foreach (MethodInfo action in typeof(SessionTimeoutController).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) | ||
| { | ||
| Assert.Empty(action.GetCustomAttributes(typeof(ApiSessionUpdateFilterAttribute), inherit: true)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 @@ | ||
| namespace Viper.Classes | ||
| { | ||
| /// <summary> | ||
| /// Session expiry contract polled by the session timeout dialog. | ||
| /// </summary> | ||
| public class SessionTimeoutStatus | ||
| { | ||
| public string SessionTimeoutDateTime { get; set; } = string.Empty; | ||
|
|
||
| public int SecondsUntilTimeout { get; set; } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,94 @@ | ||
| using System.Globalization; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.Data.SqlClient; | ||
| using NLog; | ||
| using Viper.Classes; | ||
| using Viper.Classes.SQLContext; | ||
| using Viper.Classes.Utilities; | ||
|
|
||
| namespace Viper.Controllers | ||
| { | ||
| /// <summary> | ||
| /// Read-only session expiry poll for the session timeout dialog. Takes no parameters: the user | ||
| /// comes from the auth cookie. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Inherits ControllerBase, not Viper.Classes.ApiController or Viper.Classes.AreaController. | ||
| /// Both of those extend the session on every action, which would stop the session ever expiring, | ||
| /// and ApiController also wraps responses in an ApiResponse envelope the dialog cannot read. The | ||
| /// [ApiController] attribute below is Microsoft.AspNetCore.Mvc.ApiControllerAttribute and | ||
| /// carries none of that behaviour. | ||
| /// | ||
| /// No [Authorize]: attribute-routed actions do not pick up RequireAuthorization from the | ||
| /// conventional routes, and the action takes no parameters and reads identity from the cookie, | ||
| /// so an anonymous caller learns only that it has no session. The dialog then offers a log in | ||
| /// rather than retrying a call that would 401. | ||
| /// </remarks> | ||
| [ApiController] | ||
|
rlorenzo marked this conversation as resolved.
|
||
| [Route("/api/sessionTimeout")] | ||
| [ResponseCache(NoStore = true, Location = ResponseCacheLocation.None)] | ||
| public class SessionTimeoutController : ControllerBase | ||
|
rlorenzo marked this conversation as resolved.
|
||
| { | ||
| // Ten minutes rather than zero, so a database blip cannot strand the user behind a warning | ||
| // dialog they cannot dismiss. | ||
| private const int SecondsOnError = 600; | ||
|
|
||
| private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); | ||
|
|
||
| private readonly VIPERContext _viperContext; | ||
|
|
||
| public SessionTimeoutController(VIPERContext viperContext) | ||
| { | ||
| _viperContext = viperContext; | ||
| } | ||
|
|
||
| [HttpGet] | ||
| public ActionResult<SessionTimeoutStatus> GetSessionTimeout() | ||
| { | ||
| try | ||
| { | ||
| Models.VIPER.SessionTimeout? record = SessionTimeoutService.GetSessionTimeout(_viperContext); | ||
|
rlorenzo marked this conversation as resolved.
|
||
| if (record != null) | ||
| { | ||
| return Status(record.SessionTimeoutDateTime, | ||
| (int)(record.SessionTimeoutDateTime - DateTime.Now).TotalSeconds); | ||
| } | ||
|
|
||
| // Authenticated but untracked. Only AreaController, ApiSessionUpdateFilter and | ||
| // RefreshSession write the row, so a page served by a plain Controller leaves a | ||
| // valid session with no row. Grant a full window rather than report it expired. | ||
| return User.Identity?.IsAuthenticated == true | ||
| ? Status(DateTime.Now.AddSeconds(SessionTimeoutService.SessionTimeoutSeconds), | ||
| SessionTimeoutService.SessionTimeoutSeconds) | ||
| : Status(DateTime.Now, 0); | ||
| } | ||
| catch (SqlException ex) | ||
| { | ||
| return CouldNotRead(ex); | ||
| } | ||
| catch (InvalidOperationException ex) | ||
| { | ||
| return CouldNotRead(ex); | ||
| } | ||
| } | ||
|
|
||
| private static SessionTimeoutStatus CouldNotRead(Exception ex) | ||
| { | ||
| Logger.Error(ex, "Could not read session timeout"); | ||
| return Status(DateTime.Now.AddSeconds(SecondsOnError), SecondsOnError); | ||
| } | ||
|
rlorenzo marked this conversation as resolved.
|
||
|
|
||
| // Carry the offset. The column is a bare datetime written from DateTime.Now, so it is local | ||
| // wall-clock; without the offset a client in another timezone reads it as its own and shows | ||
| // the wrong expiry time. | ||
| private static SessionTimeoutStatus Status(DateTime sessionTimeout, int secondsUntilTimeout) | ||
| { | ||
| DateTimeOffset local = new(DateTime.SpecifyKind(sessionTimeout, DateTimeKind.Local)); | ||
| return new SessionTimeoutStatus | ||
| { | ||
| SessionTimeoutDateTime = local.ToString("yyyy-MM-dd'T'HH:mm:sszzz", CultureInfo.InvariantCulture), | ||
| SecondsUntilTimeout = secondsUntilTimeout | ||
| }; | ||
| } | ||
| } | ||
| } | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.