Simplified LogicResults
This commit is contained in:
15
.idea/.idea.HopFrame/.idea/dataSources.xml
generated
Normal file
15
.idea/.idea.HopFrame/.idea/dataSources.xml
generated
Normal file
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
|
||||
<data-source source="LOCAL" name="test" uuid="28cc3a93-4d15-4879-8000-e9f1baf83f1c">
|
||||
<driver-ref>sqlite.xerial</driver-ref>
|
||||
<synchronize>true</synchronize>
|
||||
<jdbc-driver>org.sqlite.JDBC</jdbc-driver>
|
||||
<jdbc-url>jdbc:sqlite:C:\Users\Remote\Documents\Projekte\HopFrame\DatabaseTest\test.db</jdbc-url>
|
||||
<jdbc-additional-properties>
|
||||
<property name="com.intellij.clouds.kubernetes.db.enabled" value="false" />
|
||||
</jdbc-additional-properties>
|
||||
<working-dir>$ProjectFileDir$</working-dir>
|
||||
</data-source>
|
||||
</component>
|
||||
</project>
|
||||
@@ -24,11 +24,11 @@ public class SecurityController<TDbContext>(TDbContext context) : ControllerBase
|
||||
var user = await context.Users.SingleOrDefaultAsync(user => user.Email == login.Email);
|
||||
|
||||
if (user is null)
|
||||
return this.FromLogicResult(LogicResult<SingleValueResult<string>>.NotFound("The provided email address was not found"));
|
||||
return LogicResult<SingleValueResult<string>>.NotFound("The provided email address was not found");
|
||||
|
||||
var hashedPassword = EncryptionManager.Hash(login.Password, Encoding.Default.GetBytes(user.CreatedAt.ToString(CultureInfo.InvariantCulture)));
|
||||
if (hashedPassword != user.Password)
|
||||
return this.FromLogicResult(LogicResult<SingleValueResult<string>>.Forbidden("The provided password is not correct"));
|
||||
return LogicResult<SingleValueResult<string>>.Forbidden("The provided password is not correct");
|
||||
|
||||
var refreshToken = new TokenEntry {
|
||||
CreatedAt = DateTime.Now,
|
||||
@@ -52,16 +52,16 @@ public class SecurityController<TDbContext>(TDbContext context) : ControllerBase
|
||||
await context.Tokens.AddRangeAsync(refreshToken, accessToken);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
return this.FromLogicResult(LogicResult<SingleValueResult<string>>.Ok(accessToken.Token));
|
||||
return LogicResult<SingleValueResult<string>>.Ok(accessToken.Token);
|
||||
}
|
||||
|
||||
[HttpPost("register")]
|
||||
public async Task<ActionResult<SingleValueResult<string>>> Register([FromBody] UserRegister register) {
|
||||
if (register.Password.Length < 8)
|
||||
return this.FromLogicResult(LogicResult<SingleValueResult<string>>.Conflict("Password needs to be at least 8 characters long"));
|
||||
return LogicResult<SingleValueResult<string>>.Conflict("Password needs to be at least 8 characters long");
|
||||
|
||||
if (await context.Users.AnyAsync(user => user.Username == register.Username || user.Email == register.Email))
|
||||
return this.FromLogicResult(LogicResult<SingleValueResult<string>>.Conflict("Username or Email is already registered"));
|
||||
return LogicResult<SingleValueResult<string>>.Conflict("Username or Email is already registered");
|
||||
|
||||
var user = new UserEntry {
|
||||
CreatedAt = DateTime.Now,
|
||||
@@ -106,7 +106,7 @@ public class SecurityController<TDbContext>(TDbContext context) : ControllerBase
|
||||
await context.Tokens.AddRangeAsync(refreshToken, accessToken);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
return this.FromLogicResult(LogicResult<SingleValueResult<string>>.Ok(accessToken.Token));
|
||||
return LogicResult<SingleValueResult<string>>.Ok(accessToken.Token);
|
||||
}
|
||||
|
||||
[HttpGet("authenticate")]
|
||||
@@ -114,15 +114,15 @@ public class SecurityController<TDbContext>(TDbContext context) : ControllerBase
|
||||
var refreshToken = HttpContext.Request.Cookies[RefreshTokenType];
|
||||
|
||||
if (string.IsNullOrEmpty(refreshToken))
|
||||
return this.FromLogicResult(LogicResult<SingleValueResult<string>>.Conflict("Refresh token not provided"));
|
||||
return LogicResult<SingleValueResult<string>>.Conflict("Refresh token not provided");
|
||||
|
||||
var token = await context.Tokens.SingleOrDefaultAsync(token => token.Token == refreshToken && token.Type == TokenEntry.RefreshTokenType);
|
||||
|
||||
if (token is null)
|
||||
return this.FromLogicResult(LogicResult<SingleValueResult<string>>.NotFound("Refresh token not valid"));
|
||||
return LogicResult<SingleValueResult<string>>.NotFound("Refresh token not valid");
|
||||
|
||||
if (token.CreatedAt + HopFrameAuthentication<TDbContext>.RefreshTokenTime < DateTime.Now)
|
||||
return this.FromLogicResult(LogicResult<SingleValueResult<string>>.Conflict("Refresh token is expired"));
|
||||
return LogicResult<SingleValueResult<string>>.Conflict("Refresh token is expired");
|
||||
|
||||
var accessToken = new TokenEntry {
|
||||
CreatedAt = DateTime.Now,
|
||||
@@ -134,7 +134,7 @@ public class SecurityController<TDbContext>(TDbContext context) : ControllerBase
|
||||
await context.Tokens.AddAsync(accessToken);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
return this.FromLogicResult(LogicResult<SingleValueResult<string>>.Ok(accessToken.Token));
|
||||
return LogicResult<SingleValueResult<string>>.Ok(accessToken.Token);
|
||||
}
|
||||
|
||||
[HttpDelete("logout"), Authorized]
|
||||
@@ -143,7 +143,7 @@ public class SecurityController<TDbContext>(TDbContext context) : ControllerBase
|
||||
var refreshToken = HttpContext.Request.Cookies[RefreshTokenType];
|
||||
|
||||
if (string.IsNullOrEmpty(accessToken) || string.IsNullOrEmpty(refreshToken))
|
||||
return this.FromLogicResult(LogicResult.Conflict("access or refresh token not provided"));
|
||||
return LogicResult.Conflict("access or refresh token not provided");
|
||||
|
||||
var tokenEntries = await context.Tokens.Where(token =>
|
||||
(token.Token == accessToken && token.Type == TokenEntry.AccessTokenType) ||
|
||||
@@ -151,7 +151,7 @@ public class SecurityController<TDbContext>(TDbContext context) : ControllerBase
|
||||
.ToArrayAsync();
|
||||
|
||||
if (tokenEntries.Length != 2)
|
||||
return this.FromLogicResult(LogicResult.NotFound("One or more of the provided tokens was not found"));
|
||||
return LogicResult.NotFound("One or more of the provided tokens was not found");
|
||||
|
||||
context.Tokens.Remove(tokenEntries[0]);
|
||||
context.Tokens.Remove(tokenEntries[1]);
|
||||
@@ -159,7 +159,7 @@ public class SecurityController<TDbContext>(TDbContext context) : ControllerBase
|
||||
|
||||
HttpContext.Response.Cookies.Delete(RefreshTokenType);
|
||||
|
||||
return this.FromLogicResult(LogicResult.Ok());
|
||||
return LogicResult.Ok();
|
||||
}
|
||||
|
||||
[HttpDelete("delete"), Authorized]
|
||||
@@ -168,13 +168,13 @@ public class SecurityController<TDbContext>(TDbContext context) : ControllerBase
|
||||
var userId = (await context.Tokens.SingleOrDefaultAsync(t => t.Token == token && t.Type == TokenEntry.AccessTokenType))?.UserId;
|
||||
|
||||
if (string.IsNullOrEmpty(userId))
|
||||
return this.FromLogicResult(LogicResult.NotFound("Access token does not match any user"));
|
||||
return LogicResult.NotFound("Access token does not match any user");
|
||||
|
||||
var user = await context.Users.SingleAsync(user => user.Id == userId);
|
||||
|
||||
var password = EncryptionManager.Hash(login.Password, Encoding.Default.GetBytes(user.CreatedAt.ToString(CultureInfo.InvariantCulture)));
|
||||
if (user.Password != password)
|
||||
return this.FromLogicResult(LogicResult.Forbidden("The provided password is not correct"));
|
||||
return LogicResult.Forbidden("The provided password is not correct");
|
||||
|
||||
var tokens = await context.Tokens.Where(t => t.UserId == userId).ToArrayAsync();
|
||||
var permissions = await context.Permissions.Where(perm => perm.UserId == userId).ToArrayAsync();
|
||||
@@ -186,7 +186,7 @@ public class SecurityController<TDbContext>(TDbContext context) : ControllerBase
|
||||
|
||||
HttpContext.Response.Cookies.Delete(RefreshTokenType);
|
||||
|
||||
return this.FromLogicResult(LogicResult.Ok());
|
||||
return LogicResult.Ok();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,6 +6,7 @@ namespace HopFrame.Api;
|
||||
public static class ControllerExtensions {
|
||||
|
||||
public static IMvcBuilder AddController<TController>(this IMvcBuilder builder) where TController : ControllerBase {
|
||||
//TODO: Change implementation method
|
||||
return builder.AddApplicationPart(typeof(TController).Assembly);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,50 +0,0 @@
|
||||
using System.Net;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace HopFrame.Api.Logic;
|
||||
|
||||
public static class ControllerBaseExtension {
|
||||
public static ActionResult FromLogicResult(this ControllerBase controller, ILogicResult result) {
|
||||
switch (result.State) {
|
||||
case LogicResultState.Ok:
|
||||
return controller.Ok();
|
||||
|
||||
case LogicResultState.BadRequest:
|
||||
return controller.StatusCode((int)HttpStatusCode.BadRequest, result.Message);
|
||||
|
||||
case LogicResultState.Forbidden:
|
||||
return controller.StatusCode((int)HttpStatusCode.Forbidden, result.Message);
|
||||
|
||||
case LogicResultState.NotFound:
|
||||
return controller.StatusCode((int)HttpStatusCode.NotFound, result.Message);
|
||||
|
||||
case LogicResultState.Conflict:
|
||||
return controller.StatusCode((int)HttpStatusCode.Conflict, result.Message);
|
||||
|
||||
default:
|
||||
throw new Exception("An unhandled result has occurred as a result of a service call.");
|
||||
}
|
||||
}
|
||||
|
||||
public static ActionResult FromLogicResult<T>(this ControllerBase controller, ILogicResult<T> result) {
|
||||
switch (result.State) {
|
||||
case LogicResultState.Ok:
|
||||
return controller.Ok(result.Data);
|
||||
|
||||
case LogicResultState.BadRequest:
|
||||
return controller.StatusCode((int)HttpStatusCode.BadRequest, result.Message);
|
||||
|
||||
case LogicResultState.Forbidden:
|
||||
return controller.StatusCode((int)HttpStatusCode.Forbidden, result.Message);
|
||||
|
||||
case LogicResultState.NotFound:
|
||||
return controller.StatusCode((int)HttpStatusCode.NotFound, result.Message);
|
||||
|
||||
case LogicResultState.Conflict:
|
||||
return controller.StatusCode((int)HttpStatusCode.Conflict, result.Message);
|
||||
|
||||
default:
|
||||
throw new Exception("An unhandled result has occurred as a result of a service call.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
using System.Net;
|
||||
|
||||
namespace HopFrame.Api.Logic;
|
||||
|
||||
public interface ILogicResult {
|
||||
LogicResultState State { get; set; }
|
||||
HttpStatusCode State { get; set; }
|
||||
|
||||
string Message { get; set; }
|
||||
|
||||
@@ -9,7 +11,7 @@ public interface ILogicResult {
|
||||
}
|
||||
|
||||
public interface ILogicResult<T> {
|
||||
LogicResultState State { get; set; }
|
||||
HttpStatusCode State { get; set; }
|
||||
|
||||
T Data { get; set; }
|
||||
|
||||
|
||||
@@ -1,68 +1,69 @@
|
||||
using System.Net;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace HopFrame.Api.Logic;
|
||||
|
||||
public class LogicResult : ILogicResult {
|
||||
public LogicResultState State { get; set; }
|
||||
public HttpStatusCode State { get; set; }
|
||||
|
||||
public string Message { get; set; }
|
||||
|
||||
public bool IsSuccessful => State == LogicResultState.Ok;
|
||||
public bool IsSuccessful => State == HttpStatusCode.OK;
|
||||
|
||||
public static LogicResult Ok() {
|
||||
return new LogicResult() {
|
||||
State = LogicResultState.Ok
|
||||
State = HttpStatusCode.OK
|
||||
};
|
||||
}
|
||||
|
||||
public static LogicResult BadRequest() {
|
||||
return new LogicResult() {
|
||||
State = LogicResultState.BadRequest
|
||||
State = HttpStatusCode.BadRequest
|
||||
};
|
||||
}
|
||||
|
||||
public static LogicResult BadRequest(string message) {
|
||||
return new LogicResult() {
|
||||
State = LogicResultState.BadRequest,
|
||||
State = HttpStatusCode.BadRequest,
|
||||
Message = message
|
||||
};
|
||||
}
|
||||
|
||||
public static LogicResult Forbidden() {
|
||||
return new LogicResult() {
|
||||
State = LogicResultState.Forbidden
|
||||
State = HttpStatusCode.Forbidden
|
||||
};
|
||||
}
|
||||
|
||||
public static LogicResult Forbidden(string message) {
|
||||
return new LogicResult() {
|
||||
State = LogicResultState.Forbidden,
|
||||
State = HttpStatusCode.Forbidden,
|
||||
Message = message
|
||||
};
|
||||
}
|
||||
|
||||
public static LogicResult NotFound() {
|
||||
return new LogicResult() {
|
||||
State = LogicResultState.NotFound
|
||||
State = HttpStatusCode.NotFound
|
||||
};
|
||||
}
|
||||
|
||||
public static LogicResult NotFound(string message) {
|
||||
return new LogicResult() {
|
||||
State = LogicResultState.NotFound,
|
||||
State = HttpStatusCode.NotFound,
|
||||
Message = message
|
||||
};
|
||||
}
|
||||
|
||||
public static LogicResult Conflict() {
|
||||
return new LogicResult() {
|
||||
State = LogicResultState.Conflict
|
||||
State = HttpStatusCode.Conflict
|
||||
};
|
||||
}
|
||||
|
||||
public static LogicResult Conflict(string message) {
|
||||
return new LogicResult() {
|
||||
State = LogicResultState.Conflict,
|
||||
State = HttpStatusCode.Conflict,
|
||||
Message = message
|
||||
};
|
||||
}
|
||||
@@ -80,78 +81,86 @@ public class LogicResult : ILogicResult {
|
||||
Message = result.Message
|
||||
};
|
||||
}
|
||||
|
||||
public static implicit operator ActionResult(LogicResult v) {
|
||||
if (v.State == HttpStatusCode.OK) return new OkResult();
|
||||
|
||||
return new ObjectResult(v.Message) {
|
||||
StatusCode = (int)v.State
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public class LogicResult<T> : ILogicResult<T> {
|
||||
public LogicResultState State { get; set; }
|
||||
public HttpStatusCode State { get; set; }
|
||||
|
||||
public T Data { get; set; }
|
||||
|
||||
public string Message { get; set; }
|
||||
|
||||
public bool IsSuccessful => State == LogicResultState.Ok;
|
||||
public bool IsSuccessful => State == HttpStatusCode.OK;
|
||||
|
||||
public static LogicResult<T> Ok() {
|
||||
return new LogicResult<T>() {
|
||||
State = LogicResultState.Ok
|
||||
State = HttpStatusCode.OK
|
||||
};
|
||||
}
|
||||
|
||||
public static LogicResult<T> Ok(T result) {
|
||||
return new LogicResult<T>() {
|
||||
State = LogicResultState.Ok,
|
||||
State = HttpStatusCode.OK,
|
||||
Data = result
|
||||
};
|
||||
}
|
||||
|
||||
public static LogicResult<T> BadRequest() {
|
||||
return new LogicResult<T>() {
|
||||
State = LogicResultState.BadRequest
|
||||
State = HttpStatusCode.BadRequest
|
||||
};
|
||||
}
|
||||
|
||||
public static LogicResult<T> BadRequest(string message) {
|
||||
return new LogicResult<T>() {
|
||||
State = LogicResultState.BadRequest,
|
||||
State = HttpStatusCode.BadRequest,
|
||||
Message = message
|
||||
};
|
||||
}
|
||||
|
||||
public static LogicResult<T> Forbidden() {
|
||||
return new LogicResult<T>() {
|
||||
State = LogicResultState.Forbidden
|
||||
State = HttpStatusCode.Forbidden
|
||||
};
|
||||
}
|
||||
|
||||
public static LogicResult<T> Forbidden(string message) {
|
||||
return new LogicResult<T>() {
|
||||
State = LogicResultState.Forbidden,
|
||||
State = HttpStatusCode.Forbidden,
|
||||
Message = message
|
||||
};
|
||||
}
|
||||
|
||||
public static LogicResult<T> NotFound() {
|
||||
return new LogicResult<T>() {
|
||||
State = LogicResultState.NotFound
|
||||
State = HttpStatusCode.NotFound
|
||||
};
|
||||
}
|
||||
|
||||
public static LogicResult<T> NotFound(string message) {
|
||||
return new LogicResult<T>() {
|
||||
State = LogicResultState.NotFound,
|
||||
State = HttpStatusCode.NotFound,
|
||||
Message = message
|
||||
};
|
||||
}
|
||||
|
||||
public static LogicResult<T> Conflict() {
|
||||
return new LogicResult<T>() {
|
||||
State = LogicResultState.Conflict
|
||||
State = HttpStatusCode.Conflict
|
||||
};
|
||||
}
|
||||
|
||||
public static LogicResult<T> Conflict(string message) {
|
||||
return new LogicResult<T>() {
|
||||
State = LogicResultState.Conflict,
|
||||
State = HttpStatusCode.Conflict,
|
||||
Message = message
|
||||
};
|
||||
}
|
||||
@@ -169,4 +178,12 @@ public class LogicResult<T> : ILogicResult<T> {
|
||||
Message = result.Message
|
||||
};
|
||||
}
|
||||
|
||||
public static implicit operator ActionResult<T>(LogicResult<T> v) {
|
||||
if (v.State == HttpStatusCode.OK) return new OkObjectResult(v.Data);
|
||||
|
||||
return new ObjectResult(v.Message) {
|
||||
StatusCode = (int)v.State
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
namespace HopFrame.Api.Logic;
|
||||
|
||||
public enum LogicResultState {
|
||||
Ok,
|
||||
BadRequest,
|
||||
Forbidden,
|
||||
NotFound,
|
||||
Conflict
|
||||
}
|
||||
Reference in New Issue
Block a user