CoreID/app/jobs/ForeignIPLoginAlert.job.js

50 lines
2.0 KiB
JavaScript
Raw Normal View History

2020-07-12 21:05:59 +00:00
const { Job } = require('flitter-jobs')
class ForeignIPLoginAlertJob extends Job {
static get services() { return [...super.services, 'models', 'jobs', 'output', 'configs'] }
async execute(job) {
const { data } = job
const { user_id, ip } = data
try {
const User = this.models.get('auth:User')
const user = await User.findById(user_id)
if ( !user ) throw new Error('Unable to find user with ID: '+user_id)
2020-12-05 02:40:27 +00:00
this.info('Sending foreign IP login alert to user ' + user.uid)
2020-07-12 21:05:59 +00:00
await this.jobs.queue('mailer').add('EMail', {
to: user.email,
subject: `Security Alert | ${this.configs.get('app.name')}`,
email_params: {
header_text: 'Login From New IP',
body_paragraphs: [
`We've detected a login to your ${this.configs.get('app.name')} account from a new IP address (${ip}).`,
'If this was you, no further action is required. If this was not you, please log into your account and reset your password.',
'Also, consider enabling multi-factor authentication to protect your account.',
],
button_text: 'Account Settings',
button_link: `${this.configs.get('app.url')}dash/profile`,
}
})
2020-12-05 02:40:27 +00:00
this.info('Logged e-mail job')
if ( user.notify_config && user.notify_config.active ) {
await user.notify_config.log({
title: `${this.configs.get('app.name')}: Sign-In From New IP`,
message: `Someone signed into your account (${user.uid}) from the IP address ${ip}. If this was you, no further action is required.`,
})
2020-12-05 02:40:27 +00:00
this.info('Logged push notification job')
}
2020-07-12 21:05:59 +00:00
} catch (e) {
2020-12-05 02:40:27 +00:00
this.error(e)
throw e
2020-07-12 21:05:59 +00:00
}
}
}
module.exports = exports = ForeignIPLoginAlertJob