From 926e0a718e10c08640b79aeb4fc4522339b843d4 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Sun, 25 Feb 2024 14:20:36 +0000 Subject: [PATCH] refactor: move Admin to own area; not sub Blog --- OliverBooth/Pages/Admin/Index.cshtml | 52 ++++++- OliverBooth/Pages/Blog/Admin/Index.cshtml | 9 -- OliverBooth/Pages/Blog/Admin/Index.cshtml.cs | 81 ---------- OliverBooth/Pages/Blog/Admin/Login.cshtml | 22 --- OliverBooth/Pages/Blog/Admin/Login.cshtml.cs | 31 ---- OliverBooth/Pages/Shared/_AdminLayout.cshtml | 154 +++++++++++++++++++ 6 files changed, 205 insertions(+), 144 deletions(-) delete mode 100644 OliverBooth/Pages/Blog/Admin/Index.cshtml delete mode 100644 OliverBooth/Pages/Blog/Admin/Index.cshtml.cs delete mode 100644 OliverBooth/Pages/Blog/Admin/Login.cshtml delete mode 100644 OliverBooth/Pages/Blog/Admin/Login.cshtml.cs create mode 100644 OliverBooth/Pages/Shared/_AdminLayout.cshtml diff --git a/OliverBooth/Pages/Admin/Index.cshtml b/OliverBooth/Pages/Admin/Index.cshtml index 128ae22..4e61f00 100644 --- a/OliverBooth/Pages/Admin/Index.cshtml +++ b/OliverBooth/Pages/Admin/Index.cshtml @@ -1,8 +1,58 @@ @page +@using System.Reflection @model OliverBooth.Pages.Admin.Index @{ ViewData["Title"] = "Admin"; + Layout = "Shared/_AdminLayout"; } -

Hello @Model.CurrentUser.DisplayName!

\ No newline at end of file +
+
+
+
+
+
+
+ + Site Version +
+
@(Assembly.GetAssembly(typeof(Program))?.GetCustomAttribute()?.InformationalVersion)
+
+
+
+
+
+ +
+
+
+
+
+
+ + CLR Version +
+
@(Environment.Version)
+
+
+
+
+
+ +
+
+
+
+
+
+ + Host Version +
+
@(Environment.OSVersion)
+
+
+
+
+
+
\ No newline at end of file diff --git a/OliverBooth/Pages/Blog/Admin/Index.cshtml b/OliverBooth/Pages/Blog/Admin/Index.cshtml deleted file mode 100644 index a34a35a..0000000 --- a/OliverBooth/Pages/Blog/Admin/Index.cshtml +++ /dev/null @@ -1,9 +0,0 @@ -@page -@model OliverBooth.Pages.Blog.Admin.Index - -@{ - ViewData["Title"] = "Admin"; -} - -

Hello @(Model.CurrentUser.DisplayName)!

