Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions openapi.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"hash": "beb77cb0b37924ed4121b87dbd01aea157eb2cde455025934bcea22b64dba1a6",
"hash": "8428bef64b9fc5a5c46282383ecf8d80999b834f948d8a5b3565c9306068f66b",
"openapi": "3.0.0",
"paths": {
"/hello": {
Expand Down Expand Up @@ -7705,16 +7705,6 @@
"UpdateUserDto": {
"type": "object",
"properties": {
"passwordChangedAt": {
"type": "string",
"description": "上次修改密码时间(与密码哈希一并维护,用于口令轮换等策略)",
"format": "date-time"
},
"hasPassword": {
"type": "boolean",
"description": "是否有密码",
"readOnly": true
},
"avatar": {
"type": "string",
"description": "头像"
Expand Down
10 changes: 10 additions & 0 deletions src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import {
BadRequestException,
Body,
Expand All @@ -7,13 +8,15 @@ import {
Get,
HttpCode,
HttpStatus,
Inject,
NotFoundException,
Post,
Query,
UnauthorizedException,
} from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { ApiOkResponse, ApiOperation, ApiTags } from '@nestjs/swagger';
import { Cache } from 'cache-manager';
import { get, isEqual } from 'lodash';

import { JwtPayload } from 'src/auth';
Expand Down Expand Up @@ -59,6 +62,7 @@ function checkUserActive(user: UserDocument) {
@Controller('auth')
export class AuthController {
constructor(
@Inject(CACHE_MANAGER) private readonly cacheManager: Cache,
private readonly sessionService: SessionService,
private readonly userService: UserService,
private readonly jwtService: JwtService,
Expand All @@ -69,6 +73,10 @@ export class AuthController {
private readonly thirdPartyService: ThirdPartyService
) {}

private invalidateUserCache(userId: string) {
return this.cacheManager.del(`/users/${userId}`);
}

/**
* login with username/phone/email and password
*/
Expand Down Expand Up @@ -615,6 +623,7 @@ export class AuthController {
}

await this.userService.updatePassword(user.id, dto.password);
await this.invalidateUserCache(user.id);
}

/**
Expand All @@ -640,6 +649,7 @@ export class AuthController {
}

await this.userService.updatePassword(user.id, dto.password);
await this.invalidateUserCache(user.id);
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/user/dto/update-user.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ import { OmitType, PartialType } from '@nestjs/swagger';

import { UserDoc } from '../entities/user.entity';

export class UpdateUserDto extends OmitType(PartialType(UserDoc), ['password'] as const) {}
export class UpdateUserDto extends OmitType(PartialType(UserDoc), [
'password',
'passwordChangedAt',
Comment thread
coderprepares marked this conversation as resolved.
'hasPassword',
] as const) {}
56 changes: 56 additions & 0 deletions test/auth-login-logout.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Connection } from 'mongoose';
import request from 'supertest';

import { SessionWithToken } from 'src/auth';
import { CaptchaService } from 'src/captcha';
import { auth } from 'src/config';
import { NamespaceService } from 'src/namespace';
import { UserService } from 'src/user';
Expand All @@ -30,6 +31,7 @@ describe('Web auth (e2e)', () => {
let app: INestApplication;
let userService: UserService;
let namespaceService: NamespaceService;
let captchaService: CaptchaService;

const dbName = 'auth-login-logout-e2e';
const mongoUrl = `${mongoTestBaseUrl}/${dbName}`;
Expand All @@ -48,6 +50,7 @@ describe('Web auth (e2e)', () => {

userService = moduleFixture.get<UserService>(UserService);
namespaceService = moduleFixture.get<NamespaceService>(NamespaceService);
captchaService = moduleFixture.get<CaptchaService>(CaptchaService);

// 准备一个初始化 namespace
await namespaceService.create({
Expand Down Expand Up @@ -162,4 +165,57 @@ describe('Web auth (e2e)', () => {
.set('Accept', 'application/json')
.expect(401);
});

it('reset password by email should invalidate cached user details', async () => {
const userDoc = mockUser();
const user = await userService.create(userDoc);
const originalPasswordChangedAt = user.passwordChangedAt?.toISOString();
const captchaKey = `reset-email-${user.id}`;
const captchaCode = '123456';

await request(app.getHttpServer())
.get(`/users/${user.id}`)
.set('Content-Type', 'application/json')
.set('x-api-key', auth.apiKey)
.set('Accept', 'application/json')
.expect(200);

await captchaService.create({
key: captchaKey,
code: captchaCode,
});

await request(app.getHttpServer())
.post('/auth/@resetPasswordByEmail')
.send({
email: userDoc.email,
key: captchaKey,
code: captchaCode,
password: 'Abc12345@',
})
.set('Content-Type', 'application/json')
.set('x-api-key', auth.apiKey)
.set('Accept', 'application/json')
.expect(204);

const refreshedUserResp = await request(app.getHttpServer())
.get(`/users/${user.id}`)
.set('Content-Type', 'application/json')
.set('x-api-key', auth.apiKey)
.set('Accept', 'application/json')
.expect(200);

expect(refreshedUserResp.body.passwordChangedAt).not.toBe(originalPasswordChangedAt);

await request(app.getHttpServer())
.post('/auth/@login')
.send({
login: userDoc.username,
password: 'Abc12345@',
})
.set('Content-Type', 'application/json')
.set('x-api-key', auth.apiKey)
.set('Accept', 'application/json')
.expect(200);
});
});
Loading