fix(user:reset-password): generate policy-compliant random passwords#33
Merged
Conversation
The previous --random implementation produced bin2hex(random_bytes(12)) — hex-only, no uppercase or symbols. Installs with secure_password enabled rejected the password at next login, leaving a "successful" reset that produced an unusable account. Introduce Service\PasswordPolicy that mirrors AuthUtils' private validators against the install's runtime $GLOBALS, and rework the generator to produce a 20-char password covering all four character classes, validated through PasswordPolicy with a retry cap. The duplication is acknowledged in the class docblock with a pointer to the upstream lines; openemr/openemr#12127 tracks the path to deletion once a public, side-effect-free validator lands in OpenEMR. Closes #12 Assisted-by: Claude Code
… type Self-review: the empty-alphabet LogicException in pick() was unreachable defensive code added only to satisfy PHPStan's random_int argument analysis. A non-empty-string param type expresses the same invariant statically and lets the body stay one line. Assisted-by: Claude Code
Self-review: PasswordPolicy had no extension hook, so making it final expresses intent and prevents accidental subclassing. PHPUnit's createMock cannot double final classes, so callers (the command, tests) now depend on PasswordPolicyInterface and the concrete is sealed. Assisted-by: Claude Code
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
--randompreviously emittedbin2hex(random_bytes(12))— hex-only, no uppercase or symbols. Installs withsecure_passwordenabled rejected the password at next login, leaving a "successful" reset that produced an unusable account.Service\PasswordPolicythat mirrorsAuthUtils' private validators against the install's runtime$GLOBALS(gbl_minimum_password_length,gbl_maximum_password_length,secure_password).PasswordPolicywith a 32-attempt retry cap before failing loudly.Why duplicate the policy?
OpenEMR's checks are
privateand the only public path (AuthUtils::updatePassword()) couples validation with persistence anddie(). There is no side-effect-free validator to call. The class docblock acknowledges this and points at the upstream lines; openemr/openemr#12127 proposes the upstream fix soService\PasswordPolicycan later be deleted in favor of a thin wrapper. Until our minimum-supported version moves past that change, the workaround stays.Test plan
composer phpunit— 51 tests, 189 assertionscomposer check(php-lint, phpcs, phpstan level 9, rector) cleantask smokeagainst the dev container — passed (Generated password: W4trECTuLP_seM=fmXgS→ 20 chars, all four classes present)Closes #12