commit 399a2cdcb0dd397be186ba710001df3b6e03204c
parent 8c396da65c5a1edbb2d6c23173b0438223a6f06e
Author: Gabriel G. de Brito <me@gboncoffee.dev.br>
Date: Fri, 6 Nov 2026 11:44:20 -0300
Login in works.
Diffstat:
5 files changed, 94 insertions(+), 7 deletions(-)
diff --git a/auth/credentials.go b/auth/credentials.go
@@ -1,9 +1,11 @@
package main
import (
+ "crypto/sha512"
"encoding/json"
- "github.com/gboncoffee/siga/auth/repository"
"net/http"
+
+ "github.com/gboncoffee/siga/auth/repository"
)
type CredentialsRequest struct {
@@ -20,10 +22,12 @@ func credentials(w http.ResponseWriter, r *http.Request) {
c := repository.Credential{
Username: req.Username,
- Password: req.Password,
+ Password: sha512.Sum512([]byte(req.Password)),
}
if err := repository.NewCredential(c); err != nil {
http.Error(w, "internal server error", http.StatusInternalServerError)
return
}
+
+ w.WriteHeader(http.StatusOK)
}
diff --git a/auth/login.go b/auth/login.go
@@ -0,0 +1,55 @@
+package main
+
+import (
+ "crypto/sha512"
+ "encoding/json"
+ "log"
+ "net/http"
+ "uuid"
+
+ "github.com/gboncoffee/siga/auth/repository"
+)
+
+type LoginRequest struct {
+ Username string
+ Password string
+}
+
+type LoginResponse struct {
+ Token uuid.UUID
+}
+
+func login(w http.ResponseWriter, r *http.Request) {
+ var req LoginRequest
+ if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
+ http.Error(w, "invalid JSON", http.StatusBadRequest)
+ return
+ }
+
+ 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
+ }
+
+ token := uuid.New()
+ if err := repository.NewSession(token); err != nil {
+ http.Error(w,
+ "internal server error",
+ http.StatusInternalServerError,
+ )
+ return
+ }
+
+ w.Header().Set("Content-Type", "application/json")
+ json.NewEncoder(w).Encode(LoginResponse{Token: token})
+}
diff --git a/auth/main.go b/auth/main.go
@@ -16,6 +16,7 @@ func main() {
flag.Parse()
router()
+
log.Println("Listening on " + addr + ":" + port)
log.Fatal(http.ListenAndServe(addr+":"+port, nil))
}
diff --git a/auth/repository/repository.go b/auth/repository/repository.go
@@ -1,16 +1,18 @@
package repository
import (
+ "crypto/sha512"
"database/sql"
"errors"
"log"
+ "uuid"
_ "modernc.org/sqlite"
)
type Credential struct {
Username string
- Password string
+ Password [sha512.Size]byte
}
var ErrNotFound = errors.New("not found")
@@ -26,7 +28,14 @@ func init() {
_, err = db.Exec(`
CREATE TABLE IF NOT EXISTS credentials (
username TEXT NOT NULL UNIQUE,
- password TEXT NOT NULL
+ password BLOB NOT NULL CHECK (LENGTH(password) == 64)
+ )`)
+ if err != nil {
+ log.Fatal(err)
+ }
+ _, err = db.Exec(`
+ CREATE TABLE IF NOT EXISTS sessions (
+ token BLOB CHECK (LENGTH(token) == 16)
)`)
if err != nil {
log.Fatal(err)
@@ -36,16 +45,34 @@ func init() {
func NewCredential(c Credential) error {
_, err := db.Exec(
`INSERT INTO credentials (username, password) VALUES (?, ?)`,
- c.Username, c.Password,
+ c.Username, c.Password[:],
)
return err
}
func GetByUsername(username string) (Credential, error) {
var c Credential
+ var password []byte
err := db.QueryRow(
`SELECT username, password FROM credentials WHERE username = ?`,
username,
- ).Scan(&c.Username, &c.Password)
+ ).Scan(&c.Username, &password)
+ copy(c.Password[:], password)
return c, err
}
+
+func NewSession(token uuid.UUID) error {
+ _, err := db.Exec(
+ `INSERT INTO sessions (token) VALUES (?)`, token[:],
+ )
+ return err
+}
+
+func SessionExists(token uuid.UUID) bool {
+ var t uuid.UUID
+ err := db.QueryRow(
+ `SELECT token FROM sessions WHERE token = ?`,
+ token,
+ ).Scan(&t)
+ return err == nil
+}
diff --git a/auth/router.go b/auth/router.go
@@ -3,7 +3,7 @@ package main
import "net/http"
func router() {
- // http.HandleFunc("POST /login", login)
+ http.HandleFunc("POST /login", login)
// http.HandleFunc("POST /logout", logout)
http.HandleFunc("POST /credentials", credentials)
// http.HandleFunc("GET /{uuid}", authenticated)