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.db
.[0-9a-f]*
vino.sqlite*
invinoveritas
\ No newline at end of file
......@@ -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) {
var token string
cookie, err := r.Cookie("session")
if err != nil {
anonAuth(w, r)
return
if err == nil {
token = cookie.Value
}
level.Debug(log.Get(r)).
Log("cookie", cookie, "msg", "got auth cookie")
user, err := provider.Valid(r.Context(), cookie.Value)
user, err := provider.Valid(r.Context(), token)
if errors.Is(err, ErrAuthFailed) {
anonAuth(w, r)
return
......
......@@ -33,6 +33,8 @@ func (h Handler) index() http.Handler {
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" {
wines, err := h.Q.ListWines(r.Context())
if err != nil {
......
......@@ -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 {
level.Debug(log.GetContext(ctx)).Log("msg", "checking session token", "token", token)
user.Name, err = q.IsValidSession(ctx, token)
if errors.Is(err, sql.ErrNoRows) {
// 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 {
return fmt.Errorf("executing auth template: %w", err)
}
return nil
return auth.ErrAuthFailed
}
if err != nil {
return fmt.Errorf("getting salted password: %w", err)
......@@ -215,7 +217,7 @@ func (a Provider) Handler(templateFS fs.FS) http.Handler {
return fmt.Errorf("executing auth template: %w", err)
}
return nil
return auth.ErrAuthFailed
}
log = kitlog.With(log, "user_id", userData.UserID)
......@@ -252,7 +254,7 @@ func (a Provider) Handler(templateFS fs.FS) http.Handler {
}
level.Debug(log).
Log("cookie", cookie, "msg", "setting token cookie")
Log("cookie", fmt.Sprintf("%#v", cookie), "msg", "setting token 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