Batch of fixes from the automated QA pass on develop. Each was reproduced and then verified fixed live (browser/curl); logic-bearing fixes have unit tests. Functional bugs: - #122 collab-token was capped by the anonymous public-share-AI throttler (5/min); skip all non-AUTH named throttlers on this auth-guarded, client-cached route. - #123 editor onAuthenticationFailed threw `jwtDecode(undefined)` and never reconnected; read the token via a ref, guard the decode (incl. missing exp), and refetch+reconnect on any auth failure. - #124 a slash command containing a space ("/Heading 1") inserted literal text; enable allowSpaces and close the menu when the query matches no items. - #125 space slug auto-gen produced uppercase initials for multi-word names; computeSpaceSlug now yields a lowercase alphanumeric slug. - #126 AI chat window position/size now persisted (atomWithStorage) across reload; also fixes a latent ResizeObserver-attach bug on first open. - #127 workspace name update accepted URLs; add @NoUrls (parity with setup). - #132 icon-columns 4/5 passed calc() into SVG width/height attrs (console spam); size via style. share-for-page query returns null instead of undefined. - #134 "Reindex now" counter looked stuck: reindex runs async; the client now polls coverage (bounded) so the counter climbs live; misleading server comment reworded. UX / consistency: - #128 add success toasts to favorite/label/avatar/member-(de)activate. - #129 "1 result found" pluralization; hide the single-option Type filter. - #130 replace raw Zod strings with friendly messages (name/password/group). - #131 unify "Untitled" casing in tree/breadcrumb/tab; stop force-uppercasing space-name chips; fix confirm-dialog labels (Cancel / Remove), invite placeholder typo, Export/Move-to-space labels. - #133 disable profile Save when clean; toast on unsupported avatar image; style the invalid-invitation page with a CTA; hide Share for read-only users; align the dictation "not configured" message; "Go to login page" typo. Tests: computeSpaceSlug, workspace-name NoUrls DTO, share-query null normalization, slash getSuggestionItems empty-close. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
245 lines
7.2 KiB
TypeScript
245 lines
7.2 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
HttpCode,
|
|
HttpStatus,
|
|
Inject,
|
|
Post,
|
|
Req,
|
|
Res,
|
|
UseGuards,
|
|
Logger,
|
|
} from '@nestjs/common';
|
|
import { SkipThrottle, ThrottlerGuard } from '@nestjs/throttler';
|
|
import {
|
|
AI_CHAT_THROTTLER,
|
|
AUTH_THROTTLER,
|
|
PAGE_TEMPLATE_THROTTLER,
|
|
PUBLIC_SHARE_AI_THROTTLER,
|
|
} from '../../integrations/throttle/throttler-names';
|
|
import { LoginDto } from './dto/login.dto';
|
|
import { AuthService } from './services/auth.service';
|
|
import { SessionService } from '../session/session.service';
|
|
import { SetupGuard } from './guards/setup.guard';
|
|
import { EnvironmentService } from '../../integrations/environment/environment.service';
|
|
import { CreateAdminUserDto } from './dto/create-admin-user.dto';
|
|
import { ChangePasswordDto } from './dto/change-password.dto';
|
|
import { AuthUser } from '../../common/decorators/auth-user.decorator';
|
|
import { User, Workspace } from '@docmost/db/types/entity.types';
|
|
import { AuthWorkspace } from '../../common/decorators/auth-workspace.decorator';
|
|
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
|
|
import { ForgotPasswordDto } from './dto/forgot-password.dto';
|
|
import { PasswordResetDto } from './dto/password-reset.dto';
|
|
import { VerifyUserTokenDto } from './dto/verify-user-token.dto';
|
|
import { FastifyReply, FastifyRequest } from 'fastify';
|
|
import { validateSsoEnforcement } from './auth.util';
|
|
import { ModuleRef } from '@nestjs/core';
|
|
import { AuditEvent, AuditResource } from '../../common/events/audit-events';
|
|
import {
|
|
AUDIT_SERVICE,
|
|
IAuditService,
|
|
} from '../../integrations/audit/audit.service';
|
|
|
|
@SkipThrottle({ [AI_CHAT_THROTTLER]: true })
|
|
@UseGuards(ThrottlerGuard)
|
|
@Controller('auth')
|
|
export class AuthController {
|
|
private readonly logger = new Logger(AuthController.name);
|
|
|
|
constructor(
|
|
private authService: AuthService,
|
|
private sessionService: SessionService,
|
|
private environmentService: EnvironmentService,
|
|
private moduleRef: ModuleRef,
|
|
@Inject(AUDIT_SERVICE) private readonly auditService: IAuditService,
|
|
) {}
|
|
|
|
@HttpCode(HttpStatus.OK)
|
|
@Post('login')
|
|
async login(
|
|
@AuthWorkspace() workspace: Workspace,
|
|
@Res({ passthrough: true }) res: FastifyReply,
|
|
@Body() loginInput: LoginDto,
|
|
) {
|
|
validateSsoEnforcement(workspace);
|
|
|
|
let MfaModule: any;
|
|
let isMfaModuleReady = false;
|
|
try {
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
MfaModule = require('./../../ee/mfa/services/mfa.service');
|
|
isMfaModuleReady = true;
|
|
} catch (err) {
|
|
this.logger.debug(
|
|
'MFA module requested but EE module not bundled in this build',
|
|
);
|
|
isMfaModuleReady = false;
|
|
}
|
|
if (isMfaModuleReady) {
|
|
const mfaService = this.moduleRef.get(MfaModule.MfaService, {
|
|
strict: false,
|
|
});
|
|
|
|
const mfaResult = await mfaService.checkMfaRequirements(
|
|
loginInput,
|
|
workspace,
|
|
res,
|
|
);
|
|
|
|
if (mfaResult) {
|
|
// If user has MFA enabled OR workspace enforces MFA, require MFA verification
|
|
if (mfaResult.userHasMfa || mfaResult.requiresMfaSetup) {
|
|
return {
|
|
userHasMfa: mfaResult.userHasMfa,
|
|
requiresMfaSetup: mfaResult.requiresMfaSetup,
|
|
isMfaEnforced: mfaResult.isMfaEnforced,
|
|
};
|
|
} else if (mfaResult.authToken) {
|
|
// User doesn't have MFA and workspace doesn't require it
|
|
this.setAuthCookie(res, mfaResult.authToken);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
const authToken = await this.authService.login(loginInput, workspace.id);
|
|
this.setAuthCookie(res, authToken);
|
|
}
|
|
|
|
@UseGuards(SetupGuard)
|
|
@HttpCode(HttpStatus.OK)
|
|
@Post('setup')
|
|
async setupWorkspace(
|
|
@Res({ passthrough: true }) res: FastifyReply,
|
|
@Body() createAdminUserDto: CreateAdminUserDto,
|
|
) {
|
|
const { workspace, authToken } =
|
|
await this.authService.setup(createAdminUserDto);
|
|
|
|
this.setAuthCookie(res, authToken);
|
|
return workspace;
|
|
}
|
|
|
|
@SkipThrottle({ [AUTH_THROTTLER]: true })
|
|
@UseGuards(JwtAuthGuard)
|
|
@HttpCode(HttpStatus.OK)
|
|
@Post('change-password')
|
|
async changePassword(
|
|
@Body() dto: ChangePasswordDto,
|
|
@AuthUser() user: User,
|
|
@AuthWorkspace() workspace: Workspace,
|
|
@Req() req: FastifyRequest,
|
|
) {
|
|
const currentSessionId = (req.raw as any).sessionId;
|
|
return this.authService.changePassword(
|
|
dto,
|
|
user.id,
|
|
workspace.id,
|
|
currentSessionId,
|
|
);
|
|
}
|
|
|
|
@HttpCode(HttpStatus.OK)
|
|
@Post('forgot-password')
|
|
async forgotPassword(
|
|
@Body() forgotPasswordDto: ForgotPasswordDto,
|
|
@AuthWorkspace() workspace: Workspace,
|
|
) {
|
|
validateSsoEnforcement(workspace);
|
|
return this.authService.forgotPassword(forgotPasswordDto, workspace);
|
|
}
|
|
|
|
@HttpCode(HttpStatus.OK)
|
|
@Post('password-reset')
|
|
async passwordReset(
|
|
@Res({ passthrough: true }) res: FastifyReply,
|
|
@Body() passwordResetDto: PasswordResetDto,
|
|
@AuthWorkspace() workspace: Workspace,
|
|
) {
|
|
const result = await this.authService.passwordReset(
|
|
passwordResetDto,
|
|
workspace,
|
|
);
|
|
|
|
if (result.requiresLogin) {
|
|
return {
|
|
requiresLogin: true,
|
|
};
|
|
}
|
|
|
|
// Set auth cookie if no MFA is required
|
|
this.setAuthCookie(res, result.authToken);
|
|
return {
|
|
requiresLogin: false,
|
|
};
|
|
}
|
|
|
|
@HttpCode(HttpStatus.OK)
|
|
@Post('verify-token')
|
|
async verifyResetToken(
|
|
@Body() verifyUserTokenDto: VerifyUserTokenDto,
|
|
@AuthWorkspace() workspace: Workspace,
|
|
) {
|
|
return this.authService.verifyUserToken(verifyUserTokenDto, workspace.id);
|
|
}
|
|
|
|
// The global ThrottlerGuard applies ALL named throttlers to every route by
|
|
// default, so each non-AUTH bucket (AI chat, page template, public-share AI)
|
|
// is explicitly skipped here. collab-token is auth-guarded (JwtAuthGuard),
|
|
// per-user and client-cached, so those feature buckets are irrelevant to it;
|
|
// skipping them avoids spurious 429s when a user opens many pages in a short
|
|
// window. The AUTH bucket is skipped too for the same per-user, cached reason.
|
|
@SkipThrottle({
|
|
[AUTH_THROTTLER]: true,
|
|
[AI_CHAT_THROTTLER]: true,
|
|
[PAGE_TEMPLATE_THROTTLER]: true,
|
|
[PUBLIC_SHARE_AI_THROTTLER]: true,
|
|
})
|
|
@UseGuards(JwtAuthGuard)
|
|
@HttpCode(HttpStatus.OK)
|
|
@Post('collab-token')
|
|
async collabToken(
|
|
@AuthUser() user: User,
|
|
@AuthWorkspace() workspace: Workspace,
|
|
) {
|
|
return this.authService.getCollabToken(user, workspace.id);
|
|
}
|
|
|
|
@SkipThrottle({ [AUTH_THROTTLER]: true })
|
|
@UseGuards(JwtAuthGuard)
|
|
@HttpCode(HttpStatus.OK)
|
|
@Post('logout')
|
|
async logout(
|
|
@AuthUser() user: User,
|
|
@Req() req: FastifyRequest,
|
|
@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');
|
|
|
|
this.auditService.log({
|
|
event: AuditEvent.USER_LOGOUT,
|
|
resourceType: AuditResource.USER,
|
|
resourceId: user.id,
|
|
});
|
|
}
|
|
|
|
setAuthCookie(res: FastifyReply, token: string) {
|
|
res.setCookie('authToken', token, {
|
|
httpOnly: true,
|
|
sameSite: 'lax',
|
|
path: '/',
|
|
expires: this.environmentService.getCookieExpiresIn(),
|
|
secure: this.environmentService.isHttps(),
|
|
});
|
|
}
|
|
}
|