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
88 changes: 88 additions & 0 deletions zsphere/bff/src/identity/validate/validate.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { UnauthorizedException } from '@nestjs/common'

import { ValidatePasswordService } from './validate.service'

jest.mock('@nestjs/sequelize', () => ({
InjectModel: () => () => {},
InjectConnection: () => () => {},
InjectDataSource: () => () => {}
}))

jest.mock('@/model/zs-session.model', () => ({
ZsSession: class ZsSession {}
}))

jest.mock('@/api/zstack/ValidatePasswordAction', () => ({
ValidatePasswordAction: class ValidatePasswordAction {}
}))

jest.mock('@/common/trans/zs-http-service/zs-http-service.service', () => ({
ZsHttpService: class ZsHttpService {}
}))

const createService = ({
sessionRow = null,
available = true
}: {
sessionRow?: unknown
available?: boolean
} = {}) => {
const service = new ValidatePasswordService()
const mocks = {
zsSession: {
findOne: jest.fn().mockResolvedValue(sessionRow)
},
validatePasswordAction: {
call: jest.fn().mockResolvedValue({ available })
}
}

Object.assign(service as unknown as Record<string, unknown>, mocks, {
context: { req: { headers: { 'x-session-id': 'session-1' } } }
})

return { service, mocks }
}

describe('ValidatePasswordService', () => {
it('rejects when no session header is present (unauthenticated oracle fix)', async () => {
const { service, mocks } = createService()
Object.assign(service as unknown as Record<string, unknown>, {
context: { req: { headers: {} } }
})

const promise = service.validatePassword('admin', 'guess', 'account')

await expect(promise).rejects.toBeInstanceOf(UnauthorizedException)
// 与 UIPrivilegeService 等现有鉴权检查保持一致的文案,便于客户端统一处理
await expect(promise).rejects.toThrow('Session not found')
expect(mocks.validatePasswordAction.call).not.toHaveBeenCalled()
})

it('rejects when the session is not registered locally', async () => {
const { service, mocks } = createService({ sessionRow: null })

const promise = service.validatePassword('admin', 'guess', 'account')

await expect(promise).rejects.toBeInstanceOf(UnauthorizedException)
await expect(promise).rejects.toThrow('Invalid session')
expect(mocks.validatePasswordAction.call).not.toHaveBeenCalled()
})

it('forwards to the action when the caller is authenticated', async () => {
const { service, mocks } = createService({ sessionRow: { sessionId: 'session-1' } })

await expect(service.validatePassword('admin', 'secret', 'account')).resolves.toBe(true)
expect(mocks.validatePasswordAction.call).toHaveBeenCalledWith({
loginName: 'admin',
loginType: 'account',
password: 'secret'
})
})

it('returns false when the password is wrong', async () => {
const { service } = createService({ sessionRow: { sessionId: 'session-1' }, available: false })

await expect(service.validatePassword('admin', 'wrong', 'account')).resolves.toBe(false)
})
})
21 changes: 20 additions & 1 deletion zsphere/bff/src/identity/validate/validate.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Inject, Injectable } from '@nestjs/common'
import { Inject, Injectable, UnauthorizedException } from '@nestjs/common'

import { ValidatePasswordAction } from '@/api/zstack/ValidatePasswordAction'
import { ActionService } from '@/base/action-service'
Expand All @@ -7,7 +7,26 @@ import { ActionService } from '@/base/action-service'
export class ValidatePasswordService extends ActionService {
@Inject() validatePasswordAction: ValidatePasswordAction

/**
* 校验密码是否正确(GraphQL: validatePassword)。
*
* 安全要求:必须携带有效会话(x-session-id 且存在于本地 zs_session),
* 否则抛出 UnauthorizedException。该接口本质是"密码是否正确"的判定器,
* 未认证开放会变成在线密码爆破预言机(已实测可通过它确认默认口令)。
*/
async validatePassword(loginName: string, password: string, loginType: string): Promise<boolean> {
const sessionId = this.getSessionId()
if (!sessionId) {
throw new UnauthorizedException('Session not found')
}

const session = await this.getZsSession().findOne({
where: { sessionId }
})
if (!session) {
throw new UnauthorizedException('Invalid session')
}

const { available } = await this.validatePasswordAction.call({
loginName,
loginType,
Expand Down