-
Notifications
You must be signed in to change notification settings - Fork 6
fix(user model): Fix default password generation. #158
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c3c664c
ci:test main
crankynetman 0f9f825
fix(pre-commit): apparently the config syntax has changed
crankynetman ff4fb7f
fix(default password): make default password use non-deprecated funct…
crankynetman 82bd9b3
fix(default password): dagnabbit, use the right thing
crankynetman 39538f0
refactor(make_random_password): add sanity checks and error handling.
crankynetman 5a247e5
fix(settings): Remove MAOR special character crud.
crankynetman 98273c9
fix(secrets): derp, forgot to update the function we're calling
crankynetman 9354b68
fix(secrets): and this time I mean it (calling the right function)
crankynetman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """A Module for defining shared code and constants.""" |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| """A Module for defining shared code.""" | ||
|
|
||
| import secrets | ||
| import string | ||
|
|
||
|
|
||
| def make_random_password(length: int = 20, min_digits: int = 5, max_attempts: int = 10000) -> str: | ||
| """make_random_password replaces the deprecated django make_random_password function. | ||
|
|
||
| Generates a random password of a specified length containing at least a specified number of digits using the | ||
| official python best practices and some additional sanity checks. The python docs for this can be found at | ||
| https://docs.python.org/3/library/secrets.html#recipes-and-best-practices. Note that generating long passwords | ||
| with a high number of digits (>100) is inefficient and should be avoided. This password should only be used for | ||
| temporary purposes, such as for a user to log in to the web interface and change their password. | ||
|
|
||
| Args: | ||
| length (int, optional): The total length of the password to generate. Defaults to 20. | ||
| min_digits (int, optional): The minimum number of digits the password needs. Defaults to 5. | ||
| max_attempts (int, optional): The maximum number of attempts to generate a valid password. Defaults to 10000. | ||
|
|
||
| Raises: | ||
| ValueError: Password length must be at least 1 | ||
| ValueError: min_digits cannot be negative | ||
| ValueError: min_digits cannot exceed password length | ||
| ValueError: For performance reasons, min_digits cannot exceed 30% of the password length | ||
| RuntimeError: Failed to generate a valid password after max_attempts attempts | ||
|
|
||
| Returns: | ||
| password (str): The generated password. | ||
| """ | ||
| if length < 1: | ||
| message = "Password length must be at least 1" | ||
| raise ValueError(message) | ||
| if min_digits < 0: | ||
| message = "min_digits cannot be negative" | ||
| raise ValueError(message) | ||
| if min_digits > length: | ||
| message = "min_digits cannot exceed password length" | ||
| raise ValueError(message) | ||
| # Only allow a somewhat arbitrary threshold of 30% of the password length for min_digits, for performance reasons. | ||
| if min_digits > length * 0.3: | ||
| message = "For performance reasons, min_digits cannot exceed 30% of the password length" | ||
| raise ValueError(message) | ||
|
|
||
| alphabet = string.ascii_letters + string.digits | ||
|
|
||
| for _attempt in range(max_attempts): | ||
| password = "".join(secrets.choice(alphabet) for _i in range(length)) | ||
| if ( | ||
| any(c.islower() for c in password) | ||
| and any(c.isupper() for c in password) | ||
| and sum(c.isdigit() for c in password) >= min_digits | ||
| ): | ||
| return password | ||
|
|
||
| # If we reached this point, we failed to generate a valid password after max_attempts attempts, likely due to the | ||
| # required password length being very long and the min_digits being high. | ||
| message = f"Failed to generate a valid password after {max_attempts} attempts" | ||
| raise RuntimeError(message) |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.