aboutsummaryrefslogtreecommitdiff
path: root/docker/crupest-api/CrupestApi/CrupestApi.Secrets/SecretsWebApplicationExtensions.cs
blob: 12d939bc013f269e33137247d26cc12ff0f95dfd (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using CrupestApi.Commons;

namespace CrupestApi.Secrets;

public static class SecretsWebApplicationExtensions
{
    public static WebApplication UseCatchVerifySecretException(this WebApplication app)
    {
        app.Use(async (context, next) =>
        {
            try
            {
                await next(context);
            }
            catch (VerifySecretException e)
            {
                await context.Response.WriteErrorMessageAsync(e.Message, e.Kind == VerifySecretException.ErrorKind.Unauthorized ? 401 : 403);
            }
        });

        return app;
    }

    public static async Task CheckSecret(this HttpContext context, string? key)
    {
        var secretsService = context.RequestServices.GetRequiredService<ISecretsService>();
        await secretsService.VerifySecretForHttpRequestAsync(context.Request, key);
    }

    public static WebApplication MapSecrets(this WebApplication app, string path)
    {
        app.MapGet(path, async (context) =>
        {
            await context.CheckSecret(SecretsConstants.SecretManagementKey);
            var secretsService = context.RequestServices.GetRequiredService<ISecretsService>();
            var secrets = secretsService.GetSecretListAsync();
            await context.Response.WriteJsonAsync(secrets);
        });

        app.MapGet(path + "/:secret", async (context) =>
        {
            await context.CheckSecret(SecretsConstants.SecretManagementKey);
            var secretsService = context.RequestServices.GetRequiredService<ISecretsService>();
            var secret = context.Request.RouteValues["secret"];
            if (secret is null)
            {
                await context.Response.WriteErrorMessageAsync("Secret path parameter is invalid.");
                return;
            }
            var secretInfo = secretsService.GetSecretAsync((string)secret);
            await context.Response.WriteJsonAsync(secretInfo);
        });

        app.MapPost(path, async (context) =>
        {
            await context.CheckSecret(SecretsConstants.SecretManagementKey);
            var secretsService = context.RequestServices.GetRequiredService<ISecretsService>();
            var request = await context.Request.ReadFromJsonAsync<SecretCreateRequest>();
            if (request is null)
            {
                await context.Response.WriteErrorMessageAsync("Failed to deserialize request body to SecretCreateRequest.");
                return;
            }
            var secret = await secretsService.CreateSecretAsync(request.Key, request.Description, request.ExpireTime);
            await context.Response.WriteJsonAsync(secret, 201, beforeWriteBody: (response) =>
            {
                response.Headers.Location = context.Request.Path + "/" + secret.Secret;
            });
        });

        app.MapPost(path + "/:secret/revoke", async (context) =>
        {
            await context.CheckSecret(SecretsConstants.SecretManagementKey);
            var secretsService = context.RequestServices.GetRequiredService<ISecretsService>();
            var secret = context.Request.RouteValues["secret"];
            if (secret is null)
            {
                await context.Response.WriteErrorMessageAsync("Secret path parameter is invalid.");
                return;
            }

            try
            {
                await secretsService.RevokeSecretAsync((string)secret);
                await context.Response.WriteMessageAsync("Secret revoked.");
            }
            catch (EntityNotExistException)
            {
                await context.Response.WriteErrorMessageAsync("Secret to revoke is invalid.");
            }
        });

        return app;
    }
}