aboutsummaryrefslogtreecommitdiff
path: root/dropped/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretInfo.cs
blob: c3a4de0e28221d69b734ec61fc033343422a9ae9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System.Security.Cryptography;
using System.Text;
using CrupestApi.Commons.Crud;

namespace CrupestApi.Commons.Secrets;

public class SecretInfo
{
    [Column(NotNull = true)]
    public string Key { get; set; } = default!;
    [Column(NotNull = true, NoUpdate = true, ActAsKey = true)]
    public string Secret { get; set; } = default!;
    [Column(DefaultEmptyForString = true)]
    public string Description { get; set; } = default!;
    [Column(NotNull = false)]
    public DateTime? ExpireTime { get; set; }
    [Column(NotNull = true, DefaultValue = false)]
    public bool Revoked { get; set; }
    [Column(NotNull = true)]
    public DateTime CreateTime { get; set; }

    private static RandomNumberGenerator RandomNumberGenerator = RandomNumberGenerator.Create();

    private static string GenerateRandomKey(int length)
    {
        const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        var result = new StringBuilder(length);
        lock (RandomNumberGenerator)
        {
            for (int i = 0; i < length; i++)
            {
                result.Append(chars[RandomNumberGenerator.GetInt32(chars.Length)]);
            }
        }
        return result.ToString();
    }


    public static string SecretDefaultValueGenerator()
    {
        return GenerateRandomKey(16);
    }

    public static DateTime CreateTimeDefaultValueGenerator()
    {
        return DateTime.UtcNow;
    }
}