refactor: extract checkAuthCode()

This commit is contained in:
Settel 2024-02-18 10:29:20 +01:00
parent ca609fad94
commit a06e50b704
3 changed files with 27 additions and 20 deletions

View File

@ -11,7 +11,7 @@ func (authMux *AuthMux) Cameo(usr *user.User, w http.ResponseWriter, r *http.Req
if usr.IsAdmin() {
cookie := authMux.createCookie()
cookie.Name = cookie.Name + "-cameo"
usrCameo, err := authMux.checkCode(r)
usrCameo, err := authMux.checkAuthCode(r)
if err != nil {
http.SetCookie(w, cookie)
authMux.accessDenied(w, r)

View File

@ -0,0 +1,25 @@
package handler
import (
"fmt"
"net/http"
"sirlab.de/go/knowyt/user"
)
func (authMux *AuthMux) checkAuthCode(r *http.Request) (*user.User, error) {
r.ParseForm()
form := r.Form
code := form.Get("code")
if len(code) != 6 {
return nil, fmt.Errorf("invalid code \"%s\"", code)
}
usr, err := authMux.app.GetUserByAuthcode(code)
if err != nil {
return nil, fmt.Errorf("invalid code: \"%s\"", code)
}
return usr, nil
}

View File

@ -5,7 +5,6 @@ import (
"net/http"
"sirlab.de/go/knowyt/log"
"sirlab.de/go/knowyt/user"
)
func (authMux *AuthMux) createCookie() *http.Cookie {
@ -25,7 +24,7 @@ func (authMux *AuthMux) Logout(w http.ResponseWriter, r *http.Request) {
}
func (authMux *AuthMux) Login(w http.ResponseWriter, r *http.Request) {
usr, err := authMux.checkCode(r)
usr, err := authMux.checkAuthCode(r)
if err != nil {
log.ErrorLog(err)
http.SetCookie(w, authMux.createCookie())
@ -52,20 +51,3 @@ func (authMux *AuthMux) Login(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/plain")
fmt.Fprintf(w, "ok")
}
func (authMux *AuthMux) checkCode(r *http.Request) (*user.User, error) {
r.ParseForm()
form := r.Form
code := form.Get("code")
if len(code) != 6 {
return nil, fmt.Errorf("invalid code \"%s\"", code)
}
usr, err := authMux.app.GetUserByAuthcode(code)
if err != nil {
return nil, fmt.Errorf("invalid code: \"%s\"", code)
}
return usr, nil
}