"""Canonical profile-password policy and independent strength guidance.Profile-password validity is an exact-sequence capability contract: acceptedpasswords contain 8 through 256 Unicode scalar values and encode to at most1,024 strict UTF-8 bytes. The assessment never normalises, rewrites, retains,or returns the submitted password. Its result contains only a finite refusalreason and numeric measurements that are safe for prospective-passwordguidance.Strength is deliberately advisory. Character-class variety can improve thedisplay band for a shorter passphrase, but no composition rule affects profilepassword validity."""from__future__importannotationsfromdataclassesimportdataclassfromenumimportStrEnumfromtypingimportFinalPROFILE_PASSWORD_MIN_SCALARS:Final[int]=8"""Minimum Unicode scalar count for a profile password."""PROFILE_PASSWORD_MAX_SCALARS:Final[int]=256"""Maximum Unicode scalar count for a profile password."""PROFILE_PASSWORD_MAX_UTF8_BYTES:Final[int]=1024"""Maximum strict UTF-8 byte count for a profile password."""LENGTH_ALONE_IS_STRONG:Final[int]=20"""Length at which a passphrase is advised as strong without composition."""LENGTH_FAIR_FLOOR:Final[int]=12"""Length at which a passphrase is at least advised as fair."""
[docs]classProfilePasswordRefusalReason(StrEnum):"""Finite, secret-free reasons a prospective profile password is refused."""CONTAINS_SURROGATE="contains_surrogate"TOO_FEW_SCALARS="too_few_scalars"TOO_MANY_SCALARS="too_many_scalars"TOO_MANY_UTF8_BYTES="too_many_utf8_bytes"
[docs]classPassphraseStrength(StrEnum):"""Advisory display band that never determines password validity."""WEAK="weak"FAIR="fair"STRONG="strong"
[docs]@dataclass(frozen=True,slots=True)classProfilePasswordAssessment:"""Secret-free assessment of a prospective profile password. ``utf8_byte_count`` is unavailable only when a surrogate prevents strict UTF-8 encoding. No field retains or derives a reproducible fingerprint of the candidate. """reason:ProfilePasswordRefusalReason|Nonescalar_count:intutf8_byte_count:int|Nonestrength:PassphraseStrength@propertydefaccepted(self)->bool:"""Return whether the candidate satisfies the profile-password contract."""returnself.reasonisNone
def_character_class_count(candidate:str)->int:"""Count character classes for advisory strength only."""returnsum((any(character.islower()forcharacterincandidate),any(character.isupper()forcharacterincandidate),any(character.isdigit()forcharacterincandidate),any(notcharacter.isalnum()forcharacterincandidate),),)
[docs]defassess_passphrase_strength(candidate:str)->PassphraseStrength:"""Return advisory strength without applying a validity threshold."""length=len(candidate)iflength>=LENGTH_ALONE_IS_STRONG:returnPassphraseStrength.STRONGiflength>=LENGTH_FAIR_FLOOR:returnPassphraseStrength.STRONGif_character_class_count(candidate)>=3elsePassphraseStrength.FAIRreturnPassphraseStrength.FAIRif_character_class_count(candidate)>=3elsePassphraseStrength.WEAK
[docs]defassess_profile_password(candidate:str)->ProfilePasswordAssessment:"""Assess ``candidate`` exactly without retaining or rewriting it."""scalar_count=len(candidate)strength=assess_passphrase_strength(candidate)ifany(0xD800<=ord(character)<=0xDFFFforcharacterincandidate):returnProfilePasswordAssessment(reason=ProfilePasswordRefusalReason.CONTAINS_SURROGATE,scalar_count=scalar_count,utf8_byte_count=None,strength=strength,)utf8_byte_count=len(candidate.encode("utf-8",errors="strict"))ifscalar_count<PROFILE_PASSWORD_MIN_SCALARS:reason=ProfilePasswordRefusalReason.TOO_FEW_SCALARSelifutf8_byte_count>PROFILE_PASSWORD_MAX_UTF8_BYTES:reason=ProfilePasswordRefusalReason.TOO_MANY_UTF8_BYTESelifscalar_count>PROFILE_PASSWORD_MAX_SCALARS:reason=ProfilePasswordRefusalReason.TOO_MANY_SCALARSelse:reason=NonereturnProfilePasswordAssessment(reason=reason,scalar_count=scalar_count,utf8_byte_count=utf8_byte_count,strength=strength,)