Skip to content
Snippets Groups Projects
Commit 87419b8f authored by gbe's avatar gbe
Browse files

Load users from DB

parent 5d17134f
No related branches found
No related tags found
No related merge requests found
...@@ -2,6 +2,8 @@ package auth ...@@ -2,6 +2,8 @@ package auth
import ( import (
"context" "context"
"fmt"
"log"
"net/http" "net/http"
) )
...@@ -9,7 +11,7 @@ type contextKey string ...@@ -9,7 +11,7 @@ type contextKey string
type Provider interface { type Provider interface {
// Returns true if pass is a valid password for the given user // Returns true if pass is a valid password for the given user
Valid(user, pass string) bool Valid(ctx context.Context, user, pass string) (bool, error)
} }
// Require wraps hdlr so that it requires authentication to use. Requests handled by hdlr will have the user // Require wraps hdlr so that it requires authentication to use. Requests handled by hdlr will have the user
...@@ -24,7 +26,18 @@ func Require(hdlr http.HandlerFunc, provider Provider) http.HandlerFunc { ...@@ -24,7 +26,18 @@ func Require(hdlr http.HandlerFunc, provider Provider) http.HandlerFunc {
return return
} }
if !provider.Valid(user, pass) { valid, err := provider.Valid(r.Context(), user, pass)
if err != nil {
log.Printf("can't authenticate %s %s from %s: %s", r.Method, r.URL, r.RemoteAddr, err)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintln(w, "can't confirm your authentication")
return
}
if !valid {
w.Header().Add("WWW-Authenticate", `Basic realm="In Vino Veritas"`) w.Header().Add("WWW-Authenticate", `Basic realm="In Vino Veritas"`)
w.WriteHeader(http.StatusUnauthorized) w.WriteHeader(http.StatusUnauthorized)
......
package main package main
import ( import (
"context"
"database/sql"
"embed" "embed"
"errors"
"log" "log"
"net/http" "net/http"
"github.com/Masterminds/squirrel"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
_ "modernc.org/sqlite" // Imported for side effects: registers DB driver _ "modernc.org/sqlite" // Imported for side effects: registers DB driver
...@@ -31,14 +35,35 @@ func httpError(w http.ResponseWriter, msg string, err error, status int) { ...@@ -31,14 +35,35 @@ func httpError(w http.ResponseWriter, msg string, err error, status int) {
http.Error(w, msg, status) http.Error(w, msg, status)
} }
type authProvider struct{} type authProvider struct {
db *sqlx.DB
}
func (a authProvider) Valid(ctx context.Context, user, pass string) (bool, error) {
query, args, err := squirrel.Select("password").
From("users").
Where(squirrel.Eq{"name": user}).
ToSql()
if err != nil {
return false, err
}
var dbPass string
err = a.db.GetContext(ctx, &dbPass, query, args...)
if errors.Is(err, sql.ErrNoRows) {
// User not found isn't an error, it's just an invalid auth.
return false, nil
}
if err != nil {
return false, err
}
func (a authProvider) Valid(user, pass string) bool { if dbPass == pass {
if user == "wine" && pass == "potatoe" { return true, nil
return true
} }
return false return false, nil
} }
func logRequest(r *http.Request) { func logRequest(r *http.Request) {
...@@ -65,7 +90,9 @@ func main() { ...@@ -65,7 +90,9 @@ func main() {
db: db, db: db,
} }
ap := authProvider{} ap := authProvider{
db: db,
}
http.HandleFunc("/details/img", auth.Require(http.HandlerFunc(handler.img), ap)) http.HandleFunc("/details/img", auth.Require(http.HandlerFunc(handler.img), ap))
http.HandleFunc("/details/", auth.Require(http.HandlerFunc(handler.details), ap)) http.HandleFunc("/details/", auth.Require(http.HandlerFunc(handler.details), ap))
......
CREATE TABLE users (
name TEXT,
password TEXT,
UNIQUE(name)
);
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment