Added client side functionality and created register page
This commit is contained in:
@@ -28,7 +28,7 @@ public class HopFrameAuthentication<TDbContext>(
|
||||
public static readonly TimeSpan RefreshTokenTime = new(30, 0, 0, 0);
|
||||
|
||||
protected override async Task<AuthenticateResult> HandleAuthenticateAsync() {
|
||||
var accessToken = Request.Headers["Authorization"].ToString();
|
||||
var accessToken = Request.Cookies[ITokenContext.AccessTokenType];
|
||||
if (string.IsNullOrEmpty(accessToken)) return AuthenticateResult.Fail("No Access Token provided");
|
||||
|
||||
var tokenEntry = await context.Tokens.SingleOrDefaultAsync(token => token.Token == accessToken);
|
||||
@@ -36,7 +36,7 @@ public class HopFrameAuthentication<TDbContext>(
|
||||
if (tokenEntry is null) return AuthenticateResult.Fail("The provided Access Token does not exist");
|
||||
if (tokenEntry.CreatedAt + AccessTokenTime < DateTime.Now) return AuthenticateResult.Fail("The provided Access Token is expired");
|
||||
|
||||
if (!(await context.Users.AnyAsync(user => user.Id == tokenEntry.UserId)))
|
||||
if (!await context.Users.AnyAsync(user => user.Id == tokenEntry.UserId))
|
||||
return AuthenticateResult.Fail("The provided Access Token does not match any user");
|
||||
|
||||
var claims = new List<Claim> {
|
||||
|
||||
@@ -17,13 +17,16 @@ public static class HopFrameAuthenticationExtensions {
|
||||
/// <param name="service">The service provider to add the services to</param>
|
||||
/// <typeparam name="TDbContext">The database object that saves all entities that are important for the security api</typeparam>
|
||||
/// <returns></returns>
|
||||
public static AuthenticationBuilder AddHopFrameAuthentication<TDbContext>(this IServiceCollection service) where TDbContext : HopDbContextBase {
|
||||
public static IServiceCollection AddHopFrameAuthentication<TDbContext>(this IServiceCollection service) where TDbContext : HopDbContextBase {
|
||||
service.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
||||
service.AddScoped<ITokenContext, TokenContextImplementor<TDbContext>>();
|
||||
service.AddScoped<IPermissionService, PermissionService<TDbContext>>();
|
||||
service.AddScoped<IUserService, UserService<TDbContext>>();
|
||||
|
||||
return service.AddAuthentication(HopFrameAuthentication<TDbContext>.SchemeName).AddScheme<AuthenticationSchemeOptions, HopFrameAuthentication<TDbContext>>(HopFrameAuthentication<TDbContext>.SchemeName, _ => {});
|
||||
service.AddAuthentication(HopFrameAuthentication<TDbContext>.SchemeName).AddScheme<AuthenticationSchemeOptions, HopFrameAuthentication<TDbContext>>(HopFrameAuthentication<TDbContext>.SchemeName, _ => {});
|
||||
service.AddAuthorization();
|
||||
|
||||
return service;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -24,7 +24,7 @@ public class AuthorizedFilter : IAuthorizationFilter {
|
||||
|
||||
var permissions = context.HttpContext.User.GetPermissions();
|
||||
|
||||
if (!_permissions.Any(permission => PermissionValidator.IncludesPermission(permission, permissions))) {
|
||||
if (!_permissions.All(permission => PermissionValidator.IncludesPermission(permission, permissions))) {
|
||||
context.Result = new UnauthorizedResult();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace HopFrame.Security.Authorization;
|
||||
|
||||
internal static class PermissionValidator {
|
||||
public static class PermissionValidator {
|
||||
|
||||
public static bool IncludesPermission(string permission, string[] permissions) {
|
||||
if (permission == "*") return true;
|
||||
|
||||
@@ -4,6 +4,9 @@ namespace HopFrame.Security.Claims;
|
||||
|
||||
public interface ITokenContext {
|
||||
|
||||
public const string RefreshTokenType = "HopFrame.Security.RefreshToken";
|
||||
public const string AccessTokenType = "HopFrame.Security.AccessToken";
|
||||
|
||||
/// <summary>
|
||||
/// This field specifies that a valid user is accessing the endpoint
|
||||
/// </summary>
|
||||
|
||||
6
HopFrame.Security/Models/UserLogin.cs
Normal file
6
HopFrame.Security/Models/UserLogin.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace HopFrame.Security.Models;
|
||||
|
||||
public struct UserLogin {
|
||||
public string Email { get; set; }
|
||||
public string Password { get; set; }
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace HopFrame.Security.Models;
|
||||
|
||||
public struct UserRegister {
|
||||
public class UserRegister {
|
||||
public string Username { get; set; }
|
||||
public string Email { get; set; }
|
||||
public string Password { get; set; }
|
||||
|
||||
@@ -23,5 +23,5 @@ public interface IUserService {
|
||||
|
||||
Task DeleteUser(User user);
|
||||
|
||||
Task<string> GetUserPassword(User user);
|
||||
Task<bool> CheckUserPassword(User user, string password);
|
||||
}
|
||||
@@ -39,6 +39,9 @@ internal sealed class UserService<TDbContext>(TDbContext context) : IUserService
|
||||
}
|
||||
|
||||
public async Task<User> AddUser(UserRegister user) {
|
||||
if (await GetUserByEmail(user.Email) is not null) return null;
|
||||
if (await GetUserByUsername(user.Username) is not null) return null;
|
||||
|
||||
var entry = new UserEntry {
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
Email = user.Email,
|
||||
@@ -100,11 +103,14 @@ internal sealed class UserService<TDbContext>(TDbContext context) : IUserService
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public Task<string> GetUserPassword(User user) {
|
||||
public async Task<bool> CheckUserPassword(User user, string password) {
|
||||
var id = user.Id.ToString();
|
||||
return context.Users
|
||||
var hash = EncryptionManager.Hash(password, Encoding.Default.GetBytes(user.CreatedAt.ToString(CultureInfo.InvariantCulture)));
|
||||
|
||||
var entry = await context.Users
|
||||
.Where(entry => entry.Id == id)
|
||||
.Select(entry => entry.Password)
|
||||
.SingleOrDefaultAsync();
|
||||
|
||||
return entry.Password == hash;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user