Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | 1x 311x 311x 2x 309x 311x 2743x 2743x 614x 614x 311x 2x 2x 2x 2x 309x 309x 309x 309x 1x | 'use strict';
/** Type definition for authorization.
* @typedef {Object} Authorization
* @property {Array | undefined} permissions
*/
/** Type definition for ClientProfile.
* @typedef {Object} ClientProfile
* @property {boolean} isClient
* @property {string} clientId
* @property {Object} clientMetadata
* @property {Authorization} authorization
*/
/** Type definition for UserProfile.
* @typedef {Object} UserProfile
* @property {string} userId
* @property {string} email
* @property {Object} userMetadata
* @property {Authorization} authorization
*/
const { deepMapKeysToCamel } = use('C2C/Helpers');
class Auth {
/**
* Generate client/user profile from jwt payload
* @param {*} tokenPayload
* @returns { ClientProfile | UserProfile }
*/
generateProfile(tokenPayload) {
const isClient = tokenPayload.gty === 'client-credentials';
if (isClient) {
return this._generateClientProfile(tokenPayload);
}
return this._generateUserProfile(tokenPayload);
}
/**
* Remove the public namespaces and convert properties key to camelcase
* @param {Object} tokenPayload
* @returns {Object}
*/
_normalizeTokenPayload(tokenPayload) {
Object.keys(tokenPayload).forEach(function (k) {
const removedNamespaceKey = k.replace('https://', '');
if (removedNamespaceKey !== k) {
tokenPayload[removedNamespaceKey] = tokenPayload[k];
delete tokenPayload[k];
}
});
return deepMapKeysToCamel(tokenPayload);
}
/**
* Generate the client profile
* @param {Object} tokenPayload
* @returns {ClientProfile}
*/
_generateClientProfile(tokenPayload) {
const payload = this._normalizeTokenPayload(tokenPayload);
const clientMetadata = payload.clientMetadata || {};
const profile = {
clientId: payload.sub,
clientMetadata,
authorization: {
isClient: true,
permissions: payload.permissions || [],
},
};
return profile;
}
/**
* Generate the user profile
* @param {Object} tokenPayload
* @returns {UserProfile}
*/
_generateUserProfile(tokenPayload) {
const payload = this._normalizeTokenPayload(tokenPayload);
const userMetadata = payload.userMetadata || {};
const profile = {
userId: payload.sub,
email: payload.email,
userMetadata,
authorization: {
...(payload.authorization || {}),
},
};
return profile;
}
}
module.exports = Auth;
|