commit d2c214bf240af317548050aaa6068b3e6bd8d7c2
parent effc0379e054b9b94ce8c1e5cdec1f13f1096875
Author: Dalien-S <91556739+Dalien-S@users.noreply.github.com>
Date: Mon, 9 Nov 2026 15:25:59 -0300
Merge branch 'gboncoffee:master' into master
Diffstat:
5 files changed, 53 insertions(+), 13 deletions(-)
diff --git a/auth/authenticated.go b/auth/authenticated.go
@@ -0,0 +1,22 @@
+package main
+
+import (
+ "net/http"
+ "uuid"
+
+ "github.com/gboncoffee/siga/auth/repository"
+)
+
+func authenticated(w http.ResponseWriter, r *http.Request) {
+ token, err := uuid.Parse(r.PathValue("uuid"))
+ if err != nil {
+ http.Error(w, "invalid token", http.StatusBadRequest)
+ return
+ }
+ if err := repository.SessionExists(token); err != nil {
+ http.Error(w, "invalid token", http.StatusUnauthorized)
+ return
+ }
+
+ w.WriteHeader(http.StatusOK)
+}
diff --git a/auth/login.go b/auth/login.go
@@ -3,7 +3,6 @@ package main
import (
"crypto/sha512"
"encoding/json"
- "log"
"net/http"
"uuid"
@@ -28,15 +27,11 @@ func login(w http.ResponseWriter, r *http.Request) {
c, err := repository.GetByUsername(req.Username)
if err != nil {
- log.Println("here")
- log.Println(err)
http.Error(w, "invalid username or password", http.StatusUnauthorized)
return
}
if c.Password != sha512.Sum512([]byte(req.Password)) {
- log.Println("there")
- log.Println(err)
http.Error(w, "invalid username or password", http.StatusUnauthorized)
return
}
diff --git a/auth/logout.go b/auth/logout.go
@@ -0,0 +1,19 @@
+package main
+
+import (
+ "net/http"
+ "uuid"
+
+ "github.com/gboncoffee/siga/auth/repository"
+)
+
+func logout(w http.ResponseWriter, r *http.Request) {
+ token, err := uuid.Parse(r.PathValue("uuid"))
+ if err != nil {
+ http.Error(w, "invalid token", http.StatusBadRequest)
+ return
+ }
+
+ repository.DeleteSession(token)
+ w.WriteHeader(http.StatusOK)
+}
diff --git a/auth/repository/repository.go b/auth/repository/repository.go
@@ -3,7 +3,6 @@ package repository
import (
"crypto/sha512"
"database/sql"
- "errors"
"log"
"uuid"
@@ -15,8 +14,6 @@ type Credential struct {
Password [sha512.Size]byte
}
-var ErrNotFound = errors.New("not found")
-
var db *sql.DB
func init() {
@@ -68,11 +65,18 @@ func NewSession(token uuid.UUID) error {
return err
}
-func SessionExists(token uuid.UUID) bool {
+func SessionExists(token uuid.UUID) error {
var t uuid.UUID
err := db.QueryRow(
`SELECT token FROM sessions WHERE token = ?`,
- token,
+ token[:],
).Scan(&t)
- return err == nil
+ return err
+}
+
+func DeleteSession(token uuid.UUID) error {
+ _, err := db.Exec(
+ `DELETE FROM sessions WHERE token = ?`, token[:],
+ )
+ return err
}
diff --git a/auth/router.go b/auth/router.go
@@ -4,7 +4,7 @@ import "net/http"
func router() {
http.HandleFunc("POST /login", login)
- // http.HandleFunc("POST /logout", logout)
+ http.HandleFunc("POST /logout/{uuid}", logout)
http.HandleFunc("POST /credentials", credentials)
- // http.HandleFunc("GET /{uuid}", authenticated)
+ http.HandleFunc("GET /{uuid}", authenticated)
}