Skip to content
This repository was archived by the owner on Apr 3, 2019. It is now read-only.

Commit e12cd08

Browse files
authored
fix(server): reinstate placeholder devices for sync sessions
This reverts commit c4c6733. r=seanmonstar
1 parent 6acb9e0 commit e12cd08

File tree

10 files changed

+732
-289
lines changed

10 files changed

+732
-289
lines changed

lib/db.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,17 @@ module.exports = function (
439439
type: item.type,
440440
pushCallback: item.callbackURL,
441441
pushPublicKey: item.callbackPublicKey,
442-
pushAuthKey: item.callbackAuthKey
442+
pushAuthKey: item.callbackAuthKey,
443+
uaBrowser: item.uaBrowser,
444+
uaBrowserVersion: item.uaBrowserVersion,
445+
uaOS: item.uaOS,
446+
uaOSVersion: item.uaOSVersion,
447+
uaDeviceType: item.uaDeviceType
443448
}, {
444-
ignore: [ 'name', 'type', 'pushCallback', 'pushPublicKey', 'pushAuthKey' ]
449+
ignore: [
450+
'name', 'type', 'pushCallback', 'pushPublicKey', 'pushAuthKey',
451+
'uaBrowser', 'uaBrowserVersion', 'uaOS', 'uaOSVersion', 'uaDeviceType'
452+
]
445453
})
446454
})
447455
},

lib/devices.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
'use strict'
6+
7+
module.exports = function (log, db, push) {
8+
return {
9+
upsert: upsert,
10+
synthesizeName: synthesizeName
11+
}
12+
13+
function upsert (request, sessionToken, deviceInfo) {
14+
var operation, event, result
15+
if (deviceInfo.id) {
16+
operation = 'updateDevice'
17+
event = 'device.updated'
18+
} else {
19+
operation = 'createDevice'
20+
event = 'device.created'
21+
}
22+
var isPlaceholderDevice = Object.keys(deviceInfo).length === 0
23+
24+
return db[operation](sessionToken.uid, sessionToken.tokenId, deviceInfo)
25+
.then(function (device) {
26+
result = device
27+
return log.activityEvent(event, request, {
28+
uid: sessionToken.uid.toString('hex'),
29+
device_id: result.id.toString('hex'),
30+
is_placeholder: isPlaceholderDevice
31+
})
32+
})
33+
.then(function () {
34+
if (operation === 'createDevice') {
35+
push.notifyDeviceConnected(sessionToken.uid, result.name, result.id.toString('hex'))
36+
if (isPlaceholderDevice) {
37+
log.info({
38+
op: 'device:createPlaceholder',
39+
uid: sessionToken.uid,
40+
id: result.id
41+
})
42+
}
43+
return log.notifyAttachedServices('device:create', request, {
44+
uid: sessionToken.uid,
45+
id: result.id,
46+
type: result.type,
47+
timestamp: result.createdAt,
48+
isPlaceholder: isPlaceholderDevice
49+
})
50+
}
51+
})
52+
.then(function () {
53+
return result
54+
})
55+
}
56+
57+
function synthesizeName (device) {
58+
var browserPart = part('uaBrowser')
59+
var osPart = part('uaOS')
60+
61+
if (browserPart) {
62+
if (osPart) {
63+
return browserPart + ', ' + osPart
64+
}
65+
66+
return browserPart
67+
}
68+
69+
return osPart || ''
70+
71+
function part (key) {
72+
if (device[key]) {
73+
var versionKey = key + 'Version'
74+
75+
if (device[versionKey]) {
76+
return device[key] + ' ' + device[versionKey]
77+
}
78+
79+
return device[key]
80+
}
81+
}
82+
}
83+
}
84+

lib/routes/account.js

