Skip to content
Snippets Groups Projects
main.go 2.75 KiB
Newer Older
gbe's avatar
gbe committed
package main

import (
gbe's avatar
gbe committed
	"context"
gbe's avatar
gbe committed
	"crypto/sha256"
gbe's avatar
gbe committed
	"embed"
gbe's avatar
gbe committed
	"fmt"
gbe's avatar
gbe committed
	"log"
	"net/http"
gbe's avatar
gbe committed

	"github.com/jmoiron/sqlx"
	_ "modernc.org/sqlite" // Imported for side effects: registers DB driver

	"git.c3pb.de/gbe/invinoveritas/auth"
gbe's avatar
gbe committed
)

//go:embed templates/*.tpl
var templateFS embed.FS

//go:embed static/*
var staticFS embed.FS

gbe's avatar
gbe committed
type Handler struct {
	db *sqlx.DB
}

gbe's avatar
gbe committed
func httpError(w http.ResponseWriter, msg string, err error, status int) {
gbe's avatar
gbe committed
	if err != nil {
		msg += ": " + err.Error()
	}

	log.Println(msg)

	http.Error(w, msg, status)
func hashPassword(ctx context.Context, db *sqlx.DB, user, pass string) (string, error) {
gbe's avatar
gbe committed
	// Look up password salt from DB and hash password with it
	query := `SELECT val FROM state WHERE key = 'pwsalt'`

	var salt []byte
	err := db.GetContext(ctx, &salt, query)
	if err != nil {
		return "", err
	}

	h := sha256.New()

	_, err = h.Write(salt)
	if err != nil {
		return "", err
	}

	fmt.Fprint(h, ":", user, ":", pass)
gbe's avatar
gbe committed

	return fmt.Sprintf("%02x", h.Sum(nil)), nil
}

gbe's avatar
gbe committed
type authProvider struct {
	db *sqlx.DB
}

func (a authProvider) Valid(ctx context.Context, userName, pass string) (*auth.User, error) {
gbe's avatar
gbe committed
	query := `SELECT count(*) FROM users;`

	var count int
	err := a.db.GetContext(ctx, &count, query)
gbe's avatar
gbe committed
	if err != nil {
		return nil, err
	}

	user := &auth.User{
		Name: userName,
gbe's avatar
gbe committed
	}

gbe's avatar
gbe committed
	// If no entry in user DB, just allow everything, so that initial user creation works.
	if count == 0 {
		log.Println("user database empty, allowing access by", user, "regardless of password")
gbe's avatar
gbe committed
	}

	hashedPw, err := hashPassword(ctx, a.db, userName, pass)
gbe's avatar
gbe committed

	log.Printf("hashed pw for %s: %s", userName, hashedPw)
gbe's avatar
gbe committed

	query = `SELECT rowid, name FROM users WHERE name = ? AND password = ?`
gbe's avatar
gbe committed

	err = a.db.GetContext(ctx, user, query, userName, hashedPw)
gbe's avatar
gbe committed
	if err != nil {
}

func logRequest(r *http.Request) {
	log.Println("handling", r.Method, r.URL, "from", r.RemoteAddr, "by", auth.Get(r))
gbe's avatar
gbe committed
func main() {
gbe's avatar
gbe committed
	db, err := sqlx.Open("sqlite", "vino.sqlite")
	if err != nil {
		log.Fatalln("can't open DB:", err)
	}
	defer db.Close()

	err = initDB(db)
	if err != nil {
		log.Fatalln("can't initialize DB:", err)
	}

	http.HandleFunc("/favicon.ico", http.NotFound)

gbe's avatar
gbe committed
	http.Handle("/static/", http.FileServer(http.FS(staticFS)))

gbe's avatar
gbe committed
	handler := Handler{
		db: db,
	}
gbe's avatar
gbe committed

gbe's avatar
gbe committed
	ap := authProvider{
		db: db,
	}

	http.HandleFunc("/details/img", auth.Require(http.HandlerFunc(handler.img), ap))
	http.HandleFunc("/details/", auth.Require(http.HandlerFunc(handler.details), ap))
	http.HandleFunc("/", auth.Require(http.HandlerFunc(handler.index), ap))
gbe's avatar
gbe committed

gbe's avatar
gbe committed
	const listenAddr = ":7878"
gbe's avatar
gbe committed

	log.Printf("here we go, listening on http://%s", listenAddr)

gbe's avatar
gbe committed
	err = http.ListenAndServe(listenAddr, nil)
gbe's avatar
gbe committed
	if err != nil {
gbe's avatar
gbe committed
		log.Fatalln("http handler failed:", err)
gbe's avatar
gbe committed
	}
}