mirror of
https://github.com/Ed1s0nZ/CyberStrikeAI.git
synced 2026-12-01 12:03:26 +01:00
Merge pull request #426 from Sycun/codex/restore-sidebar-version
fix(auth): restore the sidebar version badge
This commit is contained in:
2 files changed
+62
-2
No files matched your search
@@ -96,6 +96,7 @@ func (h *AuthHandler) Login(c *gin.Context) {
|
||||
"token": token,
|
||||
"expires_at": expiresAt.UTC().Format(time.RFC3339),
|
||||
"session_duration_hr": h.manager.SessionDurationHours(),
|
||||
"app_version": h.config.Version,
|
||||
"user": gin.H{
|
||||
"id": session.UserID,
|
||||
"username": session.Username,
|
||||
@@ -218,8 +219,9 @@ func (h *AuthHandler) Validate(c *gin.Context) {
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"token": session.Token,
|
||||
"expires_at": session.ExpiresAt.UTC().Format(time.RFC3339),
|
||||
"token": session.Token,
|
||||
"expires_at": session.ExpiresAt.UTC().Format(time.RFC3339),
|
||||
"app_version": h.config.Version,
|
||||
"user": gin.H{
|
||||
"id": session.UserID,
|
||||
"username": session.Username,
|
||||
|
||||
@@ -5,10 +5,16 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"cyberstrike-ai/internal/config"
|
||||
"cyberstrike-ai/internal/database"
|
||||
"cyberstrike-ai/internal/security"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func TestLoginRejectsMissingAndLongUsernameBeforeAuthentication(t *testing.T) {
|
||||
@@ -26,3 +32,55 @@ func TestLoginRejectsMissingAndLongUsernameBeforeAuthentication(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthSessionResponsesIncludeAppVersion(t *testing.T) {
|
||||
const version = "v9.8.7-test"
|
||||
logger := zap.NewNop()
|
||||
db, err := database.NewDB(filepath.Join(t.TempDir(), "auth.db"), logger)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = db.Close() })
|
||||
manager := security.NewAuthManager(12)
|
||||
password, err := manager.AttachRBACStore(db)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
h := NewAuthHandler(manager, &config.Config{Version: version}, "", logger)
|
||||
r := gin.New()
|
||||
r.POST("/api/auth/login", h.Login)
|
||||
r.GET("/api/auth/validate", security.AuthMiddleware(manager), h.Validate)
|
||||
|
||||
checkVersion := func(req *http.Request) string {
|
||||
t.Helper()
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("%s returned status %d", req.URL.Path, w.Code)
|
||||
}
|
||||
var session struct {
|
||||
Token string `json:"token"`
|
||||
AppVersion string `json:"app_version"`
|
||||
}
|
||||
if err := json.NewDecoder(w.Body).Decode(&session); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if session.AppVersion != version {
|
||||
t.Errorf("%s app_version = %q, want %q", req.URL.Path, session.AppVersion, version)
|
||||
}
|
||||
return session.Token
|
||||
}
|
||||
|
||||
body, err := json.Marshal(loginRequest{Username: "admin", Password: password})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
login := httptest.NewRequest(http.MethodPost, "/api/auth/login", bytes.NewReader(body))
|
||||
login.Header.Set("Content-Type", "application/json")
|
||||
token := checkVersion(login)
|
||||
|
||||
validate := httptest.NewRequest(http.MethodGet, "/api/auth/validate", nil)
|
||||
validate.Header.Set("Authorization", "Bearer "+token)
|
||||
checkVersion(validate)
|
||||
}
|
||||
Reference in new issue
Block a user