Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

Add opt-in email notification for successful logins with new-device and location detection (#7737)

Sourav Sarkar417ba51fa8d8
diff --git a/package.json b/package.json
index 831f6ae76d..385b726b4f 100644
--- a/package.json
+++ b/package.json
@@ -198,6 +198,7 @@
"sql-formatter": "^15.8.2",
"srt-to-vtt": "^1.1.3",
"tslib": "^2.8.1",
+ "ua-parser-js": "^2.0.10",
"uuid": "^14.0.1",
"validator": "^13.15.35",
"webfinger.js": "3.0.4",
diff --git a/packages/tests/src/api/users/login-notifications.ts b/packages/tests/src/api/users/login-notifications.ts
new file mode 100644
index 0000000000..72c8fb4b80
--- /dev/null
+++ b/packages/tests/src/api/users/login-notifications.ts
@@ -0,0 +1,85 @@
+/* oxlint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
+
+import { expect } from 'chai'
+import { MockSmtpServer } from '@tests/shared/mock-servers/mock-email.js'
+import {
+ cleanupTests,
+ ConfigCommand,
+ createSingleServer,
+ PeerTubeServer,
+ setAccessTokensToServers,
+ waitJobs
+} from '@peertube/peertube-server-commands'
+
+describe('Test login notifications', function () {
+ let server: PeerTubeServer
+ const emails: object[] = []
+
+ const user = {
+ username: 'user_login_notif',
+ password: 'super password'
+ }
+
+ const userEmail = '[email protected]'
+ const firefoxUserAgent = 'Mozilla/5.0 (X11; Linux x86_64; rv:143.0) Gecko/20100101 Firefox/143.0'
+
+ // Other users of the platform (root) also receive login notifications, so only consider the ones of our user
+ function getUserEmails () {
+ return emails.filter(e => e['to'][0]['address'] === userEmail)
+ }
+
+ before(async function () {
+ this.timeout(30000)
+
+ const port = await MockSmtpServer.Instance.collectEmails(emails, { collectLoginNotifications: true })
+ server = await createSingleServer(1, ConfigCommand.getEmailOverrideConfig(port))
+
+ await setAccessTokensToServers([ server ])
+
+ await server.users.create({ username: user.username, password: user.password })
+ })
+
+ it('Should not send an email on the first login after signup', async function () {
+ this.timeout(30000)
+
+ await server.login.login({ user, userAgent: firefoxUserAgent })
+ await waitJobs(server)
+
+ expect(getUserEmails()).to.have.lengthOf(0)
+ })
+
+ it('Should not send an email when logging in again from the same device', async function () {
+ this.timeout(30000)
+
+ await server.login.login({ user, userAgent: firefoxUserAgent })
+ await waitJobs(server)
+
+ expect(getUserEmails()).to.have.lengthOf(0)
+ })
+
+ it('Should send an email on a login from another device', async function () {
+ this.timeout(30000)
+
+ await server.login.login({ user, userAgent: 'another-device' })
+ await waitJobs(server)
+
+ const userEmails = getUserEmails()
+ expect(userEmails).to.have.lengthOf(1)
+ expect(userEmails[0]['subject']).to.contain('new device')
+ })
+
+ it('Should not send an email when logging in again from that second device', async function () {
+ this.timeout(30000)
+
+ await server.login.login({ user, userAgent: 'another-device' })
+ await waitJobs(server)
+
+ expect(getUserEmails()).to.have.lengthOf(1)
+ })
+
+ after(async function () {
+ await MockSmtpServer.Instance.kill()
+
+ await cleanupTests([ server ])
+ })
+})
diff --git a/packages/tests/src/shared/mock-servers/mock-email.ts b/packages/tests/src/shared/mock-servers/mock-email.ts
index b0c598045c..07685a02d8 100644
--- a/packages/tests/src/shared/mock-servers/mock-email.ts
+++ b/packages/tests/src/shared/mock-servers/mock-email.ts
@@ -6,13 +6,19 @@ class MockSmtpServer {
private started = false
private maildev: any
private emails: object[]
+ private collectLoginNotifications = false
private relayingEmail: Promise<void>
private
private constructor () {}
- collectEmails (emailsCollection: object[]) {
+ collectEmails (emailsCollection: object[], options: {
+ // If true, will collect emails about new logins
+ collectLoginNotifications?: boolean // Default: false
+ } = {}) {
+ this.collectLoginNotifications = options.collectLoginNotifications === true
+
const outgoingHost = process.env.MAILDEV_RELAY_HOST
const outgoingPort = process.env.MAILDEV_RELAY_PORT
? parseInt(process.env.MAILDEV_RELAY_PORT)
@@ -38,6 +44,8 @@ class MockSmtpServer {
})
this.maildev.on('new', email => {
+ if (!this.collectLoginNotifications && email.subject?.includes('New login to your account')) return
+
this.emails.push(email)
if (outgoingHost || outgoingPort) {
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index d05678103f..26ace4561f 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -347,6 +347,9 @@ importers:
tslib:
specifier: ^2.8.1
version: 2.8.1
+ ua-parser-js:
+ specifier: ^2.0.10
+ version: 2.0.10
uuid:
specifier: ^14.0.1
version: 14.0.1
diff --git a/server/core/assets/email-templates/login-success/html.hbs b/server/core/assets/email-templates/login-success/html.hbs
new file mode 100644
index 0000000000..d769e1d8f9
--- /dev/null
+++ b/server/core/assets/email-templates/login-success/html.hbs
@@ -0,0 +1,42 @@
+{{#> base title=(t "New login to your account")}}
+ <p>
+ {{{t "A new login to your account on {instanceName} was detected from a <strong>new device</strong>." instanceName=instanceName}}}
+ </p>
+
+ <table role="presentation" border="0" cellpadding="0" cellspacing="0" width="100%" style="margin: 16px 0;">
+ <tr>
+ <td style="padding: 6px 0; font-family: sans-serif; font-size: 14px;">
+ <strong>{{t "Date"}}</strong>
+ </td>
+ <td style="padding: 6px 0; font-family: sans-serif; font-size: 14px;">{{date}}</td>
+ </tr>
+ {{#if ip}}
+ <tr>
+ <td style="padding: 6px 0; font-family: sans-serif; font-size: 14px;">
+ <strong>{{t "IP address"}}</strong>
+ </td>
+ <td style="padding: 6px 0; font-family: sans-serif; font-size: 14px;">{{ip}}</td>
+ </tr>
+ {{/if}}
+ {{#if location}}
+ <tr>
+ <td style="padding: 6px 0; font-family: sans-serif; font-size: 14px;">
+ <strong>{{t "Approximate location"}}</strong>
+ </td>
+ <td style="padding: 6px 0; font-family: sans-serif; font-size: 14px;">{{location}}</td>
+ </tr>
+ {{/if}}
+ {{#if device}}
+ <tr>
+ <td style="padding: 6px 0; font-family: sans-serif; font-size: 14px; vertical-align: top;">
+ <strong>{{t "Browser / device"}}</strong>
+ </td>
+ <td style="padding: 6px 0; font-family: sans-serif; font-size: 14px;">{{device}}</td>
+ </tr>
+ {{/if}}
+ </table>
+
+ <p>
+ {{t "If this was you, you can safely ignore this email. If you don't recognize this activity, we recommend changing your password as soon as possible."}}
+ </p>
+{{/base}}
diff --git a/server/core/initializers/constants.ts b/server/core/initializers/constants.ts
index 17dca02f2f..1ac2438361 100644
--- a/server/core/initializers/constants.ts
+++ b/server/core/initializers/constants.ts
@@ -62,7 +62,7 @@ import { CONFIG, registerConfigChangedHandler } from './config.js'
// ---------------------------------------------------------------------------
-export const LAST_MIGRATION_VERSION = 1100
+export const LAST_MIGRATION_VERSION = 1105
// ---------------------------------------------------------------------------
@@ -381,9 +381,13 @@ export const SCHEDULER_INTERVALS_MS = {
REMOVE_DANGLING_RESUMABLE_UPLOADS: 60000 * 60, // 1 hour
CHANNEL_SYNC_CHECK_INTERVAL: CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.CHECK_INTERVAL,
BLOCKLIST_SUBSCRIPTIONS_SYNC: 60000 * 60, // 1 hour
- WATCHED_WORDS_SUBSCRIPTIONS_SYNC: 60000 * 60 // 1 hour
+ WATCHED_WORDS_SUBSCRIPTIONS_SYNC: 60000 * 60, // 1 hour
+ REMOVE_OLD_USER_LOGIN_DEVICES: 60000 * 60 * 24 // 1 day
}
+// Devices not seen again after this delay are forgotten, so a login from that IP/user-agent pair will be treated as new again
+export const USER_LOGIN_DEVICE_MAX_AGE = 60000 * 60 * 24 * 365 // 1 year
+
// ---------------------------------------------------------------------------
export const CONSTRAINTS_FIELDS = {
diff --git a/server/core/initializers/database.ts b/server/core/initializers/database.ts
index b9ab687ccf..1ce17ccec5 100644
--- a/server/core/initializers/database.ts
+++ b/server/core/initializers/database.ts
@@ -59,6 +59,7 @@ import { BlocklistSubscriptionModel } from '../models/blocklist/blocklist-subscr
import { ServerBlocklistModel } from '../models/blocklist/server-blocklist.js'
import { ServerModel } from '../models/server/server.js'
import { VideoStatModel } from '../models/stat/video-stat.js'
+import { UserLoginDeviceModel } from '../models/user/user-login-device.js'
import { UserNotificationSettingModel } from '../models/user/user-notification-setting.js'
import { ChangeOwnershipModel } from '../models/video/change-ownership.js'
import { ScheduleVideoUpdateModel } from '../models/video/schedule-video-update.js'
@@ -189,6 +190,7 @@ export async function initDatabaseModels (silent: boolean) {
BlocklistLogModel,
UserNotificationModel,
UserNotificationSettingModel,
+ UserLoginDeviceModel,
VideoStreamingPlaylistModel,
VideoInfohashModel,
VideoPlaylistModel,
diff --git a/server/core/initializers/migrations/1105-user-login-device.ts b/server/core/initializers/migrations/1105-user-login-device.ts
new file mode 100644
index 0000000000..c421a05494
--- /dev/null
+++ b/server/core/initializers/migrations/1105-user-login-device.ts
@@ -0,0 +1,32 @@
+import * as Sequelize from 'sequelize'
+
+async function up (utils: {
+ transaction: Sequelize.Transaction
+ queryInterface: Sequelize.QueryInterface
+ sequelize: Sequelize.Sequelize
+}): Promise<void> {
+ const { transaction } = utils
+
+ {
+ const query = `
+CREATE TABLE IF NOT EXISTS "userLoginDevice" (
+ "id" SERIAL,
+ "fingerprint" VARCHAR(255) NOT NULL,
+ "userId" INTEGER NOT NULL REFERENCES "user" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
+ "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL,
+ "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL,
+ PRIMARY KEY ("id")
+);`
+
+ await utils.sequelize.query(query, { transaction })
+ }
+}
+
+function down () {
+ throw new Error('Not implemented.')
+}
+
+export {
+ down,
+ up
+}
diff --git a/server/core/lib/auth/login-notifier.ts b/server/core/lib/auth/login-notifier.ts
new file mode 100644
index 0000000000..7b7bdba017
--- /dev/null
+++ b/server/core/lib/auth/login-notifier.ts
@@ -0,0 +1,57 @@
+import { GeoIP } from '@server/helpers/geo-ip.js'
+import { CONFIG, isEmailEnabled } from '@server/initializers/config.js'
+import { Emailer } from '@server/lib/emailer.js'
+import { UserLoginDeviceModel } from '@server/models/user/user-login-device.js'
+import { MUserDefault } from '@server/types/models/index.js'
+
+async function notifyOnLoginSuccess (options: {
+ user: MUserDefault
+ ip: string
+ userAgent: string
+}) {
+ const { user, ip, userAgent } = options
+
+ const isNewDevice = await UserLoginDeviceModel.registerDevice({ userId: user.id, ip, userAgent })
+ if (!isNewDevice) return
+
+ if (!isEmailEnabled()) return
+ if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION && user.emailVerified === false) return
+
+ Emailer.Instance.addLoginSuccessEmailJob({
+ username: user.username,
+ email: user.email,
+ language: user.getLanguage(),
+ ip,
+ device: await buildDevice(userAgent),
+ location: await buildLocation(ip)
+ })
+}
+
+export {
+ notifyOnLoginSuccess
+}
+
+// ---------------------------------------------------------------------------
+// Private
+// ---------------------------------------------------------------------------
+
+async function buildLocation (ip: string) {
+ if (!ip) return undefined
+
+ const { country, subdivisionName } = await GeoIP.Instance.safeIPISOLookup(ip)
+
+ return [ subdivisionName, country ].filter(v => !!v).join(', ') || undefined
+}
+
+// Build a human readable device description, since a raw user agent is difficult to read and understand
+async function buildDevice (userAgent: string) {
+ if (!userAgent) return undefined
+
+ const { UAParser } = await import('ua-parser-js')
+ const { browser, os } = UAParser(userAgent)
+
+ const browserName = [ browser.name, browser.version ].filter(v => !!v).join(' ')
+ const osName = [ os.name, os.version ].filter(v => !!v).join(' ')
+
+ return [ browserName, osName ].filter(v => !!v).join(' - ') || userAgent
+}
diff --git a/server/core/lib/auth/oauth-handlers.ts b/server/core/lib/auth/oauth-handlers.ts
index 12de93bc25..3641ff1a5e 100644
--- a/server/core/lib/auth/oauth-handlers.ts
+++ b/server/core/lib/auth/oauth-handlers.ts
@@ -16,6 +16,7 @@ import { CONFIG } from '../../initializers/config.js'
import { OAuthClientModel } from '../../models/oauth/oauth-client.js'
import { Hooks } from '../plugins/hooks.js'
import { BypassLogin } from './bypass-login.model.js'
+import { notifyOnLoginSuccess } from './login-notifier.js'
import { TooLongPasswordError } from './oauth-errors.js'
import { buildToken, getAccessToken, getRefreshToken, revokeToken, saveToken } from './oauth-token.js'
import { checkUserNotBlockedOrThrow, getUserOrThrow } from './oauth-user.js'
@@ -162,7 +163,12 @@ async function handlePasswordGrant (options: {
lastActivityDate: now
})
- return saveToken(token, client, user, { bypassLogin })
+ const result = await saveToken(token, client, user, { bypassLogin })
+
+ notifyOnLoginSuccess({ user, ip: options.ip, userAgent: options.userAgent })
+ .catch(err => logger.error('Cannot notify user of successful login.', { err }))
+
+ return result
}
async function handleRefreshGrant (options: {
diff --git a/server/core/lib/emailer.ts b/server/core/lib/emailer.ts
index e002fe146c..eb478b7037 100644
--- a/server/core/lib/emailer.ts
+++ b/server/core/lib/emailer.ts
@@ -260,6 +260,33 @@ export class Emailer {
return JobQueue.Instance.createJobAsync({ type: 'email', payload: emailPayload })
}
+ addLoginSuccessEmailJob (options: {
+ username: string
+ email: string
+ language: string
+ ip: string
+ device?: string
+ location?: string
+ }) {
+ const { username, email, language, ip, device, location } = options
+
+ const emailPayload: EmailPayload = {
+ template: 'login-success',
+ to: { email, language },
+ subject: t('New login to your account from a new device', language),
+ locals: {
+ username,
+ instanceName: CONFIG.INSTANCE.NAME,
+ ip,
+ device,
+ location,
+ date: new Date().toLocaleString(language)
+ }
+ }
+
+ return JobQueue.Instance.createJobAsync({ type: 'email', payload: emailPayload })
+ }
+
addContactFormJob (options: {
fromEmail: string
diff --git a/server/core/lib/schedulers/remove-old-user-login-devices-scheduler.ts b/server/core/lib/schedulers/remove-old-user-login-devices-scheduler.ts
new file mode 100644
index 0000000000..9b5e7efb74
--- /dev/null
+++ b/server/core/lib/schedulers/remove-old-user-login-devices-scheduler.ts
@@ -0,0 +1,29 @@
+import { logger, loggerTagsFactory } from '../../helpers/logger.js'
+import { SCHEDULER_INTERVALS_MS, USER_LOGIN_DEVICE_MAX_AGE } from '../../initializers/constants.js'
+import { UserLoginDeviceModel } from '../../models/user/user-login-device.js'
+import { AbstractScheduler } from './abstract-scheduler.js'
+
+const lTags = loggerTagsFactory('schedulers')
+
+export class RemoveOldUserLoginDevicesScheduler extends AbstractScheduler {
+ private static instance: AbstractScheduler
+
+ protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.REMOVE_OLD_USER_LOGIN_DEVICES
+
+ private constructor () {
+ super({ randomRunOnEnable: true })
+ }
+
+ protected internalExecute () {
+ logger.info('Removing old user login devices.', lTags())
+
+ const now = new Date()
+ const beforeDate = new Date(now.getTime() - USER_LOGIN_DEVICE_MAX_AGE).toISOString()
+
+ return UserLoginDeviceModel.removeOldDevices(beforeDate)
+ }
+
+ static get Instance () {
+ return this.instance || (this.instance = new this())
+ }
+}
diff --git a/server/core/models/user/user-login-device.ts b/server/core/models/user/user-login-device.ts
new file mode 100644
index 0000000000..d8166d9d34
--- /dev/null
+++ b/server/core/models/user/user-login-device.ts
@@ -0,0 +1,98 @@
+import { CONFIG } from '@server/initializers/config.js'
+import { createHmac } from 'crypto'
+import { DestroyOptions, Op, Transaction } from 'sequelize'
+import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Table, UpdatedAt } from 'sequelize-typescript'
+import { SequelizeModel } from '../shared/index.js'
+import { UserModel } from './user.js'
+
+/**
+ * Store the devices (IP + user agent) already seen for a particular user, so we can detect a login from a new device.
+ * Only a keyed hash of these information is stored to avoid keeping private information of our users.
+ */
+
+@Table({
+ tableName: 'userLoginDevice',
+ indexes: [
+ {
+ fields: [ 'userId', 'fingerprint' ],
+ unique: true
+ }
+ ]
+})
+export class UserLoginDeviceModel extends SequelizeModel<UserLoginDeviceModel> {
+ @AllowNull(false)
+ @Column
+ declare fingerprint: string
+
+ @CreatedAt
+ declare createdAt: Date
+
+ @UpdatedAt
+ declare updatedAt: Date
+
+ @ForeignKey(() => UserModel)
+ @Column
+ declare userId: number
+
+ @BelongsTo(() => UserModel, {
+ foreignKey: {
+ allowNull: false
+ },
+ onDelete: 'cascade'
+ })
+ declare User: Awaited<UserModel>
+
+ // ---------------------------------------------------------------------------
+
+ // Returns true if the device was not already known for this user, excluding their very first device ever
+ static async registerDevice (options: {
+ userId: number
+ ip: string
+ userAgent: string
+ transaction?: Transaction
+ }) {
+ const { userId, ip, userAgent, transaction } = options
+
+ const fingerprint = this.buildFingerprint({ ip, userAgent })
+
+ const priorDevicesCount = await UserLoginDeviceModel.count({ where: { userId }, transaction })
+
+ const [ device, created ] = await UserLoginDeviceModel.findOrCreate({
+ where: { userId, fingerprint },
+ defaults: { userId, fingerprint },
+ transaction
+ })
+
+ // Bump the last-seen date of this already known device, so it's not pruned by removeOldDevices() while still in use
+ if (!created) await device.update({ updatedAt: new Date() }, { transaction })
+
+ return created && priorDevicesCount > 0
+ }
+
+ static removeOldDevices (beforeDate: string) {
+ const query: DestroyOptions = {
+ where: {
+ updatedAt: {
+ [Op.lt]: beforeDate
+ }
+ }
+ }
+
+ return UserLoginDeviceModel.destroy(query)
+ }
+
+ static removeUserDevices (userId: number, transaction?: Transaction) {
+ return UserLoginDeviceModel.destroy({ where: { userId }, transaction })
+ }
+
+ private static buildFingerprint (options: {
+ ip: string
+ userAgent: string
+ }) {
+ const { ip, userAgent } = options
+
+ return createHmac('sha256', CONFIG.SECRETS.PEERTUBE)
+ .update(`${ip || ''}|${userAgent || ''}`)
+ .digest('hex')
+ }
+}
diff --git a/server/server.ts b/server/server.ts
index c0aef4c539..6ad6d010b7 100644
--- a/server/server.ts
+++ b/server/server.ts
@@ -149,6 +149,7 @@ import { PluginsCheckScheduler } from './core/lib/schedulers/plugins-check-sched
import { RemoveDanglingResumableUploadsScheduler } from './core/lib/schedulers/remove-dangling-resumable-uploads-scheduler.js'
import { RemoveOldHistoryScheduler } from './core/lib/schedulers/remove-old-history-scheduler.js'
import { RemoveOldStatsScheduler } from './core/lib/schedulers/remove-old-stats-scheduler.js'
+import { RemoveOldUserLoginDevicesScheduler } from './core/lib/schedulers/remove-old-user-login-devices-scheduler.js'
import { RunnerJobWatchDogScheduler } from './core/lib/schedulers/runner-job-watch-dog-scheduler.js'
import { UpdateVideosScheduler } from './core/lib/schedulers/update-videos-scheduler.js'
import { VideoStatsBufferScheduler } from './core/lib/schedulers/video-stats-buffer-scheduler.js'
@@ -337,6 +338,7 @@ async function startApplication () {
RunnerJobWatchDogScheduler.Instance.enable()
RemoveExpiredUserExportsScheduler.Instance.enable()
UpdateTokenSessionScheduler.Instance.enable()
+ RemoveOldUserLoginDevicesScheduler.Instance.enable()
OpenTelemetryMetrics.Instance.registerMetrics({ trackerServer })