2020-06-04 15:25:42 -04:00
|
|
|
import scrypt from 'scrypt-kdf'
|
|
|
|
import { UUIDV4 } from 'sequelize'
|
2020-04-12 07:52:50 -04:00
|
|
|
import {
|
|
|
|
BeforeCreate,
|
|
|
|
BeforeUpdate,
|
|
|
|
Column,
|
|
|
|
DataType,
|
|
|
|
Default,
|
|
|
|
HasMany,
|
|
|
|
IsEmail,
|
|
|
|
Model,
|
|
|
|
PrimaryKey,
|
|
|
|
Table,
|
|
|
|
Unique
|
|
|
|
} from 'sequelize-typescript'
|
2020-06-04 15:25:42 -04:00
|
|
|
import { Note } from './note'
|
2016-04-20 06:03:55 -04:00
|
|
|
|
2020-06-04 15:25:42 -04:00
|
|
|
@Table
|
|
|
|
export class User extends Model<User> {
|
|
|
|
@PrimaryKey
|
|
|
|
@Default(UUIDV4)
|
|
|
|
@Column(DataType.UUID)
|
|
|
|
id: string
|
|
|
|
|
|
|
|
@Unique
|
|
|
|
@Column(DataType.STRING)
|
|
|
|
profileid: string
|
|
|
|
|
|
|
|
@Column(DataType.TEXT)
|
|
|
|
profile: string
|
|
|
|
|
|
|
|
@Column(DataType.TEXT)
|
|
|
|
history: string
|
|
|
|
|
|
|
|
@Column(DataType.TEXT)
|
|
|
|
accessToken: string
|
|
|
|
|
|
|
|
@Column(DataType.TEXT)
|
|
|
|
refreshToken: string
|
|
|
|
|
|
|
|
@Column(DataType.UUID)
|
|
|
|
deleteToken: string
|
|
|
|
|
|
|
|
@IsEmail
|
|
|
|
@Column(DataType.TEXT)
|
|
|
|
email: string
|
|
|
|
|
|
|
|
@Column(DataType.TEXT)
|
|
|
|
password: string
|
|
|
|
|
|
|
|
@HasMany(() => Note, { foreignKey: 'lastchangeuserId', constraints: false })
|
|
|
|
@HasMany(() => Note, { foreignKey: 'ownerId', constraints: false })
|
2016-05-11 17:04:45 -04:00
|
|
|
|
2020-04-10 16:06:12 -04:00
|
|
|
@BeforeUpdate
|
|
|
|
@BeforeCreate
|
2020-04-11 08:12:39 -04:00
|
|
|
static async updatePasswordHashHook (user: User): Promise<void> {
|
2019-05-13 04:55:37 -04:00
|
|
|
// suggested way to hash passwords to be able to do this asynchronously:
|
|
|
|
// @see https://github.com/sequelize/sequelize/issues/1821#issuecomment-44265819
|
2019-05-13 05:51:05 -04:00
|
|
|
|
2019-04-12 00:05:32 -04:00
|
|
|
if (!user.changed('password')) {
|
2020-04-11 08:12:39 -04:00
|
|
|
return Promise.resolve()
|
2019-04-12 00:05:32 -04:00
|
|
|
}
|
|
|
|
|
2020-04-11 08:12:39 -04:00
|
|
|
return scrypt
|
|
|
|
.kdf(user.getDataValue('password'), { logN: 15, r: 8, p: 1 })
|
|
|
|
.then(keyBuf => {
|
|
|
|
user.setDataValue('password', keyBuf.toString('hex'))
|
|
|
|
})
|
2019-05-13 04:55:37 -04:00
|
|
|
}
|
|
|
|
|
2020-04-12 07:52:50 -04:00
|
|
|
verifyPassword (attempt: string): Promise<boolean> {
|
|
|
|
return scrypt.verify(Buffer.from(this.password, 'hex'), attempt)
|
|
|
|
}
|
2017-03-08 05:45:51 -05:00
|
|
|
}
|