CSOL-site/openbadger.js

233 строки
5.2 KiB
JavaScript
Исходник Обычный вид История

const Api = require('./api');
2013-05-16 17:19:30 +04:00
const errors = require('./lib/errors');
const _ = require('underscore');
const jwt = require('jwt-simple');
const ENDPOINT = process.env['CSOL_OPENBADGER_URL'];
const JWT_SECRET = process.env['CSOL_OPENBADGER_SECRET'];
const TOKEN_LIFETIME = process.env['CSOL_OPENBADGER_TOKEN_LIFETIME'] || 10000;
if (!ENDPOINT)
throw new Error('Must specify CSOL_OPENBADGER_URL in the environment');
if (!JWT_SECRET)
throw new Error('Must specify CSOL_OPENBADGER_SECRET in the environment');
function normalizeBadge (badge, id) {
if (!id)
id = badge.shortname;
if (!badge.id)
badge.id = id;
if (!badge.url)
badge.url = '/earn/' + badge.id;
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
}
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;
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;
}
function getJWTToken(email) {
var claims = {
prn: email,
exp: Date.now() + TOKEN_LIFETIME
};
return jwt.encode(claims, JWT_SECRET);
}
var openbadger = new Api(ENDPOINT, {
getBadges: {
func: function getBadges (query, callback) {
this.get('/badges', function(err, data) {
if (err)
return callback(err, data);
return callback(null, {
badges: _.map(data.badges, normalizeBadge)
});
});
},
2013-05-31 14:47:03 +04:00
filters: filterBadges,
paginate: true,
key: 'badges'
},
getBadge: function getBadge (query, callback) {
var id = query.id;
if (!id)
2013-05-16 17:19:30 +04:00
return callback(new errors.BadRequest('Invalid badge key'));
this.get('/badge/' + id, function(err, data) {
if (err)
return callback(err, data);
return callback(null, {
badge: normalizeBadge(data.badge, id)
});
});
},
getPrograms: {
func: function getPrograms (query, callback) {
this.get('/programs', function(err, data) {
if (err)
return callback(err, data);
return callback(null, {
programs: _.map(data.programs, normalizeProgram)
});
});
},
paginate: true,
key: 'programs'
},
getProgram: function getProgram (query, callback) {
var id = query.id;
if (!id)
2013-05-16 17:19:30 +04:00
return callback(new errors.BadRequest('Invalid program key'));
this.get('/program/' + id, function(err, data) {
if (err)
return callback(err, data);
return callback(null, {
program: normalizeProgram(data.program, id)
});
});
},
getOrgs: function getOrgs (query, callback) {
this.get('/issuers/', function(err, data) {
if (err)
return callback(err, data);
return callback(null, {
orgs: _.values(data.issuers)
});
});
},
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)
return callback(null, {
2013-05-31 20:21:00 +04:00
badges: badges.sort(function(a, b) {
return b.issuedOn - a.issuedOn;
})
});
});
},
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)
});
});
},
getBadgeFromCode: function getBadgeFromCode (query, callback) {
var email = query.email;
var code = query.code;
var params = {
auth: getJWTToken(email),
email: email,
code: code,
};
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;
var params = {
auth: getJWTToken(email),
email: email,
code: code,
};
this.post('/claim', { json: params }, function(err, data) {
return callback(err, data);
});
},
});
module.exports = openbadger;