Use System Users for Webinterface Auth

This commit is contained in:
Samuel Lorch 2023-05-14 03:24:58 +02:00
parent bd26cf893d
commit 0e5cd1d3a5
4 changed files with 13 additions and 8 deletions

View file

@ -12,7 +12,6 @@ import (
) )
func HandleAPI(w http.ResponseWriter, r *http.Request) { func HandleAPI(w http.ResponseWriter, r *http.Request) {
slog.Info("Api Handler hit")
_, s := session.GetSession(r) _, s := session.GetSession(r)
if s == nil { if s == nil {
// Fallthrough after so that jsonrpc can still deliver a valid jsonrpc error // Fallthrough after so that jsonrpc can still deliver a valid jsonrpc error

View file

@ -17,11 +17,13 @@ var server http.Server
var mux = http.NewServeMux() var mux = http.NewServeMux()
var apiHandler *jsonrpc.Handler var apiHandler *jsonrpc.Handler
var stopCleanup chan struct{} var stopCleanup chan struct{}
var configManager *config.ConfigManager
func StartWebserver(configManager *config.ConfigManager, _apiHandler *jsonrpc.Handler) { func StartWebserver(_configManager *config.ConfigManager, _apiHandler *jsonrpc.Handler) {
server.Addr = ":8080" server.Addr = ":8080"
server.Handler = mux server.Handler = mux
apiHandler = _apiHandler apiHandler = _apiHandler
configManager = _configManager
// Routing // Routing
mux.HandleFunc("/login", HandleLogin) mux.HandleFunc("/login", HandleLogin)

View file

@ -7,6 +7,7 @@ import (
"time" "time"
"golang.org/x/exp/slog" "golang.org/x/exp/slog"
"nfsense.net/nfsense/internal/auth"
"nfsense.net/nfsense/internal/session" "nfsense.net/nfsense/internal/session"
) )
@ -27,13 +28,16 @@ func HandleLogin(w http.ResponseWriter, r *http.Request) {
slog.Error("Unmarshal", err) slog.Error("Unmarshal", err)
return return
} }
if req.Username == "admin" && req.Password == "12345" { err = auth.AuthenticateUser(configManager.GetCurrentConfig(), req.Username, req.Password)
slog.Info("User Login Successfull") if err != nil {
session.GenerateSession(w, req.Username) slog.Error("User Login failed", "err", err, "username", req.Username)
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusUnauthorized)
return return
} }
w.WriteHeader(http.StatusUnauthorized)
slog.Info("User Login Successful", "username", req.Username)
session.GenerateSession(w, req.Username)
w.WriteHeader(http.StatusOK)
} }
func HandleLogout(w http.ResponseWriter, r *http.Request) { func HandleLogout(w http.ResponseWriter, r *http.Request) {

View file

@ -18,7 +18,7 @@ const SessionLifeTime = time.Minute * 15
type Session struct { type Session struct {
Username string Username string
Expires time.Time Expires time.Time
// TODO Add []websocket.Conn pointer to close all active websockets, alternativly do this via context cancelation // TODO Add []websocket.Conn pointer to close all active websockets, alternatively do this via context cancelation
} }
type SessionResponse struct { type SessionResponse struct {