From 5036788df84b3963dfff174161224f88d1dfd59d Mon Sep 17 00:00:00 2001 From: Cara Salter Date: Fri, 1 Sep 2023 10:08:55 -0400 Subject: setup basic authentication --- internal/handlers/hkgi.go | 11 +++++++++++ internal/router.go | 44 +++++++++++++++++++++++++++++++++++++++++++- internal/routers/hkgi.go | 6 ++++-- 3 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 internal/handlers/hkgi.go diff --git a/internal/handlers/hkgi.go b/internal/handlers/hkgi.go new file mode 100644 index 0000000..4f46d1f --- /dev/null +++ b/internal/handlers/hkgi.go @@ -0,0 +1,11 @@ +package handlers + +import ( + "git.devcara.com/hkgi/database" + "github.com/gofiber/fiber/v2" +) + +func GetStead(c *fiber.Ctx) { + db := database.DB + +} diff --git a/internal/router.go b/internal/router.go index 8c6bb45..79f8d82 100644 --- a/internal/router.go +++ b/internal/router.go @@ -1,7 +1,49 @@ package internal -import "github.com/gofiber/fiber/v2" +import ( + "context" + "log" + + "git.devcara.com/hkgi/database" + "git.devcara.com/hkgi/internal/routers" + "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/middleware/basicauth" + "golang.org/x/crypto/bcrypt" +) + +type User struct { + Username string + Password string +} func SetupRoutes(app *fiber.App) { + app.Use(basicauth.New(basicauth.Config{ + Authorizer: func(username, password string) bool { + db,err := database.DB.Acquire() + + if (err != nil) { + log.Fatal(err.Error()) + } + + var user User + err = db.QueryRow(context.Background(), "SELECT username, password FROM steads WHERE username=$1", username).Scan(&user) + + if (err != nil) { + return false; + } + + err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)) + + if (err == nil) { + return true + } + + return false + }, + Unauthorized: func(c *fiber.Ctx) error { + return c.SendStatus(403) + }, + })) + routers.SetupHkgiRoutes(app) } diff --git a/internal/routers/hkgi.go b/internal/routers/hkgi.go index 942bc5a..46809c2 100644 --- a/internal/routers/hkgi.go +++ b/internal/routers/hkgi.go @@ -1,8 +1,10 @@ package routers -import "github.com/gofiber/fiber/v2" +import ( + "github.com/gofiber/fiber/v2" +) -func SetupHkgiRoutes(router fiber.Router) { +func SetupHkgiRoutes(router fiber.App) { hkgi := router.Group("/hkgi") hkgi.Get("/getstead", func(c *fiber.Ctx) error {}) -- cgit v1.2.3