diff options
| author | crupest <crupest@outlook.com> | 2022-12-02 19:05:35 +0800 | 
|---|---|---|
| committer | crupest <crupest@outlook.com> | 2022-12-20 20:32:52 +0800 | 
| commit | 9fe25354118f55cecb5cef08071b2b6cf88b6be5 (patch) | |
| tree | 77696bef08b7c565c5ce563292b0fd111025b768 /docker | |
| parent | 87fc365b7debf990aab668783401c746f8e7cd3e (diff) | |
| download | crupest-9fe25354118f55cecb5cef08071b2b6cf88b6be5.tar.gz crupest-9fe25354118f55cecb5cef08071b2b6cf88b6be5.tar.bz2 crupest-9fe25354118f55cecb5cef08071b2b6cf88b6be5.zip  | |
Develop secret api. v2
Diffstat (limited to 'docker')
| -rw-r--r-- | docker/crupest-api/CrupestApi/CrupestApi.Secrets/SecretsService.cs | 35 | 
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)  | 
