diff options
| -rw-r--r-- | cmd/rpserver/main.go | 5 | ||||
| -rw-r--r-- | internal/auth/auth.go | 2 | ||||
| -rw-r--r-- | internal/auth/users/passwords.go (renamed from internal/users/passwords.go) | 0 | ||||
| -rw-r--r-- | internal/auth/users/sessionids.go (renamed from internal/users/sessionids.go) | 0 | ||||
| -rw-r--r-- | internal/cmd/root.go | 22 | ||||
| -rw-r--r-- | internal/models/user.go | 9 | ||||
| -rw-r--r-- | migrations/001_create_users_table.down.sql | 1 | ||||
| -rw-r--r-- | migrations/001_create_users_table.up.sql | 9 | ||||
| -rw-r--r-- | migrations/002_create_users_sessions.down.sql | 0 | ||||
| -rw-r--r-- | migrations/002_create_users_sessions_table.up.sql | 9 | ||||
| -rw-r--r-- | templates/deployments/deployments.html | 0 | ||||
| -rw-r--r-- | templates/index.html | 8 | ||||
| -rw-r--r-- | templates/settings/settings.html | 0 |
13 files changed, 41 insertions, 24 deletions
diff --git a/cmd/rpserver/main.go b/cmd/rpserver/main.go index 7852c0a..632e1ae 100644 --- a/cmd/rpserver/main.go +++ b/cmd/rpserver/main.go @@ -11,8 +11,8 @@ import ( ) func main() { - certFile := flag.String("cert", "certs/localhost.pem", "TLS certificate file") - keyFile := flag.String("key", "certs/localhost-key.pem", "TLS key file") + certFile := flag.String("cert", "/rpserver/certs/localhost.pem", "TLS certificate file") + keyFile := flag.String("key", "/rpserver/certs/localhost-key.pem", "TLS key file") flag.Parse() godotenv.Load() @@ -21,6 +21,7 @@ func main() { log.Println("a critical env var is not set!") os.Exit(1) } + go func() { log.Println("HTTPS server is running on https://localhost:8443") err := cmd.ExecuteServer(":8443", *certFile, *keyFile) diff --git a/internal/auth/auth.go b/internal/auth/auth.go index b58af82..da2a4b8 100644 --- a/internal/auth/auth.go +++ b/internal/auth/auth.go @@ -17,7 +17,7 @@ func init() { var jwtSecret = []byte(os.Getenv("JWT_SECRET")) -const algo = "HS256" +var algo = string(os.Getenv("JWT_ALGO")) func GenerateJWT(userID string, duration time.Duration) (string, error) { claims := jwt.MapClaims{ diff --git a/internal/users/passwords.go b/internal/auth/users/passwords.go index 72215d3..72215d3 100644 --- a/internal/users/passwords.go +++ b/internal/auth/users/passwords.go diff --git a/internal/users/sessionids.go b/internal/auth/users/sessionids.go index 9b0d59b..9b0d59b 100644 --- a/internal/users/sessionids.go +++ b/internal/auth/users/sessionids.go diff --git a/internal/cmd/root.go b/internal/cmd/root.go index ab877ea..ef0119d 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -11,8 +11,8 @@ import ( "sync" "time" + "github.com/Wacky404/rpserver/internal/auth/users" "github.com/Wacky404/rpserver/internal/middleware" - "github.com/Wacky404/rpserver/internal/users" "github.com/golang-jwt/jwt/v5" ) @@ -28,6 +28,7 @@ func ExecuteServer(port string, cert string, key string) error { mux.Handle("/auth/login", middleware.Recover(http.HandlerFunc(handleLogin))) mux.Handle("/dashboard", middleware.Recover(middleware.Cookies(http.HandlerFunc(serveDashboard)))) mux.Handle("/proxy", middleware.Recover(middleware.JWT(http.HandlerFunc(handleProxy)))) + // mux.Handle("/settings/generate", middleware.Recover(middleware.Cookies(http.HandlerFunc()))) mux.Handle("/status", middleware.Recover(http.HandlerFunc(handleStatus))) err := http.ListenAndServeTLS(port, cert, key, mux) @@ -56,18 +57,8 @@ func handleLogin(w http.ResponseWriter, r *http.Request) { // pull this out into auth function if username == "admin" && password == "password4321" { - //token, err := auth.GenerateJWT(username, time.Hour) - //if err != nil { - // log.Printf("JWT generation error: %v", err) - // http.Error(w, "Could not generate token:", http.StatusInternalServerError) - // return - //} + newSID := users.SessionPrefix + users.GenID(16) - //w.Header().Set("Content-Type", "application/json") - //fmt.Fprintf(w, `{"token": "%s"}`, token) - - //return - newSID := users.SessionPrefix + users.GenID(16) // hash and store in sessions table cookie := &http.Cookie{ Name: middleware.AdmitCookies[0], Value: newSID, @@ -78,9 +69,14 @@ func handleLogin(w http.ResponseWriter, r *http.Request) { Expires: time.Now().Add(time.Minute * 2), } http.SetCookie(w, cookie) + w.Header().Set("HX-Redirect", "/dashboard") w.WriteHeader(http.StatusOK) + return } + + w.WriteHeader(http.StatusBadRequest) + fmt.Fprint(w, `Invalid username or password`) } func handleStatus(w http.ResponseWriter, r *http.Request) { @@ -91,7 +87,6 @@ func handleStatus(w http.ResponseWriter, r *http.Request) { func handleProxy(w http.ResponseWriter, r *http.Request) { claims, ok := r.Context().Value("claims").(jwt.MapClaims) if !ok { - fmt.Println("Is this failing...") http.Error(w, "Failed to get JWT claims", http.StatusInternalServerError) return } @@ -101,7 +96,6 @@ func handleProxy(w http.ResponseWriter, r *http.Request) { backendURL, err := getBackendURL(r) if err != nil { - fmt.Println("Is this failing...2") http.Error(w, "Backend URL not provided", http.StatusBadRequest) return } diff --git a/internal/models/user.go b/internal/models/user.go index 938cf71..b165b24 100644 --- a/internal/models/user.go +++ b/internal/models/user.go @@ -3,8 +3,10 @@ package models import "time" // this is more than likely going to change -type Password [16]byte -type SessionID [16]byte +type ( + Password [16]byte + SessionID [16]byte +) type Token struct { ID ID `json:"id"` @@ -24,9 +26,10 @@ type User struct { type UserSession struct { SessionID SessionID `json:"session_id"` + UserID ID `json:"user_id"` IP string `json:"ip"` UA string `json:"ua"` - UserID ID `json:"user_id"` ExpiresAt time.Time `json:"expires_at"` + CreatedAt time.Time `json:"created_at"` LastUpdated time.Time `json:"last_login"` } diff --git a/migrations/001_create_users_table.down.sql b/migrations/001_create_users_table.down.sql new file mode 100644 index 0000000..c99ddcd --- /dev/null +++ b/migrations/001_create_users_table.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS users; diff --git a/migrations/001_create_users_table.up.sql b/migrations/001_create_users_table.up.sql new file mode 100644 index 0000000..bce2771 --- /dev/null +++ b/migrations/001_create_users_table.up.sql @@ -0,0 +1,9 @@ +CREATE TABLE users ( + id UUID PRIMARY KEY, + name TEXT NOT NULL UNIQUE, + password TEXT NOT NULL, + admin BOOLEAN NOT NULL DEFAULT false, + token VARCHAR(36), + created_at TIMESTAMP DEFAULT now(), + last_updated TIMESTAMP DEFAULT now() +); diff --git a/migrations/002_create_users_sessions.down.sql b/migrations/002_create_users_sessions.down.sql new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/migrations/002_create_users_sessions.down.sql diff --git a/migrations/002_create_users_sessions_table.up.sql b/migrations/002_create_users_sessions_table.up.sql new file mode 100644 index 0000000..8c7167f --- /dev/null +++ b/migrations/002_create_users_sessions_table.up.sql @@ -0,0 +1,9 @@ +CREATE TABLE users_sessions ( + session_id UUID PRIMARY KEY, + user_id UUID, + ip TEXT NOT NULL, + ua TEXT NOT NULL, + expires_at TIMESTAMP NOT NULL, + created_at TIMESTAMP DEFAULT now(), + last_updated TIMESTAMP DEFAULT now() +); diff --git a/templates/deployments/deployments.html b/templates/deployments/deployments.html new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/templates/deployments/deployments.html diff --git a/templates/index.html b/templates/index.html index c01a765..96bf77b 100644 --- a/templates/index.html +++ b/templates/index.html @@ -32,8 +32,8 @@ max-width: 400px; width: 100%; box-sizing: border-box; - animation: fadeIn 0.5s ease-in-out; - transition: all 0.3s ease; + /*animation: fadeIn 0.5s ease-in-out; + transition: all 0.3s ease;*/ } .login-card.shake { @@ -237,8 +237,8 @@ <h2>Login</h2> <form hx-post="/auth/login" - hx-target="#loginCard" - hx-swap="outerHTML" + hx-target="#errorMessage" + hx-swap="innerHTML" hx-indicator="#loginBtn" id="loginForm" > diff --git a/templates/settings/settings.html b/templates/settings/settings.html new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/templates/settings/settings.html |
