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: use password regex [DHIS2-15704] #1469

Merged
merged 7 commits into from
Feb 6, 2025
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
14 changes: 8 additions & 6 deletions i18n/en.pot
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ msgstr ""
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
"POT-Creation-Date: 2025-01-29T12:48:34.727Z\n"
"PO-Revision-Date: 2025-01-29T12:48:34.727Z\n"
"POT-Creation-Date: 2025-02-05T15:02:02.227Z\n"
"PO-Revision-Date: 2025-02-05T15:02:02.227Z\n"

msgid "Never"
msgstr "Never"
Expand Down Expand Up @@ -156,11 +156,13 @@ msgid "New password"
msgstr "New password"

msgid ""
"Password should be at least 8 characters with at least 1 digit, 1 uppercase "
"letter and 1 special character"
"Password should be between {{minPasswordLength}} and {{maxPasswordLength}} "
"characters long, with at least one lowercase character, one uppercase "
"character, one number, and one special character."
msgstr ""
"Password should be at least 8 characters with at least 1 digit, 1 uppercase "
"letter and 1 special character"
"Password should be between {{minPasswordLength}} and {{maxPasswordLength}} "
"characters long, with at least one lowercase character, one uppercase "
"character, one number, and one special character."

msgid "Repeat new password"
msgstr "Repeat new password"
Expand Down
27 changes: 24 additions & 3 deletions src/account/AccountEditor.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import React, { Component } from 'react'
import appActions from '../app.actions.js'
import i18n from '../locales/index.js'
import accountActions from './account.actions.js'
import isValidPassword from './isValidPassword.js'

const styles = {
notification: {
Expand All @@ -16,6 +15,10 @@ const styles = {
},
}

// from our frontend validator https://github.com/dhis2/ui/blob/master/collections/forms/src/validators/dhis2Password.js
const DEFAULT_MIN_PASSWORD_LENGTH = 8
const DEFAULT_MAX_PASSWORD_LENGTH = 72

class AccountEditor extends Component {
constructor(props) {
super(props)
Expand Down Expand Up @@ -96,8 +99,24 @@ class AccountEditor extends Component {
this.setState({ [e]: v })
}

validatePassword = (val, regEx) => (!val ? true : regEx.test(val))

render() {
const usesOpenIdConnect = this.context.d2.currentUser.externalAuth
const { minPasswordLength, maxPasswordLength } =
this.context.d2.system.settings.settings

const minLength = Number.isInteger(Number(minPasswordLength))
? Number(minPasswordLength)
: DEFAULT_MIN_PASSWORD_LENGTH
const maxLength = Number.isInteger(Number(maxPasswordLength))
? Number(maxPasswordLength)
: DEFAULT_MAX_PASSWORD_LENGTH

const passwordRegEx = new RegExp(
`^(?=.*[A-Z])(?=.*[a-z])(?=.*\\d)(?=.*[\\W_])[A-Za-z\\d\\W_]{${minLength},${maxLength}}$`
)

const fields = [
{
name: 'username',
Expand Down Expand Up @@ -142,9 +161,11 @@ class AccountEditor extends Component {
},
validators: [
{
validator: isValidPassword,
validator: (val) =>
this.validatePassword(val, passwordRegEx),
message: i18n.t(
'Password should be at least 8 characters with at least 1 digit, 1 uppercase letter and 1 special character'
'Password should be between {{minPasswordLength}} and {{maxPasswordLength}} characters long, with at least one lowercase character, one uppercase character, one number, and one special character.',
{ minPasswordLength, maxPasswordLength }
),
},
],
Expand Down
23 changes: 0 additions & 23 deletions src/account/isValidPassword.js

This file was deleted.

Loading