mirror of
https://github.com/docmost/docmost.git
synced 2026-05-07 06:23:06 +08:00
user session management
This commit is contained in:
@@ -708,5 +708,13 @@
|
|||||||
"Resend verification email": "Resend verification email",
|
"Resend verification email": "Resend verification email",
|
||||||
"Verification email sent. Please check your inbox.": "Verification email sent. Please check your inbox.",
|
"Verification email sent. Please check your inbox.": "Verification email sent. Please check your inbox.",
|
||||||
"Failed to resend verification email. Please try again.": "Failed to resend verification email. Please try again.",
|
"Failed to resend verification email. Please try again.": "Failed to resend verification email. Please try again.",
|
||||||
"We've sent you an email with your associated workspaces.": "We've sent you an email with your associated workspaces."
|
"We've sent you an email with your associated workspaces.": "We've sent you an email with your associated workspaces.",
|
||||||
|
"Load more": "Load more",
|
||||||
|
"Log out of all devices": "Log out of all devices",
|
||||||
|
"Log out of all sessions except this device": "Log out of all sessions except this device",
|
||||||
|
"This Device": "This Device",
|
||||||
|
"Unknown device": "Unknown device",
|
||||||
|
"No active sessions": "No active sessions",
|
||||||
|
"Session revoked": "Session revoked",
|
||||||
|
"All other sessions revoked": "All other sessions revoked"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,165 @@
|
|||||||
|
import { useState } from "react";
|
||||||
|
import {
|
||||||
|
Button,
|
||||||
|
Divider,
|
||||||
|
Group,
|
||||||
|
Skeleton,
|
||||||
|
Stack,
|
||||||
|
Table,
|
||||||
|
Text,
|
||||||
|
} from "@mantine/core";
|
||||||
|
import { IconDevices } from "@tabler/icons-react";
|
||||||
|
import { useTranslation } from "react-i18next";
|
||||||
|
import {
|
||||||
|
useGetSessionsQuery,
|
||||||
|
useRevokeSessionMutation,
|
||||||
|
useRevokeAllSessionsMutation,
|
||||||
|
} from "@/features/session/queries/session-query";
|
||||||
|
import { formattedDate } from "@/lib/time";
|
||||||
|
|
||||||
|
const PAGE_SIZE = 5;
|
||||||
|
|
||||||
|
export default function SessionList() {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const { data: sessions, isLoading } = useGetSessionsQuery();
|
||||||
|
const revokeSessionMutation = useRevokeSessionMutation();
|
||||||
|
const revokeAllSessionsMutation = useRevokeAllSessionsMutation();
|
||||||
|
const [visibleCount, setVisibleCount] = useState(PAGE_SIZE);
|
||||||
|
|
||||||
|
const otherSessions = sessions?.filter((s) => !s.isCurrent) ?? [];
|
||||||
|
const visibleSessions = sessions?.slice(0, visibleCount) ?? [];
|
||||||
|
const hasMore = sessions && visibleCount < sessions.length;
|
||||||
|
|
||||||
|
if (isLoading) {
|
||||||
|
return (
|
||||||
|
<Table verticalSpacing="md">
|
||||||
|
<Table.Thead>
|
||||||
|
<Table.Tr>
|
||||||
|
<Table.Th>{t("Device Name")}</Table.Th>
|
||||||
|
<Table.Th>{t("Last Active")}</Table.Th>
|
||||||
|
<Table.Th />
|
||||||
|
</Table.Tr>
|
||||||
|
</Table.Thead>
|
||||||
|
<Table.Tbody>
|
||||||
|
{[1, 2, 3].map((i) => (
|
||||||
|
<Table.Tr key={i}>
|
||||||
|
<Table.Td>
|
||||||
|
<Group gap="xs">
|
||||||
|
<Skeleton height={18} width={18} radius="sm" />
|
||||||
|
<Skeleton height={14} width={140} radius="xs" />
|
||||||
|
</Group>
|
||||||
|
</Table.Td>
|
||||||
|
<Table.Td>
|
||||||
|
<Skeleton height={14} width={120} radius="xs" />
|
||||||
|
</Table.Td>
|
||||||
|
<Table.Td>
|
||||||
|
<Skeleton height={30} width={70} radius="sm" />
|
||||||
|
</Table.Td>
|
||||||
|
</Table.Tr>
|
||||||
|
))}
|
||||||
|
</Table.Tbody>
|
||||||
|
</Table>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Stack>
|
||||||
|
{otherSessions.length > 0 && (
|
||||||
|
<>
|
||||||
|
<div>
|
||||||
|
<Text fw={500}>{t("Log out of all devices")}</Text>
|
||||||
|
<Group justify="space-between" align="center" mt={4}>
|
||||||
|
<Text size="sm" c="dimmed">
|
||||||
|
{t(
|
||||||
|
"Log out of all sessions except this device",
|
||||||
|
)}
|
||||||
|
</Text>
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
color="red"
|
||||||
|
size="xs"
|
||||||
|
loading={revokeAllSessionsMutation.isPending}
|
||||||
|
onClick={() => revokeAllSessionsMutation.mutate()}
|
||||||
|
>
|
||||||
|
{t("Log out of all devices")}
|
||||||
|
</Button>
|
||||||
|
</Group>
|
||||||
|
</div>
|
||||||
|
<Divider />
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<Table verticalSpacing="md">
|
||||||
|
<Table.Thead>
|
||||||
|
<Table.Tr>
|
||||||
|
<Table.Th>{t("Device Name")}</Table.Th>
|
||||||
|
<Table.Th>{t("Last Active")}</Table.Th>
|
||||||
|
{otherSessions.length > 0 && <Table.Th />}
|
||||||
|
</Table.Tr>
|
||||||
|
</Table.Thead>
|
||||||
|
<Table.Tbody>
|
||||||
|
{visibleSessions.map((session) => (
|
||||||
|
<Table.Tr key={session.id}>
|
||||||
|
<Table.Td>
|
||||||
|
<Group gap="xs">
|
||||||
|
<IconDevices size={18} stroke={1.5} />
|
||||||
|
<div>
|
||||||
|
<Text size="sm">
|
||||||
|
{session.deviceName || t("Unknown device")}
|
||||||
|
</Text>
|
||||||
|
{session.isCurrent && (
|
||||||
|
<Text size="xs" c="blue">
|
||||||
|
{t("This Device")}
|
||||||
|
</Text>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</Group>
|
||||||
|
</Table.Td>
|
||||||
|
<Table.Td>
|
||||||
|
<Text size="sm">
|
||||||
|
{session.isCurrent
|
||||||
|
? t("Now")
|
||||||
|
: formattedDate(new Date(session.lastActiveAt))}
|
||||||
|
</Text>
|
||||||
|
</Table.Td>
|
||||||
|
{otherSessions.length > 0 && (
|
||||||
|
<Table.Td>
|
||||||
|
{!session.isCurrent && (
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
size="xs"
|
||||||
|
loading={revokeSessionMutation.isPending}
|
||||||
|
onClick={() =>
|
||||||
|
revokeSessionMutation.mutate({
|
||||||
|
sessionId: session.id,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
>
|
||||||
|
{t("Log out")}
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</Table.Td>
|
||||||
|
)}
|
||||||
|
</Table.Tr>
|
||||||
|
))}
|
||||||
|
</Table.Tbody>
|
||||||
|
</Table>
|
||||||
|
|
||||||
|
{hasMore && (
|
||||||
|
<Button
|
||||||
|
variant="subtle"
|
||||||
|
size="xs"
|
||||||
|
onClick={() => setVisibleCount((c) => c + PAGE_SIZE)}
|
||||||
|
>
|
||||||
|
{t("Load more")}
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{(!sessions || sessions.length === 0) && (
|
||||||
|
<Text size="sm" c="dimmed" ta="center">
|
||||||
|
{t("No active sessions")}
|
||||||
|
</Text>
|
||||||
|
)}
|
||||||
|
</Stack>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
import {
|
||||||
|
useMutation,
|
||||||
|
useQuery,
|
||||||
|
useQueryClient,
|
||||||
|
UseQueryResult,
|
||||||
|
} from "@tanstack/react-query";
|
||||||
|
import {
|
||||||
|
getSessions,
|
||||||
|
revokeSession,
|
||||||
|
revokeAllSessions,
|
||||||
|
} from "@/features/session/services/session-service";
|
||||||
|
import { ISession } from "@/features/session/types/session.types";
|
||||||
|
import { notifications } from "@mantine/notifications";
|
||||||
|
import { useTranslation } from "react-i18next";
|
||||||
|
|
||||||
|
export function useGetSessionsQuery(): UseQueryResult<ISession[], Error> {
|
||||||
|
return useQuery({
|
||||||
|
queryKey: ["session-list"],
|
||||||
|
queryFn: () => getSessions(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useRevokeSessionMutation() {
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
const { t } = useTranslation();
|
||||||
|
|
||||||
|
return useMutation<void, Error, { sessionId: string }>({
|
||||||
|
mutationFn: (data) => revokeSession(data),
|
||||||
|
onSuccess: () => {
|
||||||
|
notifications.show({ message: t("Session revoked") });
|
||||||
|
queryClient.invalidateQueries({ queryKey: ["session-list"] });
|
||||||
|
},
|
||||||
|
onError: (error) => {
|
||||||
|
const errorMessage = error["response"]?.data?.message;
|
||||||
|
notifications.show({ message: errorMessage, color: "red" });
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useRevokeAllSessionsMutation() {
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
const { t } = useTranslation();
|
||||||
|
|
||||||
|
return useMutation<void, Error, void>({
|
||||||
|
mutationFn: () => revokeAllSessions(),
|
||||||
|
onSuccess: () => {
|
||||||
|
notifications.show({ message: t("All other sessions revoked") });
|
||||||
|
queryClient.invalidateQueries({ queryKey: ["session-list"] });
|
||||||
|
},
|
||||||
|
onError: (error) => {
|
||||||
|
const errorMessage = error["response"]?.data?.message;
|
||||||
|
notifications.show({ message: errorMessage, color: "red" });
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
import api from "@/lib/api-client";
|
||||||
|
import { ISession } from "@/features/session/types/session.types";
|
||||||
|
|
||||||
|
export async function getSessions(): Promise<ISession[]> {
|
||||||
|
const req = await api.post<{ sessions: ISession[] }>("/sessions");
|
||||||
|
return req.data.sessions;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function revokeSession(data: {
|
||||||
|
sessionId: string;
|
||||||
|
}): Promise<void> {
|
||||||
|
await api.post("/sessions/revoke", data);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function revokeAllSessions(): Promise<void> {
|
||||||
|
await api.post("/sessions/revoke-all");
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
export type ISession = {
|
||||||
|
id: string;
|
||||||
|
deviceName: string | null;
|
||||||
|
geoLocation: string | null;
|
||||||
|
lastActiveAt: string;
|
||||||
|
createdAt: string;
|
||||||
|
isCurrent: boolean;
|
||||||
|
};
|
||||||
@@ -1,9 +1,8 @@
|
|||||||
import { useAtom } from "jotai";
|
import { useAtom } from "jotai";
|
||||||
import { focusAtom } from "jotai-optics";
|
|
||||||
import { z } from "zod/v4";
|
import { z } from "zod/v4";
|
||||||
import { useForm } from "@mantine/form";
|
import { useForm } from "@mantine/form";
|
||||||
import { zod4Resolver } from "mantine-form-zod-resolver";
|
import { zod4Resolver } from "mantine-form-zod-resolver";
|
||||||
import { currentUserAtom } from "@/features/user/atoms/current-user-atom.ts";
|
import { userAtom } from "@/features/user/atoms/current-user-atom.ts";
|
||||||
import { updateUser } from "@/features/user/services/user-service.ts";
|
import { updateUser } from "@/features/user/services/user-service.ts";
|
||||||
import { IUser } from "@/features/user/types/user.types.ts";
|
import { IUser } from "@/features/user/types/user.types.ts";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
@@ -17,18 +16,15 @@ const formSchema = z.object({
|
|||||||
|
|
||||||
type FormValues = z.infer<typeof formSchema>;
|
type FormValues = z.infer<typeof formSchema>;
|
||||||
|
|
||||||
const userAtom = focusAtom(currentUserAtom, (optic) => optic.prop("user"));
|
|
||||||
|
|
||||||
export default function AccountNameForm() {
|
export default function AccountNameForm() {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
const [currentUser] = useAtom(currentUserAtom);
|
const [user, setUser] = useAtom(userAtom);
|
||||||
const [, setUser] = useAtom(userAtom);
|
|
||||||
|
|
||||||
const form = useForm<FormValues>({
|
const form = useForm<FormValues>({
|
||||||
validate: zod4Resolver(formSchema),
|
validate: zod4Resolver(formSchema),
|
||||||
initialValues: {
|
initialValues: {
|
||||||
name: currentUser?.user.name,
|
name: user?.name,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import { getAppName } from "@/lib/config.ts";
|
|||||||
import { Helmet } from "react-helmet-async";
|
import { Helmet } from "react-helmet-async";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { AccountMfaSection } from "@/features/user/components/account-mfa-section";
|
import { AccountMfaSection } from "@/features/user/components/account-mfa-section";
|
||||||
|
import SessionList from "@/features/session/components/session-list";
|
||||||
|
|
||||||
export default function AccountSettings() {
|
export default function AccountSettings() {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
@@ -36,6 +37,10 @@ export default function AccountSettings() {
|
|||||||
<Divider my="lg" />
|
<Divider my="lg" />
|
||||||
|
|
||||||
<AccountMfaSection />
|
<AccountMfaSection />
|
||||||
|
|
||||||
|
<Divider my="lg" />
|
||||||
|
|
||||||
|
<SessionList />
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,6 +66,7 @@
|
|||||||
"ai": "^6.0.134",
|
"ai": "^6.0.134",
|
||||||
"ai-sdk-ollama": "^3.8.1",
|
"ai-sdk-ollama": "^3.8.1",
|
||||||
"bcrypt": "^6.0.0",
|
"bcrypt": "^6.0.0",
|
||||||
|
"bowser": "^2.14.1",
|
||||||
"bullmq": "^5.71.0",
|
"bullmq": "^5.71.0",
|
||||||
"cache-manager": "^7.2.8",
|
"cache-manager": "^7.2.8",
|
||||||
"cheerio": "^1.2.0",
|
"cheerio": "^1.2.0",
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ export interface AuditContext {
|
|||||||
actorId: string | null;
|
actorId: string | null;
|
||||||
actorType: 'user' | 'system' | 'api_key';
|
actorType: 'user' | 'system' | 'api_key';
|
||||||
ipAddress: string | null;
|
ipAddress: string | null;
|
||||||
|
userAgent: string | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const AUDIT_CONTEXT_KEY = 'auditContext';
|
export const AUDIT_CONTEXT_KEY = 'auditContext';
|
||||||
@@ -19,11 +20,15 @@ export class AuditContextMiddleware implements NestMiddleware {
|
|||||||
const workspaceId = (req as any).workspaceId ?? null;
|
const workspaceId = (req as any).workspaceId ?? null;
|
||||||
const ipAddress = this.extractIpAddress(req);
|
const ipAddress = this.extractIpAddress(req);
|
||||||
|
|
||||||
|
const userAgent =
|
||||||
|
(req.headers['user-agent'] as string) ?? null;
|
||||||
|
|
||||||
const auditContext: AuditContext = {
|
const auditContext: AuditContext = {
|
||||||
workspaceId,
|
workspaceId,
|
||||||
actorId: null,
|
actorId: null,
|
||||||
actorType: 'user',
|
actorType: 'user',
|
||||||
ipAddress,
|
ipAddress,
|
||||||
|
userAgent,
|
||||||
};
|
};
|
||||||
|
|
||||||
this.cls.set(AUDIT_CONTEXT_KEY, auditContext);
|
this.cls.set(AUDIT_CONTEXT_KEY, auditContext);
|
||||||
|
|||||||
@@ -5,12 +5,14 @@ import {
|
|||||||
HttpStatus,
|
HttpStatus,
|
||||||
Inject,
|
Inject,
|
||||||
Post,
|
Post,
|
||||||
|
Req,
|
||||||
Res,
|
Res,
|
||||||
UseGuards,
|
UseGuards,
|
||||||
Logger,
|
Logger,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { LoginDto } from './dto/login.dto';
|
import { LoginDto } from './dto/login.dto';
|
||||||
import { AuthService } from './services/auth.service';
|
import { AuthService } from './services/auth.service';
|
||||||
|
import { SessionService } from '../session/session.service';
|
||||||
import { SetupGuard } from './guards/setup.guard';
|
import { SetupGuard } from './guards/setup.guard';
|
||||||
import { EnvironmentService } from '../../integrations/environment/environment.service';
|
import { EnvironmentService } from '../../integrations/environment/environment.service';
|
||||||
import { CreateAdminUserDto } from './dto/create-admin-user.dto';
|
import { CreateAdminUserDto } from './dto/create-admin-user.dto';
|
||||||
@@ -22,7 +24,7 @@ import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
|
|||||||
import { ForgotPasswordDto } from './dto/forgot-password.dto';
|
import { ForgotPasswordDto } from './dto/forgot-password.dto';
|
||||||
import { PasswordResetDto } from './dto/password-reset.dto';
|
import { PasswordResetDto } from './dto/password-reset.dto';
|
||||||
import { VerifyUserTokenDto } from './dto/verify-user-token.dto';
|
import { VerifyUserTokenDto } from './dto/verify-user-token.dto';
|
||||||
import { FastifyReply } from 'fastify';
|
import { FastifyReply, FastifyRequest } from 'fastify';
|
||||||
import { validateSsoEnforcement } from './auth.util';
|
import { validateSsoEnforcement } from './auth.util';
|
||||||
import { ModuleRef } from '@nestjs/core';
|
import { ModuleRef } from '@nestjs/core';
|
||||||
import { AuditEvent, AuditResource } from '../../common/events/audit-events';
|
import { AuditEvent, AuditResource } from '../../common/events/audit-events';
|
||||||
@@ -37,6 +39,7 @@ export class AuthController {
|
|||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private authService: AuthService,
|
private authService: AuthService,
|
||||||
|
private sessionService: SessionService,
|
||||||
private environmentService: EnvironmentService,
|
private environmentService: EnvironmentService,
|
||||||
private moduleRef: ModuleRef,
|
private moduleRef: ModuleRef,
|
||||||
@Inject(AUDIT_SERVICE) private readonly auditService: IAuditService,
|
@Inject(AUDIT_SERVICE) private readonly auditService: IAuditService,
|
||||||
@@ -115,8 +118,15 @@ export class AuthController {
|
|||||||
@Body() dto: ChangePasswordDto,
|
@Body() dto: ChangePasswordDto,
|
||||||
@AuthUser() user: User,
|
@AuthUser() user: User,
|
||||||
@AuthWorkspace() workspace: Workspace,
|
@AuthWorkspace() workspace: Workspace,
|
||||||
|
@Req() req: FastifyRequest,
|
||||||
) {
|
) {
|
||||||
return this.authService.changePassword(dto, user.id, workspace.id);
|
const currentSessionId = (req.raw as any).sessionId;
|
||||||
|
return this.authService.changePassword(
|
||||||
|
dto,
|
||||||
|
user.id,
|
||||||
|
workspace.id,
|
||||||
|
currentSessionId,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@HttpCode(HttpStatus.OK)
|
@HttpCode(HttpStatus.OK)
|
||||||
@@ -178,8 +188,18 @@ export class AuthController {
|
|||||||
@Post('logout')
|
@Post('logout')
|
||||||
async logout(
|
async logout(
|
||||||
@AuthUser() user: User,
|
@AuthUser() user: User,
|
||||||
|
@Req() req: FastifyRequest,
|
||||||
@Res({ passthrough: true }) res: FastifyReply,
|
@Res({ passthrough: true }) res: FastifyReply,
|
||||||
) {
|
) {
|
||||||
|
const sessionId = (req.raw as any).sessionId;
|
||||||
|
if (sessionId) {
|
||||||
|
await this.sessionService.revokeSession(
|
||||||
|
sessionId,
|
||||||
|
user.id,
|
||||||
|
user.workspaceId,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
res.clearCookie('authToken');
|
res.clearCookie('authToken');
|
||||||
|
|
||||||
this.auditService.log({
|
this.auditService.log({
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ export type JwtPayload = {
|
|||||||
email: string;
|
email: string;
|
||||||
workspaceId: string;
|
workspaceId: string;
|
||||||
type: 'access';
|
type: 'access';
|
||||||
|
sessionId?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type JwtCollabPayload = {
|
export type JwtCollabPayload = {
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ import {
|
|||||||
import { LoginDto } from '../dto/login.dto';
|
import { LoginDto } from '../dto/login.dto';
|
||||||
import { CreateUserDto } from '../dto/create-user.dto';
|
import { CreateUserDto } from '../dto/create-user.dto';
|
||||||
import { TokenService } from './token.service';
|
import { TokenService } from './token.service';
|
||||||
|
import { SessionService } from '../../session/session.service';
|
||||||
|
import { UserSessionRepo } from '@docmost/db/repos/session/user-session.repo';
|
||||||
import { SignupService } from './signup.service';
|
import { SignupService } from './signup.service';
|
||||||
import { CreateAdminUserDto } from '../dto/create-admin-user.dto';
|
import { CreateAdminUserDto } from '../dto/create-admin-user.dto';
|
||||||
import { UserRepo } from '@docmost/db/repos/user/user.repo';
|
import { UserRepo } from '@docmost/db/repos/user/user.repo';
|
||||||
@@ -44,6 +46,8 @@ export class AuthService {
|
|||||||
constructor(
|
constructor(
|
||||||
private signupService: SignupService,
|
private signupService: SignupService,
|
||||||
private tokenService: TokenService,
|
private tokenService: TokenService,
|
||||||
|
private sessionService: SessionService,
|
||||||
|
private userSessionRepo: UserSessionRepo,
|
||||||
private userRepo: UserRepo,
|
private userRepo: UserRepo,
|
||||||
private userTokenRepo: UserTokenRepo,
|
private userTokenRepo: UserTokenRepo,
|
||||||
private mailService: MailService,
|
private mailService: MailService,
|
||||||
@@ -90,19 +94,19 @@ export class AuthService {
|
|||||||
metadata: { source: 'password' },
|
metadata: { source: 'password' },
|
||||||
});
|
});
|
||||||
|
|
||||||
return this.tokenService.generateAccessToken(user);
|
return this.sessionService.createSessionAndToken(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
async register(createUserDto: CreateUserDto, workspaceId: string) {
|
async register(createUserDto: CreateUserDto, workspaceId: string) {
|
||||||
const user = await this.signupService.signup(createUserDto, workspaceId);
|
const user = await this.signupService.signup(createUserDto, workspaceId);
|
||||||
return this.tokenService.generateAccessToken(user);
|
return this.sessionService.createSessionAndToken(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
async setup(createAdminUserDto: CreateAdminUserDto) {
|
async setup(createAdminUserDto: CreateAdminUserDto) {
|
||||||
const { workspace, user } =
|
const { workspace, user } =
|
||||||
await this.signupService.initialSetup(createAdminUserDto);
|
await this.signupService.initialSetup(createAdminUserDto);
|
||||||
|
|
||||||
const authToken = await this.tokenService.generateAccessToken(user);
|
const authToken = await this.sessionService.createSessionAndToken(user);
|
||||||
return { workspace, authToken };
|
return { workspace, authToken };
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,6 +114,7 @@ export class AuthService {
|
|||||||
dto: ChangePasswordDto,
|
dto: ChangePasswordDto,
|
||||||
userId: string,
|
userId: string,
|
||||||
workspaceId: string,
|
workspaceId: string,
|
||||||
|
currentSessionId?: string,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const user = await this.userRepo.findById(userId, workspaceId, {
|
const user = await this.userRepo.findById(userId, workspaceId, {
|
||||||
includePassword: true,
|
includePassword: true,
|
||||||
@@ -138,6 +143,16 @@ export class AuthService {
|
|||||||
workspaceId,
|
workspaceId,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (currentSessionId) {
|
||||||
|
await this.userSessionRepo.deleteAllExceptCurrent(
|
||||||
|
currentSessionId,
|
||||||
|
userId,
|
||||||
|
workspaceId,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
await this.userSessionRepo.deleteByUserId(userId, workspaceId);
|
||||||
|
}
|
||||||
|
|
||||||
this.auditService.log({
|
this.auditService.log({
|
||||||
event: AuditEvent.USER_PASSWORD_CHANGED,
|
event: AuditEvent.USER_PASSWORD_CHANGED,
|
||||||
resourceType: AuditResource.USER,
|
resourceType: AuditResource.USER,
|
||||||
@@ -244,6 +259,8 @@ export class AuthService {
|
|||||||
.execute();
|
.execute();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await this.userSessionRepo.deleteByUserId(user.id, workspace.id);
|
||||||
|
|
||||||
this.auditService.setActorId(user.id);
|
this.auditService.setActorId(user.id);
|
||||||
this.auditService.log({
|
this.auditService.log({
|
||||||
event: AuditEvent.USER_PASSWORD_RESET,
|
event: AuditEvent.USER_PASSWORD_RESET,
|
||||||
@@ -276,7 +293,7 @@ export class AuthService {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const authToken = await this.tokenService.generateAccessToken(user);
|
const authToken = await this.sessionService.createSessionAndToken(user);
|
||||||
return { authToken };
|
return { authToken };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ export class TokenService {
|
|||||||
private environmentService: EnvironmentService,
|
private environmentService: EnvironmentService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async generateAccessToken(user: User): Promise<string> {
|
async generateAccessToken(user: User, sessionId: string): Promise<string> {
|
||||||
if (isUserDisabled(user)) {
|
if (isUserDisabled(user)) {
|
||||||
throw new ForbiddenException();
|
throw new ForbiddenException();
|
||||||
}
|
}
|
||||||
@@ -35,6 +35,7 @@ export class TokenService {
|
|||||||
email: user.email,
|
email: user.email,
|
||||||
workspaceId: user.workspaceId,
|
workspaceId: user.workspaceId,
|
||||||
type: JwtType.ACCESS,
|
type: JwtType.ACCESS,
|
||||||
|
sessionId,
|
||||||
};
|
};
|
||||||
return this.jwtService.sign(payload);
|
return this.jwtService.sign(payload);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import { EnvironmentService } from '../../../integrations/environment/environmen
|
|||||||
import { JwtApiKeyPayload, JwtPayload, JwtType } from '../dto/jwt-payload';
|
import { JwtApiKeyPayload, JwtPayload, JwtType } from '../dto/jwt-payload';
|
||||||
import { WorkspaceRepo } from '@docmost/db/repos/workspace/workspace.repo';
|
import { WorkspaceRepo } from '@docmost/db/repos/workspace/workspace.repo';
|
||||||
import { UserRepo } from '@docmost/db/repos/user/user.repo';
|
import { UserRepo } from '@docmost/db/repos/user/user.repo';
|
||||||
|
import { UserSessionRepo } from '@docmost/db/repos/session/user-session.repo';
|
||||||
|
import { SessionActivityService } from '../../session/session-activity.service';
|
||||||
import { FastifyRequest } from 'fastify';
|
import { FastifyRequest } from 'fastify';
|
||||||
import { extractBearerTokenFromHeader, isUserDisabled } from '../../../common/helpers';
|
import { extractBearerTokenFromHeader, isUserDisabled } from '../../../common/helpers';
|
||||||
import { ModuleRef } from '@nestjs/core';
|
import { ModuleRef } from '@nestjs/core';
|
||||||
@@ -16,6 +18,8 @@ export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
|
|||||||
constructor(
|
constructor(
|
||||||
private userRepo: UserRepo,
|
private userRepo: UserRepo,
|
||||||
private workspaceRepo: WorkspaceRepo,
|
private workspaceRepo: WorkspaceRepo,
|
||||||
|
private userSessionRepo: UserSessionRepo,
|
||||||
|
private sessionActivityService: SessionActivityService,
|
||||||
private readonly environmentService: EnvironmentService,
|
private readonly environmentService: EnvironmentService,
|
||||||
private moduleRef: ModuleRef,
|
private moduleRef: ModuleRef,
|
||||||
) {
|
) {
|
||||||
@@ -57,6 +61,16 @@ export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
|
|||||||
throw new UnauthorizedException();
|
throw new UnauthorizedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ((payload as JwtPayload).sessionId) {
|
||||||
|
const sessionId = (payload as JwtPayload).sessionId;
|
||||||
|
const session = await this.userSessionRepo.findActiveById(sessionId);
|
||||||
|
if (!session || session.userId !== payload.sub || session.workspaceId !== payload.workspaceId) {
|
||||||
|
throw new UnauthorizedException('Session revoked');
|
||||||
|
}
|
||||||
|
req.raw.sessionId = sessionId;
|
||||||
|
this.sessionActivityService.trackActivity(sessionId, payload.sub, payload.workspaceId);
|
||||||
|
}
|
||||||
|
|
||||||
return { user, workspace };
|
return { user, workspace };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import { AuditContextMiddleware } from '../common/middlewares/audit-context.midd
|
|||||||
import { ShareModule } from './share/share.module';
|
import { ShareModule } from './share/share.module';
|
||||||
import { NotificationModule } from './notification/notification.module';
|
import { NotificationModule } from './notification/notification.module';
|
||||||
import { WatcherModule } from './watcher/watcher.module';
|
import { WatcherModule } from './watcher/watcher.module';
|
||||||
|
import { SessionModule } from './session/session.module';
|
||||||
import { ClsMiddleware } from 'nestjs-cls';
|
import { ClsMiddleware } from 'nestjs-cls';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
@@ -38,6 +39,7 @@ import { ClsMiddleware } from 'nestjs-cls';
|
|||||||
ShareModule,
|
ShareModule,
|
||||||
NotificationModule,
|
NotificationModule,
|
||||||
WatcherModule,
|
WatcherModule,
|
||||||
|
SessionModule,
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
export class CoreModule implements NestModule {
|
export class CoreModule implements NestModule {
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
import { IsNotEmpty, IsUUID } from 'class-validator';
|
||||||
|
|
||||||
|
export class RevokeSessionDto {
|
||||||
|
@IsUUID()
|
||||||
|
@IsNotEmpty()
|
||||||
|
sessionId: string;
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { RedisService } from '@nestjs-labs/nestjs-ioredis';
|
||||||
|
import type { Redis } from 'ioredis';
|
||||||
|
import { UserSessionRepo } from '@docmost/db/repos/session/user-session.repo';
|
||||||
|
import { UserRepo } from '@docmost/db/repos/user/user.repo';
|
||||||
|
|
||||||
|
const THROTTLE_SECONDS = 15 * 60; // 15 minutes
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class SessionActivityService {
|
||||||
|
private readonly redis: Redis;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
private readonly redisService: RedisService,
|
||||||
|
private readonly userSessionRepo: UserSessionRepo,
|
||||||
|
private readonly userRepo: UserRepo,
|
||||||
|
) {
|
||||||
|
this.redis = this.redisService.getOrThrow();
|
||||||
|
}
|
||||||
|
|
||||||
|
trackActivity(sessionId: string, userId: string, workspaceId: string): void {
|
||||||
|
const key = `session:activity:${sessionId}`;
|
||||||
|
|
||||||
|
this.redis
|
||||||
|
.set(key, '1', 'EX', THROTTLE_SECONDS, 'NX')
|
||||||
|
.then((result) => {
|
||||||
|
if (result === null) return; // key already exists, throttled
|
||||||
|
|
||||||
|
this.userSessionRepo.updateLastActiveAt(sessionId).catch(() => {});
|
||||||
|
this.userRepo
|
||||||
|
.updateUser({ lastActiveAt: new Date() }, userId, workspaceId)
|
||||||
|
.catch(() => {});
|
||||||
|
})
|
||||||
|
.catch(() => {});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
import {
|
||||||
|
BadRequestException,
|
||||||
|
Body,
|
||||||
|
Controller,
|
||||||
|
HttpCode,
|
||||||
|
HttpStatus,
|
||||||
|
Post,
|
||||||
|
Req,
|
||||||
|
UseGuards,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
import { SessionService } from './session.service';
|
||||||
|
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
|
||||||
|
import { AuthUser } from '../../common/decorators/auth-user.decorator';
|
||||||
|
import { AuthWorkspace } from '../../common/decorators/auth-workspace.decorator';
|
||||||
|
import { User, Workspace } from '@docmost/db/types/entity.types';
|
||||||
|
import { RevokeSessionDto } from './dto/revoke-session.dto';
|
||||||
|
import { FastifyRequest } from 'fastify';
|
||||||
|
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Controller('sessions')
|
||||||
|
export class SessionController {
|
||||||
|
constructor(private readonly sessionService: SessionService) {}
|
||||||
|
|
||||||
|
@HttpCode(HttpStatus.OK)
|
||||||
|
@Post()
|
||||||
|
async listSessions(
|
||||||
|
@AuthUser() user: User,
|
||||||
|
@AuthWorkspace() workspace: Workspace,
|
||||||
|
@Req() req: FastifyRequest,
|
||||||
|
) {
|
||||||
|
const currentSessionId = (req.raw as any).sessionId ?? null;
|
||||||
|
const sessions = await this.sessionService.getActiveSessions(
|
||||||
|
user.id,
|
||||||
|
workspace.id,
|
||||||
|
currentSessionId,
|
||||||
|
);
|
||||||
|
return { sessions };
|
||||||
|
}
|
||||||
|
|
||||||
|
@HttpCode(HttpStatus.OK)
|
||||||
|
@Post('revoke')
|
||||||
|
async revokeSession(
|
||||||
|
@Body() dto: RevokeSessionDto,
|
||||||
|
@AuthUser() user: User,
|
||||||
|
@AuthWorkspace() workspace: Workspace,
|
||||||
|
@Req() req: FastifyRequest,
|
||||||
|
) {
|
||||||
|
const currentSessionId = (req.raw as any).sessionId;
|
||||||
|
if (dto.sessionId === currentSessionId) {
|
||||||
|
throw new BadRequestException(
|
||||||
|
'Cannot revoke current session. Use logout instead.',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
await this.sessionService.revokeSession(
|
||||||
|
dto.sessionId,
|
||||||
|
user.id,
|
||||||
|
workspace.id,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@HttpCode(HttpStatus.OK)
|
||||||
|
@Post('revoke-all')
|
||||||
|
async revokeAllSessions(
|
||||||
|
@AuthUser() user: User,
|
||||||
|
@AuthWorkspace() workspace: Workspace,
|
||||||
|
@Req() req: FastifyRequest,
|
||||||
|
) {
|
||||||
|
const currentSessionId = (req.raw as any).sessionId;
|
||||||
|
if (!currentSessionId) {
|
||||||
|
throw new BadRequestException(
|
||||||
|
'Current session not found. Please log in again.',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
await this.sessionService.revokeAllOtherSessions(
|
||||||
|
currentSessionId,
|
||||||
|
user.id,
|
||||||
|
workspace.id,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import { Global, Module } from '@nestjs/common';
|
||||||
|
import { SessionService } from './session.service';
|
||||||
|
import { SessionActivityService } from './session-activity.service';
|
||||||
|
import { SessionController } from './session.controller';
|
||||||
|
import { TokenModule } from '../auth/token.module';
|
||||||
|
|
||||||
|
@Global()
|
||||||
|
@Module({
|
||||||
|
imports: [TokenModule],
|
||||||
|
controllers: [SessionController],
|
||||||
|
providers: [SessionService, SessionActivityService],
|
||||||
|
exports: [SessionService, SessionActivityService],
|
||||||
|
})
|
||||||
|
export class SessionModule {}
|
||||||
@@ -0,0 +1,111 @@
|
|||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { TokenService } from '../auth/services/token.service';
|
||||||
|
import { UserSessionRepo } from '@docmost/db/repos/session/user-session.repo';
|
||||||
|
import { EnvironmentService } from '../../integrations/environment/environment.service';
|
||||||
|
import { User } from '@docmost/db/types/entity.types';
|
||||||
|
import { ClsService } from 'nestjs-cls';
|
||||||
|
import {
|
||||||
|
AuditContext,
|
||||||
|
AUDIT_CONTEXT_KEY,
|
||||||
|
} from '../../common/middlewares/audit-context.middleware';
|
||||||
|
import * as Bowser from 'bowser';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class SessionService {
|
||||||
|
constructor(
|
||||||
|
private readonly tokenService: TokenService,
|
||||||
|
private readonly userSessionRepo: UserSessionRepo,
|
||||||
|
private readonly environmentService: EnvironmentService,
|
||||||
|
private readonly cls: ClsService,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
async createSessionAndToken(user: User): Promise<string> {
|
||||||
|
const auditContext = this.cls.get<AuditContext>(AUDIT_CONTEXT_KEY);
|
||||||
|
const ipAddress = auditContext?.ipAddress ?? null;
|
||||||
|
const userAgent = auditContext?.userAgent ?? null;
|
||||||
|
|
||||||
|
const deviceName = this.parseDeviceName(userAgent);
|
||||||
|
const expiresAt = this.environmentService.getCookieExpiresIn();
|
||||||
|
|
||||||
|
const session = await this.userSessionRepo.insertSession({
|
||||||
|
userId: user.id,
|
||||||
|
workspaceId: user.workspaceId,
|
||||||
|
deviceName,
|
||||||
|
ipAddress,
|
||||||
|
expiresAt,
|
||||||
|
});
|
||||||
|
|
||||||
|
return this.tokenService.generateAccessToken(user, session.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
async getActiveSessions(
|
||||||
|
userId: string,
|
||||||
|
workspaceId: string,
|
||||||
|
currentSessionId: string | null,
|
||||||
|
) {
|
||||||
|
const sessions = await this.userSessionRepo.findActiveByUser(
|
||||||
|
userId,
|
||||||
|
workspaceId,
|
||||||
|
);
|
||||||
|
|
||||||
|
const mapped = sessions.map((s) => ({
|
||||||
|
id: s.id,
|
||||||
|
deviceName: s.deviceName,
|
||||||
|
ipAddress: s.ipAddress,
|
||||||
|
geoLocation: s.geoLocation,
|
||||||
|
lastActiveAt: s.lastActiveAt,
|
||||||
|
createdAt: s.createdAt,
|
||||||
|
isCurrent: s.id === currentSessionId,
|
||||||
|
}));
|
||||||
|
|
||||||
|
return mapped.sort((a, b) => {
|
||||||
|
if (a.isCurrent) return -1;
|
||||||
|
if (b.isCurrent) return 1;
|
||||||
|
return 0;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async revokeSession(
|
||||||
|
sessionId: string,
|
||||||
|
userId: string,
|
||||||
|
workspaceId: string,
|
||||||
|
): Promise<void> {
|
||||||
|
await this.userSessionRepo.revokeById(sessionId, userId, workspaceId);
|
||||||
|
}
|
||||||
|
|
||||||
|
async revokeAllOtherSessions(
|
||||||
|
currentSessionId: string,
|
||||||
|
userId: string,
|
||||||
|
workspaceId: string,
|
||||||
|
): Promise<void> {
|
||||||
|
await this.userSessionRepo.revokeAllExceptCurrent(
|
||||||
|
currentSessionId,
|
||||||
|
userId,
|
||||||
|
workspaceId,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private parseDeviceName(userAgent: string | null): string | null {
|
||||||
|
if (!userAgent) return null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const parsed = Bowser.parse(userAgent);
|
||||||
|
|
||||||
|
const os = parsed.os?.name;
|
||||||
|
const browser = parsed.browser?.name;
|
||||||
|
const platformType = parsed.platform?.type;
|
||||||
|
|
||||||
|
if (platformType === 'mobile' || platformType === 'tablet') {
|
||||||
|
return parsed.platform?.model || os || 'Mobile Device';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (os) {
|
||||||
|
return browser ? `${browser} on ${os}` : os;
|
||||||
|
}
|
||||||
|
|
||||||
|
return browser || null;
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -22,6 +22,7 @@ import InvitationEmail from '@docmost/transactional/emails/invitation-email';
|
|||||||
import { GroupUserRepo } from '@docmost/db/repos/group/group-user.repo';
|
import { GroupUserRepo } from '@docmost/db/repos/group/group-user.repo';
|
||||||
import InvitationAcceptedEmail from '@docmost/transactional/emails/invitation-accepted-email';
|
import InvitationAcceptedEmail from '@docmost/transactional/emails/invitation-accepted-email';
|
||||||
import { TokenService } from '../../auth/services/token.service';
|
import { TokenService } from '../../auth/services/token.service';
|
||||||
|
import { SessionService } from '../../session/session.service';
|
||||||
import { nanoIdGen } from '../../../common/helpers';
|
import { nanoIdGen } from '../../../common/helpers';
|
||||||
import { PaginationOptions } from '@docmost/db/pagination/pagination-options';
|
import { PaginationOptions } from '@docmost/db/pagination/pagination-options';
|
||||||
import { executeWithCursorPagination } from '@docmost/db/pagination/cursor-pagination';
|
import { executeWithCursorPagination } from '@docmost/db/pagination/cursor-pagination';
|
||||||
@@ -49,6 +50,7 @@ export class WorkspaceInvitationService {
|
|||||||
private mailService: MailService,
|
private mailService: MailService,
|
||||||
private domainService: DomainService,
|
private domainService: DomainService,
|
||||||
private tokenService: TokenService,
|
private tokenService: TokenService,
|
||||||
|
private sessionService: SessionService,
|
||||||
@InjectKysely() private readonly db: KyselyDB,
|
@InjectKysely() private readonly db: KyselyDB,
|
||||||
@InjectQueue(QueueName.BILLING_QUEUE) private billingQueue: Queue,
|
@InjectQueue(QueueName.BILLING_QUEUE) private billingQueue: Queue,
|
||||||
private readonly environmentService: EnvironmentService,
|
private readonly environmentService: EnvironmentService,
|
||||||
@@ -350,7 +352,7 @@ export class WorkspaceInvitationService {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const authToken = await this.tokenService.generateAccessToken(newUser);
|
const authToken = await this.sessionService.createSessionAndToken(newUser);
|
||||||
return { authToken };
|
return { authToken };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import {
|
|||||||
NotFoundException,
|
NotFoundException,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { LicenseCheckService } from '../../../integrations/environment/license-check.service';
|
import { LicenseCheckService } from '../../../integrations/environment/license-check.service';
|
||||||
|
import { UserSessionRepo } from '@docmost/db/repos/session/user-session.repo';
|
||||||
import { CreateWorkspaceDto } from '../dto/create-workspace.dto';
|
import { CreateWorkspaceDto } from '../dto/create-workspace.dto';
|
||||||
import { UpdateWorkspaceDto } from '../dto/update-workspace.dto';
|
import { UpdateWorkspaceDto } from '../dto/update-workspace.dto';
|
||||||
import { SpaceService } from '../../space/services/space.service';
|
import { SpaceService } from '../../space/services/space.service';
|
||||||
@@ -67,6 +68,7 @@ export class WorkspaceService {
|
|||||||
@InjectQueue(QueueName.BILLING_QUEUE) private billingQueue: Queue,
|
@InjectQueue(QueueName.BILLING_QUEUE) private billingQueue: Queue,
|
||||||
@InjectQueue(QueueName.AI_QUEUE) private aiQueue: Queue,
|
@InjectQueue(QueueName.AI_QUEUE) private aiQueue: Queue,
|
||||||
@Inject(AUDIT_SERVICE) private readonly auditService: IAuditService,
|
@Inject(AUDIT_SERVICE) private readonly auditService: IAuditService,
|
||||||
|
private userSessionRepo: UserSessionRepo,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async findById(workspaceId: string) {
|
async findById(workspaceId: string) {
|
||||||
@@ -673,6 +675,8 @@ export class WorkspaceService {
|
|||||||
workspaceId,
|
workspaceId,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
await this.userSessionRepo.revokeByUserId(userId, workspaceId);
|
||||||
|
|
||||||
this.auditService.log({
|
this.auditService.log({
|
||||||
event: AuditEvent.USER_DEACTIVATED,
|
event: AuditEvent.USER_DEACTIVATED,
|
||||||
resourceType: AuditResource.USER,
|
resourceType: AuditResource.USER,
|
||||||
@@ -787,6 +791,8 @@ export class WorkspaceService {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await this.userSessionRepo.revokeByUserId(userId, workspaceId);
|
||||||
|
|
||||||
this.auditService.log({
|
this.auditService.log({
|
||||||
event: AuditEvent.USER_DELETED,
|
event: AuditEvent.USER_DELETED,
|
||||||
resourceType: AuditResource.USER,
|
resourceType: AuditResource.USER,
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import { KyselyDB } from '@docmost/db/types/kysely.types';
|
|||||||
import * as process from 'node:process';
|
import * as process from 'node:process';
|
||||||
import { MigrationService } from '@docmost/db/services/migration.service';
|
import { MigrationService } from '@docmost/db/services/migration.service';
|
||||||
import { UserTokenRepo } from './repos/user-token/user-token.repo';
|
import { UserTokenRepo } from './repos/user-token/user-token.repo';
|
||||||
|
import { UserSessionRepo } from '@docmost/db/repos/session/user-session.repo';
|
||||||
import { BacklinkRepo } from '@docmost/db/repos/backlink/backlink.repo';
|
import { BacklinkRepo } from '@docmost/db/repos/backlink/backlink.repo';
|
||||||
import { ShareRepo } from '@docmost/db/repos/share/share.repo';
|
import { ShareRepo } from '@docmost/db/repos/share/share.repo';
|
||||||
import { NotificationRepo } from '@docmost/db/repos/notification/notification.repo';
|
import { NotificationRepo } from '@docmost/db/repos/notification/notification.repo';
|
||||||
@@ -76,6 +77,7 @@ import { normalizePostgresUrl } from '../common/helpers';
|
|||||||
CommentRepo,
|
CommentRepo,
|
||||||
AttachmentRepo,
|
AttachmentRepo,
|
||||||
UserTokenRepo,
|
UserTokenRepo,
|
||||||
|
UserSessionRepo,
|
||||||
BacklinkRepo,
|
BacklinkRepo,
|
||||||
ShareRepo,
|
ShareRepo,
|
||||||
NotificationRepo,
|
NotificationRepo,
|
||||||
@@ -95,6 +97,7 @@ import { normalizePostgresUrl } from '../common/helpers';
|
|||||||
CommentRepo,
|
CommentRepo,
|
||||||
AttachmentRepo,
|
AttachmentRepo,
|
||||||
UserTokenRepo,
|
UserTokenRepo,
|
||||||
|
UserSessionRepo,
|
||||||
BacklinkRepo,
|
BacklinkRepo,
|
||||||
ShareRepo,
|
ShareRepo,
|
||||||
NotificationRepo,
|
NotificationRepo,
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
import { Kysely, sql } from 'kysely';
|
||||||
|
|
||||||
|
export async function up(db: Kysely<any>): Promise<void> {
|
||||||
|
await db.schema
|
||||||
|
.createTable('user_sessions')
|
||||||
|
.addColumn('id', 'uuid', (col) =>
|
||||||
|
col.primaryKey().defaultTo(sql`gen_uuid_v7()`),
|
||||||
|
)
|
||||||
|
.addColumn('user_id', 'uuid', (col) =>
|
||||||
|
col.notNull().references('users.id').onDelete('cascade'),
|
||||||
|
)
|
||||||
|
.addColumn('workspace_id', 'uuid', (col) =>
|
||||||
|
col.notNull().references('workspaces.id').onDelete('cascade'),
|
||||||
|
)
|
||||||
|
.addColumn('device_name', 'varchar')
|
||||||
|
.addColumn('user_agent', 'text')
|
||||||
|
.addColumn('ip_address', sql`inet`)
|
||||||
|
.addColumn('geo_location', 'varchar')
|
||||||
|
.addColumn('last_active_at', 'timestamptz', (col) =>
|
||||||
|
col.notNull().defaultTo(sql`now()`),
|
||||||
|
)
|
||||||
|
.addColumn('expires_at', 'timestamptz', (col) => col.notNull())
|
||||||
|
.addColumn('revoked_at', 'timestamptz')
|
||||||
|
.addColumn('created_at', 'timestamptz', (col) =>
|
||||||
|
col.notNull().defaultTo(sql`now()`),
|
||||||
|
)
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
await db.schema
|
||||||
|
.createIndex('idx_user_sessions_user_workspace')
|
||||||
|
.on('user_sessions')
|
||||||
|
.columns(['user_id', 'workspace_id'])
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
await db.schema
|
||||||
|
.createIndex('idx_user_sessions_expires_at')
|
||||||
|
.on('user_sessions')
|
||||||
|
.column('expires_at')
|
||||||
|
.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function down(db: Kysely<any>): Promise<void> {
|
||||||
|
await db.schema.dropTable('user_sessions').execute();
|
||||||
|
}
|
||||||
@@ -0,0 +1,125 @@
|
|||||||
|
import {
|
||||||
|
InsertableUserSession,
|
||||||
|
UserSession,
|
||||||
|
} from '@docmost/db/types/entity.types';
|
||||||
|
import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
|
||||||
|
import { dbOrTx } from '@docmost/db/utils';
|
||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { InjectKysely } from 'nestjs-kysely';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class UserSessionRepo {
|
||||||
|
constructor(@InjectKysely() private readonly db: KyselyDB) {}
|
||||||
|
|
||||||
|
async insertSession(
|
||||||
|
session: InsertableUserSession,
|
||||||
|
trx?: KyselyTransaction,
|
||||||
|
): Promise<UserSession> {
|
||||||
|
const db = dbOrTx(this.db, trx);
|
||||||
|
return db
|
||||||
|
.insertInto('userSessions')
|
||||||
|
.values(session)
|
||||||
|
.returningAll()
|
||||||
|
.executeTakeFirstOrThrow();
|
||||||
|
}
|
||||||
|
|
||||||
|
async findActiveById(id: string): Promise<UserSession | undefined> {
|
||||||
|
return this.db
|
||||||
|
.selectFrom('userSessions')
|
||||||
|
.selectAll()
|
||||||
|
.where('id', '=', id)
|
||||||
|
.where('expiresAt', '>', new Date())
|
||||||
|
.where('revokedAt', 'is', null)
|
||||||
|
.executeTakeFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
async findActiveByUser(
|
||||||
|
userId: string,
|
||||||
|
workspaceId: string,
|
||||||
|
): Promise<UserSession[]> {
|
||||||
|
return this.db
|
||||||
|
.selectFrom('userSessions')
|
||||||
|
.selectAll()
|
||||||
|
.where('userId', '=', userId)
|
||||||
|
.where('workspaceId', '=', workspaceId)
|
||||||
|
.where('expiresAt', '>', new Date())
|
||||||
|
.where('revokedAt', 'is', null)
|
||||||
|
.orderBy('lastActiveAt', 'desc')
|
||||||
|
.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
async updateLastActiveAt(id: string): Promise<void> {
|
||||||
|
await this.db
|
||||||
|
.updateTable('userSessions')
|
||||||
|
.set({ lastActiveAt: new Date() })
|
||||||
|
.where('id', '=', id)
|
||||||
|
.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
async revokeById(
|
||||||
|
id: string,
|
||||||
|
userId: string,
|
||||||
|
workspaceId: string,
|
||||||
|
): Promise<void> {
|
||||||
|
await this.db
|
||||||
|
.updateTable('userSessions')
|
||||||
|
.set({ revokedAt: new Date() })
|
||||||
|
.where('id', '=', id)
|
||||||
|
.where('userId', '=', userId)
|
||||||
|
.where('workspaceId', '=', workspaceId)
|
||||||
|
.where('revokedAt', 'is', null)
|
||||||
|
.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
async revokeAllExceptCurrent(
|
||||||
|
currentSessionId: string,
|
||||||
|
userId: string,
|
||||||
|
workspaceId: string,
|
||||||
|
): Promise<void> {
|
||||||
|
await this.db
|
||||||
|
.updateTable('userSessions')
|
||||||
|
.set({ revokedAt: new Date() })
|
||||||
|
.where('userId', '=', userId)
|
||||||
|
.where('workspaceId', '=', workspaceId)
|
||||||
|
.where('id', '!=', currentSessionId)
|
||||||
|
.where('revokedAt', 'is', null)
|
||||||
|
.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
async revokeByUserId(
|
||||||
|
userId: string,
|
||||||
|
workspaceId: string,
|
||||||
|
): Promise<void> {
|
||||||
|
await this.db
|
||||||
|
.updateTable('userSessions')
|
||||||
|
.set({ revokedAt: new Date() })
|
||||||
|
.where('userId', '=', userId)
|
||||||
|
.where('workspaceId', '=', workspaceId)
|
||||||
|
.where('revokedAt', 'is', null)
|
||||||
|
.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
async deleteByUserId(
|
||||||
|
userId: string,
|
||||||
|
workspaceId: string,
|
||||||
|
): Promise<void> {
|
||||||
|
await this.db
|
||||||
|
.deleteFrom('userSessions')
|
||||||
|
.where('userId', '=', userId)
|
||||||
|
.where('workspaceId', '=', workspaceId)
|
||||||
|
.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
async deleteAllExceptCurrent(
|
||||||
|
currentSessionId: string,
|
||||||
|
userId: string,
|
||||||
|
workspaceId: string,
|
||||||
|
): Promise<void> {
|
||||||
|
await this.db
|
||||||
|
.deleteFrom('userSessions')
|
||||||
|
.where('userId', '=', userId)
|
||||||
|
.where('workspaceId', '=', workspaceId)
|
||||||
|
.where('id', '!=', currentSessionId)
|
||||||
|
.execute();
|
||||||
|
}
|
||||||
|
}
|
||||||
+15
@@ -429,6 +429,20 @@ export interface PagePermissions {
|
|||||||
updatedAt: Generated<Timestamp>;
|
updatedAt: Generated<Timestamp>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface UserSessions {
|
||||||
|
id: Generated<string>;
|
||||||
|
userId: string;
|
||||||
|
workspaceId: string;
|
||||||
|
deviceName: string | null;
|
||||||
|
userAgent: string | null;
|
||||||
|
ipAddress: string | null;
|
||||||
|
geoLocation: string | null;
|
||||||
|
lastActiveAt: Generated<Timestamp>;
|
||||||
|
expiresAt: Timestamp;
|
||||||
|
revokedAt: Timestamp | null;
|
||||||
|
createdAt: Generated<Timestamp>;
|
||||||
|
}
|
||||||
|
|
||||||
export interface DB {
|
export interface DB {
|
||||||
apiKeys: ApiKeys;
|
apiKeys: ApiKeys;
|
||||||
attachments: Attachments;
|
attachments: Attachments;
|
||||||
@@ -451,6 +465,7 @@ export interface DB {
|
|||||||
spaces: Spaces;
|
spaces: Spaces;
|
||||||
userMfa: UserMfa;
|
userMfa: UserMfa;
|
||||||
users: Users;
|
users: Users;
|
||||||
|
userSessions: UserSessions;
|
||||||
userTokens: UserTokens;
|
userTokens: UserTokens;
|
||||||
watchers: Watchers;
|
watchers: Watchers;
|
||||||
workspaceInvitations: WorkspaceInvitations;
|
workspaceInvitations: WorkspaceInvitations;
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import {
|
|||||||
Shares,
|
Shares,
|
||||||
FileTasks,
|
FileTasks,
|
||||||
UserMfa as _UserMFA,
|
UserMfa as _UserMFA,
|
||||||
|
UserSessions,
|
||||||
ApiKeys,
|
ApiKeys,
|
||||||
Watchers,
|
Watchers,
|
||||||
Audit as _Audit,
|
Audit as _Audit,
|
||||||
@@ -157,6 +158,11 @@ export type PagePermission = Selectable<_PagePermissions>;
|
|||||||
export type InsertablePagePermission = Insertable<_PagePermissions>;
|
export type InsertablePagePermission = Insertable<_PagePermissions>;
|
||||||
export type UpdatablePagePermission = Updateable<Omit<_PagePermissions, 'id'>>;
|
export type UpdatablePagePermission = Updateable<Omit<_PagePermissions, 'id'>>;
|
||||||
|
|
||||||
|
// User Session
|
||||||
|
export type UserSession = Selectable<UserSessions>;
|
||||||
|
export type InsertableUserSession = Insertable<UserSessions>;
|
||||||
|
export type UpdatableUserSession = Updateable<Omit<UserSessions, 'id'>>;
|
||||||
|
|
||||||
// Audit
|
// Audit
|
||||||
export type Audit = Selectable<_Audit>;
|
export type Audit = Selectable<_Audit>;
|
||||||
export type InsertableAudit = Insertable<_Audit>;
|
export type InsertableAudit = Insertable<_Audit>;
|
||||||
|
|||||||
+1
-1
Submodule apps/server/src/ee updated: 73b6953888...ebf891554c
Generated
+7
-4
@@ -552,6 +552,9 @@ importers:
|
|||||||
bcrypt:
|
bcrypt:
|
||||||
specifier: ^6.0.0
|
specifier: ^6.0.0
|
||||||
version: 6.0.0
|
version: 6.0.0
|
||||||
|
bowser:
|
||||||
|
specifier: ^2.14.1
|
||||||
|
version: 2.14.1
|
||||||
bullmq:
|
bullmq:
|
||||||
specifier: ^5.71.0
|
specifier: ^5.71.0
|
||||||
version: 5.71.0
|
version: 5.71.0
|
||||||
@@ -5840,8 +5843,8 @@ packages:
|
|||||||
boolbase@1.0.0:
|
boolbase@1.0.0:
|
||||||
resolution: {integrity: sha512-JZOSA7Mo9sNGB8+UjSgzdLtokWAky1zbztM3WRLCbZ70/3cTANmQmOdR7y2g+J0e2WXywy1yS468tY+IruqEww==}
|
resolution: {integrity: sha512-JZOSA7Mo9sNGB8+UjSgzdLtokWAky1zbztM3WRLCbZ70/3cTANmQmOdR7y2g+J0e2WXywy1yS468tY+IruqEww==}
|
||||||
|
|
||||||
bowser@2.11.0:
|
bowser@2.14.1:
|
||||||
resolution: {integrity: sha512-AlcaJBi/pqqJBIQ8U9Mcpc9i8Aqxn88Skv5d+xBX006BY5u8N3mGLHa5Lgppa7L/HfwgwLgZ6NYs+Ag6uUmJRA==}
|
resolution: {integrity: sha512-tzPjzCxygAKWFOJP011oxFHs57HzIhOEracIgAePE4pqB3LikALKnSzUyU4MGs9/iCEUuHlAJTjTc5M+u7YEGg==}
|
||||||
|
|
||||||
boxen@5.1.2:
|
boxen@5.1.2:
|
||||||
resolution: {integrity: sha512-9gYgQKXx+1nP8mP7CzFyaUARhg7D3n1dF/FnErWmu9l6JvGpNUN278h0aSb+QjoiKSWG+iZ3uHrcqk0qrY9RQQ==}
|
resolution: {integrity: sha512-9gYgQKXx+1nP8mP7CzFyaUARhg7D3n1dF/FnErWmu9l6JvGpNUN278h0aSb+QjoiKSWG+iZ3uHrcqk0qrY9RQQ==}
|
||||||
@@ -11381,7 +11384,7 @@ snapshots:
|
|||||||
dependencies:
|
dependencies:
|
||||||
'@aws-sdk/types': 3.973.6
|
'@aws-sdk/types': 3.973.6
|
||||||
'@smithy/types': 4.13.1
|
'@smithy/types': 4.13.1
|
||||||
bowser: 2.11.0
|
bowser: 2.14.1
|
||||||
tslib: 2.8.1
|
tslib: 2.8.1
|
||||||
|
|
||||||
'@aws-sdk/util-user-agent-node@3.973.10':
|
'@aws-sdk/util-user-agent-node@3.973.10':
|
||||||
@@ -16625,7 +16628,7 @@ snapshots:
|
|||||||
|
|
||||||
boolbase@1.0.0: {}
|
boolbase@1.0.0: {}
|
||||||
|
|
||||||
bowser@2.11.0: {}
|
bowser@2.14.1: {}
|
||||||
|
|
||||||
boxen@5.1.2:
|
boxen@5.1.2:
|
||||||
dependencies:
|
dependencies:
|
||||||
|
|||||||
Reference in New Issue
Block a user