0

put password regex rules into config-constants.tsx

This commit is contained in:
dorj222 2023-02-07 17:22:52 +01:00
parent 3711588909
commit aa2504b354
3 changed files with 44 additions and 50 deletions

View File

@ -5,6 +5,7 @@ import { FieldUpdaterFunc } from '../../types/config-section';
// import InfoTip from '../info-tip'; // import InfoTip from '../info-tip';
import { StatusState } from '../../utils/input-statuses'; import { StatusState } from '../../utils/input-statuses';
import { FormStatusIndicator } from './FormStatusIndicator'; import { FormStatusIndicator } from './FormStatusIndicator';
import { PASSWORD_COMPLEXITY_RULES, REGEX_PASSWORD } from '../../utils/config-constants';
export const TEXTFIELD_TYPE_TEXT = 'default'; export const TEXTFIELD_TYPE_TEXT = 'default';
export const TEXTFIELD_TYPE_PASSWORD = 'password'; // Input.Password export const TEXTFIELD_TYPE_PASSWORD = 'password'; // Input.Password
@ -58,14 +59,13 @@ export const TextField: FC<TextFieldProps> = ({
}) => { }) => {
const [hasPwdChanged, setHasPwdChanged] = useState(false); const [hasPwdChanged, setHasPwdChanged] = useState(false);
const [showPwdButton, setShowPwdButton] = useState(false); const [showPwdButton, setShowPwdButton] = useState(false);
const regex = /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[!@#$%^&*]).{8,192}$/;
const [form] = Form.useForm(); const [form] = Form.useForm();
const handleChange = (e: any) => { const handleChange = (e: any) => {
// if an extra onChange handler was sent in as a prop, let's run that too. // if an extra onChange handler was sent in as a prop, let's run that too.
if (onChange) { if (onChange) {
const val = type === TEXTFIELD_TYPE_NUMBER ? e : e.target.value; const val = type === TEXTFIELD_TYPE_NUMBER ? e : e.target.value;
setShowPwdButton(true); setShowPwdButton(true);
if (isAdminPwdField && regex.test(val)) { if (isAdminPwdField && REGEX_PASSWORD.test(val)) {
setHasPwdChanged(true); setHasPwdChanged(true);
} else { } else {
setHasPwdChanged(false); setHasPwdChanged(false);
@ -92,7 +92,8 @@ export const TextField: FC<TextFieldProps> = ({
onPressEnter(); onPressEnter();
} }
}; };
// Password Complexity rules
const passwordComplexityRules = [];
// display the appropriate Ant text field // display the appropriate Ant text field
let Field = Input as let Field = Input as
| typeof Input | typeof Input
@ -106,6 +107,9 @@ export const TextField: FC<TextFieldProps> = ({
autoSize: true, autoSize: true,
}; };
} else if (type === TEXTFIELD_TYPE_PASSWORD) { } else if (type === TEXTFIELD_TYPE_PASSWORD) {
PASSWORD_COMPLEXITY_RULES.forEach(element => {
passwordComplexityRules.push(element);
});
Field = Input.Password; Field = Input.Password;
fieldProps = { fieldProps = {
visibilityToggle: true, visibilityToggle: true,
@ -175,29 +179,7 @@ export const TextField: FC<TextFieldProps> = ({
initialValues={{ inputFieldPassword: value }} initialValues={{ inputFieldPassword: value }}
style={{ width: '100%' }} style={{ width: '100%' }}
> >
<Form.Item <Form.Item name="inputFieldPassword" rules={passwordComplexityRules}>
name="inputFieldPassword"
rules={[
{ min: 8, message: '- minimum 8 characters' },
{ max: 192, message: '- maximum 192 characters' },
{
pattern: /^(?=.*[a-z])/,
message: '- at least one lowercase letter',
},
{
pattern: /^(?=.*[A-Z])/,
message: '- at least one uppercase letter',
},
{
pattern: /\d/,
message: '- at least one digit',
},
{
pattern: /^(?=.*?[#?!@$%^&*-])/,
message: '- at least one special character: !@#$%^&*',
},
]}
>
<Input.Password <Input.Password
id={fieldId} id={fieldId}
className={`field ${className} ${fieldId}`} className={`field ${className} ${fieldId}`}

View File

@ -1,14 +1,13 @@
import React, { useContext, useState } from 'react'; import React, { useContext, useEffect, useState } from 'react';
import { Table, Space, Button, Typography, Alert, Input, Form } from 'antd'; import { Table, Space, Button, Typography, Alert, Input, Form } from 'antd';
import dynamic from 'next/dynamic'; import dynamic from 'next/dynamic';
import { ServerStatusContext } from '../../../../utils/server-status-context'; import { ServerStatusContext } from '../../../../utils/server-status-context';
import { fetchData, UPDATE_STREAM_KEYS } from '../../../../utils/apis'; import { fetchData, UPDATE_STREAM_KEYS } from '../../../../utils/apis';
import { PASSWORD_COMPLEXITY_RULES, REGEX_PASSWORD } from '../../../../utils/config-constants';
const { Paragraph } = Typography; const { Paragraph } = Typography;
const regex = /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[!@#$%^&*]).{8,192}$/;
// Lazy loaded components // Lazy loaded components
const DeleteOutlined = dynamic(() => import('@ant-design/icons/DeleteOutlined'), { const DeleteOutlined = dynamic(() => import('@ant-design/icons/DeleteOutlined'), {
@ -40,6 +39,15 @@ const AddKeyForm = ({ setShowAddKeyForm, setFieldInConfigState, streamKeys, setE
const [hasChanged, setHasChanged] = useState(false); const [hasChanged, setHasChanged] = useState(false);
const [form] = Form.useForm(); const [form] = Form.useForm();
const { Item } = Form; const { Item } = Form;
// Password Complexity rules
const passwordComplexityRules = [];
useEffect(() => {
PASSWORD_COMPLEXITY_RULES.forEach(element => {
passwordComplexityRules.push(element);
});
}, []);
const handleAddKey = (newkey: any) => { const handleAddKey = (newkey: any) => {
const updatedKeys = [...streamKeys, newkey]; const updatedKeys = [...streamKeys, newkey];
@ -55,7 +63,7 @@ const AddKeyForm = ({ setShowAddKeyForm, setFieldInConfigState, streamKeys, setE
const handleInputChange = (event: any) => { const handleInputChange = (event: any) => {
const val = event.target.value; const val = event.target.value;
if (regex.test(val)) { if (REGEX_PASSWORD.test(val)) {
setHasChanged(true); setHasChanged(true);
} else { } else {
setHasChanged(false); setHasChanged(false);
@ -81,26 +89,7 @@ const AddKeyForm = ({ setShowAddKeyForm, setFieldInConfigState, streamKeys, setE
lowercase letter, at least one special character, and at least one number. lowercase letter, at least one special character, and at least one number.
</p> </p>
} }
rules={[ rules={PASSWORD_COMPLEXITY_RULES}
{ min: 8, message: '- minimum 8 characters' },
{ max: 192, message: '- maximum 192 characters' },
{
pattern: /^(?=.*[a-z])/,
message: '- at least one lowercase letter',
},
{
pattern: /^(?=.*[A-Z])/,
message: '- at least one uppercase letter',
},
{
pattern: /\d/,
message: '- at least one digit',
},
{
pattern: /^(?=.*?[#?!@$%^&*-])/,
message: '- at least one special character: !@#$%^&*',
},
]}
> >
<Input onChange={handleInputChange} /> <Input onChange={handleInputChange} />
</Item> </Item>

View File

@ -562,3 +562,26 @@ export const BROWSER_PUSH_CONFIG_FIELDS = {
placeholder: `I've gone live! Come watch!`, placeholder: `I've gone live! Come watch!`,
}, },
}; };
export const PASSWORD_COMPLEXITY_RULES = [
{ min: 8, message: '- minimum 8 characters' },
{ max: 192, message: '- maximum 192 characters' },
{
pattern: /^(?=.*[a-z])/,
message: '- at least one lowercase letter',
},
{
pattern: /^(?=.*[A-Z])/,
message: '- at least one uppercase letter',
},
{
pattern: /\d/,
message: '- at least one digit',
},
{
pattern: /^(?=.*?[#?!@$%^&*-])/,
message: '- at least one special character: !@#$%^&*',
},
];
export const REGEX_PASSWORD = /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[!@#$%^&*]).{8,192}$/g;