aboutsummaryrefslogtreecommitdiff
path: root/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets
diff options
context:
space:
mode:
Diffstat (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets')
-rw-r--r--docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretInfo.cs48
-rw-r--r--docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretService.cs20
-rw-r--r--docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretServiceCollectionExtensions.cs12
-rw-r--r--docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretsConstants.cs6
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";
+}