Skip to content
This repository was archived by the owner on Apr 3, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions db-server/lib/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,22 @@ AppError.invalidVerificationMethod = function () {
)
}

AppError.unknownDeviceCapability = function () {
return new AppError(
{
code: 400,
error: 'Bad request',
errno: 139,
message: 'Unknown device capability'
}
)
}

AppError.wrap = function (err) {
// Don't re-wrap!
if (err instanceof AppError) {
return err
}
return new AppError(
{
code: 500,
Expand Down
48 changes: 45 additions & 3 deletions db-server/test/backend/db_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ function makeMockDevice(tokenId) {
callbackURL: 'https://push.server',
callbackPublicKey: 'foo',
callbackAuthKey: 'bar',
callbackIsExpired: false
callbackIsExpired: false,
capabilities: ['pushbox']
}
device.deviceId = newUuid()
return device
Expand Down Expand Up @@ -272,7 +273,7 @@ module.exports = function (config, DB) {
assert(Array.isArray(sessions), 'sessions is an array')
assert.equal(sessions.length, 1, 'sessions has one item')

assert.equal(Object.keys(sessions[0]).length, 19, 'session has correct properties')
assert.equal(Object.keys(sessions[0]).length, 20, 'session has correct properties')
assert.equal(sessions[0].tokenId.toString('hex'), sessionTokenData.tokenId.toString('hex'), 'tokenId is correct')
assert.equal(sessions[0].uid.toString('hex'), accountData.uid.toString('hex'), 'uid is correct')
assert.equal(sessions[0].createdAt, sessionTokenData.createdAt, 'createdAt is correct')
Expand Down Expand Up @@ -479,6 +480,7 @@ module.exports = function (config, DB) {
assert.equal(sessions[0].deviceCallbackPublicKey, 'foo')
assert.equal(sessions[0].deviceCallbackAuthKey, 'bar')
assert.equal(sessions[0].deviceCallbackIsExpired, false)
assert.deepEqual(sessions[0].deviceCapabilities, ['pushbox'])
})
})

Expand Down Expand Up @@ -887,7 +889,9 @@ module.exports = function (config, DB) {

return db.createSessionToken(sessionTokenData.tokenId, sessionTokenData)
.then(() => db.createDevice(accountData.uid, deviceInfo.deviceId, deviceInfo))
.then((result) => assert.deepEqual(result, {}, 'returned empty object'))
.then((result) => {
return assert.deepEqual(result, {}, 'returned empty object')
})
})

it('should have created device', () => {
Expand All @@ -902,6 +906,7 @@ module.exports = function (config, DB) {
assert.equal(s.deviceCallbackPublicKey, deviceInfo.callbackPublicKey, 'callbackPublicKey')
assert.equal(s.deviceCallbackAuthKey, deviceInfo.callbackAuthKey, 'callbackAuthKey')
assert.equal(s.deviceCallbackIsExpired, deviceInfo.callbackIsExpired, 'callbackIsExpired')
assert.deepEqual(s.deviceCapabilities, deviceInfo.capabilities, 'capabilities')
assert.equal(!! s.mustVerify, !! sessionTokenData.mustVerify, 'mustVerify is correct')
assert.deepEqual(s.tokenVerificationId, sessionTokenData.tokenVerificationId, 'tokenVerificationId is correct')
})
Expand All @@ -921,6 +926,7 @@ module.exports = function (config, DB) {
assert.equal(device.callbackPublicKey, deviceInfo.callbackPublicKey, 'callbackPublicKey')
assert.equal(device.callbackAuthKey, deviceInfo.callbackAuthKey, 'callbackAuthKey')
assert.equal(device.callbackIsExpired, deviceInfo.callbackIsExpired, 'callbackIsExpired')
assert.deepEqual(device.capabilities, deviceInfo.capabilities, 'capabilities')
assert(device.lastAccessTime > 0, 'has a lastAccessTime')
assert.equal(device.email, accountData.email, 'email should be account email')
})
Expand All @@ -933,6 +939,7 @@ module.exports = function (config, DB) {
deviceInfo.callbackPublicKey = ''
deviceInfo.callbackAuthKey = ''
deviceInfo.callbackIsExpired = true
deviceInfo.capabilities = []

const newSessionTokenData = makeMockSessionToken(accountData.uid)
deviceInfo.sessionTokenId = newSessionTokenData.tokenId
Expand All @@ -955,6 +962,7 @@ module.exports = function (config, DB) {
assert.equal(device.callbackPublicKey, '', 'callbackPublicKey unchanged')
assert.equal(device.callbackAuthKey, '', 'callbackAuthKey unchanged')
assert.equal(device.callbackIsExpired, true, 'callbackIsExpired unchanged')
assert.deepEqual(device.capabilities, [], 'capabilities updated')
})
})

