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/. */
|
|
|
|
|
2013-02-23 03:41:48 +04:00
|
|
|
const Hapi = require('hapi');
|
2013-02-20 01:13:12 +04:00
|
|
|
|
|
|
|
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-03-06 03:10:06 +04:00
|
|
|
pre: [ prereqs.assertionGet, prereqs.userId, prereqs.user ],
|
2013-02-26 01:11:16 +04:00
|
|
|
validate: {
|
|
|
|
query: {
|
2013-03-06 03:10:06 +04:00
|
|
|
assertion: Str(),
|
|
|
|
email: Str()
|
2013-02-26 01:11:16 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
response: {
|
|
|
|
schema: {
|
|
|
|
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-03-06 03:10:06 +04:00
|
|
|
pre: [ prereqs.assertion ],
|
2013-02-20 01:13:12 +04:00
|
|
|
validate: {
|
|
|
|
schema: {
|
2013-03-06 03:10:06 +04:00
|
|
|
assertion: Str(),
|
|
|
|
email: Str()
|
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',
|
2013-03-06 03:10:06 +04:00
|
|
|
pre: [ prereqs.assertion, prereqs.userId ],
|
2013-02-20 01:13:12 +04:00
|
|
|
validate: {
|
|
|
|
schema: {
|
2013-03-06 03:10:06 +04:00
|
|
|
assertion: Str(),
|
|
|
|
email: Str()
|
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:41:48 +04:00
|
|
|
request.reply.payload({
|
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
|
|
|
|