Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(authentication-local): Add passwordHash property resolver #2660

Merged
merged 1 commit into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default 'development';
export default 'development'
6 changes: 5 additions & 1 deletion packages/mongodb/src/index.ts
Original file line number Diff line number Diff line change
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