from typing import Any, Dict from .auth_normalise_email import normalise_email from .auth_validate_login_types import LoginCheck _ASCII_SPACE = " \t\n\r\f\v" def _text(value: Any) -> str: # A non-string (a malformed JSON body) is an empty field, not an exception. return value if isinstance(value, str) else "" def validate_login(email: str, password: str) -> LoginCheck: """A login form checked for shape only: the email to look up when valid, and a message for each field that needs fixing. The password is never judged against a policy here, so a password set under older rules still logs in.""" email_text, password_text = _text(email), _text(password) fields: Dict[str, str] = {} normalised = normalise_email(email_text) if normalised is None: fields["email"] = ( "Enter your email address." if email_text.strip(_ASCII_SPACE) == "" else "Enter a valid email address, like name@example.com." ) if password_text == "": fields["password"] = "Enter your password." return LoginCheck(valid=len(fields) == 0, email=normalised, fields=fields)