UNPKG

mozu-node-sdk

Version:

Mozu JavaScript SDK for Node.js and Arc.js environments

56 lines (51 loc) 1.72 kB
'use strict'; var assert = require('assert'); function isExpired(ticket) { var ungraceperiod = 60000; var compareDate = new Date(); compareDate.setTime(compareDate.getTime() + ungraceperiod); return new Date(ticket.refreshTokenExpiration) < compareDate; } function generateCacheKey(claimtype, context) { var cmps; if (!process.env.mozuHosted) { assert(context.appKey, "No application key in context!"); cmps = [context.appKey]; } else { cmps = ['mozuHosted']; } switch (claimtype) { case "developer": assert(context.developerAccount && context.developerAccount.emailAddress, "No developer account email address in context!"); cmps.push(context.developerAccount.emailAddress, context.developerAccountId); break; case "admin-user": assert(context.tenant, "No tenant in context!"); assert(context.adminUser && context.adminUser.emailAddress, "No admin user email address in context!"); cmps.push(context.tenant, context.adminUser.emailAddress); break; default: break; } return cmps.join(); } module.exports = function InMemoryAuthCache() { var claimsCaches = { application: {}, developer: {}, 'admin-user': {} }; return { get: function get(claimtype, context, callback) { var ticket = claimsCaches[claimtype][generateCacheKey(claimtype, context)]; setImmediate(function () { callback(null, ticket && !isExpired(ticket) && ticket || undefined); }); }, set: function set(claimtype, context, ticket, callback) { claimsCaches[claimtype][generateCacheKey(claimtype, context)] = ticket; setImmediate(callback); }, constructor: InMemoryAuthCache }; };