email.saslprep — SASLprep string preparation¶
Source code: Lib/email/_saslprep.py
- email.saslprep.saslprep(data, *, allow_unassigned_code_points)¶
Prepare a Unicode string according to RFC 4013 (SASLprep), which is a profile of the RFC 3454 stringprep algorithm. SASLprep is used to normalise usernames and passwords before they are transmitted in authentication protocols such as SASL (e.g. SMTP, IMAP, LDAP).
data may be a
strorbytes. Byte strings are returned unchanged. Unicode strings are processed in four steps:Map — non-ASCII space characters (table C.1.2) are replaced with
U+0020; characters commonly mapped to nothing (table B.1) are removed.Normalise — the string is normalised using Unicode NFKC.
Prohibit — a
ValueErroris raised if the string contains any character from the RFC 4013 prohibited-output tables (control characters, private-use characters, non-characters, and others).Bidi check — a
ValueErroris raised if the string mixes right-to-left and left-to-right text in a way that violates RFC 3454 section 6.
allow_unassigned_code_points must be supplied as a keyword argument. Pass
Falsefor stored strings such as passwords stored in a database (unassigned code points are prohibited, per RFC 3454 section 7). PassTruefor queries such as a password typed at a prompt (unassigned code points are permitted). Always pass this explicitly; there is no default.Returns the prepared
str, or the original data unchanged if it is abytesobject.>>> from email import saslprep >>> saslprep("I\u00ADX", allow_unassigned_code_points=False) # soft hyphen removed 'IX' >>> saslprep("\u2168", allow_unassigned_code_points=False) # Roman numeral IX 'IX' >>> saslprep(b"user", allow_unassigned_code_points=False) # bytes unchanged b'user'
Added in version 3.15.
See also
- RFC 4013
SASLprep: Stringprep Profile for User Names and Passwords.
- RFC 3454
Preparation of Internationalized Strings (“stringprep”).
stringprepThe underlying Unicode character tables used by this function.
smtplib.SMTP.login()Uses
saslprep()when authenticating with Unicode credentials.