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/. */
|
|
|
|
|
2019-03-25 16:55:37 +03:00
|
|
|
'use strict';
|
2016-08-09 12:55:15 +03:00
|
|
|
|
2019-03-25 16:55:37 +03:00
|
|
|
const isA = require('joi');
|
|
|
|
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
|
2019-03-25 16:55:37 +03:00
|
|
|
} = validators;
|
|
|
|
const PUSH_SERVER_REGEX = require('../config').get('push.allowedServerRegex');
|
2017-08-25 22:43:08 +03:00
|
|
|
|
|
|
|
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))
|
2019-03-25 16:55:37 +03:00
|
|
|
};
|
2017-08-25 22:43:08 +03:00
|
|
|
|
2017-08-07 18:25:44 +03:00
|
|
|
module.exports = (log, db, push) => {
|
2019-03-25 16:55:37 +03:00
|
|
|
return { isSpuriousUpdate, upsert, synthesizeName };
|
2018-08-21 14:04:40 +03:00
|
|
|
|
|
|
|
// 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) {
|
2019-03-25 16:55:37 +03:00
|
|
|
return false;
|
2018-08-21 14:04:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (payload.name && payload.name !== token.deviceName) {
|
2019-03-25 16:55:37 +03:00
|
|
|
return false;
|
2018-08-21 14:04:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (payload.type && payload.type !== token.deviceType) {
|
2019-03-25 16:55:37 +03:00
|
|
|
return false;
|
2018-08-21 14:04:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (payload.pushCallback && payload.pushCallback !== token.deviceCallbackURL) {
|
2019-03-25 16:55:37 +03:00
|
|
|
return false;
|
2018-08-21 14:04:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (payload.pushPublicKey && payload.pushPublicKey !== token.deviceCallbackPublicKey) {
|
2019-03-25 16:55:37 +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) {
|
2019-03-25 16:55:37 +03:00
|
|
|
return false;
|
2018-08-21 14:14:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (! isLike(token.deviceAvailableCommands, payload.availableCommands)) {
|
2019-03-25 16:55:37 +03:00
|
|
|
return false;
|
2018-08-21 14:14:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (! isLike(payload.availableCommands, token.deviceAvailableCommands)) {
|
2019-03-25 16:55:37 +03:00
|
|
|
return false;
|
2018-08-21 14:04:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-25 16:55:37 +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) {
|
2019-03-25 16:55:37 +03:00
|
|
|
let operation, event, result;
|
2016-08-09 12:55:15 +03:00
|
|
|
if (deviceInfo.id) {
|
2019-03-25 16:55:37 +03:00
|
|
|
operation = 'updateDevice';
|
|
|
|
event = 'device.updated';
|
2016-08-09 12:55:15 +03:00
|
|
|
} else {
|
2019-03-25 16:55:37 +03:00
|
|
|
operation = 'createDevice';
|
|
|
|
event = 'device.created';
|
2019-01-30 13:54:27 +03:00
|
|
|
if (! deviceInfo.name) {
|
2019-03-25 16:55:37 +03:00
|
|
|
deviceInfo.name = credentials.client && credentials.client.name || '';
|
2019-01-30 13:54:27 +03:00
|
|
|
}
|
2016-08-09 12:55:15 +03:00
|
|
|
}
|
2019-01-30 13:54:27 +03:00
|
|
|
|
2019-03-25 16:55:37 +03:00
|
|
|
deviceInfo.sessionTokenId = credentials.id;
|
|
|
|
deviceInfo.refreshTokenId = credentials.refreshTokenId;
|
2019-01-30 13:54:27 +03:00
|
|
|
|
2019-03-25 16:55:37 +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 => {
|
2019-03-25 16:55:37 +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
|
2019-03-25 16:55:37 +03:00
|
|
|
});
|
2016-08-09 12:55:15 +03:00
|
|
|
})
|
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.
|
2019-03-25 16:55:37 +03:00
|
|
|
let deviceName = result.name;
|
2017-01-31 12:08:34 +03:00
|
|
|
if (! deviceName) {
|
2019-03-25 16:55:37 +03:00
|
|
|
deviceName = synthesizeName(deviceInfo);
|
2017-01-31 12:08:34 +03:00
|
|
|
}
|
2019-01-30 13:54:27 +03:00
|
|
|
if (credentials.tokenVerified) {
|
2019-03-24 00:58:44 +03:00
|
|
|
db.devices(credentials.uid).then(devices => {
|
2019-03-25 16:55:37 +03:00
|
|
|
const otherDevices = devices.filter(device => device.id !== result.id);
|
|
|
|
return push.notifyDeviceConnected(credentials.uid, otherDevices, deviceName);
|
|
|
|
});
|
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
|
2019-03-25 16:55:37 +03:00
|
|
|
});
|
2016-08-09 12:55:15 +03:00
|
|
|
}
|
|
|
|
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
|
2019-03-25 16:55:37 +03:00
|
|
|
});
|
2016-08-09 12:55:15 +03:00
|
|
|
}
|
|
|
|
})
|
2019-03-25 21:52:22 +03:00
|
|
|
.then(() => {
|
2019-03-25 16:55:37 +03:00
|
|
|
delete result.sessionTokenId;
|
|
|
|
delete result.refreshTokenId;
|
|
|
|
return result;
|
|
|
|
});
|
2016-08-09 12:55:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function synthesizeName (device) {
|
2019-03-25 16:55:37 +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) {
|
2019-03-25 16:55:37 +03:00
|
|
|
const splitIndex = uaBrowserVersion.indexOf('.');
|
|
|
|
result = `${uaBrowser} ${splitIndex === -1 ? uaBrowserVersion : uaBrowserVersion.substr(0, splitIndex)}`;
|
2017-08-07 18:25:44 +03:00
|
|
|
} else {
|
2019-03-25 16:55:37 +03:00
|
|
|
result = uaBrowser;
|
2016-08-09 12:55:15 +03:00
|
|
|
}
|
|
|
|
|
2017-08-07 18:25:44 +03:00
|
|
|
if (uaOS || uaFormFactor) {
|
2019-03-25 16:55:37 +03:00
|
|
|
result += ', ';
|
2017-08-07 18:25:44 +03:00
|
|
|
}
|
2016-08-09 12:55:15 +03:00
|
|
|
}
|
|
|
|
|
2017-08-11 20:44:55 +03:00
|
|
|
if (uaFormFactor) {
|
2019-03-25 16:55:37 +03:00
|
|
|
return `${result}${uaFormFactor}`;
|
2017-08-11 20:44:55 +03:00
|
|
|
}
|
|
|
|
|
2017-08-07 18:25:44 +03:00
|
|
|
if (uaOS) {
|
2019-03-25 16:55:37 +03:00
|
|
|
result += uaOS;
|
2016-08-09 12:55:15 +03:00
|
|
|
|
2017-08-11 20:44:55 +03:00
|
|
|
if (uaOSVersion) {
|
2019-03-25 16:55:37 +03:00
|
|
|
result += ` ${uaOSVersion}`;
|
2016-08-09 12:55:15 +03:00
|
|
|
}
|
|
|
|
}
|
2017-08-07 18:25:44 +03:00
|
|
|
|
2019-03-25 16:55:37 +03:00
|
|
|
return result;
|
2016-08-09 12:55:15 +03:00
|
|
|
}
|
2019-03-25 16:55:37 +03:00
|
|
|
};
|
2016-08-09 12:55:15 +03:00
|
|
|
|
2019-03-25 16:55:37 +03:00
|
|
|
module.exports.schema = SCHEMA;
|
2017-08-25 22:43:08 +03:00
|
|
|
|
2018-08-21 14:14:36 +03:00
|
|
|
function isLike (object, archetype) {
|
2019-03-25 16:55:37 +03:00
|
|
|
return Object.entries(archetype).every(([ key, value ]) => object[key] === value);
|
2018-08-21 14:14:36 +03:00
|
|
|
}
|