#73 setup password hashing with argin2id and update some infra
Some checks failed
Build Docker Image / Explore-Gitea-Actions (push) Failing after 59s

This commit is contained in:
Tim
2024-08-10 00:14:38 +02:00
parent 2f1b4fc8a7
commit 0df0e7f6f9
15 changed files with 139 additions and 344 deletions

42
api/utils/auth.go Normal file
View File

@@ -0,0 +1,42 @@
package utils
import (
// "context"
"errors"
// "log"
"net/http"
)
type UserId string
func GetUserOrWriteInResponse(w http.ResponseWriter, r *http.Request) (UserId, error) {
tokenStr := r.Header.Get("Authorization")
if (tokenStr == "") || (len(tokenStr) < len("Bearer ")) {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return "", errors.New("Unauthorized")
}
tokenStr = tokenStr[len("Bearer "):]
// token, err := verifyToken(tokenStr)
// if err != nil {
// log.Println(err)
// http.Error(w, "Unauthorized", http.StatusUnauthorized)
// return "", errors.New("Unauthorized")
// }
// return UserId(token.UID), nil
return UserId("xxx"), nil
}
// func verifyToken(token string) (*auth.Token, error) {
// if app == nil {
// setup()
// }
//
// client, err := app.Auth(context.Background())
// if err != nil {
// log.Fatalf("error getting Auth client: %v\n", err)
// }
// return client.VerifyIDToken(context.Background(), token)
// }

32
api/utils/db.go Normal file
View File

@@ -0,0 +1,32 @@
package utils
import (
"database/sql"
"log"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/sqlite3"
_ "github.com/golang-migrate/migrate/v4/source/file"
)
func RunMigrations(db *sql.DB) {
driver, err := sqlite3.WithInstance(db, &sqlite3.Config{})
if err != nil {
log.Fatal(err)
}
m, err := migrate.NewWithDatabaseInstance(
"file://./migrations/",
"",
driver)
if err != nil {
log.Fatal("Could not create migrations instance: ", err)
}
err = m.Up()
if err != nil {
if err.Error() != "no change" {
log.Fatal("Could not run migrations: ", err)
}
}
}

11
api/utils/http-utils.go Normal file
View File

@@ -0,0 +1,11 @@
package utils
import (
"encoding/json"
"net/http"
)
func WriteJSON(w http.ResponseWriter, data interface{}) error {
w.Header().Set("Content-Type", "application/json")
return json.NewEncoder(w).Encode(data)
}