From 3b9a51c76712ca0d0cacd57150115cb7d05220c2 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Tue, 19 Sep 2023 16:47:57 -0400 Subject: [PATCH] Ajouter API keyauth Defer dbclient.DB.Close() dans apiCmd --- cmd/api.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cmd/api.go b/cmd/api.go index a612846..14ba555 100644 --- a/cmd/api.go +++ b/cmd/api.go @@ -4,6 +4,7 @@ Copyright © 2023 AGECEM & Victor Lacasse-Beaudoin package cmd import ( + "crypto/subtle" "fmt" "log" @@ -30,6 +31,13 @@ var apiCmd = &cobra.Command{ e.Pre(middleware.AddTrailingSlash()) + if cfg.API.Key != "" { + e.Use(middleware.KeyAuth(func(key string, c echo.Context) (bool, error) { + return subtle.ConstantTimeCompare([]byte(key), []byte(cfg.API.Key)) == 1, nil + })) + log.Println("API server is using an API key") + } + v0 := e.Group("/v0") bottinApiClient := bottindata.NewApiClient(cfg.Bottin.API.Key, cfg.Bottin.API.Host, cfg.Bottin.API.Protocol, cfg.Bottin.API.Port) @@ -38,6 +46,7 @@ var apiCmd = &cobra.Command{ if err != nil { log.Fatal(err) } + defer dbClient.DB.Close() handler := apihandler.New(bottinApiClient, dbClient)