mirror of
https://github.com/phishingclub/phishingclub.git
synced 2026-12-06 10:03:46 +01:00
183 lines
6.5 KiB
Go
183 lines
6.5 KiB
Go
package service
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/oapi-codegen/nullable"
|
|
"github.com/phishingclub/phishingclub/data"
|
|
"github.com/phishingclub/phishingclub/database"
|
|
"github.com/phishingclub/phishingclub/model"
|
|
"github.com/phishingclub/phishingclub/repository"
|
|
)
|
|
|
|
// a converted service function: the database is gone after the permission
|
|
// check passed
|
|
func TestExpireByIDFailedIsAudited(t *testing.T) {
|
|
f := newLoginAuditFixture(t)
|
|
sqlDB, err := f.db.DB()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_ = sqlDB.Close()
|
|
id := uuid.New()
|
|
if err := f.sessions.ExpireByID(f.ctx, adminSession(uuid.New(), data.RoleSuperAdministrator), &id); err == nil {
|
|
t.Fatal("expected a closed database to fail")
|
|
}
|
|
ev := f.events("Session.Expire")
|
|
if len(ev) != 1 || ev[0]["result"] != "failed" || details(ev[0])["errorClass"] != "internal" {
|
|
t.Fatalf("failed expire event: %v", ev)
|
|
}
|
|
}
|
|
|
|
func TestLogoutAudit(t *testing.T) {
|
|
f := newLoginAuditFixture(t)
|
|
sessionID := f.insertSession(f.adminID, "203.0.113.9")
|
|
|
|
if err := f.sessions.Logout(f.ctx, sessionID, "203.0.113.10"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// the session is gone, a second logout writes no event
|
|
if err := f.sessions.Logout(f.ctx, sessionID, "203.0.113.10"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
random := uuid.New()
|
|
if err := f.sessions.Logout(f.ctx, &random, "203.0.113.10"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
ev := f.events("User.Logout")
|
|
if len(ev) != 1 {
|
|
t.Fatalf("expected one logout event, got %d: %v", len(ev), ev)
|
|
}
|
|
if ev[0]["authorized"] != true || ev[0]["userId"] != f.adminID.String() || ev[0]["ip"] != "203.0.113.10" ||
|
|
details(ev[0])["sessionIdPrefix"] != sessionID.String()[:8] {
|
|
t.Fatalf("logout event: %v", ev[0])
|
|
}
|
|
if _, err := f.sessions.SessionRepository.GetByID(f.ctx, sessionID, &repository.SessionOption{}); err == nil {
|
|
t.Fatal("session should be expired")
|
|
}
|
|
}
|
|
|
|
func TestExpireSessionByIDAudit(t *testing.T) {
|
|
f := newLoginAuditFixture(t)
|
|
target := f.insertSession(f.adminID, "203.0.113.11")
|
|
actorID := uuid.New()
|
|
|
|
err := f.sessions.ExpireByID(f.ctx, adminSession(actorID, data.RoleCompanyUser), target)
|
|
if err == nil {
|
|
t.Fatal("expected a role without permissions to be refused")
|
|
}
|
|
if _, err := f.sessions.SessionRepository.GetByID(f.ctx, target, &repository.SessionOption{}); err != nil {
|
|
t.Fatal("a refused expire must leave the session active")
|
|
}
|
|
if err := f.sessions.ExpireByID(f.ctx, adminSession(actorID, data.RoleSuperAdministrator), target); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
ev := f.events("Session.Expire")
|
|
if len(ev) != 2 {
|
|
t.Fatalf("expected 2 expire events, got %d: %v", len(ev), ev)
|
|
}
|
|
if ev[0]["authorized"] != false || ev[0]["userId"] != actorID.String() {
|
|
t.Errorf("refused expire event: %v", ev[0])
|
|
}
|
|
d := details(ev[1])
|
|
if ev[1]["authorized"] != true || ev[1]["userId"] != actorID.String() ||
|
|
d["sessionIdPrefix"] != target.String()[:8] || d["userId"] != f.adminID.String() {
|
|
t.Errorf("expire event: %v", ev[1])
|
|
}
|
|
}
|
|
|
|
func TestExpireSessionByIDUnknownAudit(t *testing.T) {
|
|
f := newLoginAuditFixture(t)
|
|
unknown := uuid.New()
|
|
if err := f.sessions.ExpireByID(f.ctx, adminSession(uuid.New(), data.RoleSuperAdministrator), &unknown); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ev := f.events("Session.Expire")
|
|
if len(ev) != 1 || ev[0]["authorized"] != true || details(ev[0])["sessionIdPrefix"] != unknown.String()[:8] {
|
|
t.Fatalf("expire unknown event: %v", ev)
|
|
}
|
|
if _, has := details(ev[0])["userId"]; has {
|
|
t.Fatal("an unknown session has no owner to name")
|
|
}
|
|
}
|
|
|
|
// the full session id is the login cookie and is never in the audit log
|
|
func TestSessionIDNotInAuditLog(t *testing.T) {
|
|
f := newLoginAuditFixture(t)
|
|
a := f.insertSession(f.adminID, "203.0.113.40")
|
|
b := f.insertSession(f.adminID, "203.0.113.41")
|
|
if err := f.sessions.Logout(f.ctx, a, "203.0.113.40"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := f.sessions.ExpireByID(f.ctx, adminSession(uuid.New(), data.RoleSuperAdministrator), b); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
out := f.buf.String()
|
|
if strings.Contains(out, a.String()) || strings.Contains(out, b.String()) {
|
|
t.Fatalf("a full session id reached the audit log: %q", out)
|
|
}
|
|
}
|
|
|
|
// TestExpireAllAndStillValid ends every session of a user in one statement
|
|
// and checks a stream session sees it, and that an API key session holds
|
|
// only while the key does
|
|
func TestExpireAllAndStillValid(t *testing.T) {
|
|
f := newLoginAuditFixture(t)
|
|
f.sessions.UserRepository = f.users.UserRepository
|
|
a := f.insertSession(f.adminID, "198.51.100.1")
|
|
b := f.insertSession(f.adminID, "198.51.100.2")
|
|
stream := &model.Session{ID: a}
|
|
if ok, err := f.sessions.StillValid(f.ctx, stream, ""); err != nil || !ok {
|
|
t.Fatalf("live session: %v %v", ok, err)
|
|
}
|
|
if err := f.sessions.ExpireAllByUserID(f.ctx, f.fixtureUserSession(), &f.adminID); err != nil {
|
|
t.Fatalf("expire all: %v", err)
|
|
}
|
|
if ok, _ := f.sessions.StillValid(f.ctx, stream, ""); ok {
|
|
t.Fatal("a stream kept an expired session")
|
|
}
|
|
if ok, _ := f.sessions.SessionRepository.IsLiveByID(f.ctx, b); ok {
|
|
t.Fatal("expire all left a session")
|
|
}
|
|
ev := f.events("Session.ExpireAllByUserID")
|
|
if len(ev) != 1 || details(ev[0])["expiredSessions"] != float64(2) {
|
|
t.Fatalf("expire all event: %v", ev)
|
|
}
|
|
// a session of a deleted user does not hold
|
|
c := f.insertSession(f.adminID, "198.51.100.4")
|
|
if ok, _ := f.sessions.StillValid(f.ctx, &model.Session{ID: c}, ""); !ok {
|
|
t.Fatal("a new session is not live")
|
|
}
|
|
if err := f.db.Where("id = ?", f.adminID.String()).Delete(&database.User{}).Error; err != nil {
|
|
t.Fatalf("delete user: %v", err)
|
|
}
|
|
if ok, _ := f.sessions.StillValid(f.ctx, &model.Session{ID: c}, ""); ok {
|
|
t.Fatal("a session of a deleted user kept a stream")
|
|
}
|
|
if err := f.db.Unscoped().Model(&database.User{}).Where("id = ?", f.adminID.String()).
|
|
Update("deleted_at", nil).Error; err != nil {
|
|
t.Fatalf("restore user: %v", err)
|
|
}
|
|
// an API key session
|
|
if err := f.users.UserRepository.UpsertAPIKey(f.ctx, &f.adminID, "the-api-key"); err != nil {
|
|
t.Fatalf("api key: %v", err)
|
|
}
|
|
api := &model.Session{IsAPITokenRequest: true, User: &model.User{ID: nullable.NewNullableWithValue(f.adminID)}}
|
|
if ok, err := f.sessions.StillValid(f.ctx, api, "the-api-key"); err != nil || !ok {
|
|
t.Fatalf("api key session: %v %v", ok, err)
|
|
}
|
|
if ok, _ := f.sessions.StillValid(f.ctx, api, "another-key"); ok {
|
|
t.Fatal("a wrong key kept the stream")
|
|
}
|
|
if err := f.users.UserRepository.RemoveAPIKey(f.ctx, &f.adminID); err != nil {
|
|
t.Fatalf("remove api key: %v", err)
|
|
}
|
|
if ok, _ := f.sessions.StillValid(f.ctx, api, "the-api-key"); ok {
|
|
t.Fatal("a removed key kept the stream")
|
|
}
|
|
}
|