additional logg-statements

This commit is contained in:
Claus-Peter Hübner 2022-05-05 01:01:06 +02:00
parent 541e7231d3
commit 4afffea797
2 changed files with 31 additions and 16 deletions

View File

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

View File

@ -31,21 +31,20 @@ type ServerDef = { apollo: ApolloServer; app: Express; con: Connection }
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const createServer = async (context: any = serverContext): Promise<ServerDef> => {
const logger = log4js.getLogger('server.createServer')
logger.debug('This little thing went to market')
logger.info('This little thing stayed at home')
logger.error('This little thing had roast beef')
logger.fatal('This little thing had none')
logger.trace('and this little thing went wee, wee, wee, all the way home.')
logger.trace('createServer...')
// open mysql connection
const con = await connection()
if (!con || !con.isConnected) {
logger.fatal(`Couldn't open connection to database!`)
throw new Error(`Fatal: Couldn't open connection to database`)
}
// check for correct database version
const dbVersion = await checkDBVersion(CONFIG.DB_VERSION)
if (!dbVersion) {
logger.fatal('Missmatching Database Versions! configured=' +
CONFIG.DB_VERSION + ', dbVersion=' + dbVersion )
throw new Error('Fatal: Database Version incorrect')
}
@ -73,6 +72,7 @@ const createServer = async (context: any = serverContext): Promise<ServerDef> =>
logger,
})
apollo.applyMiddleware({ app, path: '/' })
logger.trace('createServer...successful')
return { apollo, app, con }
}