lint files with changed eof config

This commit is contained in:
Claus-Peter Hübner 2022-05-05 16:20:42 +02:00
parent b57611ab28
commit 64167692e8
2 changed files with 37 additions and 28 deletions

View File

@ -28,7 +28,7 @@ const sodium = require('sodium-native')
// eslint-disable-next-line @typescript-eslint/no-var-requires // eslint-disable-next-line @typescript-eslint/no-var-requires
const random = require('random-bigint') const random = require('random-bigint')
const logger = log4js.getLogger('graphql.UserResolver'); const logger = log4js.getLogger('graphql.UserResolver')
// We will reuse this for changePassword // We will reuse this for changePassword
const isPassword = (password: string): boolean => { const isPassword = (password: string): boolean => {
@ -47,7 +47,7 @@ const WORDS = fs
.toString() .toString()
.split(',') .split(',')
const PassphraseGenerate = (): string[] => { const PassphraseGenerate = (): string[] => {
logger.trace("PassphraseGenerate..."); logger.trace('PassphraseGenerate...')
const result = [] const result = []
for (let i = 0; i < PHRASE_WORD_COUNT; i++) { for (let i = 0; i < PHRASE_WORD_COUNT; i++) {
result.push(WORDS[sodium.randombytes_random() % 2048]) result.push(WORDS[sodium.randombytes_random() % 2048])
@ -56,9 +56,9 @@ const PassphraseGenerate = (): string[] => {
} }
const KeyPairEd25519Create = (passphrase: string[]): Buffer[] => { const KeyPairEd25519Create = (passphrase: string[]): Buffer[] => {
logger.trace("KeyPairEd25519Create..."); logger.trace('KeyPairEd25519Create...')
if (!passphrase.length || passphrase.length < PHRASE_WORD_COUNT) { if (!passphrase.length || passphrase.length < PHRASE_WORD_COUNT) {
logger.error("passphrase empty or to short"); logger.error('passphrase empty or to short')
throw new Error('passphrase empty or to short') throw new Error('passphrase empty or to short')
} }
@ -86,17 +86,19 @@ const KeyPairEd25519Create = (passphrase: string[]): Buffer[] => {
privKey, privKey,
outputHashBuffer.slice(0, sodium.crypto_sign_SEEDBYTES), outputHashBuffer.slice(0, sodium.crypto_sign_SEEDBYTES),
) )
logger.trace("KeyPair creation ready. pubKey=" + pubKey); logger.trace(`KeyPair creation ready. pubKey=${pubKey}`)
return [pubKey, privKey] return [pubKey, privKey]
} }
const SecretKeyCryptographyCreateKey = (salt: string, password: string): Buffer[] => { const SecretKeyCryptographyCreateKey = (salt: string, password: string): Buffer[] => {
logger.trace("SecretKeyCryptographyCreateKey..."); logger.trace('SecretKeyCryptographyCreateKey...')
const configLoginAppSecret = Buffer.from(CONFIG.LOGIN_APP_SECRET, 'hex') const configLoginAppSecret = Buffer.from(CONFIG.LOGIN_APP_SECRET, 'hex')
const configLoginServerKey = Buffer.from(CONFIG.LOGIN_SERVER_KEY, 'hex') const configLoginServerKey = Buffer.from(CONFIG.LOGIN_SERVER_KEY, 'hex')
if (configLoginServerKey.length !== sodium.crypto_shorthash_KEYBYTES) { if (configLoginServerKey.length !== sodium.crypto_shorthash_KEYBYTES) {
logger.error("ServerKey has an invalid size. The size must be ${sodium.crypto_shorthash_KEYBYTES} bytes."); logger.error(
`ServerKey has an invalid size. The size must be ${sodium.crypto_shorthash_KEYBYTES} bytes.`,
)
throw new Error( throw new Error(
`ServerKey has an invalid size. The size must be ${sodium.crypto_shorthash_KEYBYTES} bytes.`, `ServerKey has an invalid size. The size must be ${sodium.crypto_shorthash_KEYBYTES} bytes.`,
) )
@ -125,9 +127,9 @@ const SecretKeyCryptographyCreateKey = (salt: string, password: string): Buffer[
const encryptionKeyHash = Buffer.alloc(sodium.crypto_shorthash_BYTES) const encryptionKeyHash = Buffer.alloc(sodium.crypto_shorthash_BYTES)
sodium.crypto_shorthash(encryptionKeyHash, encryptionKey, configLoginServerKey) sodium.crypto_shorthash(encryptionKeyHash, encryptionKey, configLoginServerKey)
logger.trace("SecretKeyCryptographyCreateKey...successful"); logger.trace('SecretKeyCryptographyCreateKey...successful')
logger.trace("encryptionKeyHash= " + ${encryptionKeyHash}); logger.trace(`encryptionKeyHash= ${encryptionKeyHash}`)
logger.trace("encryptionKey=" + ${encryptionKey}); logger.trace(`encryptionKey= ${encryptionKey}`)
return [encryptionKeyHash, encryptionKey] return [encryptionKeyHash, encryptionKey]
} }
@ -135,40 +137,40 @@ const getEmailHash = (email: string): Buffer => {
logger.trace('getEmailHash...') logger.trace('getEmailHash...')
const emailHash = Buffer.alloc(sodium.crypto_generichash_BYTES) const emailHash = Buffer.alloc(sodium.crypto_generichash_BYTES)
sodium.crypto_generichash(emailHash, Buffer.from(email)) sodium.crypto_generichash(emailHash, Buffer.from(email))
logger.trace("getEmailHash...successful: " + emailHash); logger.trace('getEmailHash...successful: ' + emailHash)
return emailHash return emailHash
} }
const SecretKeyCryptographyEncrypt = (message: Buffer, encryptionKey: Buffer): Buffer => { const SecretKeyCryptographyEncrypt = (message: Buffer, encryptionKey: Buffer): Buffer => {
logger.trace("SecretKeyCryptographyEncrypt..."); logger.trace('SecretKeyCryptographyEncrypt...')
const encrypted = Buffer.alloc(message.length + sodium.crypto_secretbox_MACBYTES) const encrypted = Buffer.alloc(message.length + sodium.crypto_secretbox_MACBYTES)
const nonce = Buffer.alloc(sodium.crypto_secretbox_NONCEBYTES) const nonce = Buffer.alloc(sodium.crypto_secretbox_NONCEBYTES)
nonce.fill(31) // static nonce nonce.fill(31) // static nonce
sodium.crypto_secretbox_easy(encrypted, message, nonce, encryptionKey) sodium.crypto_secretbox_easy(encrypted, message, nonce, encryptionKey)
logger.trace("SecretKeyCryptographyEncrypt...successful: " + encrypted); logger.trace('SecretKeyCryptographyEncrypt...successful: ' + encrypted)
return encrypted return encrypted
} }
const SecretKeyCryptographyDecrypt = (encryptedMessage: Buffer, encryptionKey: Buffer): Buffer => { const SecretKeyCryptographyDecrypt = (encryptedMessage: Buffer, encryptionKey: Buffer): Buffer => {
logger.trace("SecretKeyCryptographyDecrypt..."); logger.trace('SecretKeyCryptographyDecrypt...')
const message = Buffer.alloc(encryptedMessage.length - sodium.crypto_secretbox_MACBYTES) const message = Buffer.alloc(encryptedMessage.length - sodium.crypto_secretbox_MACBYTES)
const nonce = Buffer.alloc(sodium.crypto_secretbox_NONCEBYTES) const nonce = Buffer.alloc(sodium.crypto_secretbox_NONCEBYTES)
nonce.fill(31) // static nonce nonce.fill(31) // static nonce
sodium.crypto_secretbox_open_easy(message, encryptedMessage, nonce, encryptionKey) sodium.crypto_secretbox_open_easy(message, encryptedMessage, nonce, encryptionKey)
logger.trace("SecretKeyCryptographyDecrypt...successful: "+ message); logger.trace('SecretKeyCryptographyDecrypt...successful: ' + message)
return message return message
} }
const newEmailOptIn = (userId: number): LoginEmailOptIn => { const newEmailOptIn = (userId: number): LoginEmailOptIn => {
logger.trace("newEmailOptIn..."); logger.trace('newEmailOptIn...')
const emailOptIn = new LoginEmailOptIn() const emailOptIn = new LoginEmailOptIn()
emailOptIn.verificationCode = random(64) emailOptIn.verificationCode = random(64)
emailOptIn.userId = userId emailOptIn.userId = userId
emailOptIn.emailOptInTypeId = OptInType.EMAIL_OPT_IN_REGISTER emailOptIn.emailOptInTypeId = OptInType.EMAIL_OPT_IN_REGISTER
logger.trace("newEmailOptIn...successful: " + emailOptIn); logger.trace('newEmailOptIn...successful: ' + emailOptIn)
return emailOptIn return emailOptIn
} }
@ -180,11 +182,14 @@ export const checkOptInCode = async (
userId: number, userId: number,
optInType: OptInType = OptInType.EMAIL_OPT_IN_REGISTER, optInType: OptInType = OptInType.EMAIL_OPT_IN_REGISTER,
): Promise<LoginEmailOptIn> => { ): Promise<LoginEmailOptIn> => {
logger.trace("checkOptInCode..." + optInCode); logger.trace('checkOptInCode...' + optInCode)
if (optInCode) { if (optInCode) {
if (!canResendOptIn(optInCode)) { if (!canResendOptIn(optInCode)) {
logger.error(`email already sent less than ${printTimeDuration( logger.error(
CONFIG.EMAIL_CODE_REQUEST_TIME,)} minutes ago`); `email already sent less than ${printTimeDuration(
CONFIG.EMAIL_CODE_REQUEST_TIME,
)} minutes ago`,
)
throw new Error( throw new Error(
`email already sent less than ${printTimeDuration( `email already sent less than ${printTimeDuration(
CONFIG.EMAIL_CODE_REQUEST_TIME, CONFIG.EMAIL_CODE_REQUEST_TIME,
@ -194,20 +199,20 @@ export const checkOptInCode = async (
optInCode.updatedAt = new Date() optInCode.updatedAt = new Date()
optInCode.resendCount++ optInCode.resendCount++
} else { } else {
logger.trace("create new OptIn for userId=" + userId); logger.trace('create new OptIn for userId=' + userId)
optInCode = newEmailOptIn(userId) optInCode = newEmailOptIn(userId)
} }
optInCode.emailOptInTypeId = optInType optInCode.emailOptInTypeId = optInType
await LoginEmailOptIn.save(optInCode).catch(() => { await LoginEmailOptIn.save(optInCode).catch(() => {
logger.error("Unable to save optin code= " + optInCode); logger.error('Unable to save optin code= ' + optInCode)
throw new Error('Unable to save optin code.') throw new Error('Unable to save optin code.')
}) })
logger.trace("checkOptInCode...successful: " + optInCode); logger.trace('checkOptInCode...successful: ' + optInCode)
return optInCode return optInCode
} }
export const activationLink = (optInCode: LoginEmailOptIn): string => { export const activationLink = (optInCode: LoginEmailOptIn): string => {
logger.trace("activationLink..."); logger.trace('activationLink...')
return CONFIG.EMAIL_LINK_SETPASSWORD.replace(/{optin}/g, optInCode.verificationCode.toString()) return CONFIG.EMAIL_LINK_SETPASSWORD.replace(/{optin}/g, optInCode.verificationCode.toString())
} }
@ -217,7 +222,7 @@ export class UserResolver {
@Query(() => User) @Query(() => User)
@UseMiddleware(klicktippNewsletterStateMiddleware) @UseMiddleware(klicktippNewsletterStateMiddleware)
async verifyLogin(@Ctx() context: Context): Promise<User> { async verifyLogin(@Ctx() context: Context): Promise<User> {
logger.trace("verifyLogin..."); logger.trace('verifyLogin...')
// TODO refactor and do not have duplicate code with login(see below) // TODO refactor and do not have duplicate code with login(see below)
const userEntity = getUser(context) const userEntity = getUser(context)
const user = new User(userEntity) const user = new User(userEntity)
@ -230,7 +235,7 @@ export class UserResolver {
const coinanimation = await userSettingRepository const coinanimation = await userSettingRepository
.readBoolean(userEntity.id, Setting.COIN_ANIMATION) .readBoolean(userEntity.id, Setting.COIN_ANIMATION)
.catch((error) => { .catch((error) => {
logger.error("error:", error); logger.error('error:', error)
throw new Error(error) throw new Error(error)
}) })
user.coinanimation = coinanimation user.coinanimation = coinanimation

View File

@ -43,8 +43,12 @@ const createServer = async (context: any = serverContext): Promise<ServerDef> =>
// check for correct database version // check for correct database version
const dbVersion = await checkDBVersion(CONFIG.DB_VERSION) const dbVersion = await checkDBVersion(CONFIG.DB_VERSION)
if (!dbVersion) { if (!dbVersion) {
logger.fatal('Missmatching Database Versions! configured=' + logger.fatal(
CONFIG.DB_VERSION + ', dbVersion=' + dbVersion ) 'Missmatching Database Versions! configured=' +
CONFIG.DB_VERSION +
', dbVersion=' +
dbVersion,
)
throw new Error('Fatal: Database Version incorrect') throw new Error('Fatal: Database Version incorrect')
} }