Refactoring some server side stuff
This commit is contained in:
+3
-3
@@ -99,7 +99,7 @@ func addAuthEndpoints(r chi.Router, config *ServerConfig) {
|
||||
return
|
||||
}
|
||||
|
||||
if username == spaceConfig.Auth.User && password == spaceConfig.Auth.Pass {
|
||||
if spaceConfig.Authorize(username, password) {
|
||||
// Generate JWT with username (not currently used)
|
||||
payload := map[string]any{
|
||||
"username": username,
|
||||
@@ -224,8 +224,8 @@ func authMiddleware(config *ServerConfig) func(http.Handler) http.Handler {
|
||||
return
|
||||
}
|
||||
|
||||
username, ok := claims["username"].(string)
|
||||
if !ok || username != spaceConfig.Auth.User {
|
||||
_, ok := claims["username"].(string)
|
||||
if !ok {
|
||||
log.Printf("Username mismatch in JWT on %s", path)
|
||||
redirectToAuth(w, "/.auth", path, config.HostURLPrefix)
|
||||
return
|
||||
|
||||
@@ -29,8 +29,8 @@ func buildConfig(bundledFiles fs.FS, args []string) *server.ServerConfig {
|
||||
}
|
||||
|
||||
// For now just point every request to the rootSpaceConfig
|
||||
serverConfig.SpaceConfigResolver = func(r *http.Request) *server.SpaceConfig {
|
||||
return rootSpaceConfig
|
||||
serverConfig.SpaceConfigResolver = func(r *http.Request) (*server.SpaceConfig, error) {
|
||||
return rootSpaceConfig, nil
|
||||
}
|
||||
|
||||
if os.Getenv("SB_HOSTNAME") != "" {
|
||||
@@ -105,6 +105,10 @@ func buildConfig(bundledFiles fs.FS, args []string) *server.ServerConfig {
|
||||
LockoutTime: 60,
|
||||
}
|
||||
|
||||
rootSpaceConfig.Authorize = func(username, password string) bool {
|
||||
return username == rootSpaceConfig.Auth.User && password == rootSpaceConfig.Auth.Pass
|
||||
}
|
||||
|
||||
if os.Getenv("SB_LOCKOUT_LIMIT") != "" {
|
||||
rootSpaceConfig.Auth.LockoutLimit, err = strconv.Atoi(os.Getenv("SB_LOCKOUT_LIMIT"))
|
||||
if err != nil {
|
||||
|
||||
+5
-1
@@ -28,7 +28,7 @@ type BootConfig struct {
|
||||
LogPush bool `json:"logPush"`
|
||||
}
|
||||
|
||||
func RunServer(config *ServerConfig) error {
|
||||
func Router(config *ServerConfig) chi.Router {
|
||||
r := chi.NewRouter()
|
||||
|
||||
if config.EnableHTTPLogging {
|
||||
@@ -115,7 +115,11 @@ func RunServer(config *ServerConfig) error {
|
||||
} else {
|
||||
r.Mount(config.HostURLPrefix, routes)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func RunServer(config *ServerConfig) error {
|
||||
r := Router(config)
|
||||
// Display the final server running message
|
||||
visibleHostname := config.BindHost
|
||||
if config.BindHost == "127.0.0.1" {
|
||||
|
||||
+5
-1
@@ -11,7 +11,11 @@ const serverConfigKey = "serverConfig"
|
||||
func spaceMiddleware(config *ServerConfig) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
spaceConfig := config.SpaceConfigResolver(r)
|
||||
spaceConfig, err := config.SpaceConfigResolver(r)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
ctx := context.WithValue(r.Context(), spaceConfigKey, spaceConfig)
|
||||
ctx = context.WithValue(ctx, serverConfigKey, config)
|
||||
augmentedRequest := r.WithContext(ctx)
|
||||
|
||||
+5
-1
@@ -17,10 +17,14 @@ type ServerConfig struct {
|
||||
ClientBundle SpacePrimitives
|
||||
}
|
||||
|
||||
type UserPasswordAuthorizer func(username, password string) bool
|
||||
|
||||
type SpaceConfig struct {
|
||||
Hostname string
|
||||
Auth *AuthOptions
|
||||
|
||||
Authorize UserPasswordAuthorizer
|
||||
|
||||
SpacePrimitives SpacePrimitives
|
||||
|
||||
SpaceFolderPath string
|
||||
@@ -41,7 +45,7 @@ type SpaceConfig struct {
|
||||
LockoutTimer *LockoutTimer
|
||||
}
|
||||
|
||||
type ConfigResolver func(r *http.Request) *SpaceConfig
|
||||
type ConfigResolver func(r *http.Request) (*SpaceConfig, error)
|
||||
|
||||
// FileMeta represents metadata for a file in the space
|
||||
type FileMeta struct {
|
||||
|
||||
Reference in New Issue
Block a user