diff --git a/client/dist/index.html b/client/dist/index.html index 75ddc0e..381b1a5 100644 --- a/client/dist/index.html +++ b/client/dist/index.html @@ -5,6 +5,9 @@ KnYC -KnYC +

KnYC

+

+ /private/ +

diff --git a/server/src/app/app.go b/server/src/app/app.go deleted file mode 100644 index dd53425..0000000 --- a/server/src/app/app.go +++ /dev/null @@ -1,16 +0,0 @@ -package app - -import ( - "sirlab.de/go/knyt/handler" -) - -type App struct { - Config AppConfig - Mux *handler.AuthMux -} - -func NewApp(config AppConfig) *App { - return &App{ - Config: config, - } -} diff --git a/server/src/app/appconfig.go b/server/src/app/appconfig.go deleted file mode 100644 index 15ea124..0000000 --- a/server/src/app/appconfig.go +++ /dev/null @@ -1,11 +0,0 @@ -package app - -type AppConfig struct { - DataDir string -} - -func NewAppConfig() AppConfig { - return AppConfig{ - DataDir: "data/", - } -} diff --git a/server/src/application/appconfig.go b/server/src/application/appconfig.go new file mode 100644 index 0000000..e7d98f2 --- /dev/null +++ b/server/src/application/appconfig.go @@ -0,0 +1,11 @@ +package application + +type ApplicationConfig struct { + DataDir string +} + +func NewApplicationConfig() ApplicationConfig { + return ApplicationConfig{ + DataDir: "data/", + } +} diff --git a/server/src/application/application.go b/server/src/application/application.go new file mode 100644 index 0000000..d954c01 --- /dev/null +++ b/server/src/application/application.go @@ -0,0 +1,26 @@ +package application + +import ( + "sirlab.de/go/knyt/user" + "sirlab.de/go/knyt/users" +) + +type Application struct { + Config ApplicationConfig + users users.Users +} + +func NewApplication(config ApplicationConfig) *Application { + app := Application{ + Config: config, + users: users.NewUsers(), + } + + app.users.AddUser(user.NewUser("120", "Settel", user.ROLE_ADMIN)) + + return &app +} + +func (app *Application) GetUsers() *users.Users { + return &app.users +} diff --git a/server/src/handler/authmux.go b/server/src/handler/authmux.go index f20fd4e..688cdaa 100644 --- a/server/src/handler/authmux.go +++ b/server/src/handler/authmux.go @@ -2,6 +2,7 @@ package handler import ( "net/http" + "sirlab.de/go/knyt/application" ) type HandlerFunc func(http.ResponseWriter, *http.Request) @@ -9,10 +10,12 @@ type HandlerFunc func(http.ResponseWriter, *http.Request) type AuthMux struct { mux *http.ServeMux Port int + app *application.Application } -func NewAuthMux() *AuthMux { +func NewAuthMux(app *application.Application) *AuthMux { mux := AuthMux{ + app: app, mux: http.NewServeMux(), Port: 32039, } diff --git a/server/src/handler/private.go b/server/src/handler/private.go new file mode 100644 index 0000000..ed38a1e --- /dev/null +++ b/server/src/handler/private.go @@ -0,0 +1,43 @@ +package handler + +import ( + "fmt" + "net/http" +) + +func (authMux *AuthMux) PrivateHandleFunc(pattern string, handlerFunc HandlerFunc) { + authMux.mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) { + if authMux.isAuthenticated(r) { + handlerFunc(w, r) + return + } + authMux.accessDenied(w, r) + }) +} + +func (authMux *AuthMux) PrivateHandle(pattern string, handler http.Handler) { + authMux.PrivateHandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) { + handler.ServeHTTP(w, r) + }) +} + +func (authMux *AuthMux) accessDenied(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusForbidden) + fmt.Fprintf(w, "Forbidden") +} + +func (authMux *AuthMux) isAuthenticated(r *http.Request) bool { + authCookie, err := r.Cookie("knyt-auth") + if err != nil { + return false + } + fmt.Printf("authCookie: %s\n", authCookie.Value) + + usr, usrErr := authMux.app.GetUsers().GetUserById(authCookie.Value) + if usrErr != nil { + return false + } + + fmt.Printf("%v\n", usr) + return true +} diff --git a/server/src/knyt.go b/server/src/knyt.go index bcac18c..dd222cc 100644 --- a/server/src/knyt.go +++ b/server/src/knyt.go @@ -3,22 +3,23 @@ package main import ( "fmt" "net/http" - "sirlab.de/go/knyt/app" + "sirlab.de/go/knyt/application" "sirlab.de/go/knyt/handler" ) func main() { - appConfig := app.NewAppConfig() - App := app.NewApp(appConfig) - App.Mux = handler.NewAuthMux() + appConfig := application.NewApplicationConfig() + app := application.NewApplication(appConfig) + mux := handler.NewAuthMux(app) - App.Mux.PublicHandleFunc("/__intern__/exit", handler.Exit) + mux.PublicHandleFunc("/__intern__/exit", handler.Exit) // default handler fsHandler := http.FileServer(http.Dir("../../client/dist/")) - App.Mux.PublicHandle("/", fsHandler) + mux.PrivateHandle("/private/", fsHandler) + mux.PublicHandle("/", fsHandler) // start listening - fmt.Printf("http://localhost:%d/\n", App.Mux.Port) - App.Mux.ListenAndServe() + fmt.Printf("http://localhost:%d/\n", mux.Port) + mux.ListenAndServe() } diff --git a/server/src/users/users.go b/server/src/users/users.go index fa76cda..ea7aa45 100644 --- a/server/src/users/users.go +++ b/server/src/users/users.go @@ -1,6 +1,7 @@ package users import ( + "fmt" "sirlab.de/go/knyt/user" ) @@ -14,6 +15,10 @@ func (users Users) AddUser(usr *user.User) { users[usr.Id] = usr } -func (users Users) GetUserById(id string) *user.User { - return users[id] +func (users Users) GetUserById(id string) (*user.User, error) { + usr := users[id] + if usr == nil { + return nil, fmt.Errorf("unknown id") + } + return usr, nil }