Expand Down Expand Up @@ -988,6 +996,40 @@ module.exports = function (config, DB) {
})
})

it('should fail to update a device with unknown capabilities', () => {
const newDevice = Object.assign({}, deviceInfo, {
capabilities: ['unknown', 'newpushbox']
})
return db.updateDevice(accountData.uid, deviceInfo.deviceId, newDevice)
.then(assert.fail, (err) => {
assert.equal(err.code, 400, 'err.code')
assert.equal(err.errno, 139, 'err.errno')
return db.accountDevices(accountData.uid)
})
.then((devices) => assert.deepEqual(devices[0].capabilities, ['pushbox']))
})

it('capabilities are not cleared if not specified', () => {
const newDevice = Object.assign({}, deviceInfo)
delete newDevice.capabilities
return db.updateDevice(accountData.uid, deviceInfo.deviceId, newDevice)
.then(() => {
return db.accountDevices(accountData.uid)
})
.then((devices) => assert.deepEqual(devices[0].capabilities, ['pushbox']))
})

it('capabilities are overwritten on update', () => {
const newDevice = Object.assign({}, deviceInfo, {
capabilities: []
})
return db.updateDevice(accountData.uid, deviceInfo.deviceId, newDevice)
.then(() => {
return db.accountDevices(accountData.uid)
})
.then((devices) => assert.deepEqual(devices[0].capabilities, []))
})

