diff options
author | crupest <crupest@outlook.com> | 2022-12-19 12:24:54 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-12-20 20:32:53 +0800 |
commit | e9b958cc41a3b941d878fd455e297a50e050be1a (patch) | |
tree | 1e431c0b2038cde586be12cfb96c6c2da75b11e8 /docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets | |
parent | f73342cb6b0592d3a6310f9a12fd40b4bf218e5c (diff) | |
download | crupest-e9b958cc41a3b941d878fd455e297a50e050be1a.tar.gz crupest-e9b958cc41a3b941d878fd455e297a50e050be1a.tar.bz2 crupest-e9b958cc41a3b941d878fd455e297a50e050be1a.zip |
Develop secret api. v43
Diffstat (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets')
4 files changed, 86 insertions, 0 deletions
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretInfo.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretInfo.cs new file mode 100644 index 0000000..8b9420a --- /dev/null +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretInfo.cs @@ -0,0 +1,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, Generated = 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)] + 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; + } +} diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretService.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretService.cs new file mode 100644 index 0000000..fc13707 --- /dev/null +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretService.cs @@ -0,0 +1,20 @@ +using CrupestApi.Commons; +using CrupestApi.Commons.Crud; + +namespace CrupestApi.Commons.Secrets; + +public class SecretService : CrudService<SecretInfo>, ISecretService +{ + public SecretService(ITableInfoFactory tableInfoFactory, IDbConnectionFactory dbConnectionFactory, EntityJsonHelper<SecretInfo> jsonHelper, ILoggerFactory loggerFactory) + : base(tableInfoFactory, dbConnectionFactory, jsonHelper, loggerFactory) + { + + } + + public List<string> GetPermissions(string secret) + { + var list = _table.Select<SecretInfo>(_dbConnection, + where: WhereClause.Create().Eq(nameof(SecretInfo.Secret), secret)); + return list.Select(x => x.Key).ToList(); + } +} diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretServiceCollectionExtensions.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretServiceCollectionExtensions.cs new file mode 100644 index 0000000..a9c0e5f --- /dev/null +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretServiceCollectionExtensions.cs @@ -0,0 +1,12 @@ +using Microsoft.Extensions.DependencyInjection.Extensions; + +namespace CrupestApi.Commons.Secrets; + +public static class SecretServiceCollectionExtensions +{ + public static IServiceCollection AddSecrets(this IServiceCollection services) + { + services.TryAddScoped<ISecretService, SecretService>(); + return services; + } +} diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretsConstants.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretsConstants.cs new file mode 100644 index 0000000..207cc45 --- /dev/null +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretsConstants.cs @@ -0,0 +1,6 @@ +namespace CrupestApi.Commons.Secrets; + +public static class SecretsConstants +{ + public const string SecretManagementKey = "crupest.secrets.management"; +} |