Skip to content

Commit

Permalink
Authorization.
Browse files Browse the repository at this point in the history
  • Loading branch information
Utar94 committed Jul 11, 2024
1 parent 0397f3c commit 3120100
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Logitar.Cms.Contracts.Users;
using Microsoft.AspNetCore.Authorization;

namespace Logitar.Cms.Web.Authorization;

public class UserAuthorizationHandler : AuthorizationHandler<UserAuthorizationRequirement>
{
private readonly IHttpContextAccessor _httpContextAccessor;

public UserAuthorizationHandler(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}

protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, UserAuthorizationRequirement requirement)
{
HttpContext? httpContext = _httpContextAccessor.HttpContext;
if (httpContext != null)
{
User? user = httpContext.GetUser();
if (user == null)
{
context.Fail(new AuthorizationFailureReason(this, "The actor must be an authenticated user."));
}
else
{
context.Succeed(requirement);
}
}

return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using Microsoft.AspNetCore.Authorization;

namespace Logitar.Cms.Web.Authorization;

public class UserAuthorizationRequirement : IAuthorizationRequirement;
6 changes: 6 additions & 0 deletions backend/src/Logitar.Cms.Web/Constants/Policies.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Logitar.Cms.Web.Constants;

public static class Policies
{
public const string User = nameof(User);
}
8 changes: 5 additions & 3 deletions backend/src/Logitar.Cms.Web/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Logitar.Cms.Core;
using Logitar.Cms.Core.Sessions.Commands;
using Logitar.Cms.Web.Authentication;
using Logitar.Cms.Web.Constants;
using Logitar.Cms.Web.Models.Account;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -23,12 +24,13 @@ public AccountController(IOpenAuthenticationService openAuthenticationService, I
_pipeline = pipeline;
}

[Authorize] // TODO(fpion): will fail when using API keys
[Authorize(Policy = Policies.User)]
[HttpGet("profile")]
public ActionResult<User> GetProfile() // TODO(fpion): other return type
public ActionResult<UserProfile> GetProfile()
{
User user = HttpContext.GetUser() ?? throw new InvalidOperationException("An authenticated user is required.");
return Ok(user);
UserProfile profile = new(user);
return Ok(profile);
}

[HttpPost("sign/in")]
Expand Down
55 changes: 55 additions & 0 deletions backend/src/Logitar.Cms.Web/Controllers/UserProfile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Logitar.Cms.Contracts;
using Logitar.Cms.Contracts.Users;

namespace Logitar.Cms.Web.Controllers;

public record UserProfile
{
public string Username { get; set; }

public DateTime? PasswordChangedOn { get; set; }

public string? EmailAddress { get; set; }

public string? FirstName { get; set; }
public string? MiddleName { get; set; }
public string? LastName { get; set; }
public string? FullName { get; set; }

public Locale? Locale { get; set; }

public string? Picture { get; set; }

public DateTime CreatedOn { get; set; }
public DateTime UpdatedOn { get; set; }
public DateTime AuthenticatedOn { get; set; }

public UserProfile() : this(string.Empty)
{
}

public UserProfile(string username)
{
Username = username;
}

public UserProfile(User user) : this(user.UniqueName)
{
PasswordChangedOn = user.PasswordChangedOn;

EmailAddress = user.Email?.Address;

FirstName = user.FirstName;
MiddleName = user.MiddleName;
LastName = user.LastName;
FullName = user.FullName;

Locale = user.Locale;

Picture = user.Picture;

CreatedOn = user.CreatedOn;
UpdatedOn = user.UpdatedOn;
AuthenticatedOn = user.AuthenticatedOn ?? user.UpdatedOn;
}
}
8 changes: 7 additions & 1 deletion backend/src/Logitar.Cms.Web/DependencyInjectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Logitar.Cms.Core;
using Logitar.Cms.Web.Authentication;
using Logitar.Cms.Web.Authorization;
using Logitar.Cms.Web.Constants;
using Logitar.Cms.Web.Filters;
using Logitar.Cms.Web.Settings;
Expand Down Expand Up @@ -40,7 +41,12 @@ public static IServiceCollection AddLogitarCmsWeb(this IServiceCollection servic
services.AddTransient<IOpenAuthenticationService, OpenAuthenticationService>();

services.AddAuthorizationBuilder()
.SetDefaultPolicy(new AuthorizationPolicyBuilder(authenticationSchemes).RequireAuthenticatedUser().Build());
.SetDefaultPolicy(new AuthorizationPolicyBuilder(authenticationSchemes).RequireAuthenticatedUser().Build())
.AddPolicy(Policies.User, new AuthorizationPolicyBuilder(authenticationSchemes)
.RequireAuthenticatedUser()
.AddRequirements(new UserAuthorizationRequirement())
.Build());
services.AddSingleton<IAuthorizationHandler, UserAuthorizationHandler>();

CookiesSettings cookiesSettings = configuration.GetSection(CookiesSettings.SectionKey).Get<CookiesSettings>() ?? new();
services.AddSingleton(cookiesSettings);
Expand Down

0 comments on commit 3120100

Please sign in to comment.