it('should fail to delete non-existent device', () => {
return db.deleteDevice(accountData.uid, hex16())
.then(assert.fail, (err) => {
Expand Down
32 changes: 29 additions & 3 deletions db-server/test/backend/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ module.exports = function(cfg, makeServer) {
respOk(r)
var sessions = r.obj
assert.equal(sessions.length, 1, 'sessions contains one item')
assert.equal(Object.keys(sessions[0]).length, 19, 'session has correct properties')
assert.equal(Object.keys(sessions[0]).length, 20, 'session has correct properties')
assert.equal(sessions[0].tokenId, user.sessionTokenId, 'tokenId is correct')
assert.equal(sessions[0].uid, user.accountId, 'uid is correct')
assert.equal(sessions[0].createdAt, user.sessionToken.createdAt, 'createdAt is correct')
Expand Down Expand Up @@ -525,6 +525,7 @@ module.exports = function(cfg, makeServer) {
assert(s.deviceCallbackPublicKey)
assert.equal(s.deviceCallbackURL, 'fake callback URL')
assert.equal(s.deviceCallbackIsExpired, false)
assert.deepEqual(s.deviceCapabilities, ['pushbox'])
assert(s.deviceCreatedAt)
assert(s.deviceId)
assert.equal(s.deviceName, 'fake device name')
Expand Down Expand Up @@ -592,6 +593,27 @@ module.exports = function(cfg, makeServer) {
.then(function() {
return client.getThen('/account/' + user.accountId + '/devices')
})
.then(function(r) {
assert.equal(r.obj.length, 0, 'devices is empty')
const myDevice = Object.assign({}, user.device, {
capabilities: ['unknown', 'pushbox']
})
return client.putThen('/account/' + user.accountId + '/device/' + user.deviceId, myDevice)
.then(() => {
assert(false, 'a device with an unknown capability should make the request fail')
})
.catch(err => {
assert.equal(err.statusCode, 400, 'err.statusCode should be 400')
assert.deepEqual(err.body, {
message: 'Unknown device capability',
errno: 139,
error: 'Bad request',
code: 400
}, 'err.body should have correct properties set')
})
}).then(function () {
return client.getThen('/account/' + user.accountId + '/devices')
})
.then(function(r) {
assert.equal(r.obj.length, 0, 'devices is empty')
return client.putThen('/account/' + user.accountId + '/device/' + user.deviceId, user.device)
Expand All @@ -604,7 +626,7 @@ module.exports = function(cfg, makeServer) {
respOk(r)
var devices = r.obj
assert.equal(devices.length, 1, 'devices contains one item')
assert.equal(Object.keys(devices[0]).length, 18, 'device has eighteen properties')
assert.equal(Object.keys(devices[0]).length, 19, 'device has nineteen properties')
assert.equal(devices[0].uid, user.accountId, 'uid is correct')
assert.equal(devices[0].id, user.deviceId, 'id is correct')
assert.equal(devices[0].sessionTokenId, user.sessionTokenId, 'sessionTokenId is correct')
Expand All @@ -615,6 +637,7 @@ module.exports = function(cfg, makeServer) {
assert.equal(devices[0].callbackPublicKey, user.device.callbackPublicKey, 'callbackPublicKey is correct')
assert.equal(devices[0].callbackAuthKey, user.device.callbackAuthKey, 'callbackAuthKey is correct')
assert.equal(devices[0].callbackIsExpired, user.device.callbackIsExpired, 'callbackIsExpired is correct')
assert.deepEqual(devices[0].capabilities, user.device.capabilities, 'capabilities is correct')
assert.equal(devices[0].uaBrowser, user.sessionToken.uaBrowser, 'uaBrowser is correct')
assert.equal(devices[0].uaBrowserVersion, user.sessionToken.uaBrowserVersion, 'uaBrowserVersion is correct')
assert.equal(devices[0].uaOS, user.sessionToken.uaOS, 'uaOS is correct')
Expand All @@ -638,14 +661,16 @@ module.exports = function(cfg, makeServer) {
assert.equal(device.callbackPublicKey, user.device.callbackPublicKey, 'callbackPublicKey is correct')
assert.equal(device.callbackAuthKey, user.device.callbackAuthKey, 'callbackAuthKey is correct')
assert.equal(device.callbackIsExpired, user.device.callbackIsExpired, 'callbackIsExpired is correct')
assert.deepEqual(device.capabilities, user.device.capabilities, 'capabilities is correct')

return client.postThen('/account/' + user.accountId + '/device/' + user.deviceId + '/update', {
name: 'wibble',
type: 'mobile',
callbackURL: '',
callbackPublicKey: null,
callbackAuthKey: null,
callbackIsExpired: null
callbackIsExpired: null,
capabilities: []
})
})
.then(function(r) {
Expand All @@ -666,6 +691,7 @@ module.exports = function(cfg, makeServer) {
assert.equal(devices[0].callbackPublicKey, user.device.callbackPublicKey, 'callbackPublicKey is correct')
assert.equal(devices[0].callbackAuthKey, user.device.callbackAuthKey, 'callbackAuthKey is correct')
assert.equal(devices[0].callbackIsExpired, false, 'callbackIsExpired is correct')
assert.deepEqual(devices[0].capabilities, [], 'capabilities is correct')
assert.equal(devices[0].uaBrowser, user.sessionToken.uaBrowser, 'uaBrowser is correct')
assert.equal(devices[0].uaBrowserVersion, user.sessionToken.uaBrowserVersion, 'uaBrowserVersion is correct')
assert.equal(devices[0].uaOS, user.sessionToken.uaOS, 'uaOS is correct')
Expand Down
3 changes: 2 additions & 1 deletion db-server/test/fake.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ module.exports.newUserDataHex = function() {
callbackURL: 'fake callback URL',
callbackPublicKey: base64_65(),
callbackAuthKey: base64_16(),
callbackIsExpired: false
callbackIsExpired: false,
capabilities: ['pushbox']
}

// keyFetchToken
Expand Down
20 changes: 17 additions & 3 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,7 @@ Content-Type: application/json
"callbackPublicKey": "BCp93zru09_hab2Bg37LpTNG__Pw6eMPEP2hrQpwuytoj3h4chXpGc-3qqdKyqjuvAiEupsnOd_RLyc7erJHWgA",
"callbackAuthKey": "w3b14Zjc-Afj2SDOLOyong",
"callbackIsExpired": false,
"capabilities": ["pushbox"],
"uaBrowser": "Firefox",
"uaBrowserVersion": "42",
"uaOS": "Android",
Expand Down Expand Up @@ -720,7 +721,8 @@ Content-Type: application/json
"callbackURL": "https://updates.push.services.mozilla.com/update/abcdef01234567890abcdefabcdef01234567890abcdef",
"callbackPublicKey": "BCp93zru09_hab2Bg37LpTNG__Pw6eMPEP2hrQpwuytoj3h4chXpGc-3qqdKyqjuvAiEupsnOd_RLyc7erJHWgA",
"callbackAuthKey": "w3b14Zjc-Afj2SDOLOyong",
"callbackIsExpired": false
"callbackIsExpired": false,
"capabilities": ["pushbox"]
}
```

Expand Down Expand Up @@ -755,7 +757,8 @@ curl \
"callbackURL": "https://updates.push.services.mozilla.com/update/abcdef01234567890abcdefabcdef01234567890abcdef",
"callbackPublicKey": "BCp93zru09_hab2Bg37LpTNG__Pw6eMPEP2hrQpwuytoj3h4chXpGc-3qqdKyqjuvAiEupsnOd_RLyc7erJHWgA",
"callbackAuthKey": "w3b14Zjc-Afj2SDOLOyong",
"callbackIsExpired": false
"callbackIsExpired": false,
"capabilities": ["pushbox"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also document the new error response in the section below?

}'
```

Expand All @@ -775,6 +778,10 @@ Content-Type: application/json
* Conditions: if id already exists or sessionTokenId is already used by a different device
* Content-Type : 'application/json'
* Body : `{"errno":101",message":"Record already exists"}`
* Status Code : 400 Bad Request
* Conditions: if the device in the request body contained an unknown capability name
* Content-Type : 'application/json'
* Body : `{"errno":139",message":"Unknown device capability"}`
* Status Code : 500 Internal Server Error
* Conditions: if something goes wrong on the server
* Content-Type : 'application/json'
Expand All @@ -799,7 +806,8 @@ curl \
"callbackURL": "https://updates.push.services.mozilla.com/update/abcdef01234567890abcdefabcdef01234567890abcdef",
"callbackPublicKey": "BCp93zru09_hab2Bg37LpTNG__Pw6eMPEP2hrQpwuytoj3h4chXpGc-3qqdKyqjuvAiEupsnOd_RLyc7erJHWgA",
"callbackAuthKey": "w3b14Zjc-Afj2SDOLOyong",
"callbackIsExpired": false
"callbackIsExpired": false,
"capabilities": ["pushbox"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also document the new error response in the section below?

}'
```

Expand All @@ -819,6 +827,10 @@ Content-Type: application/json
* Conditions: if sessionTokenId is already used by a different device
* Content-Type : 'application/json'
* Body : `{"errno":101",message":"Record already exists"}`
* Status Code : 400 Bad Request
* Conditions: if the device in the request body contained an unknown capability name
* Content-Type : 'application/json'
* Body : `{"errno":139",message":"Unknown device capability"}`
* Status Code : 404 Not Found
* Conditions: if device(uid,id) is not found in the database
* Content-Type : 'application/json'
Expand Down Expand Up @@ -909,6 +921,7 @@ Content-Length: 285
"deviceCallbackURL":null,
"deviceCallbackPublicKey":null,
"deviceCallbackIsExpired":false,
"deviceCapabilities":["pushbox"],
"mustVerify":true,
"tokenVerificationId":"12c41fac80fd6149f3f695e188b5f846"
}
Expand Down Expand Up @@ -975,6 +988,7 @@ Content-Length: 285
"deviceCallbackURL":null,
"deviceCallbackPublicKey":null,
"deviceCallbackIsExpired":false,
"deviceCapabilities":["pushbox"],
"mustVerify":true,
"tokenVerificationId":"12c41fac80fd6149f3f695e188b5f846"
}
Expand Down
7 changes: 7 additions & 0 deletions docs/DB_API.md
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ The deviceCallbackPublicKey and deviceCallbackAuthKey fields are urlsafe-base64
d.callbackPublicKey AS deviceCallbackPublicKey,
d.callbackAuthKey AS deviceCallbackAuthKey,
d.callbackIsExpired AS deviceCallbackIsExpired,
d.capabilities AS deviceCapabilities,
ut.mustVerify, ut.tokenVerificationId
* keyFetchToken : t.authKey, t.uid, t.keyBundle, t.createdAt, a.emailVerified, a.verifierSetAt
* keyFetchTokenWithVerificationStatus : t.authKey, t.uid, t.keyBundle, t.createdAt, a.emailVerified,
Expand Down Expand Up @@ -743,13 +744,16 @@ Parameters:
Public key for push service
* `callbackAuthKey` (string):
Auth key for push service
* `capabilities` (array):
Array of strings describing the current device capabilities
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also document the new error under Rejects with?


Returns:

* Resolves with:
* An empty object `{}`
* Rejects with:
* `error.duplicate()` if a device already exists with the same `uid` and `deviceId`
* `error.unknownDeviceCapability()` if the input device contained an unknown capability name
* Any error from the underlying storage system (wrapped in `error.wrap()`)

## updateDevice(uid, deviceId, device)
Expand Down Expand Up @@ -777,12 +781,15 @@ Parameters:
Public key for push service
* `callbackAuthKey` (string):
Auth key for push service
* `capabilities` (array):
Array of strings describing the current device capabilities
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also document the new error under Rejects with?


Returns:

* Resolves with:
* An empty object `{}`
* Rejects with:
* `error.unknownDeviceCapability()` if the input device contained an unknown capability name
* Any error from the underlying storage system (wrapped in `error.wrap()`)

## deleteDevice(uid, deviceId)
Expand Down
Loading