@sap-cloud-sdk/core
Version:
SAP Cloud SDK for JavaScript core
124 lines • 4.95 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.userFromJwt = exports.userId = exports.userScopes = exports.userEmail = exports.userName = exports.userFamilyName = exports.userGivenName = exports.mappingUserFields = exports.customAttributes = void 0;
var jwt_1 = require("./jwt");
/**
* Extracts the custom attributes from the JWT.
* @param jwtPayload - Token payload to read the custom attributes from.
* @returns Custom attributes added by the XSUAA service to the issued JWT.
*/
function customAttributes(jwtPayload) {
if (jwtPayload[exports.mappingUserFields.customAttributes.keyInJwt]) {
return (0, jwt_1.readPropertyWithWarn)(jwtPayload, exports.mappingUserFields.customAttributes.keyInJwt);
}
return new Map();
}
exports.customAttributes = customAttributes;
/**
* Mapping between key name in the User and key name in decoded JWT and the
*/
exports.mappingUserFields = {
id: { keyInJwt: 'user_id', extractorFunction: userId },
userName: { keyInJwt: 'user_name', extractorFunction: userName },
givenName: { keyInJwt: 'given_name', extractorFunction: userGivenName },
familyName: { keyInJwt: 'family_name', extractorFunction: userFamilyName },
email: { keyInJwt: 'email', extractorFunction: userEmail },
scopes: { keyInJwt: 'scope', extractorFunction: userScopes },
customAttributes: {
keyInJwt: 'xs.user.attributes',
extractorFunction: customAttributes
}
};
/**
* Get the user's given name from the JWT payload.
* @param jwtPayload - Token payload to read the user's given name from.
* @returns The user's given name if available.
*/
function userGivenName(jwtPayload) {
if (exports.mappingUserFields.givenName) {
return (0, jwt_1.readPropertyWithWarn)(jwtPayload, exports.mappingUserFields.givenName.keyInJwt);
}
}
exports.userGivenName = userGivenName;
/**
* Get the user's family name from the JWT payload.
* @param jwtPayload - Token payload to read the user's family from.
* @returns The user's family name if available.
*/
function userFamilyName(jwtPayload) {
if (exports.mappingUserFields.familyName) {
return (0, jwt_1.readPropertyWithWarn)(jwtPayload, exports.mappingUserFields.familyName.keyInJwt);
}
}
exports.userFamilyName = userFamilyName;
/**
* Get the user name from the JWT payload.
* @param jwtPayload - Token payload to read the user name from.
* @returns The user name if available.
*/
function userName(jwtPayload) {
return (0, jwt_1.readPropertyWithWarn)(jwtPayload, exports.mappingUserFields.userName.keyInJwt);
}
exports.userName = userName;
/**
* Get the user's e-mail address from the JWT payload.
* @param jwtPayload - Token payload to read the user e-mail address from.
* @returns The user's e-mail address if available.
*/
function userEmail(jwtPayload) {
if (exports.mappingUserFields.email) {
return (0, jwt_1.readPropertyWithWarn)(jwtPayload, exports.mappingUserFields.email.keyInJwt);
}
}
exports.userEmail = userEmail;
/**
* Get the user's scopes from the JWT payload.
* @param jwtPayload - Token payload to read the user's scopes from.
* @returns The user's scopes if available.
*/
function userScopes(jwtPayload) {
if (!(jwtPayload.scope instanceof Array && jwtPayload.scope.length)) {
return [];
}
return jwtPayload.scope
.map(function (s) { return (s.includes('.') ? s.substr(s.indexOf('.') + 1, s.length) : s); })
.map(function (s) { return ({ name: s }); });
}
exports.userScopes = userScopes;
/**
* Get the user id from the JWT payload.
* @param jwtPayload - Token payload to read the user id from.
* @returns The user id if available.
*/
function userId(jwtPayload) {
return (0, jwt_1.readPropertyWithWarn)(jwtPayload, exports.mappingUserFields.id.keyInJwt);
}
exports.userId = userId;
function hasScopeWrapper(scopes) {
return function (scope) {
return scopes.find(function (scopeFromList) { return scopeFromList.name === scope.name; }) !==
undefined;
};
}
/**
* Creates a user object from the decoded JWT.
* Throws an error if no `id` or `userName` property is present on the JWT payload.
* @param jwtPayload - Token payload to get the user from.
* @returns Representation of the user.
*/
function userFromJwt(jwtPayload) {
(0, jwt_1.checkMandatoryValue)('id', exports.mappingUserFields, jwtPayload);
(0, jwt_1.checkMandatoryValue)('userName', exports.mappingUserFields, jwtPayload);
return {
id: userId(jwtPayload),
givenName: userGivenName(jwtPayload),
familyName: userFamilyName(jwtPayload),
email: userEmail(jwtPayload),
userName: userName(jwtPayload),
scopes: userScopes(jwtPayload),
customAttributes: customAttributes(jwtPayload),
hasScope: hasScopeWrapper(userScopes(jwtPayload))
};
}
exports.userFromJwt = userFromJwt;
//# sourceMappingURL=user.js.map
;