2016-08-09 12:55:15 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
'use strict'
|
|
|
|
|
2017-08-25 22:43:08 +03:00
|
|
|
const isA = require('joi')
|
2018-03-28 11:34:25 +03:00
|
|
|
const validators = require('./routes/validators')
|
2017-08-25 22:43:08 +03:00
|
|
|
const {
|
2017-09-12 18:14:12 +03:00
|
|
|
DISPLAY_SAFE_UNICODE_WITH_NON_BMP,
|
2017-08-25 22:43:08 +03:00
|
|
|
HEX_STRING,
|
|
|
|
URL_SAFE_BASE_64
|
2018-03-28 11:34:25 +03:00
|
|
|
} = validators
|
2017-08-25 22:43:08 +03:00
|
|
|
const PUSH_SERVER_REGEX = require('../config').get('push.allowedServerRegex')
|
|
|
|
|
|
|
|
const SCHEMA = {
|
|
|
|
id: isA.string().length(32).regex(HEX_STRING),
|
2017-10-27 18:21:55 +03:00
|
|
|
location: isA.object({
|
2017-11-01 00:11:01 +03:00
|
|
|
city: isA.string().optional().allow(null),
|
2017-10-27 18:21:55 +03:00
|
|
|
country: isA.string().optional().allow(null),
|
|
|
|
state: isA.string().optional().allow(null),
|
|
|
|
stateCode: isA.string().optional().allow(null)
|
|
|
|
}),
|
2017-09-12 18:14:12 +03:00
|
|
|
name: isA.string().max(255).regex(DISPLAY_SAFE_UNICODE_WITH_NON_BMP),
|
2017-08-25 22:43:08 +03:00
|
|
|
// We previously allowed devices to register with arbitrary unicode names,
|
2017-09-12 18:14:12 +03:00
|
|
|
// so we can't assert DISPLAY_SAFE_UNICODE_WITH_NON_BMP in the response schema.
|
2019-01-30 13:54:27 +03:00
|
|
|
nameResponse: isA.string().max(255).allow(''),
|
2017-08-25 22:43:08 +03:00
|
|
|
type: isA.string().max(16),
|
2018-09-07 21:31:48 +03:00
|
|
|
pushCallback: validators.pushCallbackUrl({ scheme: 'https' }).regex(PUSH_SERVER_REGEX).max(255).allow(''),
|
2017-08-25 22:43:08 +03:00
|
|
|
pushPublicKey: isA.string().max(88).regex(URL_SAFE_BASE_64).allow(''),
|
2017-08-31 20:25:54 +03:00
|
|
|
pushAuthKey: isA.string().max(24).regex(URL_SAFE_BASE_64).allow(''),
|
2018-06-27 04:04:28 +03:00
|
|
|
pushEndpointExpired: isA.boolean().strict(),
|
|
|
|
// An object mapping command names to metadata bundles.
|
|
|
|
availableCommands: isA.object().pattern(validators.DEVICE_COMMAND_NAME, isA.string().max(2048))
|
2017-08-25 22:43:08 +03:00
|
|
|
}
|
|
|
|
|
2017-08-07 18:25:44 +03:00
|
|
|
module.exports = (log, db, push) => {
|
2018-08-21 14:04:40 +03:00
|
|
|
return { isSpuriousUpdate, upsert, synthesizeName }
|
|
|
|
|
|
|
|
// Clients have been known to send spurious device updates,
|
|
|
|
// which generates lots of unnecessary database load.
|
|
|
|
// Check if anything has actually changed.
|
|
|
|
function isSpuriousUpdate (payload, token) {
|
|
|
|
if (! token.deviceId || payload.id !== token.deviceId) {
|
2018-08-21 14:14:36 +03:00
|
|
|
return false
|
2018-08-21 14:04:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (payload.name && payload.name !== token.deviceName) {
|
2018-08-21 14:14:36 +03:00
|
|
|
return false
|
2018-08-21 14:04:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (payload.type && payload.type !== token.deviceType) {
|
2018-08-21 14:14:36 +03:00
|
|
|
return false
|
2018-08-21 14:04:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (payload.pushCallback && payload.pushCallback !== token.deviceCallbackURL) {
|
2018-08-21 14:14:36 +03:00
|
|
|
return false
|
2018-08-21 14:04:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (payload.pushPublicKey && payload.pushPublicKey !== token.deviceCallbackPublicKey) {
|
2018-08-21 14:14:36 +03:00
|
|
|
return false
|
2018-08-21 14:04:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (payload.availableCommands) {
|
2018-08-21 14:14:36 +03:00
|
|
|
if (! token.deviceAvailableCommands) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! isLike(token.deviceAvailableCommands, payload.availableCommands)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! isLike(payload.availableCommands, token.deviceAvailableCommands)) {
|
|
|
|
return false
|
2018-08-21 14:04:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-21 14:14:36 +03:00
|
|
|
return true
|
2018-08-21 14:04:40 +03:00
|
|
|
}
|
2016-08-09 12:55:15 +03:00
|
|
|
|
2019-01-30 13:54:27 +03:00
|
|
|
function upsert (request, credentials, deviceInfo) {
|
2017-08-07 18:25:44 +03:00
|
|
|
let operation, event, result
|
2016-08-09 12:55:15 +03:00
|
|
|
if (deviceInfo.id) {
|
|
|
|
operation = 'updateDevice'
|
|
|
|
event = 'device.updated'
|
|
|
|
} else {
|
|
|
|
operation = 'createDevice'
|
|
|
|
event = 'device.created'
|
2019-01-30 13:54:27 +03:00
|
|
|
if (! deviceInfo.name) {
|
|
|
|
deviceInfo.name = credentials.client && credentials.client.name || ''
|
|
|
|
}
|
2016-08-09 12:55:15 +03:00
|
|
|
}
|
2019-01-30 13:54:27 +03:00
|
|
|
|
|
|
|
deviceInfo.sessionTokenId = credentials.id
|
|
|
|
deviceInfo.refreshTokenId = credentials.refreshTokenId
|
|
|
|
|
2017-08-07 18:25:44 +03:00
|
|
|
const isPlaceholderDevice = ! deviceInfo.id && ! deviceInfo.name && ! deviceInfo.type
|
2016-08-09 12:55:15 +03:00
|
|
|
|
2019-01-30 13:54:27 +03:00
|
|
|
return db[operation](credentials.uid, deviceInfo)
|
2017-08-07 18:25:44 +03:00
|
|
|
.then(device => {
|
2016-08-09 12:55:15 +03:00
|
|
|
result = device
|
2016-10-18 22:58:46 +03:00
|
|
|
return request.emitMetricsEvent(event, {
|
2019-01-30 13:54:27 +03:00
|
|
|
uid: credentials.uid,
|
2017-06-29 01:10:22 +03:00
|
|
|
device_id: result.id,
|
2016-08-09 12:55:15 +03:00
|
|
|
is_placeholder: isPlaceholderDevice
|
|
|
|
})
|
|
|
|
})
|
2017-08-07 18:25:44 +03:00
|
|
|
.then(() => {
|
2016-08-09 12:55:15 +03:00
|
|
|
if (operation === 'createDevice') {
|
2017-01-31 12:08:34 +03:00
|
|
|
// Clients expect this notification to always include a name,
|
|
|
|
// so try to synthesize one if necessary.
|
2017-08-07 18:25:44 +03:00
|
|
|
let deviceName = result.name
|
2017-01-31 12:08:34 +03:00
|
|
|
if (! deviceName) {
|
|
|
|
deviceName = synthesizeName(deviceInfo)
|
|
|
|
}
|
2019-01-30 13:54:27 +03:00
|
|
|
if (credentials.tokenVerified) {
|
2018-03-03 01:15:35 +03:00
|
|
|
request.app.devices.then(devices => {
|
|
|
|
const otherDevices = devices.filter(device => device.id !== result.id)
|
2019-01-30 13:54:27 +03:00
|
|
|
return push.notifyDeviceConnected(credentials.uid, otherDevices, deviceName)
|
2018-03-03 01:15:35 +03:00
|
|
|
})
|
2017-02-10 00:12:28 +03:00
|
|
|
}
|
2016-08-09 12:55:15 +03:00
|
|
|
if (isPlaceholderDevice) {
|
2019-03-08 18:48:58 +03:00
|
|
|
log.info('device:createPlaceholder', {
|
2019-01-30 13:54:27 +03:00
|
|
|
uid: credentials.uid,
|
2016-08-09 12:55:15 +03:00
|
|
|
id: result.id
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return log.notifyAttachedServices('device:create', request, {
|
2019-01-30 13:54:27 +03:00
|
|
|
uid: credentials.uid,
|
2016-08-09 12:55:15 +03:00
|
|
|
id: result.id,
|
|
|
|
type: result.type,
|
|
|
|
timestamp: result.createdAt,
|
|
|
|
isPlaceholder: isPlaceholderDevice
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(function () {
|
2019-01-30 13:54:27 +03:00
|
|
|
delete result.sessionTokenId
|
|
|
|
delete result.refreshTokenId
|
2016-08-09 12:55:15 +03:00
|
|
|
return result
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function synthesizeName (device) {
|
2017-08-07 18:25:44 +03:00
|
|
|
const uaBrowser = device.uaBrowser
|
|
|
|
const uaBrowserVersion = device.uaBrowserVersion
|
|
|
|
const uaOS = device.uaOS
|
|
|
|
const uaOSVersion = device.uaOSVersion
|
|
|
|
const uaFormFactor = device.uaFormFactor
|
|
|
|
let result = ''
|
2016-08-09 12:55:15 +03:00
|
|
|
|
2017-08-07 18:25:44 +03:00
|
|
|
if (uaBrowser) {
|
|
|
|
if (uaBrowserVersion) {
|
2018-03-22 19:49:05 +03:00
|
|
|
const splitIndex = uaBrowserVersion.indexOf('.')
|
|
|
|
result = `${uaBrowser} ${splitIndex === -1 ? uaBrowserVersion : uaBrowserVersion.substr(0, splitIndex)}`
|
2017-08-07 18:25:44 +03:00
|
|
|
} else {
|
|
|
|
result = uaBrowser
|
2016-08-09 12:55:15 +03:00
|
|
|
}
|
|
|
|
|
2017-08-07 18:25:44 +03:00
|
|
|
if (uaOS || uaFormFactor) {
|
|
|
|
result += ', '
|
|
|
|
}
|
2016-08-09 12:55:15 +03:00
|
|
|
}
|
|
|
|
|
2017-08-11 20:44:55 +03:00
|
|
|
if (uaFormFactor) {
|
|
|
|
return `${result}${uaFormFactor}`
|
|
|
|
}
|
|
|
|
|
2017-08-07 18:25:44 +03:00
|
|
|
if (uaOS) {
|
|
|
|
result += uaOS
|
2016-08-09 12:55:15 +03:00
|
|
|
|
2017-08-11 20:44:55 +03:00
|
|
|
if (uaOSVersion) {
|
2017-08-07 18:25:44 +03:00
|
|
|
result += ` ${uaOSVersion}`
|
2016-08-09 12:55:15 +03:00
|
|
|
}
|
|
|
|
}
|
2017-08-07 18:25:44 +03:00
|
|
|
|
|
|
|
return result
|
2016-08-09 12:55:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-25 22:43:08 +03:00
|
|
|
module.exports.schema = SCHEMA
|
|
|
|
|
2018-08-21 14:14:36 +03:00
|
|
|
function isLike (object, archetype) {
|
|
|
|
return Object.entries(archetype).every(([ key, value ]) => object[key] === value)
|
|
|
|
}
|