2013-05-15 23:25:41 +04:00
|
|
|
const Api = require('./api');
|
2013-05-16 17:19:30 +04:00
|
|
|
const errors = require('./lib/errors');
|
2013-05-13 22:27:20 +04:00
|
|
|
const _ = require('underscore');
|
2013-05-29 04:56:49 +04:00
|
|
|
const jwt = require('jwt-simple');
|
2013-05-13 22:27:20 +04:00
|
|
|
|
2013-05-14 06:59:46 +04:00
|
|
|
const ENDPOINT = process.env['CSOL_OPENBADGER_URL'];
|
2013-05-29 04:56:49 +04:00
|
|
|
const JWT_SECRET = process.env['CSOL_OPENBADGER_SECRET'];
|
|
|
|
const TOKEN_LIFETIME = process.env['CSOL_OPENBADGER_TOKEN_LIFETIME'] || 10000;
|
|
|
|
|
2013-05-16 23:46:44 +04:00
|
|
|
if (!ENDPOINT)
|
|
|
|
throw new Error('Must specify CSOL_OPENBADGER_URL in the environment');
|
2013-05-29 04:56:49 +04:00
|
|
|
if (!JWT_SECRET)
|
|
|
|
throw new Error('Must specify CSOL_OPENBADGER_SECRET in the environment');
|
2013-05-13 22:27:20 +04:00
|
|
|
|
|
|
|
function normalizeBadge (badge, id) {
|
|
|
|
if (!id)
|
|
|
|
id = badge.shortname;
|
|
|
|
|
|
|
|
if (!badge.id)
|
|
|
|
badge.id = id;
|
|
|
|
|
|
|
|
if (!badge.url)
|
2013-05-15 00:18:25 +04:00
|
|
|
badge.url = '/earn/' + badge.id;
|
2013-05-13 22:27:20 +04:00
|
|
|
|
|
|
|
return badge;
|
|
|
|
}
|
|
|
|
|
2013-05-31 20:21:00 +04:00
|
|
|
function normalizeBadgeInstance (badge, id) {
|
|
|
|
/* This is dumb, but let's us reuse current templates to
|
|
|
|
build out a single-level object. */
|
2013-06-02 02:44:36 +04:00
|
|
|
_.extend(badge, badge.badgeClass);
|
2013-05-31 20:21:00 +04:00
|
|
|
|
|
|
|
if (!badge.url)
|
|
|
|
badge.url = '/mybadges/' + id;
|
|
|
|
|
2013-06-02 02:44:36 +04:00
|
|
|
return badge;
|
2013-05-31 20:21:00 +04:00
|
|
|
}
|
|
|
|
|
2013-05-15 00:18:25 +04:00
|
|
|
function normalizeProgram(program, id) {
|
|
|
|
if (!id)
|
|
|
|
id = program.shortname;
|
|
|
|
|
|
|
|
if (!program.id)
|
|
|
|
program.id = id;
|
|
|
|
|
|
|
|
if (!program.url)
|
2013-06-02 02:44:36 +04:00
|
|
|
program.url = '/explore/' + program.id;
|
2013-05-15 00:18:25 +04:00
|
|
|
|
|
|
|
return program;
|
|
|
|
}
|
|
|
|
|
2013-05-31 14:47:03 +04:00
|
|
|
function filterBadges(data, query) {
|
|
|
|
// TO DO - We should probably be a little less naive about this, and make sure
|
|
|
|
// that these values are from an allowed list
|
|
|
|
|
|
|
|
var category = query.category,
|
|
|
|
ageGroup = query.age,
|
|
|
|
program = query.program;
|
|
|
|
|
|
|
|
data = _.filter(data, function(item) {
|
|
|
|
if (category && !_.contains(item.categories, category))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (ageGroup && !_.contains(item.ageRange, ageGroup))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (program && item.program !== program)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2013-05-30 00:39:24 +04:00
|
|
|
function getJWTToken(email) {
|
|
|
|
var claims = {
|
|
|
|
prn: email,
|
|
|
|
exp: Date.now() + TOKEN_LIFETIME
|
|
|
|
};
|
|
|
|
return jwt.encode(claims, JWT_SECRET);
|
|
|
|
}
|
|
|
|
|
2013-05-15 23:25:41 +04:00
|
|
|
var openbadger = new Api(ENDPOINT, {
|
2013-05-16 01:42:24 +04:00
|
|
|
|
2013-05-15 23:25:41 +04:00
|
|
|
getBadges: {
|
|
|
|
func: function getBadges (query, callback) {
|
2013-05-17 18:52:43 +04:00
|
|
|
this.get('/badges', function(err, data) {
|
2013-05-15 23:25:41 +04:00
|
|
|
if (err)
|
|
|
|
return callback(err, data);
|
2013-05-15 00:18:25 +04:00
|
|
|
|
2013-05-15 23:25:41 +04:00
|
|
|
return callback(null, {
|
2013-05-16 01:42:24 +04:00
|
|
|
badges: _.map(data.badges, normalizeBadge)
|
2013-05-15 23:25:41 +04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2013-05-31 14:47:03 +04:00
|
|
|
filters: filterBadges,
|
2013-05-15 23:25:41 +04:00
|
|
|
paginate: true,
|
|
|
|
key: 'badges'
|
|
|
|
},
|
2013-05-15 00:18:25 +04:00
|
|
|
|
2013-05-15 23:25:41 +04:00
|
|
|
getBadge: function getBadge (query, callback) {
|
|
|
|
var id = query.id;
|
2013-05-15 00:18:25 +04:00
|
|
|
|
2013-05-15 23:25:41 +04:00
|
|
|
if (!id)
|
2013-05-16 17:19:30 +04:00
|
|
|
return callback(new errors.BadRequest('Invalid badge key'));
|
2013-05-15 00:18:25 +04:00
|
|
|
|
2013-05-17 18:52:43 +04:00
|
|
|
this.get('/badge/' + id, function(err, data) {
|
2013-05-15 23:25:41 +04:00
|
|
|
if (err)
|
|
|
|
return callback(err, data);
|
2013-05-15 00:18:25 +04:00
|
|
|
|
2013-05-16 01:42:24 +04:00
|
|
|
return callback(null, {
|
|
|
|
badge: normalizeBadge(data.badge, id)
|
2013-05-15 23:25:41 +04:00
|
|
|
});
|
2013-05-15 00:18:25 +04:00
|
|
|
});
|
2013-05-15 23:25:41 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
getPrograms: {
|
|
|
|
func: function getPrograms (query, callback) {
|
2013-05-17 18:52:43 +04:00
|
|
|
this.get('/programs', function(err, data) {
|
2013-05-15 23:25:41 +04:00
|
|
|
if (err)
|
|
|
|
return callback(err, data);
|
|
|
|
|
|
|
|
return callback(null, {
|
2013-05-16 01:42:24 +04:00
|
|
|
programs: _.map(data.programs, normalizeProgram)
|
2013-05-15 23:25:41 +04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
paginate: true,
|
|
|
|
key: 'programs'
|
|
|
|
},
|
|
|
|
|
|
|
|
getProgram: function getProgram (query, callback) {
|
|
|
|
var id = query.id;
|
2013-05-15 00:18:25 +04:00
|
|
|
|
2013-05-15 23:25:41 +04:00
|
|
|
if (!id)
|
2013-05-16 17:19:30 +04:00
|
|
|
return callback(new errors.BadRequest('Invalid program key'));
|
2013-05-15 23:25:41 +04:00
|
|
|
|
2013-05-17 18:52:43 +04:00
|
|
|
this.get('/program/' + id, function(err, data) {
|
2013-05-15 23:25:41 +04:00
|
|
|
if (err)
|
|
|
|
return callback(err, data);
|
|
|
|
|
2013-05-16 01:42:24 +04:00
|
|
|
return callback(null, {
|
|
|
|
program: normalizeProgram(data.program, id)
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2013-05-15 23:25:41 +04:00
|
|
|
|
2013-05-16 01:42:24 +04:00
|
|
|
getOrgs: function getOrgs (query, callback) {
|
2013-05-17 19:14:53 +04:00
|
|
|
this.get('/issuers/', function(err, data) {
|
2013-05-16 01:42:24 +04:00
|
|
|
if (err)
|
|
|
|
return callback(err, data);
|
2013-05-15 23:25:41 +04:00
|
|
|
|
2013-05-16 01:42:24 +04:00
|
|
|
return callback(null, {
|
|
|
|
orgs: _.values(data.issuers)
|
2013-05-15 23:25:41 +04:00
|
|
|
});
|
|
|
|
});
|
2013-05-29 04:56:49 +04:00
|
|
|
},
|
|
|
|
|
2013-05-30 22:00:19 +04:00
|
|
|
getUserBadges: {
|
|
|
|
func: function getUserBadges (query, callback) {
|
|
|
|
var email = query.session.user.email;
|
|
|
|
var params = {
|
|
|
|
auth: getJWTToken(email),
|
|
|
|
email: email
|
|
|
|
};
|
|
|
|
this.get('/user', { qs: params }, function(err, data) {
|
|
|
|
if (err)
|
|
|
|
return callback(err, data);
|
|
|
|
|
2013-06-02 02:44:36 +04:00
|
|
|
|
2013-05-31 20:21:00 +04:00
|
|
|
console.log(data);
|
|
|
|
badges = _.map(data.badges, normalizeBadgeInstance)
|
|
|
|
|
2013-05-30 22:00:19 +04:00
|
|
|
return callback(null, {
|
2013-05-31 20:21:00 +04:00
|
|
|
badges: badges.sort(function(a, b) {
|
|
|
|
return b.issuedOn - a.issuedOn;
|
|
|
|
})
|
2013-05-30 22:00:19 +04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
paginate: true,
|
|
|
|
key: 'badges'
|
|
|
|
},
|
|
|
|
|
2013-05-31 20:21:00 +04:00
|
|
|
getUserBadge: function getUserBadge (query, callback) {
|
|
|
|
var id = query.id;
|
|
|
|
|
|
|
|
var email = query.session.user.email;
|
|
|
|
var params = {
|
|
|
|
auth: getJWTToken(email),
|
|
|
|
email: email
|
|
|
|
};
|
|
|
|
|
|
|
|
this.get('/user/badge/' + id, { qs: params }, function(err, data) {
|
|
|
|
if (err)
|
|
|
|
return callback(err, data);
|
|
|
|
|
|
|
|
return callback(null, {
|
|
|
|
badge: normalizeBadgeInstance(data.badge, id)
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-05-30 00:39:24 +04:00
|
|
|
getBadgeFromCode: function getBadgeFromCode (query, callback) {
|
2013-05-29 04:56:49 +04:00
|
|
|
var email = query.email;
|
|
|
|
var code = query.code;
|
2013-05-30 00:39:24 +04:00
|
|
|
var params = {
|
|
|
|
auth: getJWTToken(email),
|
|
|
|
email: email,
|
|
|
|
code: code,
|
2013-05-29 04:56:49 +04:00
|
|
|
};
|
2013-05-30 00:39:24 +04:00
|
|
|
this.get('/unclaimed', { qs: params }, function(err, data) {
|
|
|
|
return callback(err, data);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
claim: function claim (query, callback) {
|
|
|
|
var email = query.email;
|
|
|
|
var code = query.code;
|
2013-05-29 04:56:49 +04:00
|
|
|
var params = {
|
2013-05-30 00:39:24 +04:00
|
|
|
auth: getJWTToken(email),
|
2013-05-29 04:56:49 +04:00
|
|
|
email: email,
|
|
|
|
code: code,
|
|
|
|
};
|
|
|
|
this.post('/claim', { json: params }, function(err, data) {
|
|
|
|
return callback(err, data);
|
|
|
|
});
|
2013-05-30 22:00:19 +04:00
|
|
|
},
|
2013-05-15 00:18:25 +04:00
|
|
|
});
|
2013-05-15 23:25:41 +04:00
|
|
|
|
|
|
|
module.exports = openbadger;
|