-Logout \ No newline at end of file diff --git a/OliverBooth/Pages/Blog/Admin/Index.cshtml.cs b/OliverBooth/Pages/Blog/Admin/Index.cshtml.cs deleted file mode 100644 index 900d698..0000000 --- a/OliverBooth/Pages/Blog/Admin/Index.cshtml.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System.Net; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.RazorPages; -using OliverBooth.Data.Web; -using OliverBooth.Services; -using ISession = OliverBooth.Data.Blog.ISession; - -namespace OliverBooth.Pages.Blog.Admin; - -public class Index : PageModel -{ - private readonly IBlogUserService _userService; - private readonly ISessionService _sessionService; - - public Index(IBlogUserService userService, ISessionService sessionService) - { - _userService = userService; - _sessionService = sessionService; - } - - public IUser CurrentUser { get; private set; } = null!; - - public IActionResult OnGet() - { - IPAddress? remoteIpAddress = Request.HttpContext.Connection.RemoteIpAddress; - if (remoteIpAddress is null) - { - return RedirectToPage("login"); - } - - if (!Request.Cookies.TryGetValue("sid", out string? sessionIdCookie)) - { - return RedirectToPage("login"); - } - - Span bytes = stackalloc byte[16]; - if (!Convert.TryFromBase64Chars(sessionIdCookie, bytes, out int bytesWritten) || bytesWritten < 16) - { - Response.Cookies.Delete("sid"); - return RedirectToPage("login"); - } - - var sessionId = new Guid(bytes); - if (!_sessionService.TryGetSession(sessionId, out ISession? session)) - { - Response.Cookies.Delete("sid"); - return RedirectToPage("login"); - } - - if (session.Expires <= DateTimeOffset.UtcNow) - { - _sessionService.DeleteSession(session); - Response.Cookies.Delete("sid"); - return RedirectToPage("login"); - } - - Span remoteAddressBytes = stackalloc byte[16]; - Span sessionAddressBytes = stackalloc byte[16]; - if (!remoteIpAddress.TryWriteBytes(remoteAddressBytes, out _) || - !session.IpAddress.TryWriteBytes(sessionAddressBytes, out _)) - { - Response.Cookies.Delete("sid"); - return RedirectToPage("login"); - } - - if (!remoteAddressBytes.SequenceEqual(sessionAddressBytes)) - { - Response.Cookies.Delete("sid"); - return RedirectToPage("login"); - } - - if (!_userService.TryGetUser(session.UserId, out IUser? user)) - { - Response.Cookies.Delete("sid"); - return RedirectToPage("login"); - } - - CurrentUser = user; - return Page(); - } -} diff --git a/OliverBooth/Pages/Blog/Admin/Login.cshtml b/OliverBooth/Pages/Blog/Admin/Login.cshtml deleted file mode 100644 index fd5e551..0000000 --- a/OliverBooth/Pages/Blog/Admin/Login.cshtml +++ /dev/null @@ -1,22 +0,0 @@ -@page -@model OliverBooth.Pages.Blog.Admin.Login - -@{ - ViewData["Title"] = "Admin"; -} - -
-
-
- - -
- -
- - -
- - -
-
\ No newline at end of file diff --git a/OliverBooth/Pages/Blog/Admin/Login.cshtml.cs b/OliverBooth/Pages/Blog/Admin/Login.cshtml.cs deleted file mode 100644 index a66c6eb..0000000 --- a/OliverBooth/Pages/Blog/Admin/Login.cshtml.cs +++ /dev/null @@ -1,31 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.RazorPages; -using OtpNet; -using QRCoder; - -namespace OliverBooth.Pages.Blog.Admin; - -public class Login : PageModel -{ - public string QrCode { get; set; } - - public string Secret { get; set; } - - public IActionResult OnGet() - { - if (Request.Cookies.ContainsKey("sid")) - { - return RedirectToPage("index"); - } - - Secret = Base32Encoding.ToString(KeyGeneration.GenerateRandomKey(20)); - - var uri = $"otpauth://totp/oliverbooth.dev?secret={Secret}"; - var generator = new QRCodeGenerator(); - QRCodeData qrCodeData = generator.CreateQrCode(uri, QRCodeGenerator.ECCLevel.Q); - using var pngByteQrCode = new PngByteQRCode(qrCodeData); - byte[] data = pngByteQrCode.GetGraphic(20); - QrCode = Convert.ToBase64String(data); - return Page(); - } -} diff --git a/OliverBooth/Pages/Shared/_AdminLayout.cshtml b/OliverBooth/Pages/Shared/_AdminLayout.cshtml new file mode 100644 index 0000000..9559134 --- /dev/null +++ b/OliverBooth/Pages/Shared/_AdminLayout.cshtml @@ -0,0 +1,154 @@ +@using System.Diagnostics +@using OliverBooth.Data.Blog +@using OliverBooth.Data.Web +@using OliverBooth.Services +@inject IBlogPostService BlogPostService +@inject IUserService UserService +@inject ISessionService SessionService + +@{ + HttpRequest request = Context.Request; + var url = new Uri($"{request.Scheme}://{request.Host}{request.Path}{request.QueryString}"); + var currentPage = ViewContext.RouteData.Values["page"]?.ToString(); + + SessionService.TryGetSession(request, out ISession? session); + IUser? user = null; + if (session is not null) + { + UserService.TryGetUser(session.UserId, out user); + } + Debug.Assert(user is not null); +} + + + + + + + + + + + + + @if (ViewData["Title"] != null) + { + @ViewData["Title"] - Oliver Booth + } + else + { + Oliver Booth + } + @if (ViewData["Post"] is IBlogPost post) + { + string excerpt = BlogPostService.RenderExcerpt(post, out bool trimmed); + + + + + + + + + } + else + { + + + + + + + } + + + + + + + + + + + + + + + @await RenderSectionAsync("Styles", required: false) + + +
+
+ + + Oliver Booth + +
+ +
+ +
+ +
+ +
+ @RenderBody() +
+
+ + + + + + + + + + +@await RenderSectionAsync("Scripts", required: false) + + \ No newline at end of file