diff --git a/examples/docker-compose.yml b/examples/docker-compose.yml index 9ff3970..9cdccc5 100644 --- a/examples/docker-compose.yml +++ b/examples/docker-compose.yml @@ -6,4 +6,17 @@ services: environment: POSTGRES_PASSWORD: example POSTGRES_USER: go-urlsh - POSTGRES_DB: go-urlsh \ No newline at end of file + POSTGRES_DB: go-urlsh + volumes: + - ./postgres-data:/var/lib/postgresql/data + go-urlsh: + image: codeberg.org/lauralani/go-urlsh:0.1 + restart: always + ports: + - 127.0.0.1:3000:3000 + depends_on: + - db + environment: + # format: postgresql://:@/ + DATABASE_URL: postgres:// + diff --git a/examples/settings.env b/examples/settings.env index 384b7df..f66fa0b 100644 --- a/examples/settings.env +++ b/examples/settings.env @@ -3,12 +3,12 @@ BINDADDR=127.0.0.1 # Port to bind to -# default: 2345 -PORT=2345 +# default: 3000 +PORT=3000 # List of trusted proxy IPs separated by colons # default: 127.0.0.1,::1 -TRUSTEDPROXIES=127.0.0.1,::1 +# TRUSTEDPROXIES=127.0.0.1,::1 # Postgresql connection string # format: postgresql://:@/todos?sslmode=verify-ca diff --git a/internal/api/links.go b/internal/api/links.go index e264eb1..1e36a66 100644 --- a/internal/api/links.go +++ b/internal/api/links.go @@ -13,6 +13,10 @@ import ( ) func HandleLinkGetAll(c *fiber.Ctx) error { + if !db.IsCookieValid(c.Cookies(misc.CookieName, "")) && !db.IsApiKeyValid(c.GetRespHeader("x-api-key", "")) { + return fiber.NewError(fiber.StatusUnauthorized, "401 Unauthorized") + } + var links []models.Link err := models.DB.NewSelect().Model(&links).Scan(context.Background()) @@ -23,7 +27,10 @@ func HandleLinkGetAll(c *fiber.Ctx) error { for _, link := range links { log.Printf("id: %v, url: %v", link.ID, link.URL) } - c.JSON(links) + err = c.JSON(links) + if err != nil { + log.Println(err) + } return nil } @@ -32,11 +39,10 @@ func HandleLinkGet(c *fiber.Ctx) error { } func HandleLinkPost(c *fiber.Ctx) error { - if !db.IsCookieValid(c.Cookies(misc.CookieName, "")) { + if !db.IsCookieValid(c.Cookies(misc.CookieName, "")) && !db.IsApiKeyValid(c.GetRespHeader("x-api-key", "")) { return fiber.NewError(fiber.StatusUnauthorized, "401 Unauthorized") } - // TODO: Add API-Key Auth var newlink models.Link err := json.Unmarshal(c.Body(), &newlink) @@ -61,7 +67,11 @@ func HandleLinkPost(c *fiber.Ctx) error { c.Append("Location", c.BaseURL()+"/api/v1/links/"+newlink.ID) c.Status(fiber.StatusCreated) - c.JSON(newlink) + + err = c.JSON(newlink) + if err != nil { + log.Println(err) + } return nil } @@ -92,5 +102,12 @@ func HandleLinkDelete(c *fiber.Ctx) error { log.Println(err.Error()) return fiber.NewError(fiber.StatusInternalServerError, "500 Internal Server Error") } + + c.Status(fiber.StatusNoContent) + err = c.SendString("204 No Content") + if err != nil { + log.Println(err) + } + return nil } diff --git a/internal/api/users.go b/internal/api/users.go index a02484d..4e754f9 100644 --- a/internal/api/users.go +++ b/internal/api/users.go @@ -7,7 +7,6 @@ import ( "crypto/sha256" "encoding/hex" "encoding/json" - "fmt" "github.com/gofiber/fiber/v2" "log" "time" @@ -34,7 +33,6 @@ func HandleUserPost(c *fiber.Ctx) error { salt := misc.RandomString(15) created := time.Now() hashbytes := sha256.Sum256([]byte(salt + newuser.Password)) - fmt.Printf("%x\n", hashbytes) hash := hex.EncodeToString(hashbytes[:]) diff --git a/internal/db/apikey.go b/internal/db/apikey.go new file mode 100644 index 0000000..197370b --- /dev/null +++ b/internal/db/apikey.go @@ -0,0 +1,27 @@ +package db + +import ( + "codeberg.org/lauralani/go-urlsh/models" + "context" + "log" +) + +// IsApiKeyValid checks the database if ApiKey val is valid. +// +// Returns true if it's valid, false if not. +func IsApiKeyValid(val string) bool { + if val == "" { + return false + } + + count, err := models.DB.NewSelect().Model((*models.ApiKey)(nil)).Where("key = ?", val).Count(context.Background()) + if err != nil { + log.Printf("Error checking apikey validity for key %v\n", val) + return false + } + if count < 1 { + return false + } else { + return true + } +} diff --git a/internal/web/link.go b/internal/web/links.go similarity index 92% rename from internal/web/link.go rename to internal/web/links.go index 0a4178b..4a88e81 100644 --- a/internal/web/link.go +++ b/internal/web/links.go @@ -12,6 +12,12 @@ import ( ) func HandleAdminLinkNewGet(c *fiber.Ctx) error { + if !db.IsCookieValid(c.Cookies(misc.CookieName, "")) { + c.Location("/admin/") + c.Status(fiber.StatusSeeOther) + return nil + } + return c.Render("add_link", nil) } diff --git a/models/apikey.go b/models/apikey.go index 3328682..464e6a3 100644 --- a/models/apikey.go +++ b/models/apikey.go @@ -8,6 +8,7 @@ import ( type ApiKey struct { bun.BaseModel `bun:"table:apikeys"` Key string `bun:"key,pk,type:uuid,default:gen_random_uuid()" json:"key,omitempty"` - UserName string `bun:"username,notnull" json:"username"` - Created time.Time `bun:"created,default:now()" json:"created"` + UserName string `bun:"username,notnull" json:"username,omitempty"` + Created time.Time `bun:"created,default:now()" json:"created,omitempty"` + Description string `bun:"description,notnull" json:"description"` } diff --git a/views/add_link.tmpl b/views/add_link.tmpl index 62cc100..c954e24 100644 --- a/views/add_link.tmpl +++ b/views/add_link.tmpl @@ -36,7 +36,7 @@
-
+

Add new shortlink

@@ -46,7 +46,7 @@ - + diff --git a/views/edit_link.tmpl b/views/edit_link.tmpl index 93bb8e5..dd56808 100644 --- a/views/edit_link.tmpl +++ b/views/edit_link.tmpl @@ -8,8 +8,7 @@ - - + diff --git a/views/links.tmpl b/views/links.tmpl index cd0c92f..6457ed3 100644 --- a/views/links.tmpl +++ b/views/links.tmpl @@ -8,8 +8,7 @@ - - +