aboutsummaryrefslogtreecommitdiff
path: root/dropped/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretService.cs
blob: c693d8d4964b9e94c1af2509678adc9158ef814b (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.Data;
using CrupestApi.Commons.Crud;
using CrupestApi.Commons.Crud.Migrations;

namespace CrupestApi.Commons.Secrets;

public class SecretService : CrudService<SecretInfo>, ISecretService
{
    private readonly ILogger<SecretService> _logger;

    public SecretService(ITableInfoFactory tableInfoFactory, IDbConnectionFactory dbConnectionFactory, IDatabaseMigrator migrator, ILoggerFactory loggerFactory)
        : base(tableInfoFactory, dbConnectionFactory, migrator, loggerFactory)
    {
        _logger = loggerFactory.CreateLogger<SecretService>();
    }

    protected override void AfterMigrate(IDbConnection connection, TableInfo table)
    {
        if (table.SelectCount(connection) == 0)
        {
            _logger.LogInformation("No secrets found, insert default secrets.");
            using var transaction = connection.BeginTransaction();
            var insertClause = InsertClause.Create()
                .Add(nameof(SecretInfo.Key), SecretsConstants.SecretManagementKey)
                .Add(nameof(SecretInfo.Secret), "crupest")
                .Add(nameof(SecretInfo.Description), "This is the init key. Please revoke it immediately after creating a new one.");
            _table.Insert(connection, insertClause, out var _);
            transaction.Commit();
        }
    }

    public void CreateTestSecret(string key, string secret)
    {
        var connection = _dbConnection;
        var insertClause = InsertClause.Create()
               .Add(nameof(SecretInfo.Key), key)
               .Add(nameof(SecretInfo.Secret), secret)
               .Add(nameof(SecretInfo.Description), "Test secret.");
        _table.Insert(connection, insertClause, out var _);
    }

    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();
    }
}