aboutsummaryrefslogtreecommitdiff
path: root/services/users/users.hooks.ts
diff options
context:
space:
mode:
authorEugene Sokolov <eug-vs@keemail.me>2020-07-05 13:38:33 +0300
committerGitHub <noreply@github.com>2020-07-05 13:38:33 +0300
commit96398e544463651413ea80821c09ae7573f46cb1 (patch)
treedf77b6f65247d87dbfde8000e918c83985624fc3 /services/users/users.hooks.ts
parentda76bb1bd8797c4e1dc657c489f597bf64918ceb (diff)
parent1c2f3c9e5b39826266d64f4227e53fff139ea948 (diff)
downloadwhich-api-96398e544463651413ea80821c09ae7573f46cb1.tar.gz
Merge pull request #17 from which-ecosystem/security
Prepare release-level backend security
Diffstat (limited to 'services/users/users.hooks.ts')
-rw-r--r--services/users/users.hooks.ts16
1 files changed, 13 insertions, 3 deletions
diff --git a/services/users/users.hooks.ts b/services/users/users.hooks.ts
index 48843be..125f418 100644
--- a/services/users/users.hooks.ts
+++ b/services/users/users.hooks.ts
@@ -1,7 +1,9 @@
import _ from 'lodash';
import { hooks } from '@feathersjs/authentication-local';
-import { discard } from 'feathers-hooks-common';
+import { discard, disallow } from 'feathers-hooks-common';
import { HookContext } from '@feathersjs/feathers';
+import { NotAuthenticated } from '@feathersjs/errors';
+import requireAuth from '../../hooks/requireAuth';
const hashPassword = hooks.hashPassword('password');
@@ -12,6 +14,13 @@ const ignoreCaseRegex = async (context: HookContext): Promise<HookContext> => {
return context;
};
+const compareUser = async (context: HookContext): Promise<HookContext> => {
+ if (context.arguments[0] !== context.params.user._id) {
+ throw new NotAuthenticated('You can only PATCH/UPDATE your own user!');
+ }
+ return context;
+};
+
export default {
after: {
all: hooks.protect('password'),
@@ -20,8 +29,9 @@ export default {
before: {
find: ignoreCaseRegex,
create: hashPassword,
- patch: hashPassword,
- update: hashPassword
+ patch: [hashPassword, requireAuth, compareUser],
+ update: [hashPassword, requireAuth, compareUser],
+ remove: disallow('external')
}
};