|
|
@@ -25,6 +25,7 @@ import { PageRequest } from '../common/dto/page-request'
|
|
|
import { endOfDay, startOfDay } from 'date-fns'
|
|
|
import { BalanceRecord, BalanceType } from '../balance/entities/balance-record.entities'
|
|
|
import { generateKey, generatePasscodes, verifyPasscode } from '../utils/authenticator'
|
|
|
+import { string } from 'yup'
|
|
|
|
|
|
@Injectable()
|
|
|
export class UsersService implements OnModuleInit {
|
|
|
@@ -134,11 +135,18 @@ export class UsersService implements OnModuleInit {
|
|
|
return user
|
|
|
}
|
|
|
|
|
|
- public async binding(username: string) {
|
|
|
+ public async binding(username: string, password: string) {
|
|
|
const users = await this.userRepository.findOneBy({ username })
|
|
|
- if (users.twoFactorCode) {
|
|
|
- return 'success'
|
|
|
+ if (!users) {
|
|
|
+ throw new UnauthorizedException('Username and password doesn\'t match')
|
|
|
+ }
|
|
|
+ const isMatch = await this.hashingService.compare(password, users.password)
|
|
|
+ if (!isMatch) {
|
|
|
+ throw new UnauthorizedException('Username and password doesn\'t match')
|
|
|
}
|
|
|
+ // if (users.twoFactorCode) {
|
|
|
+ // return 'success'
|
|
|
+ // }
|
|
|
const key = await generateKey(username)
|
|
|
console.log('key', key)
|
|
|
users.twoFactorCode = key.secret
|
|
|
@@ -147,6 +155,27 @@ export class UsersService implements OnModuleInit {
|
|
|
return key.url
|
|
|
}
|
|
|
|
|
|
+ public async handleConfirmBinding(username: string, password: string, bindingCode: string) {
|
|
|
+ const users = await this.userRepository.findOneBy({ username })
|
|
|
+ if (!users) {
|
|
|
+ throw new UnauthorizedException('Username and password doesn\'t match')
|
|
|
+ }
|
|
|
+ const isMatch = await this.hashingService.compare(password, users.password)
|
|
|
+ if (!isMatch) {
|
|
|
+ throw new UnauthorizedException('Username and password doesn\'t match')
|
|
|
+ }
|
|
|
+ if (users.twoFactorCode === null || users.twoFactorCode === '') {
|
|
|
+ throw new UnauthorizedException('请绑定谷歌验证器获取认证码.')
|
|
|
+ } else {
|
|
|
+ const verified = await verifyPasscode(users.twoFactorCode, bindingCode)
|
|
|
+ if (verified) {
|
|
|
+ return 'success'
|
|
|
+ } else {
|
|
|
+ throw new UnauthorizedException('认证码错误,请重试.')
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public async create(userDto: UserCreateDto): Promise<IUsers> {
|
|
|
try {
|
|
|
if (userDto.password) {
|