Lines changed: 26 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ module.exports = function (
3535
isPreVerified,
3636
checkPassword,
3737
push,
38-
metricsContext
38+
metricsContext,
39+
devices
3940
) {
4041

4142
// Loads and compiles a json validator for the payloads received
@@ -803,7 +804,7 @@ module.exports = function (
803804
schema: isA.object({
804805
id: isA.string().length(32).regex(HEX_STRING).required(),
805806
createdAt: isA.number().positive().optional(),
806-
// We previously allowed devices to register with arbitrry unicode names,
807+
// We previously allowed devices to register with arbitrary unicode names,
807808
// so we can't assert DISPLAY_SAFE_UNICODE in the response schema.
808809
name: isA.string().max(255).optional(),
809810
type: isA.string().max(16).optional(),
@@ -829,17 +830,19 @@ module.exports = function (
829830
if (config.deviceUpdatesEnabled === false) {
830831
throw error.featureNotEnabled()
831832
}
833+
} else if (sessionToken.deviceId) {
834+
// Keep the old id, which is probably from a synthesized device record
835+
payload.id = sessionToken.deviceId.toString('hex')
832836
}
833837

834838
if (payload.pushCallback && (!payload.pushPublicKey || !payload.pushAuthKey)) {
835839
payload.pushPublicKey = ''
836840
payload.pushAuthKey = ''
837841
}
838842

839-
upsertDevice().then(
843+
devices.upsert(request, sessionToken, payload).then(
840844
function (device) {
841845
reply(butil.unbuffer(device))
842-
push.notifyDeviceConnected(sessionToken.uid, device.name, device.id.toString('hex'))
843846
},
844847
reply
845848
)
@@ -871,45 +874,6 @@ module.exports = function (
871874
}
872875
return spurious
873876
}
874-
875-
function upsertDevice () {
876-
var operation, event, result
877-
if (payload.id) {
878-
operation = 'updateDevice'
879-
event = 'device.updated'
880-
} else {
881-
operation = 'createDevice'
882-
event = 'device.created'
883-
}
884-
885-
return db[operation](sessionToken.uid, sessionToken.tokenId, payload)
886-
.then(
887-
function (res) {
888-
result = res
889-
return log.activityEvent(event, request, {
890-
uid: sessionToken.uid.toString('hex'),
891-
device_id: result.id.toString('hex')
892-
})
893-
}
894-
)
895-
.then(
896-
function () {
897-
if (operation === 'createDevice') {
898-
return log.notifyAttachedServices('device:create', request, {
899-
uid: sessionToken.uid,
900-
id: result.id,
901-
type: result.type,
902-
timestamp: result.createdAt
903-
})
904-
}
905-
}
906-
)
907-
.then(
908-
function () {
909-
return result
910-
}
911-
)
912-
}
913877
}
914878
},
915879
{
@@ -996,9 +960,9 @@ module.exports = function (
996960
id: isA.string().length(32).regex(HEX_STRING).required(),
997961
isCurrentDevice: isA.boolean().required(),
998962
lastAccessTime: isA.number().min(0).required().allow(null),
999-
// We previously allowed devices to register with arbitrry unicode names,
963+
// We previously allowed devices to register with arbitrary unicode names,
1000964
// so we can't assert DISPLAY_SAFE_UNICODE in the response schema.
1001-
name: isA.string().max(255).required(),
965+
name: isA.string().max(255).required().allow(''),
1002966
type: isA.string().max(16).required(),
1003967
pushCallback: isA.string().uri({ scheme: 'https' }).max(255).optional().allow('').allow(null),
1004968
pushPublicKey: isA.string().max(88).regex(URLSAFEBASE64).optional().allow('').allow(null),
@@ -1011,11 +975,26 @@ module.exports = function (
1011975
var sessionToken = request.auth.credentials
1012976
var uid = sessionToken.uid
1013977
db.devices(uid).then(
1014-
function (devices) {
1015-
reply(devices.map(function (device) {
978+
function (deviceArray) {
979+
reply(deviceArray.map(function (device) {
980+
if (! device.name) {
981+
device.name = devices.synthesizeName(device)
982+
}
983+
984+
if (! device.type) {
985+
device.type = device.uaDeviceType || 'desktop'
986+
}
987+
1016988
device.isCurrentDevice =
1017989
device.sessionToken.toString('hex') === sessionToken.tokenId.toString('hex')
990+
1018991
delete device.sessionToken
992+
delete device.uaBrowser
993+
delete device.uaBrowserVersion
994+
delete device.uaOS
995+
delete device.uaOSVersion
996+
delete device.uaDeviceType
997+
1019998
return butil.unbuffer(device)
1020999
}))
10211000
},

lib/routes/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module.exports = function (
2626
var idp = require('./idp')(log, serverPublicKeys)
2727
var checkPassword = require('./utils/password_check')(log, config, Password, customs, db)
2828
var push = require('../push')(log, db)
29+
var devices = require('../devices')(log, db, push)
2930
var account = require('./account')(
3031
log,
3132
crypto,
@@ -41,7 +42,8 @@ module.exports = function (
4142
isPreVerified,
4243
checkPassword,
4344
push,
44-
metricsContext
45+
metricsContext,
46+
devices
4547
)
4648
var password = require('./password')(
4749
log,
@@ -57,7 +59,7 @@ module.exports = function (
5759
push
5860
)
5961
var session = require('./session')(log, isA, error, db)
60-
var sign = require('./sign')(log, isA, error, signer, db, config.domain)
62+
var sign = require('./sign')(log, P, isA, error, signer, db, config.domain, devices)
6163
var util = require('./util')(
6264
log,
6365
crypto,

0 commit comments

Comments
 (0)