aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/Endpoints/V1/ApiTokens/CreateTokenRoute.cs
diff options
context:
space:
mode:
authorivarlovlie <git@ivarlovlie.no>2022-10-05 14:45:21 +0200
committerivarlovlie <git@ivarlovlie.no>2022-10-05 14:45:21 +0200
commitb7e39b59fd0fc7b5610ebff29035bf622079e0d8 (patch)
tree64be84ebbdac9f7ceced983390c53b10d575af5c /server/src/Endpoints/V1/ApiTokens/CreateTokenRoute.cs
parent2001c035fbb417ab0a3d42cfb04d17420bde4086 (diff)
downloadgreatoffice-b7e39b59fd0fc7b5610ebff29035bf622079e0d8.tar.xz
greatoffice-b7e39b59fd0fc7b5610ebff29035bf622079e0d8.zip
refactor: Change file structure
Diffstat (limited to 'server/src/Endpoints/V1/ApiTokens/CreateTokenRoute.cs')
-rw-r--r--server/src/Endpoints/V1/ApiTokens/CreateTokenRoute.cs57
1 files changed, 0 insertions, 57 deletions
diff --git a/server/src/Endpoints/V1/ApiTokens/CreateTokenRoute.cs b/server/src/Endpoints/V1/ApiTokens/CreateTokenRoute.cs
deleted file mode 100644
index 2086619..0000000
--- a/server/src/Endpoints/V1/ApiTokens/CreateTokenRoute.cs
+++ /dev/null
@@ -1,57 +0,0 @@
-using System.Text;
-
-namespace IOL.GreatOffice.Api.Endpoints.V1.ApiTokens;
-
-public class CreateTokenRoute : RouteBaseSync.WithRequest<ApiAccessToken.ApiAccessTokenDto>.WithActionResult
-{
- private readonly AppDbContext _context;
- private readonly AppConfiguration _configuration;
- private readonly ILogger<CreateTokenRoute> _logger;
-
- public CreateTokenRoute(AppDbContext context, VaultService vaultService, ILogger<CreateTokenRoute> logger)
- {
- _context = context;
- _configuration = vaultService.GetCurrentAppConfiguration();
- _logger = logger;
- }
-
- /// <summary>
- /// Create a new api token with the provided claims.
- /// </summary>
- /// <param name="request">The claims to set on the api token</param>
- /// <returns></returns>
- [ApiVersion(ApiSpecV1.VERSION_STRING)]
- [HttpPost("~/v{version:apiVersion}/api-tokens/create")]
- [ProducesResponseType(200, Type = typeof(string))]
- [ProducesResponseType(404, Type = typeof(ErrorResult))]
- public override ActionResult Handle(ApiAccessToken.ApiAccessTokenDto request)
- {
- var user = _context.Users.SingleOrDefault(c => c.Id == LoggedInUser.Id);
- if (user == default)
- {
- return NotFound(new ErrorResult("User does not exist"));
- }
-
- var token_entropy = _configuration.APP_AES_KEY;
- if (token_entropy.IsNullOrWhiteSpace())
- {
- _logger.LogWarning("No token entropy is available, Basic auth is disabled");
- return NotFound();
- }
-
- var access_token = new ApiAccessToken()
- {
- Id = Guid.NewGuid(),
- User = user,
- ExpiryDate = request.ExpiryDate.ToUniversalTime(),
- AllowCreate = request.AllowCreate,
- AllowRead = request.AllowRead,
- AllowDelete = request.AllowDelete,
- AllowUpdate = request.AllowUpdate
- };
-
- _context.AccessTokens.Add(access_token);
- _context.SaveChanges();
- return Ok(Convert.ToBase64String(Encoding.UTF8.GetBytes(access_token.Id.ToString().EncryptWithAes(token_entropy))));
- }
-}