- Track learner sessions and revoke them immediately on every request - Add a device replacement challenge for logins over the limit - Add learner and admin endpoints to list and remove devices - Trim excess sessions when enforcement is enabled - Stop logging refresh tokens and authorization headers Enforcement is off by default through DEVICE_LIMIT_ENFORCED.
115 lines
4.6 KiB
Go
115 lines
4.6 KiB
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"Rezina-Backend/internal/domain"
|
|
"Rezina-Backend/internal/ports"
|
|
"Rezina-Backend/internal/services/authentication"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type handlerSessionStore struct {
|
|
ports.SessionStore
|
|
owner int64
|
|
sessionID uuid.UUID
|
|
active bool
|
|
}
|
|
|
|
func TestDeviceInfoFromRequestUsesBrowserAndUserAgentOS(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
userAgent string
|
|
platform string
|
|
label string
|
|
}{
|
|
{name: "Chrome Windows", platform: "web", userAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/131.0 Safari/537.36", label: "Chrome on Windows"},
|
|
{name: "Edge Windows", platform: "web", userAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/131.0 Safari/537.36 Edg/131.0", label: "Edge on Windows"},
|
|
{name: "Chrome iPhone", platform: "ios", userAgent: "Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X) AppleWebKit/605.1 CriOS/131.0 Mobile/15E148 Safari/604.1", label: "Chrome on iPhone"},
|
|
{name: "Firefox iPad", platform: "ios", userAgent: "Mozilla/5.0 (iPad; CPU OS 18_0 like Mac OS X) AppleWebKit/605.1 FxiOS/132.0 Mobile/15E148 Safari/605.1", label: "Firefox on iPad"},
|
|
{name: "Safari macOS", platform: "web", userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 14_7) AppleWebKit/605.1 Version/18.0 Safari/605.1", label: "Safari on macOS"},
|
|
{name: "Chrome Android with web platform", platform: "web", userAgent: "Mozilla/5.0 (Linux; Android 15) AppleWebKit/537.36 Chrome/131.0 Mobile Safari/537.36", label: "Chrome on Android"},
|
|
{name: "Firefox Linux", platform: "web", userAgent: "Mozilla/5.0 (X11; Linux x86_64; rv:133.0) Gecko/20100101 Firefox/133.0", label: "Firefox on Linux"},
|
|
{name: "Unknown OS", platform: "web", userAgent: "CustomBrowser/1.0", label: "Browser"},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
app := fiber.New()
|
|
app.Get("/", func(c *fiber.Ctx) error {
|
|
device, err := deviceInfoFromRequest(c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return c.JSON(device)
|
|
})
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
req.Header.Set("User-Agent", test.userAgent)
|
|
req.Header.Set("X-Device-Platform", test.platform)
|
|
res, err := app.Test(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer res.Body.Close()
|
|
var device domain.DeviceInfo
|
|
if err := json.NewDecoder(res.Body).Decode(&device); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if device.Label != test.label || device.Platform != test.platform {
|
|
t.Fatalf("device = %#v, want label %q platform %q", device, test.label, test.platform)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func (s *handlerSessionStore) CreateRefreshToken(context.Context, domain.RefreshToken) error {
|
|
return nil
|
|
}
|
|
func (s *handlerSessionStore) GetRefreshToken(context.Context, string) (domain.RefreshToken, error) {
|
|
return domain.RefreshToken{}, authentication.ErrRefreshTokenNotFound
|
|
}
|
|
func (s *handlerSessionStore) GetRefreshTokenByUserID(context.Context, int64) (domain.RefreshToken, error) {
|
|
return domain.RefreshToken{}, authentication.ErrRefreshTokenNotFound
|
|
}
|
|
func (s *handlerSessionStore) RevokeRefreshToken(context.Context, string) error { return nil }
|
|
func (s *handlerSessionStore) ListUserSessions(context.Context, int64) ([]domain.UserSession, error) {
|
|
return []domain.UserSession{{ID: s.sessionID, UserID: s.owner, Label: "Chrome on Web", Platform: "web"}}, nil
|
|
}
|
|
func (s *handlerSessionStore) RevokeOwnUserSession(_ context.Context, userID int64, id uuid.UUID, _ string) error {
|
|
if !s.active || userID != s.owner || id != s.sessionID {
|
|
return authentication.ErrSessionNotFound
|
|
}
|
|
s.active = false
|
|
return nil
|
|
}
|
|
|
|
func TestDeleteMySessionOwnership(t *testing.T) {
|
|
id := uuid.New()
|
|
store := &handlerSessionStore{owner: 7, sessionID: id, active: true}
|
|
svc := authentication.NewService(nil, nil, nil, store, 3600)
|
|
h := &Handler{authSvc: svc}
|
|
app := fiber.New()
|
|
app.Delete("/sessions/:id", func(c *fiber.Ctx) error { c.Locals("user_id", int64(8)); return c.Next() }, h.DeleteMySession)
|
|
res, err := app.Test(httptest.NewRequest(http.MethodDelete, "/sessions/"+id.String(), nil))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if res.StatusCode != http.StatusNotFound {
|
|
t.Fatalf("other user's status=%d", res.StatusCode)
|
|
}
|
|
app = fiber.New()
|
|
app.Delete("/sessions/:id", func(c *fiber.Ctx) error { c.Locals("user_id", int64(7)); return c.Next() }, h.DeleteMySession)
|
|
res, err = app.Test(httptest.NewRequest(http.MethodDelete, "/sessions/"+id.String(), nil))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if res.StatusCode != http.StatusOK || store.active {
|
|
t.Fatalf("owner status=%d active=%v", res.StatusCode, store.active)
|
|
}
|
|
}
|