diff options
| author | crupest <crupest@outlook.com> | 2022-12-02 11:04:34 +0800 | 
|---|---|---|
| committer | crupest <crupest@outlook.com> | 2022-12-02 13:35:35 +0800 | 
| commit | 879fb614c6853ab3bb83155c82722afb2933fc60 (patch) | |
| tree | 2f80b57945408b8af83556a4efe5a92dea98e025 /docker/crupest-api/CrupestApi/CrupestApi.Secrets/SecretsWebApplicationExtensions.cs | |
| parent | 31416d8e047e4209f797881fd24e0e77256f3da1 (diff) | |
| download | crupest-879fb614c6853ab3bb83155c82722afb2933fc60.tar.gz crupest-879fb614c6853ab3bb83155c82722afb2933fc60.tar.bz2 crupest-879fb614c6853ab3bb83155c82722afb2933fc60.zip  | |
...
Diffstat (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Secrets/SecretsWebApplicationExtensions.cs')
| -rw-r--r-- | docker/crupest-api/CrupestApi/CrupestApi.Secrets/SecretsWebApplicationExtensions.cs | 42 | 
1 files changed, 42 insertions, 0 deletions
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Secrets/SecretsWebApplicationExtensions.cs b/docker/crupest-api/CrupestApi/CrupestApi.Secrets/SecretsWebApplicationExtensions.cs new file mode 100644 index 0000000..a771547 --- /dev/null +++ b/docker/crupest-api/CrupestApi/CrupestApi.Secrets/SecretsWebApplicationExtensions.cs @@ -0,0 +1,42 @@ +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, 401); +            } +        }); + +        return app; +    } + +    public static async Task CheckSecret(this HttpContext context, string key) +    { +        var secretsService = context.RequestServices.GetRequiredService<ISecretsService>(); +        await secretsService.VerifySecretForHttpRequestAsync(context.Request, SecretsConstants.SecretManagementKey); +    } + +    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); +        }); + +        return app; +    } +}  | 
