aboutsummaryrefslogtreecommitdiff
path: root/docker/crupest-api/CrupestApi/CrupestApi.Secrets/SecretsService.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2022-12-02 19:05:35 +0800
committercrupest <crupest@outlook.com>2022-12-20 20:32:52 +0800
commitdcee71fabcdefb9ee7c825d4c15812cb084e3584 (patch)
tree77696bef08b7c565c5ce563292b0fd111025b768 /docker/crupest-api/CrupestApi/CrupestApi.Secrets/SecretsService.cs
parent691a23f5f8b7f58a23d8dea841d3576b047b5336 (diff)
downloadcrupest-dcee71fabcdefb9ee7c825d4c15812cb084e3584.tar.gz
crupest-dcee71fabcdefb9ee7c825d4c15812cb084e3584.tar.bz2
crupest-dcee71fabcdefb9ee7c825d4c15812cb084e3584.zip
Develop secret api. v2
Diffstat (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Secrets/SecretsService.cs')
-rw-r--r--docker/crupest-api/CrupestApi/CrupestApi.Secrets/SecretsService.cs35
1 files changed, 33 insertions, 2 deletions
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Secrets/SecretsService.cs b/docker/crupest-api/CrupestApi/CrupestApi.Secrets/SecretsService.cs
index 3913a0b..9281cee 100644
--- a/docker/crupest-api/CrupestApi/CrupestApi.Secrets/SecretsService.cs
+++ b/docker/crupest-api/CrupestApi/CrupestApi.Secrets/SecretsService.cs
@@ -1,3 +1,5 @@
+using System.Security.Cryptography;
+using System.Text;
using CrupestApi.Commons;
using Dapper;
using Microsoft.Data.Sqlite;
@@ -72,9 +74,38 @@ INSERT INTO secrets (Key, Secret, Description, ExpireTime, Revoked, CreateTime)
}
}
- public Task<SecretInfo> CreateSecretAsync(string key, string description, DateTime? expireTime = null)
+ private string GenerateRandomKey(int length)
{
- throw new NotImplementedException();
+ const string alphanum = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
+ using var randomNumberGenerator = RandomNumberGenerator.Create();
+ var result = new StringBuilder(length);
+ for (int i = 0; i < length; i++)
+ {
+ result.Append(alphanum[i]);
+ }
+ return result.ToString();
+ }
+
+ public async Task<SecretInfo> CreateSecretAsync(string key, string description, DateTime? expireTime = null)
+ {
+ var dbConnection = await EnsureDatabase();
+
+ var secret = GenerateRandomKey(16);
+ var now = DateTime.Now;
+
+ dbConnection.Execute(@"
+INSERT INTO secrets (Key, Secret, Description, ExpireTime, Revoked, CreateTime) VALUES (@Key, @Secret, @Description, @ExpireTime, 0, @CreateTime);
+ ",
+ new
+ {
+ Key = key,
+ Secret = secret,
+ Description = description,
+ ExpireTime = expireTime?.ToString("O"),
+ CreateTime = now.ToString("O"),
+ });
+
+ return new SecretInfo(key, secret, description, expireTime, false, now);
}
public Task<List<SecretInfo>> GetSecretListAsync(bool includeExpired = false, bool includeRevoked = false)