2013-02-20 01:13:12 +04: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/. */
|
|
|
|
|
|
|
|
const Hapi = require('Hapi');
|
|
|
|
|
|
|
|
const users = require('../lib/users.js');
|
|
|
|
const prereqs = require('../lib/prereqs.js');
|
|
|
|
|
|
|
|
const Str = Hapi.Types.String;
|
|
|
|
const Bool = Hapi.Types.Boolean;
|
|
|
|
const Num = Hapi.Types.Number;
|
|
|
|
|
2013-02-26 01:11:16 +04:00
|
|
|
var getConfig = {
|
2013-02-23 03:38:01 +04:00
|
|
|
description: 'Get user meta data',
|
2013-02-26 01:11:16 +04:00
|
|
|
pre: [ prereqs.emailGet, prereqs.userId, prereqs.user ],
|
|
|
|
validate: {
|
|
|
|
query: {
|
|
|
|
email: Str().required()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
response: {
|
|
|
|
schema: {
|
|
|
|
success: Bool().required(),
|
|
|
|
kA: Str().required(),
|
|
|
|
version: Num().integer().required()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-02-20 01:13:12 +04:00
|
|
|
exports.routes = [
|
|
|
|
{
|
|
|
|
method: 'POST',
|
|
|
|
path: '/user',
|
|
|
|
handler: create,
|
|
|
|
config: {
|
2013-02-23 03:38:01 +04:00
|
|
|
description: 'Create a new user',
|
2013-02-20 01:13:12 +04:00
|
|
|
pre: [ prereqs.email ],
|
|
|
|
validate: {
|
|
|
|
schema: {
|
2013-02-20 23:53:18 +04:00
|
|
|
email: Str().required()
|
2013-02-20 01:13:12 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
response: {
|
|
|
|
schema: {
|
|
|
|
kA: Str().required(),
|
|
|
|
deviceId: Str().required(),
|
|
|
|
version: Num().integer().required()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2013-02-26 01:11:16 +04:00
|
|
|
{
|
|
|
|
method: 'GET',
|
|
|
|
path: '/user',
|
|
|
|
handler: get,
|
|
|
|
config: getConfig
|
|
|
|
},
|
2013-02-20 01:13:12 +04:00
|
|
|
{
|
|
|
|
method: 'GET',
|
2013-02-20 23:53:18 +04:00
|
|
|
path: '/user/{deviceId?}',
|
2013-02-20 01:13:12 +04:00
|
|
|
handler: get,
|
2013-02-26 01:11:16 +04:00
|
|
|
config: getConfig
|
2013-02-20 01:13:12 +04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
method: 'POST',
|
|
|
|
path: '/user/bump/{deviceId}',
|
|
|
|
handler: bump,
|
|
|
|
config: {
|
|
|
|
description: 'Create a new class A key for the user and bump the version number',
|
|
|
|
pre: [ prereqs.email, prereqs.userId ],
|
|
|
|
validate: {
|
|
|
|
schema: {
|
2013-02-21 23:49:56 +04:00
|
|
|
email: Str().required()
|
2013-02-20 01:13:12 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
response: {
|
|
|
|
schema: {
|
|
|
|
kA: Str().required(),
|
|
|
|
version: Num().integer().required()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
|
2013-02-26 01:08:12 +04:00
|
|
|
|
2013-02-20 01:13:12 +04:00
|
|
|
// create a user by assertion
|
|
|
|
function create(request) {
|
|
|
|
users.create(request.pre.email, function(err, result) {
|
2013-02-26 01:08:12 +04:00
|
|
|
if (err) return request.reply(err);
|
2013-02-20 01:13:12 +04:00
|
|
|
|
2013-02-23 03:38:01 +04:00
|
|
|
request.reply({
|
2013-02-20 01:13:12 +04:00
|
|
|
kA: result.kA,
|
|
|
|
deviceId: result.deviceId,
|
|
|
|
version: result.version
|
2013-02-26 02:02:31 +04:00
|
|
|
}).created().send();
|
2013-02-20 01:13:12 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// get a user's class A key and its version number
|
|
|
|
function get(request) {
|
|
|
|
var pre = request.pre;
|
|
|
|
|
2013-02-20 23:53:18 +04:00
|
|
|
// For NULL auth, deviceId is not required
|
|
|
|
if (! request.params.deviceId) {
|
|
|
|
return request.reply({
|
|
|
|
kA: pre.user.kA,
|
|
|
|
version: pre.user.kA_version
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-02-20 01:13:12 +04:00
|
|
|
// update the device's last kA request time
|
|
|
|
users.updateDevice(pre.userId, request.params.deviceId, function(err) {
|
2013-02-26 01:08:12 +04:00
|
|
|
if (err) return request.reply(err);
|
2013-02-20 01:13:12 +04:00
|
|
|
|
|
|
|
request.reply({
|
|
|
|
kA: pre.user.kA,
|
|
|
|
version: pre.user.kA_version
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a new class A key and bump the version number
|
|
|
|
function bump(request) {
|
|
|
|
var pre = request.pre;
|
|
|
|
|
|
|
|
// mark all other devices as having a stale key
|
|
|
|
users.outdateDevices(pre.userId, request.params.deviceId, function(err) {
|
2013-02-26 01:08:12 +04:00
|
|
|
if (err) return request.reply(err);
|
2013-02-20 01:13:12 +04:00
|
|
|
|
|
|
|
// create a new key and bump the version
|
|
|
|
users.bumpkA(pre.userId, function(err, user) {
|
2013-02-26 01:08:12 +04:00
|
|
|
if (err) return request.reply(err);
|
2013-02-20 01:13:12 +04:00
|
|
|
|
|
|
|
request.reply({
|
|
|
|
kA: user.kA,
|
|
|
|
version: user.kA_version
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2013-02-26 01:08:12 +04:00
|
|
|
|