Skip to content

Commit

Permalink
feat: add skipPasswordHash option
Browse files Browse the repository at this point in the history
  • Loading branch information
jd1378 committed Mar 12, 2024
1 parent 7b73637 commit 70e0939
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/methods/password-change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export default async function passwordChange (
app,
identifyUserProps,
passwordField,
skipPasswordHash,
sanitizeUserForClient,
service,
notifier
Expand Down Expand Up @@ -67,7 +68,7 @@ export default async function passwordChange (
}

const patchedUser = await usersService.patch(user[usersServiceId] as Id, {
password: await hashPassword(app, password, passwordField)
password: skipPasswordHash ? password : await hashPassword(app, password, passwordField)
}, Object.assign({}, params)) as User;

const userResult = await notify(notifier, 'passwordChange', patchedUser, notifierOptions);
Expand Down
3 changes: 2 additions & 1 deletion src/methods/reset-password.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ async function resetPassword (
skipIsVerifiedCheck,
reuseResetToken,
passwordField,
skipPasswordHash,
sanitizeUserForClient,
notifier
} = options;
Expand Down Expand Up @@ -160,7 +161,7 @@ async function resetPassword (
}

const patchedUser = await usersService.patch(user[usersServiceId] as Id, {
[passwordField]: await hashPassword(app, password, passwordField),
[passwordField]: skipPasswordHash ? password : await hashPassword(app, password, passwordField),
resetExpires: null,
resetAttempts: null,
resetToken: null,
Expand Down
8 changes: 5 additions & 3 deletions src/methods/verify-signup-set-password.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ async function verifySignupSetPassword (
const {
app,
passwordField,
skipPasswordHash,
sanitizeUserForClient,
service,
notifier
Expand Down Expand Up @@ -117,6 +118,7 @@ async function verifySignupSetPassword (
isDateAfterNow(user.verifyExpires),
user.verifyChanges || {},
password,
skipPasswordHash,
params
);

Expand Down Expand Up @@ -149,17 +151,17 @@ async function verifySignupSetPassword (
isVerified: boolean,
verifyChanges: VerifyChanges,
password: string,
skipPasswordHash: boolean,
params?: Params
): Promise<User> {
const hashedPassword = await hashPassword(app, password, passwordField);


const patchData = Object.assign({}, verifyChanges || {}, {
isVerified,
verifyToken: null,
verifyShortToken: null,
verifyExpires: null,
verifyChanges: {},
[passwordField]: hashedPassword
[passwordField]: skipPasswordHash ? password : await hashPassword(app, password, passwordField)
});

const result = await usersService.patch(
Expand Down
1 change: 1 addition & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const optionsDefault: AuthenticationManagementServiceOptions = {
sanitizeUserForClient,
skipIsVerifiedCheck: false,
passwordField: 'password',
skipPasswordHash: false,
passParams: undefined
};

Expand Down
1 change: 1 addition & 0 deletions src/services/PasswordChangeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class PasswordChangeService
'identifyUserProps',
'sanitizeUserForClient',
'passwordField',
'skipPasswordHash',
'passParams'
]);
this.options = Object.assign(defaultOptions, options);
Expand Down
1 change: 1 addition & 0 deletions src/services/ResetPwdLongService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class ResetPwdLongService
'reuseResetToken',
'sanitizeUserForClient',
'passwordField',
'skipPasswordHash',
'passParams'
]);
this.options = Object.assign(defaultOptions, options);
Expand Down
1 change: 1 addition & 0 deletions src/services/ResetPwdShortService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class ResetPwdShortService
'reuseResetToken',
'sanitizeUserForClient',
'passwordField',
'skipPasswordHash',
'identifyUserProps',
'passParams'
]);
Expand Down
1 change: 1 addition & 0 deletions src/services/VerifySignupSetPasswordLongService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class VerifySignupSetPasswordLongService
'notifier',
'sanitizeUserForClient',
'passwordField',
'skipPasswordHash',
'passParams'
]);
this.options = Object.assign(defaultOptions, options);
Expand Down
1 change: 1 addition & 0 deletions src/services/VerifySignupSetPasswordShortService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class VerifySignupSetPasswordShortService
'notifier',
'sanitizeUserForClient',
'passwordField',
'skipPasswordHash',
'identifyUserProps',
'passParams'
]);
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ export interface AuthenticationManagementServiceOptions {
/** Property name of the password field on your `'/users'` service
* @default 'password' */
passwordField: string
/** Should we skip hashing password for `passwordField` ? If `true`, password won't be hashed by feathers-authentication-management when patching the user. This must be set to `true` if you are hashing your password field using resolvers. */
skipPasswordHash: boolean
/** Pass params from f-a-m service to `/users` service */
passParams: (params) => Params | Promise<Params>
}
Expand All @@ -139,6 +141,7 @@ export type VerifySignupSetPasswordLongServiceOptions = Pick<AuthenticationManag
'sanitizeUserForClient' |
'notifier' |
'passwordField' |
'skipPasswordHash' |
'passParams'>;
export type VerifySignupSetPasswordOptions = VerifySignupSetPasswordLongServiceOptions & { app: Application };

Expand All @@ -148,6 +151,7 @@ export type PasswordChangeServiceOptions = Pick<AuthenticationManagementServiceO
'notifier' |
'sanitizeUserForClient' |
'passwordField' |
'skipPasswordHash' |
'passParams'>;
export type PasswordChangeOptions = PasswordChangeServiceOptions & { app: Application };

Expand All @@ -162,6 +166,7 @@ export type ResetPasswordServiceOptions = Pick<AuthenticationManagementServiceOp
'notifier' |
'sanitizeUserForClient' |
'passwordField' |
'skipPasswordHash' |
'passParams'>;
export type ResetPasswordOptions = ResetPasswordServiceOptions & { app: Application };

Expand Down
1 change: 1 addition & 0 deletions test/scaffolding.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const optionsDefault: AuthenticationManagementServiceOptions = {
sanitizeUserForClient: sanitizeUserForClient,
skipIsVerifiedCheck: false,
passwordField: "password",
skipPasswordHash: false,
passParams: async (params) => {
let { provider: _, query: __, ...passedParams } = params;
return passedParams;
Expand Down

0 comments on commit 70e0939

Please sign in to comment.