refactoring of PrivateHandleFunc()

This commit is contained in:
Settel 2021-08-05 15:35:39 +02:00
parent 2b3e2c7dc6
commit 55503395a4
6 changed files with 28 additions and 30 deletions

View File

@ -3,9 +3,18 @@ package application
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"sirlab.de/go/knyt/user"
) )
func (app *Application) SyncHandler(w http.ResponseWriter, r *http.Request) { func (app *Application) SyncHandler(usr *user.User, w http.ResponseWriter, r *http.Request) {
// usrId :=
// usr := app.GetUserById(usrId)
// if usr.Game != gameRef && usr.IsAdmin() {
// w.WriteHeader(http.StatusForbidden)
// fmt.Fprintf(w, "forbidden")
// return
// }
gameRef := r.URL.Query().Get("g") gameRef := r.URL.Query().Get("g")
gm, err := app.GetGameById(gameRef) gm, err := app.GetGameById(gameRef)
if err != nil { if err != nil {

View File

@ -5,7 +5,7 @@ import (
) )
type Game struct { type Game struct {
id string id string
Players []string `json:"players"` Name string `json:"name"`
eng *engine.Engine eng *engine.Engine
} }

View File

@ -3,9 +3,11 @@ package handler
import ( import (
"net/http" "net/http"
"sirlab.de/go/knyt/application" "sirlab.de/go/knyt/application"
"sirlab.de/go/knyt/user"
) )
type HandlerFunc func(http.ResponseWriter, *http.Request) type HandlerFunc func(http.ResponseWriter, *http.Request)
type PrivateHandlerFunc func(*user.User, http.ResponseWriter, *http.Request)
type AuthMux struct { type AuthMux struct {
mux *http.ServeMux mux *http.ServeMux

View File

@ -6,19 +6,15 @@ import (
"sirlab.de/go/knyt/user" "sirlab.de/go/knyt/user"
) )
func (authMux *AuthMux) PrivateHandleFunc(pattern string, handlerFunc HandlerFunc) { func (authMux *AuthMux) PrivateHandleFunc(pattern string, handlerFunc PrivateHandlerFunc) {
authMux.mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) { authMux.mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
if authMux.isAuthenticated(r) { usr, err := authMux.getUserFromSession(r)
handlerFunc(w, r) if err != nil {
authMux.accessDenied(w, r)
return return
} }
authMux.accessDenied(w, r)
})
}
func (authMux *AuthMux) PrivateHandle(pattern string, handler http.Handler) { handlerFunc(usr, w, r)
authMux.PrivateHandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
handler.ServeHTTP(w, r)
}) })
} }
@ -27,16 +23,6 @@ func (authMux *AuthMux) accessDenied(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Forbidden") fmt.Fprintf(w, "Forbidden")
} }
func (authMux *AuthMux) isAuthenticated(r *http.Request) bool {
_, err := authMux.getUserFromSession(r)
if err != nil {
return false
}
return true
}
func (authMux *AuthMux) getUserFromSession(r *http.Request) (*user.User, error) { func (authMux *AuthMux) getUserFromSession(r *http.Request) (*user.User, error) {
authCookie, err := r.Cookie("knyt-auth") authCookie, err := r.Cookie("knyt-auth")
if err != nil { if err != nil {

View File

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"sirlab.de/go/knyt/user"
) )
type userLight struct { type userLight struct {
@ -12,13 +13,7 @@ type userLight struct {
Game string `json:"game"` Game string `json:"game"`
} }
func (authMux *AuthMux) GetUserInfo(w http.ResponseWriter, r *http.Request) { func (authMux *AuthMux) GetUserInfo(usr *user.User, w http.ResponseWriter, r *http.Request) {
usr, err := authMux.getUserFromSession(r)
if err != nil {
authMux.accessDenied(w, r)
return
}
usrLight := userLight{ usrLight := userLight{
Name: usr.Name, Name: usr.Name,
Role: usr.Role, Role: usr.Role,

View File

@ -0,0 +1,6 @@
package statement
type Statement struct {
Id string `json:"id"`
Statement string `json:"statement"`
}