'use strict'; // use environment variable DEBUG with value 'xssec:*' for trace/error messages var debug = require('debug'); var debugTrace = debug('xssec:securitycontext'); var debugError = debug('xssec:securitycontext'); const {JwtTokenValidatorIAS} = require('../validator') debugError.log = console.error.bind(console); debugTrace.log = console.log.bind(console); module.exports.SecurityContext = function(config, configArr) { this.getConfigType = function () { return "IAS"; } var token = null; var tokenInfo = null; var userInfo = { logonName: '', givenName: '', familyName: '', email: '' }; this.getUserInfo = function() { return userInfo; } this.getAppToken = function () { return token; }; this.getTokenInfo = function () { return tokenInfo; } this.getLogonName = function () { return userInfo.logonName; }; this.getGivenName = function () { return userInfo.givenName; }; this.getFamilyName = function () { return userInfo.familyName; }; this.getEmail = function () { return userInfo.email; }; this.verifyToken = function (encodedToken, attributes, cb) { const validator = new JwtTokenValidatorIAS(configArr, config, attributes); validator.validateToken(encodedToken, function (err, tokenInfo) { if (err) { try { cb(err, null, tokenInfo); } catch(e) { debugError("xssec: Unhandled Exception in Callback"); debugError(e); } return; } tokenInfo = tokenInfo; token = encodedToken; const decodedToken = tokenInfo.getPayload(); let givenName, familyName; if (decodedToken.ext_attr) { givenName = decodedToken.ext_attr.given_name || null; familyName = decodedToken.ext_attr.family_name || null; } userInfo.givenName = givenName || decodedToken.given_name || ''; userInfo.familyName = familyName || decodedToken.family_name || ''; userInfo.email = decodedToken.email || ''; userInfo.logonName = decodedToken.user_name || decodedToken.email || decodedToken.user_uuid || ''; try { cb(null, this, tokenInfo); } catch(e) { debugError("xssec: Unhandled Exception in Callback"); debugError(e); } }.bind(this)); }; };