aboutsummaryrefslogtreecommitdiff
path: root/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretService.cs
blob: 47e95836603acf8a123d9fb0a7f5b2ebfd0592f4 (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
using CrupestApi.Commons.Crud;
using Dapper;

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)
    {

    }

    protected override void DoInitializeDatabase(System.Data.IDbConnection connection)
    {
        base.DoInitializeDatabase(connection);
        using var transaction = connection.BeginTransaction();
        _table.Insert(connection, new SecretInfo
        {
            Key = SecretsConstants.SecretManagementKey,
            Secret = "crupest",
            Description = "This is the init key. Please revoke it immediately after creating a new one."
        });
        transaction.Commit();
    }

    public void CreateTestSecret(string key, string secret)
    {
        var connection = _dbConnection;
        connection.Execute("INSERT INTO secrets (key, secret, description) VALUES (@key, @secret, @desc)", new { key, secret, desc = "Test key." });
    }

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