Handle failed login
All checks were successful
Build and push Probo image on the registry / login (push) Successful in 3m0s

This commit is contained in:
Andrea Fazzi 2025-04-29 10:15:39 +02:00
commit e87e7b3bc5
8 changed files with 31 additions and 86 deletions

View file

@ -110,7 +110,7 @@ func runServer(cmd *cobra.Command, args []string) {
logLevel := log.Level(viper.GetInt("log_level"))
log.SetLevel(logLevel)
log.Debug("Log level", "debug", log.GetLevel())
log.Info("Log", "level", log.GetLevel())
if viper.GetBool("git_sync") {
syncGitRepo()
@ -145,7 +145,7 @@ func runServer(cmd *cobra.Command, args []string) {
// If there is an error during
// recovery, respond with a
// 500 Internal Server Error
log.Fatal(err)
log.Error(err)
http.Error(w, fmt.Sprintf("Recovering from %v", err), http.StatusInternalServerError)
}
@ -218,7 +218,17 @@ func runServer(cmd *cobra.Command, args []string) {
nil,
filepath.Join(templatesPath, "home", "layout.html.tmpl"),
filepath.Join(templatesPath, "home", "home.html.tmpl")).
WithMiddlewares(middlewares.Panic)
WithMiddlewares(middlewares.Panic).
Func(func(h *templatehandler.Handler, w http.ResponseWriter, r *http.Request) {
invalidCredentials := false
if r.URL.Query().Get("invalidCredentials") != "" {
invalidCredentials = true
}
err := h.WithData(map[string]bool{"InvalidCredentials": invalidCredentials}).ExecuteTemplate(w)
if err != nil {
panic(err)
}
})
rtSessionHandler := handlers.New().
WithMiddlewares(middlewares.Panic).

View file

@ -13,11 +13,11 @@ import (
"github.com/spf13/viper"
)
type ParticipantNotFoundErr struct {
type ErrParticipantNotFound struct {
Token string
}
func (e ParticipantNotFoundErr) Error() string {
func (e ErrParticipantNotFound) Error() string {
return fmt.Sprintf("Participant with token %s was not found in the store", e.Token)
}
@ -61,6 +61,9 @@ func LoginHandler(h *template.Handler, w http.ResponseWriter, r *http.Request, s
http.Redirect(w, r, "/sessions", http.StatusSeeOther)
return
} else {
log.Warn("Admin authentication failed")
http.Redirect(w, r, "/login?invalidCredentials=true", http.StatusSeeOther)
}
} else {
var (
@ -84,21 +87,25 @@ func LoginHandler(h *template.Handler, w http.ResponseWriter, r *http.Request, s
}
if loggedParticipant == nil {
panic(ParticipantNotFoundErr{pToken})
log.Warn("Authentication failed", "token", pToken)
http.Redirect(w, r, "/?invalidCredentials=true", http.StatusSeeOther)
return
}
log.Info("Participant logged in as", "participant", loggedParticipant)
setAuthorizationCookie(w, loggedParticipant.String(), pToken, false)
log.Info("Released JWT for participant", "username", loggedParticipant)
log.Info("Redirect to", "url", "/sessions")
log.Info("Logged in", "username", loggedParticipant)
http.Redirect(w, r, "/sessions", http.StatusSeeOther)
}
}
err := h.ExecuteTemplate(w)
invalidCredentials := false
if r.URL.Query().Get("invalidCredentials") != "" {
invalidCredentials = true
}
err := h.WithData(map[string]bool{"InvalidCredentials": invalidCredentials}).ExecuteTemplate(w)
if err != nil {
panic(err)
}

View file

@ -1,30 +0,0 @@
package serve
import (
"net/http"
"github.com/charmbracelet/log"
)
func Recover(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
err := recover()
if err != nil {
log.Error("Recovering from", "err", err)
switch e := err.(type) {
case ParticipantNotFoundErr:
log.Error("Participant not found", "token", e.Token)
http.Redirect(w, r, "/", http.StatusSeeOther)
default:
http.Error(w, err.(error).Error(), http.StatusInternalServerError)
}
}
}()
next.ServeHTTP(w, r)
})
}

View file

@ -1,33 +0,0 @@
package serve
import (
"net/http"
"github.com/charmbracelet/log"
"github.com/spf13/viper"
"golang.org/x/net/webdav"
)
func WebdavServer() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// username, password, _ := r.BasicAuth()
//log.Info("Connecting to webdav", "username", username, "password", password)
//if username == "davuser" && password == "pass" {
handler := &webdav.Handler{
Prefix: "/webdav",
FileSystem: webdav.Dir(viper.GetString("sessions_path")),
LockSystem: webdav.NewMemLS(),
Logger: func(r *http.Request, err error) {
if err != nil {
log.Fatal("WebDAV", "method", r.Method, "url", r.URL, "err", err)
} else {
log.Info("WebDAV", "method", r.Method, "url", r.URL)
}
},
}
handler.ServeHTTP(w, r)
//}
})
}

View file

@ -37,13 +37,8 @@
<input type="password"name="password"placeholder="Password" required/>
</fieldset>
<input type="submit" value="Sign in"/>
{{ if .InvalidCredentials }}<small>Invalid credentials!</small>{{end}}
</form>
{{ if .Invalid }}
<hr/>
<section>
<small>Login failed!</small>
</section>
{{ end }}
</article>
</div>
{{ end }}

View file

@ -5,11 +5,6 @@
<input type="password" name="password" placeholder="Password" required/>
</fieldset>
<input type="submit" value="Sign in"/>
{{ if .InvalidCredentials }}<small>Invalid credentials!</small>{{end}}
</form>
{{ if .Invalid }}
<hr/>
<section>
<small>Login failed!</small>
</section>
{{ end }}
{{ end }}

View file

@ -5,7 +5,7 @@
{{if $.IsAdmin}}
<header>{{$session.Title}} - {{$session.NumParticipants}} participants</header>
{{else}}
<header>{{$session.Title}}</header>
<header><h1>{{$session.Title}}</h1></header>
{{end}}
{{$session.Description}}
{{if $.IsAdmin}}

View file

@ -1,6 +1,7 @@
#!/bin/bash
# Set environment variables
export PROBO_LOG_LEVEL=-4
export PROBO_SESSIONS_PATH=embed/data/sessions
export PROBO_RESPONSES_PATH=embed/data/responses
export PROBO_TEMPLATES_PATH=embed/data/templates