|
| 1 | +const AttributeReference = require('./attributeReference'); |
| 2 | + |
| 3 | +function ContextFilter(config) { |
| 4 | + const filter = {}; |
| 5 | + |
| 6 | + const allAttributesPrivate = config.allAttributesPrivate; |
| 7 | + const privateAttributes = config.privateAttributes || []; |
| 8 | + |
| 9 | + // These attributes cannot be removed via a private attribute. |
| 10 | + const protectedAttributes = ['key', 'kind', '_meta', 'transient']; |
| 11 | + |
| 12 | + const legacyTopLevelCopyAttributes = ['name', 'ip', 'firstName', 'lastName', 'email', 'avatar', 'country']; |
| 13 | + |
| 14 | + /** |
| 15 | + * For the given context and configuration get a list of attributes to filter. |
| 16 | + * @param {Object} context |
| 17 | + * @returns {string[]} A list of the attributes to filter. |
| 18 | + */ |
| 19 | + const getAttributesToFilter = context => |
| 20 | + (allAttributesPrivate |
| 21 | + ? Object.keys(context) |
| 22 | + : [...privateAttributes, ...((context._meta && context._meta.privateAttributes) || [])] |
| 23 | + ).filter(attr => !protectedAttributes.some(protectedAttr => AttributeReference.compare(attr, protectedAttr))); |
| 24 | + |
| 25 | + /** |
| 26 | + * @param {Object} context |
| 27 | + * @returns {Object} A copy of the context with private attributes removed, |
| 28 | + * and the redactedAttributes meta populated. |
| 29 | + */ |
| 30 | + const filterSingleKind = context => { |
| 31 | + if (typeof context !== 'object' || context === null || Array.isArray(context)) { |
| 32 | + return undefined; |
| 33 | + } |
| 34 | + |
| 35 | + const { cloned, excluded } = AttributeReference.cloneExcluding(context, getAttributesToFilter(context)); |
| 36 | + cloned.key = String(cloned.key); |
| 37 | + if (excluded.length) { |
| 38 | + if (!cloned._meta) { |
| 39 | + cloned._meta = {}; |
| 40 | + } |
| 41 | + cloned._meta.redactedAttributes = excluded; |
| 42 | + } |
| 43 | + if (cloned._meta) { |
| 44 | + if (cloned._meta.secondary === null) { |
| 45 | + delete cloned._meta.secondary; |
| 46 | + } |
| 47 | + if (cloned._meta.secondary !== undefined) { |
| 48 | + cloned._meta.secondary = String(cloned._meta.secondary); |
| 49 | + } |
| 50 | + delete cloned._meta['privateAttributes']; |
| 51 | + if (Object.keys(cloned._meta).length === 0) { |
| 52 | + delete cloned._meta; |
| 53 | + } |
| 54 | + } |
| 55 | + // Make sure transient is boolean if present. |
| 56 | + // Null counts as present, and would be falsy, which is the default. |
| 57 | + if (cloned.transient !== undefined) { |
| 58 | + cloned.transient = !!cloned.transient; |
| 59 | + } |
| 60 | + |
| 61 | + return cloned; |
| 62 | + }; |
| 63 | + |
| 64 | + /** |
| 65 | + * @param {Object} context |
| 66 | + * @returns {Object} A copy of the context with the private attributes removed, |
| 67 | + * and the redactedAttributes meta populated for each sub-context. |
| 68 | + */ |
| 69 | + const filterMultiKind = context => { |
| 70 | + const filtered = { |
| 71 | + kind: context.kind, |
| 72 | + }; |
| 73 | + const contextKeys = Object.keys(context); |
| 74 | + |
| 75 | + for (const contextKey of contextKeys) { |
| 76 | + if (contextKey !== 'kind') { |
| 77 | + const filteredContext = filterSingleKind(context[contextKey]); |
| 78 | + if (filteredContext) { |
| 79 | + filtered[contextKey] = filteredContext; |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + return filtered; |
| 84 | + }; |
| 85 | + |
| 86 | + /** |
| 87 | + * Convert the LDUser object into an LDContext object. |
| 88 | + * @param {Object} user The LDUser to produce an LDContext for. |
| 89 | + * @returns {Object} A single kind context based on the provided user. |
| 90 | + */ |
| 91 | + const legacyToSingleKind = user => { |
| 92 | + const filtered = { |
| 93 | + /* Destructure custom items into the top level. |
| 94 | + Duplicate keys will be overridden by previously |
| 95 | + top level items. |
| 96 | + */ |
| 97 | + ...(user.custom || {}), |
| 98 | + |
| 99 | + // Implicity a user kind. |
| 100 | + kind: 'user', |
| 101 | + |
| 102 | + key: user.key, |
| 103 | + }; |
| 104 | + |
| 105 | + if (user.anonymous !== undefined) { |
| 106 | + filtered.transient = !!user.anonymous; |
| 107 | + } |
| 108 | + |
| 109 | + // Copy top level keys and convert them to strings. |
| 110 | + // Remove keys that may have been destructured from `custom`. |
| 111 | + for (const key of legacyTopLevelCopyAttributes) { |
| 112 | + delete filtered[key]; |
| 113 | + if (user[key] !== undefined && user[key] !== null) { |
| 114 | + filtered[key] = String(user[key]); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + if (user.privateAttributeNames !== undefined && user.privateAttributeNames !== null) { |
| 119 | + filtered._meta = filtered._meta || {}; |
| 120 | + // If any private attributes started with '/' we need to convert them to references, otherwise the '/' will |
| 121 | + // cause the literal to incorrectly be treated as a reference. |
| 122 | + filtered._meta.privateAttributes = user.privateAttributeNames.map( |
| 123 | + literal => (literal.startsWith('/') ? AttributeReference.literalToReference(literal) : literal) |
| 124 | + ); |
| 125 | + } |
| 126 | + if (user.secondary !== undefined && user.secondary !== null) { |
| 127 | + filtered._meta = filtered._meta || {}; |
| 128 | + filtered._meta.secondary = String(user.secondary); |
| 129 | + } |
| 130 | + |
| 131 | + return filtered; |
| 132 | + }; |
| 133 | + |
| 134 | + filter.filter = context => { |
| 135 | + if (context.kind === undefined || context.kind === null) { |
| 136 | + return filterSingleKind(legacyToSingleKind(context)); |
| 137 | + } else if (context.kind === 'multi') { |
| 138 | + return filterMultiKind(context); |
| 139 | + } else { |
| 140 | + return filterSingleKind(context); |
| 141 | + } |
| 142 | + }; |
| 143 | + |
| 144 | + return filter; |
| 145 | +} |
| 146 | + |
| 147 | +module.exports = ContextFilter; |
0 commit comments