Skip to content
Snippets Groups Projects
Commit 0545c7b8 authored by gbe's avatar gbe
Browse files

fix auth stuff on empty db

parent 8ab51aab
No related branches found
No related tags found
No related merge requests found
vino.ql vino.sqlite*
vino.db
.[0-9a-f]*
invinoveritas invinoveritas
\ No newline at end of file
...@@ -69,16 +69,14 @@ func Require(next http.Handler, authFailed http.Handler, provider Provider) http ...@@ -69,16 +69,14 @@ func Require(next http.Handler, authFailed http.Handler, provider Provider) http
} }
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var token string
cookie, err := r.Cookie("session") cookie, err := r.Cookie("session")
if err != nil { if err == nil {
anonAuth(w, r) token = cookie.Value
return
} }
level.Debug(log.Get(r)). user, err := provider.Valid(r.Context(), token)
Log("cookie", cookie, "msg", "got auth cookie")
user, err := provider.Valid(r.Context(), cookie.Value)
if errors.Is(err, ErrAuthFailed) { if errors.Is(err, ErrAuthFailed) {
anonAuth(w, r) anonAuth(w, r)
return return
......
...@@ -33,6 +33,8 @@ func (h Handler) index() http.Handler { ...@@ -33,6 +33,8 @@ func (h Handler) index() http.Handler {
tpl = template.Must(template.ParseFS(templateFS, "templates/base.tpl", "templates/index.tpl")) tpl = template.Must(template.ParseFS(templateFS, "templates/base.tpl", "templates/index.tpl"))
}) })
level.Debug(log.Get(r)).Log("user", auth.Get(r))
if r.Method == "GET" { if r.Method == "GET" {
wines, err := h.Q.ListWines(r.Context()) wines, err := h.Q.ListWines(r.Context())
if err != nil { if err != nil {
......
...@@ -97,6 +97,8 @@ func (a Provider) Valid(ctx context.Context, token string) (*auth.User, error) { ...@@ -97,6 +97,8 @@ func (a Provider) Valid(ctx context.Context, token string) (*auth.User, error) {
) )
err = a.q.RunTx(ctx, func(ctx context.Context, q *query.Queries) error { err = a.q.RunTx(ctx, func(ctx context.Context, q *query.Queries) error {
level.Debug(log.GetContext(ctx)).Log("msg", "checking session token", "token", token)
user.Name, err = q.IsValidSession(ctx, token) user.Name, err = q.IsValidSession(ctx, token)
if errors.Is(err, sql.ErrNoRows) { if errors.Is(err, sql.ErrNoRows) {
// Let's see if there are any users at all. If not, we let 'em in. // Let's see if there are any users at all. If not, we let 'em in.
...@@ -188,7 +190,7 @@ func (a Provider) Handler(templateFS fs.FS) http.Handler { ...@@ -188,7 +190,7 @@ func (a Provider) Handler(templateFS fs.FS) http.Handler {
return fmt.Errorf("executing auth template: %w", err) return fmt.Errorf("executing auth template: %w", err)
} }
return nil return auth.ErrAuthFailed
} }
if err != nil { if err != nil {
return fmt.Errorf("getting salted password: %w", err) return fmt.Errorf("getting salted password: %w", err)
...@@ -215,7 +217,7 @@ func (a Provider) Handler(templateFS fs.FS) http.Handler { ...@@ -215,7 +217,7 @@ func (a Provider) Handler(templateFS fs.FS) http.Handler {
return fmt.Errorf("executing auth template: %w", err) return fmt.Errorf("executing auth template: %w", err)
} }
return nil return auth.ErrAuthFailed
} }
log = kitlog.With(log, "user_id", userData.UserID) log = kitlog.With(log, "user_id", userData.UserID)
...@@ -252,7 +254,7 @@ func (a Provider) Handler(templateFS fs.FS) http.Handler { ...@@ -252,7 +254,7 @@ func (a Provider) Handler(templateFS fs.FS) http.Handler {
} }
level.Debug(log). level.Debug(log).
Log("cookie", cookie, "msg", "setting token cookie") Log("cookie", fmt.Sprintf("%#v", cookie), "msg", "setting token cookie")
http.SetCookie(w, &cookie) http.SetCookie(w, &cookie)
......
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