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

fix: strong password type #702

Merged
merged 1 commit into from
Nov 12, 2024
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
5 changes: 3 additions & 2 deletions src/member/password.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { isStrongPassword } from '@/validation/isPasswordStrong.js';

export const isPasswordStrong = (password: string) =>
isStrongPassword(password, {
export const isPasswordStrong = (password: string): boolean => {
return isStrongPassword(password, {
minLength: 8,
minLowercase: 1,
minUppercase: 1,
minNumbers: 1,
minSymbols: 0,
});
};
25 changes: 21 additions & 4 deletions src/validation/isPasswordStrong.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const defaultOptions = {
minUppercase: 1,
minNumbers: 1,
minSymbols: 1,
returnScore: false,
pointsPerUnique: 1,
pointsPerRepeat: 0.5,
pointsForContainingLower: 10,
Expand Down Expand Up @@ -83,15 +82,33 @@ function scorePassword(
return points;
}

/**
* Score the password based on the requirements
* @param str input password string
* @param options define password requirements that need to be met
* @returns the password score
*/
export function getPasswordScore(
str: string,
options: Partial<PasswordOptions>,
): number {
const analysis = analyzePassword(str);
const newOptions = merge(options || {}, defaultOptions);
return scorePassword(analysis, newOptions);
}

/**
* Validate if password follows the requirements
* @param str input password string
* @param options defines requirements to be met by the password
* @returns whether the password is strong according to the requirements
*/
export function isStrongPassword(
str: string,
options: Partial<PasswordOptions>,
) {
const analysis = analyzePassword(str);
const newOptions = merge(options || {}, defaultOptions);
if (newOptions.returnScore) {
return scorePassword(analysis, newOptions);
}
return (
analysis.length >= newOptions.minLength &&
analysis.lowercaseCount >= newOptions.minLowercase &&
Expand Down