Skip to content

Commit

Permalink
feat(authentication-local): Add passwordHash property resolver (#2660)
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl committed Jun 7, 2022
1 parent 98f4260 commit b41279b
Show file tree
Hide file tree
Showing 9 changed files with 1,525 additions and 1,128 deletions.
2,590 changes: 1,469 additions & 1,121 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/authentication-local/package.json
Expand Up @@ -61,6 +61,7 @@
},
"devDependencies": {
"@feathersjs/memory": "^5.0.0-pre.23",
"@feathersjs/schema": "^5.0.0-pre.23",
"@types/bcryptjs": "^2.4.2",
"@types/lodash": "^4.14.182",
"@types/mocha": "^9.1.1",
Expand Down
6 changes: 6 additions & 0 deletions packages/authentication-local/src/hooks/hash-password.ts
Expand Up @@ -13,6 +13,12 @@ export interface HashPasswordOptions {
strategy?: string
}

/**
* @deprecated Use Feathers schema resolvers and the `passwordHash` resolver instead
* @param field
* @param options
* @returns
*/
export default function hashPassword(field: string, options: HashPasswordOptions = {}) {
if (!field) {
throw new Error('The hashPassword hook requires a field name option')
Expand Down
4 changes: 4 additions & 0 deletions packages/authentication-local/src/hooks/protect.ts
@@ -1,6 +1,10 @@
import omit from 'lodash/omit'
import { HookContext, NextFunction } from '@feathersjs/feathers'

/**
* @deprecated For reliable safe data representations use Feathers schema dispatch resolvers.
* See https://dove.docs.feathersjs.com/api/schema/resolvers.html#safe-data-resolvers for more information.
*/
export default (...fields: string[]) => {
const o = (current: any) => {
if (typeof current === 'object' && !Array.isArray(current)) {
Expand Down
21 changes: 20 additions & 1 deletion packages/authentication-local/src/index.ts
@@ -1,5 +1,24 @@
import { HookContext } from '@feathersjs/feathers'
import hashPassword from './hooks/hash-password'
import protect from './hooks/protect'
import { LocalStrategy } from './strategy'

export const hooks = { hashPassword, protect }
export { LocalStrategy } from './strategy'
export { LocalStrategy }

/**
* Returns as property resolver that hashes a given plain text password using a Local
* authentication strategy.
*
* @param options The authentication `service` and `strategy` name
* @returns
*/
export const passwordHash =
(options: { service?: string; strategy: string }) =>
async <H extends HookContext<any, any>>(value: string | undefined, _data: any, context: H) => {
const { app, params } = context
const authService = app.defaultAuthentication(options.service)
const localStrategy = authService.getStrategy(options.strategy) as LocalStrategy

return localStrategy.hashPassword(value, params)
}
19 changes: 17 additions & 2 deletions packages/authentication-local/test/strategy.test.ts
@@ -1,8 +1,9 @@
import assert from 'assert'
import omit from 'lodash/omit'
import { Application } from '@feathersjs/feathers'
import { Application, HookContext } from '@feathersjs/feathers'
import { resolve } from '@feathersjs/schema'

import { LocalStrategy } from '../src'
import { LocalStrategy, passwordHash } from '../src'
import { createApplication, ServiceTypes } from './fixture'

describe('@feathersjs/authentication-local/strategy', () => {
Expand Down Expand Up @@ -180,4 +181,18 @@ describe('@feathersjs/authentication-local/strategy', () => {

assert.strictEqual(decoded.sub, `${user.id}`)
})

it('passwordHash property resolver', async () => {
const userResolver = resolve<{ password: string }, HookContext>({
properties: {
password: passwordHash({
strategy: 'local'
})
}
})

const resolvedData = await userResolver.resolve({ password: 'supersecret' }, { app } as HookContext)

assert.notStrictEqual(resolvedData.password, 'supersecret')
})
})
4 changes: 2 additions & 2 deletions packages/feathers/package.json
Expand Up @@ -42,8 +42,8 @@
"*.js"
],
"scripts": {
"write-version": "node -e \"console.log('export default \\'' + require('./package.json').version + '\\';')\" > src/version.ts",
"reset-version": "node -e \"console.log('export default \\'development\\';')\" > src/version.ts",
"write-version": "node -e \"console.log('export default \\'' + require('./package.json').version + '\\'')\" > src/version.ts",
"reset-version": "node -e \"console.log('export default \\'development\\'')\" > src/version.ts",
"prepublish": "npm run compile",
"version": "npm run write-version",
"publish": "npm run reset-version",
Expand Down
2 changes: 1 addition & 1 deletion packages/feathers/src/version.ts
@@ -1 +1 @@
export default 'development';
export default 'development'
6 changes: 5 additions & 1 deletion packages/mongodb/src/index.ts
Expand Up @@ -5,7 +5,11 @@ import { MongoDbAdapter, MongoDBAdapterParams } from './adapter'
export * from './adapter'
export * from './error-handler'

export class MongoDBService<T = any, D = Partial<T>, P extends MongoDBAdapterParams = MongoDBAdapterParams>
export class MongoDBService<
T = any,
D = Partial<T>,
P extends MongoDBAdapterParams<any> = MongoDBAdapterParams
>
extends MongoDbAdapter<T, D, P>
implements ServiceMethods<T | Paginated<T>, D, P>
{
Expand Down

0 comments on commit b41279b

Please sign in to comment.