feat: log failed and successful login attempts
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Settel 2022-11-06 17:00:28 +01:00
parent 9502e70d3a
commit 2f58916cba
2 changed files with 8 additions and 5 deletions

View File

@ -3,6 +3,8 @@ package handler
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"sirlab.de/go/knowyt/log"
"sirlab.de/go/knowyt/user" "sirlab.de/go/knowyt/user"
) )
@ -25,17 +27,21 @@ func (authMux *AuthMux) Logout(w http.ResponseWriter, r *http.Request) {
func (authMux *AuthMux) Login(w http.ResponseWriter, r *http.Request) { func (authMux *AuthMux) Login(w http.ResponseWriter, r *http.Request) {
usr, err := authMux.checkCode(r) usr, err := authMux.checkCode(r)
if err != nil { if err != nil {
log.ErrorLog(err)
http.SetCookie(w, authMux.createCookie()) http.SetCookie(w, authMux.createCookie())
authMux.accessDenied(w, r) authMux.accessDenied(w, r)
return return
} }
log.Info("%s logged into game %s\n", usr.GetName(), usr.GetGameId())
cookie := authMux.createCookie() cookie := authMux.createCookie()
cookie.Value = usr.GetId() + ":" + usr.GetAuthCode() cookie.Value = usr.GetId() + ":" + usr.GetAuthCode()
cookie.MaxAge = 0 cookie.MaxAge = 0
http.SetCookie(w, cookie) http.SetCookie(w, cookie)
w.Header().Add("Content-Type", "text/plain") w.Header().Add("Content-Type", "text/plain")
fmt.Fprintf(w, "ok") fmt.Fprintf(w, "ok")
} }
func (authMux *AuthMux) checkCode(r *http.Request) (*user.User, error) { func (authMux *AuthMux) checkCode(r *http.Request) (*user.User, error) {
@ -44,12 +50,12 @@ func (authMux *AuthMux) checkCode(r *http.Request) (*user.User, error) {
code := form.Get("code") code := form.Get("code")
if len(code) != 6 { if len(code) != 6 {
return nil, fmt.Errorf("invalid code") return nil, fmt.Errorf("invalid code \"%s\"", code)
} }
usr, err := authMux.app.GetUserByAuthcode(code) usr, err := authMux.app.GetUserByAuthcode(code)
if err != nil { if err != nil {
return nil, fmt.Errorf("invalid code") return nil, fmt.Errorf("invalid code: \"%s\"", code)
} }
return usr, nil return usr, nil

View File

@ -52,8 +52,5 @@ func Error(format string, v ...any) {
} }
func ErrorLog(err error) { func ErrorLog(err error) {
lock.Lock()
defer lock.Unlock()
Error("%v\n", err) Error("%v\n", err)
} }