user management

This commit is contained in:
Settel 2021-08-01 19:06:33 +02:00
parent 59a81fd73e
commit 685bf8d69c
9 changed files with 104 additions and 39 deletions

View File

@ -5,6 +5,9 @@
<title>KnYC</title> <title>KnYC</title>
</head> </head>
<body> <body>
KnYC <h1>KnYC</h1>
<p>
<a href="/private/">/private/</a>
</p>
</body> </body>
</html> </html>

View File

@ -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,
}
}

View File

@ -1,11 +0,0 @@
package app
type AppConfig struct {
DataDir string
}
func NewAppConfig() AppConfig {
return AppConfig{
DataDir: "data/",
}
}

View File

@ -0,0 +1,11 @@
package application
type ApplicationConfig struct {
DataDir string
}
func NewApplicationConfig() ApplicationConfig {
return ApplicationConfig{
DataDir: "data/",
}
}

View File

@ -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
}

View File

@ -2,6 +2,7 @@ package handler
import ( import (
"net/http" "net/http"
"sirlab.de/go/knyt/application"
) )
type HandlerFunc func(http.ResponseWriter, *http.Request) type HandlerFunc func(http.ResponseWriter, *http.Request)
@ -9,10 +10,12 @@ type HandlerFunc func(http.ResponseWriter, *http.Request)
type AuthMux struct { type AuthMux struct {
mux *http.ServeMux mux *http.ServeMux
Port int Port int
app *application.Application
} }
func NewAuthMux() *AuthMux { func NewAuthMux(app *application.Application) *AuthMux {
mux := AuthMux{ mux := AuthMux{
app: app,
mux: http.NewServeMux(), mux: http.NewServeMux(),
Port: 32039, Port: 32039,
} }

View File

@ -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
}

View File

@ -3,22 +3,23 @@ package main
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"sirlab.de/go/knyt/app" "sirlab.de/go/knyt/application"
"sirlab.de/go/knyt/handler" "sirlab.de/go/knyt/handler"
) )
func main() { func main() {
appConfig := app.NewAppConfig() appConfig := application.NewApplicationConfig()
App := app.NewApp(appConfig) app := application.NewApplication(appConfig)
App.Mux = handler.NewAuthMux() mux := handler.NewAuthMux(app)
App.Mux.PublicHandleFunc("/__intern__/exit", handler.Exit) mux.PublicHandleFunc("/__intern__/exit", handler.Exit)
// default handler // default handler
fsHandler := http.FileServer(http.Dir("../../client/dist/")) fsHandler := http.FileServer(http.Dir("../../client/dist/"))
App.Mux.PublicHandle("/", fsHandler) mux.PrivateHandle("/private/", fsHandler)
mux.PublicHandle("/", fsHandler)
// start listening // start listening
fmt.Printf("http://localhost:%d/\n", App.Mux.Port) fmt.Printf("http://localhost:%d/\n", mux.Port)
App.Mux.ListenAndServe() mux.ListenAndServe()
} }

View File

@ -1,6 +1,7 @@
package users package users
import ( import (
"fmt"
"sirlab.de/go/knyt/user" "sirlab.de/go/knyt/user"
) )
@ -14,6 +15,10 @@ func (users Users) AddUser(usr *user.User) {
users[usr.Id] = usr users[usr.Id] = usr
} }
func (users Users) GetUserById(id string) *user.User { func (users Users) GetUserById(id string) (*user.User, error) {
return users[id] usr := users[id]
if usr == nil {
return nil, fmt.Errorf("unknown id")
}
return usr, nil
} }