forked from ustaxcourt/ef-cms
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a62ec46
commit fd22cca
Showing
12 changed files
with
426 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import { JoiValidationConstants } from './JoiValidationConstants'; | ||
import { JoiValidationEntity } from './JoiValidationEntity'; | ||
import joi from 'joi'; | ||
|
||
type PasswordValidation = { | ||
message: string; | ||
valid: boolean; | ||
}; | ||
|
||
export type ChangePasswordValidations = { | ||
hasNoLeadingOrTrailingSpace: PasswordValidation; | ||
hasOneLowercase: PasswordValidation; | ||
hasOneNumber: PasswordValidation; | ||
hasOneUppercase: PasswordValidation; | ||
hasSpecialCharacterOrSpace: PasswordValidation; | ||
isProperLength: PasswordValidation; | ||
}; | ||
|
||
const ChangePasswordValidationErrorMessages = { | ||
hasNoLeadingOrTrailingSpace: 'Must not contain leading or trailing space', | ||
hasOneLowercase: 'Must contain lower case letter', | ||
hasOneNumber: 'Must contain number', | ||
hasOneUppercase: 'Must contain upper case letter', | ||
hasSpecialCharacterOrSpace: 'Must contain special character or space', | ||
isProperLength: 'Must be between 8-99 characters long', | ||
}; | ||
|
||
export function getDefaultPasswordErrors(): ChangePasswordValidations { | ||
return { | ||
hasNoLeadingOrTrailingSpace: { | ||
message: | ||
ChangePasswordValidationErrorMessages.hasNoLeadingOrTrailingSpace, | ||
valid: true, | ||
}, | ||
hasOneLowercase: { | ||
message: ChangePasswordValidationErrorMessages.hasOneLowercase, | ||
valid: true, | ||
}, | ||
hasOneNumber: { | ||
message: ChangePasswordValidationErrorMessages.hasOneNumber, | ||
valid: true, | ||
}, | ||
hasOneUppercase: { | ||
message: ChangePasswordValidationErrorMessages.hasOneUppercase, | ||
valid: true, | ||
}, | ||
hasSpecialCharacterOrSpace: { | ||
message: ChangePasswordValidationErrorMessages.hasSpecialCharacterOrSpace, | ||
valid: true, | ||
}, | ||
isProperLength: { | ||
message: ChangePasswordValidationErrorMessages.isProperLength, | ||
valid: true, | ||
}, | ||
}; | ||
} | ||
|
||
export class ChangePasswordForm extends JoiValidationEntity { | ||
public password: string; | ||
public confirmPassword: string; | ||
|
||
constructor(rawProps) { | ||
super('ChangePasswordForm'); | ||
this.password = rawProps.password; | ||
this.confirmPassword = rawProps.confirmPassword; | ||
} | ||
|
||
static VALIDATION_RULES = joi.object().keys({ | ||
confirmPassword: joi | ||
.valid(joi.ref('password')) | ||
.required() | ||
.messages({ '*': 'Passwords must match' }), | ||
entityName: | ||
JoiValidationConstants.STRING.valid('ChangePasswordForm').required(), | ||
password: JoiValidationConstants.STRING.custom((value, helper) => { | ||
const errors = getDefaultPasswordErrors(); | ||
|
||
if (value.length < 8 || value.length > 99) { | ||
errors.isProperLength.valid = false; | ||
} | ||
|
||
if (!/[a-z]/.test(value)) { | ||
errors.hasOneLowercase.valid = false; | ||
} | ||
|
||
if (!/[A-Z]/.test(value)) { | ||
errors.hasOneUppercase.valid = false; | ||
} | ||
|
||
if (!/[\^$*.[\]{}()?\-“!@#%&/,><’:;|_~`]/.test(value)) { | ||
errors.hasSpecialCharacterOrSpace.valid = false; | ||
} | ||
|
||
if (!/[0-9]/.test(value)) { | ||
errors.hasOneNumber.valid = false; | ||
} | ||
|
||
if (/^\s/.test(value) || /\s$/.test(value)) { | ||
errors.hasNoLeadingOrTrailingSpace.valid = false; | ||
} | ||
|
||
const noErrors = Object.values(errors).reduce( | ||
(accumulator, currentValue) => { | ||
return accumulator && currentValue.valid; | ||
}, | ||
true, | ||
); | ||
|
||
if (noErrors) { | ||
return value; | ||
} else { | ||
return helper.message( | ||
Object.entries(errors) | ||
.filter(([, curValue]) => !curValue.valid) | ||
.map(([key]) => key) | ||
.join('|') as any, | ||
); | ||
} | ||
}).description( | ||
'Password for the account. Contains a custom validation because we want to construct a string with all the keys that failed which later we parse out to an object', | ||
), | ||
}); | ||
|
||
getValidationRules() { | ||
return ChangePasswordForm.VALIDATION_RULES; | ||
} | ||
} | ||
|
||
export type RawChangePasswordForm = ExcludeMethods<ChangePasswordForm>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
web-api/src/business/useCases/auth/changePasswordInteractor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
export const changePasswordInteractor = async ( | ||
applicationContext: IApplicationContext, | ||
{ | ||
newPassword, | ||
sessionId, | ||
userEmail, | ||
}: { newPassword: string; sessionId: string; userEmail: string }, | ||
) => { | ||
const params = { | ||
ChallengeName: 'NEW_PASSWORD_REQUIRED', | ||
ChallengeResponses: { | ||
NEW_PASSWORD: newPassword, | ||
USERNAME: userEmail, | ||
}, | ||
ClientId: process.env.COGNITO_CLIENT_ID, | ||
Session: sessionId, | ||
}; | ||
|
||
const result = await applicationContext | ||
.getCognito() | ||
.respondToAuthChallenge(params) | ||
.promise(); | ||
|
||
return result; | ||
}; |
9 changes: 2 additions & 7 deletions
9
web-client/src/presenter/actions/Login/redirectToChangePasswordAction.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,3 @@ | ||
import { state } from '@web-client/presenter/app.cerebral'; | ||
|
||
export const redirectToChangePasswordAction = ({ get, router }) => { | ||
const a = get(state.cognitoPasswordChange); | ||
console.log(`get(state.cognitoPasswordChange)[${a}]`); | ||
|
||
router.externalRoute(get(state.cognitoPasswordChange)); | ||
export const redirectToChangePasswordAction = async ({ router }) => { | ||
await router.route('/change-password'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
web-client/src/presenter/computeds/Login/changePasswordHelper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { ChangePasswordForm } from '@shared/business/entities/ChangePassword'; | ||
import { Get } from 'cerebral'; | ||
import { PasswordValidations } from '@shared/business/entities/NewPetitionerUser'; | ||
import { convertErrorMessageToPasswordValidationObject } from '@web-client/presenter/computeds/CreatePetitionerAccount/createAccountHelper'; | ||
import { state } from '@web-client/presenter/app.cerebral'; | ||
|
||
export type ChangePasswordHelperResults = { | ||
confirmPassword: boolean; | ||
formIsValid: boolean; | ||
passwordErrors?: PasswordValidations; | ||
}; | ||
|
||
export const changePasswordHelper = (get: Get): ChangePasswordHelperResults => { | ||
const form = get(state.form); | ||
const formEntity = new ChangePasswordForm(form); | ||
const errors = formEntity.getFormattedValidationErrors(); | ||
|
||
const passwordErrors: PasswordValidations = | ||
convertErrorMessageToPasswordValidationObject(errors?.password); | ||
|
||
return { | ||
confirmPassword: !errors?.confirmPassword, | ||
formIsValid: formEntity.isValid(), | ||
passwordErrors, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
web-client/src/presenter/sequences/Login/goToChangePasswordSequence.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { setupCurrentPageAction } from '../../actions/setupCurrentPageAction'; | ||
|
||
export const goToChangePasswordSequence = [ | ||
setupCurrentPageAction('ChangePassword'), | ||
] as unknown as () => void; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.