@sparkz-community/feathers-fingerprint
Advanced tools
Comparing version 0.1.2 to 0.1.3
const {JWTStrategy} = require('@feathersjs/authentication'); | ||
const {LocalStrategy} = require('@feathersjs/authentication-local'); | ||
const {callingParamsDefaults} = require('feathers-hooks-common'); | ||
const {packages: {lodash: {lget, lset, lomit, lpick, lmergeWith}}, logger, extensionUtils: {hookCustomizer}} = require('@iy4u/common-utils'); | ||
const { | ||
packages: {lodash: {lget, lset, lomit, lpick, lmergeWith}}, | ||
logger, | ||
extensionUtils: {hookCustomizer}, | ||
} = require('@iy4u/common-utils'); | ||
@@ -17,3 +21,3 @@ logger.level = process.env.NODE_ENV === 'production' ? 'info' : 'debug'; | ||
let FingerprintAuth = new CustomAuthService(app, configKey); | ||
// eslint-disable-next-line no-unused-vars | ||
@@ -25,3 +29,3 @@ class FingerprintPasswordLessStrategy extends LocalStrategy { | ||
const result = await this.findEntity(username, lomit(params, 'provider')); | ||
return { | ||
@@ -33,45 +37,41 @@ authentication: {strategy: this.name}, | ||
} | ||
FingerprintAuth.register('password-less', new FingerprintPasswordLessStrategy()); | ||
FingerprintAuth.register('local', new LocalStrategy()); | ||
FingerprintAuth.register('jwt', new JWTStrategy()); | ||
FingerprintAuth = extendStrategies(FingerprintAuth); | ||
app.use('/fingerprintAuth', FingerprintAuth); | ||
app.configure(expressOauth(app)); | ||
app.service('fingerprintAuth').hooks(lmergeWith({ | ||
before: { | ||
create: [], | ||
}, | ||
after: { | ||
create: [ | ||
// ctx => { | ||
// logger.info(ctx); | ||
// } | ||
context => { | ||
const {fingerprint} = context.result; | ||
const fingerprintAuthConfig = context.app.get('fingerprintAuth'); | ||
const defaultWhitelist = ['_id', 'visits', 'browser', 'os', 'platform', 'engine', 'userAgent', 'appVersion', 'ipInfo', 'components', 'createdAt', 'updatedAt', 'settings', 'updatedByHistory', 'external', 'nativePermissions']; | ||
let _fingerprint; | ||
let enforceWhitelist = lget(fingerprintAuthConfig, 'enforceWhitelist', true); | ||
let whitelistFingerprintFields = lget(fingerprintAuthConfig, 'whitelistFingerprintFields', []); | ||
if (enforceWhitelist || whitelistFingerprintFields.length) _fingerprint = lpick(fingerprint, whitelistFingerprintFields.concat(defaultWhitelist)); | ||
let blacklistFingerprintFields = lget(fingerprint, 'blacklistFingerprintFields', ['visitorId']); | ||
_fingerprint = lomit(_fingerprint, blacklistFingerprintFields); | ||
lset(context.params, 'configKey', configKey); | ||
context.dispatch = Object.assign({}, context.result, {fingerprint: _fingerprint}); | ||
return context; | ||
}, | ||
], | ||
}, after: { | ||
create: [context => { | ||
const {fingerprint} = context.result; | ||
const fingerprintAuthConfig = context.app.get('fingerprintAuth'); | ||
const defaultWhitelist = ['_id', 'visits', 'browser', 'os', 'platform', 'engine', 'userAgent', 'appVersion', 'ipInfo', 'components', 'createdAt', 'updatedAt', 'settings', 'updatedByHistory', 'external', 'nativePermissions']; | ||
let _fingerprint; | ||
let enforceWhitelist = lget(fingerprintAuthConfig, 'enforceWhitelist', true); | ||
let whitelistFingerprintFields = lget(fingerprintAuthConfig, 'whitelistFingerprintFields', []); | ||
if (enforceWhitelist || whitelistFingerprintFields.length) _fingerprint = lpick(fingerprint, whitelistFingerprintFields.concat(defaultWhitelist)); | ||
let blacklistFingerprintFields = lget(fingerprint, 'blacklistFingerprintFields', ['visitorId']); | ||
_fingerprint = lomit(_fingerprint, blacklistFingerprintFields); | ||
lset(context.params, 'configKey', configKey); | ||
context.dispatch = Object.assign({}, context.result, {fingerprint: _fingerprint}); | ||
return context; | ||
}], | ||
}, error: { | ||
create: [ | ||
// ctx => { | ||
// logger.error(ctx); | ||
// } | ||
], | ||
}, | ||
error: { | ||
create: [], | ||
}, | ||
}, extend_hooks, hookCustomizer)); | ||
}; |
const middleware = require('./middleware'); | ||
const services = require('./services'); | ||
const models = require('./models'); | ||
const hooks = require('./hooks'); | ||
@@ -9,4 +10,5 @@ const auth = require('./auth'); | ||
hooks, | ||
models, | ||
services, | ||
auth | ||
}; |
@@ -6,43 +6,4 @@ // fingerprints-model.js - A mongoose model | ||
const {packages: {lodash: {lunset, lmergeWith}}, extensionUtils: {schemaCustomizer}} = require('@iy4u/common-utils'); | ||
const {ipInfoSchema} = require('./common/common.schemas'); | ||
function ipInfo(app, {extend_schema = {}, schema_remove_paths = []} = {}) { | ||
const mongoose = app.get('mongoose') || app.get('mongooseClient') || require('mongoose'); | ||
const {Schema} = mongoose; | ||
const orig_schema = { | ||
ip: String, | ||
range: [Number], | ||
country: String, | ||
region: String, | ||
eu: String, | ||
timezone: String, | ||
city: String, | ||
lngLat: [Number], | ||
metro: Number, | ||
area: Number, | ||
}; | ||
schema_remove_paths.map(path => lunset(orig_schema, path)); | ||
return new Schema(lmergeWith(orig_schema, extend_schema, schemaCustomizer), { | ||
timestamps: true, | ||
}); | ||
} | ||
function visit(app, {extend_schema = {}, schema_remove_paths = []} = {}) { | ||
const mongoose = app.get('mongoose') || app.get('mongooseClient') || require('mongoose'); | ||
const {Schema} = mongoose; | ||
const orig_schema = { | ||
date: Date, | ||
confidence: { score: Number }, | ||
ipInfo: ipInfo(app), | ||
incognito: Boolean, | ||
referrer: String | ||
}; | ||
schema_remove_paths.map(path => lunset(orig_schema, path)); | ||
return new Schema(lmergeWith(orig_schema, extend_schema, schemaCustomizer), { | ||
timestamps: true, | ||
}); | ||
} | ||
module.exports = function (app, {connection, extend_schema = {}, schema_remove_paths = []} = {}) { | ||
@@ -57,3 +18,3 @@ const mongoose = app.get('mongoose') || app.get('mongooseClient') || require('mongoose'); | ||
visitorId: {type: String, unique: true}, | ||
visits: [{...visit(app).obj}], | ||
visits: [{type: Schema.Types.ObjectId, ref: 'visits'}], | ||
browserInfo: { | ||
@@ -80,3 +41,3 @@ browser: { | ||
appVersion: String, | ||
ipInfo: ipInfo(app), | ||
ipInfo: [ipInfoSchema(app)], | ||
components: { | ||
@@ -158,2 +119,4 @@ audio: {duration: Number, value: Number}, | ||
}, | ||
deleted: {type: Boolean, default: false, index: true}, | ||
deletedAt: {type: Date, default: null}, | ||
}; | ||
@@ -172,4 +135,2 @@ schema_remove_paths.map(path => lunset(orig_schema, path)); | ||
exports.visit = visit; | ||
exports.ipInfo =ipInfo; | ||
@@ -0,3 +1,11 @@ | ||
const {fastJoin} = require('feathers-hooks-common'); | ||
const { | ||
utils: { | ||
coreCall | ||
} | ||
} = require('@sparkz-community/common-server-lib'); | ||
const {packages: {lodash: {lset,lget}}} = require('@iy4u/common-utils'); | ||
const {discard,iff, isProvider, preventChanges} = require('feathers-hooks-common'); | ||
const {mapCreateToUpsert} = require('../../hooks'); | ||
// const { hashPassword,} = require('@feathersjs/authentication-local').hooks; | ||
@@ -8,2 +16,49 @@ | ||
// For implementations where fingerprint is associated with another service, eg devices: use local strategy | ||
const fingerprintResolvers = { | ||
joins: { | ||
visits: ($select, customParams = {}) => async (fingerprint, context) => { | ||
let visits = lget(fingerprint, 'visits', []); | ||
if (visits.length) { | ||
let params = { | ||
query: { | ||
$client: { | ||
...customParams, | ||
}, | ||
_id: { | ||
$in: lget(fingerprint, 'visits', []), | ||
}, | ||
}, | ||
}; | ||
if ($select) { | ||
lset(params, 'query.$select', $select); | ||
} | ||
const visits = await coreCall(context, 'visits').find({ | ||
...params, | ||
paginate: false, | ||
}) | ||
.catch(err => { | ||
console.error('error joining members to accounts: ' + err.message); | ||
}); | ||
lset(fingerprint, '_fastjoin.visits', visits); | ||
} | ||
}, | ||
}, | ||
}; | ||
function addToSetIpInfo(context){ | ||
if(!lget(context.data,'$addToSet.ipInfo') && lget(context.data,'ipInfo')) { | ||
const addToSetData = lget(context.data,'$addToSet',{}) | ||
for (const [key, value] of Object.entries(lget(context.data,'ipInfo'))) { | ||
lset(addToSetData,`ipInfo.${key}`,value) | ||
} | ||
delete context.data.ipInfo | ||
context.data= { | ||
...context.data, | ||
$addToSet:addToSetData | ||
} | ||
} | ||
return context; | ||
} | ||
module.exports = { | ||
@@ -15,3 +70,6 @@ before: { | ||
create: [ | ||
// hashPassword('visitorId') | ||
mapCreateToUpsert(context => { | ||
const { data } = context | ||
return { visitorId: data.visitorId } | ||
}), | ||
], | ||
@@ -35,3 +93,3 @@ update: [ | ||
), | ||
// hashPassword('visitorId'), | ||
addToSetIpInfo, | ||
], | ||
@@ -42,3 +100,17 @@ remove: [] | ||
after: { | ||
all: [], | ||
all: [ | ||
fastJoin( | ||
fingerprintResolvers, | ||
context => { | ||
let query = {}; | ||
if (lget(context, ['params', 'fingerprintResolversQuery'])) { | ||
Object.keys(fingerprintResolvers.joins).forEach(key => { | ||
let val = lget(context, ['params', 'fingerprintResolversQuery', key], false); | ||
if (val) lset(query, key, val); | ||
}); | ||
} | ||
return query; | ||
} | ||
), | ||
], | ||
find: [], | ||
@@ -45,0 +117,0 @@ get: [], |
@@ -19,7 +19,6 @@ // Initializes the `fingerprints` service on path `/fingerprints` | ||
paginate: app.get('paginate'), | ||
multi: true | ||
multi: ['create', 'patch'], | ||
}; | ||
if (typeof extend_class_fn === 'function') { | ||
const ExtendedClass = extend_class_fn(Fingerprints); | ||
@@ -26,0 +25,0 @@ // Initialize our service with any options it requires |
/* eslint-disable no-unused-vars */ | ||
const myIp = require('./my-ip/my-ip.service'); | ||
const fingerprints = require('./fingerprints/fingerprints.service'); | ||
const visits= require('./visits/visits.service'); | ||
const services = { | ||
myIp, | ||
fingerprints | ||
fingerprints, | ||
visits | ||
}; | ||
@@ -9,0 +11,0 @@ |
{ | ||
"name": "@sparkz-community/feathers-fingerprint", | ||
"description": "", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"homepage": "https://gitlab.com/sparkz-community/general-packages/feathers-fingerprint", | ||
@@ -52,2 +52,3 @@ "main": "lib/", | ||
"@iy4u/common-utils": "^0.3.1", | ||
"@sparkz-community/common-server-lib": "^0.2.9", | ||
"chai": "^3.5.0", | ||
@@ -74,6 +75,7 @@ "eslint": "^6.8.0", | ||
"peerDependencies": { | ||
"@iy4u/common-utils": "0.x", | ||
"@feathersjs/authentication": "4.x", | ||
"@feathersjs/authentication-local": "4.x", | ||
"@feathersjs/authentication-oauth": "4.x", | ||
"@iy4u/common-utils": "0.x", | ||
"@sparkz-community/common-server-lib": "0.x", | ||
"feathers-hooks-common": "5.x", | ||
@@ -80,0 +82,0 @@ "feathers-memory": "4.x", |
1035861
275
8312
16
16