-
Notifications
You must be signed in to change notification settings - Fork 3
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
Showing
4 changed files
with
92 additions
and
98 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
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,55 +1,70 @@ | ||
"""A password in plain text, as received from a user.""" | ||
from typing import Optional | ||
import re | ||
from re import Pattern | ||
|
||
from jupiter.core.framework.errors import InputValidationError | ||
from jupiter.core.framework.realm import ( | ||
DatabaseRealm, | ||
CliRealm, | ||
RealmDecoder, | ||
RealmEncoder, | ||
RealmThing, | ||
WebRealm, | ||
only_in_realm, | ||
) | ||
from jupiter.core.framework.value import SecretValue, secret_value | ||
|
||
_PASSWORD_PLAIN_RE: Pattern[str] = re.compile(r"^\S+$") | ||
_PASSWORD_MIN_LENGTH: int = 10 | ||
|
||
|
||
@secret_value | ||
@only_in_realm(CliRealm, WebRealm) | ||
class PasswordPlain(SecretValue): | ||
"""A new password in plain text, as received from a user.""" | ||
|
||
password_raw: str | ||
|
||
@staticmethod | ||
def from_raw(password_str: Optional[str]) -> "PasswordPlain": | ||
"""Validate and clean a raw password.""" | ||
if not password_str: | ||
raise InputValidationError("Expected password to be non null") | ||
|
||
password_str = PasswordPlain._clean_password(password_str) | ||
|
||
return PasswordPlain(password_str) | ||
|
||
@staticmethod | ||
def _clean_password(password_str_raw: str) -> str: | ||
if len(password_str_raw) == 0: | ||
raise InputValidationError("Expected password to be non-empty") | ||
class PasswordPlainCliDecoder(RealmDecoder[PasswordPlain, CliRealm]): | ||
"""Decode a password newplain from storage in the CLI.""" | ||
|
||
return password_str_raw | ||
def decode(self, value: RealmThing) -> PasswordPlain: | ||
"""Decode a password plain from storage in the database.""" | ||
if not isinstance(value, str): | ||
raise InputValidationError( | ||
f"Expected password newplain to be a string, got {value}" | ||
) | ||
|
||
if not _PASSWORD_PLAIN_RE.match(value): | ||
raise InputValidationError( | ||
"Expected password to not contain any white-space" | ||
) | ||
|
||
class PasswordPlainDatabaseEncoder(RealmEncoder[PasswordPlain, DatabaseRealm]): | ||
"""Encode a password plain for storage in the database.""" | ||
if len(value) < _PASSWORD_MIN_LENGTH: | ||
raise InputValidationError( | ||
f"Expected password to be longer than {_PASSWORD_MIN_LENGTH} characters" | ||
) | ||
|
||
def encode(self, value: PasswordPlain) -> RealmThing: | ||
"""Encode a password plain for storage in the database.""" | ||
return value.password_raw | ||
return PasswordPlain(value) | ||
|
||
|
||
class PasswordPlainDatabaseDecoder(RealmDecoder[PasswordPlain, DatabaseRealm]): | ||
"""Decode a password plain from storage in the database.""" | ||
class PasswordPlainWebDecoder(RealmDecoder[PasswordPlain, WebRealm]): | ||
"""Decode a password newplain from storage in the Web.""" | ||
|
||
def decode(self, value: RealmThing) -> PasswordPlain: | ||
"""Decode a password plain from storage in the database.""" | ||
"""Decode a password newplain from storage in the database.""" | ||
if not isinstance(value, str): | ||
raise InputValidationError( | ||
f"Expected password plain to be a string, got {value}" | ||
f"Expected password newplain to be a string, got {value}" | ||
) | ||
|
||
if not _PASSWORD_PLAIN_RE.match(value): | ||
raise InputValidationError( | ||
"Expected password to not contain any white-space" | ||
) | ||
return PasswordPlain.from_raw(value) | ||
|
||
if len(value) < _PASSWORD_MIN_LENGTH: | ||
raise InputValidationError( | ||
f"Expected password to be longer than {_PASSWORD_MIN_LENGTH} characters" | ||
) | ||
|
||
return PasswordPlain(value) |
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