precis-i18n:国际化用户名和密码
precis-i18n的Python项目详细描述
如果希望应用程序接受Unicode用户名和密码, 你必须小心如何验证和比较它们。普瑞西斯 框架使国际化的用户名和密码更安全地使用 通过申请。precis profiles将unicode字符串转换为 典型形式,适于比较。
此模块实现precis框架,如中所述:
- PRECIS框架:准备、实施和比较 应用程序协议中的国际化字符串(RFC 8264)
- 国际化字符串的准备、执行和比较 表示用户名和密码(RFC 8265)
- 国际化字符串的准备、执行和比较 表示昵称(RFC 8266)
需要Python3.3或更高版本。
用法
使用get_profile函数获取配置文件对象,然后使用 它的enforce方法。enforce方法返回Unicode字符串。
>>> from precis_i18n import get_profile >>> username = get_profile('UsernameCaseMapped') >>> username.enforce('Kevin') 'kevin' >>> username.enforce('\u212Aevin') 'kevin' >>> username.enforce('\uFF2Bevin') 'kevin' >>> username.enforce('\U0001F17Aevin') Traceback (most recent call last): ... UnicodeEncodeError: 'UsernameCaseMapped' codec can't encode character '\U0001f17a' in position 0: DISALLOWED/symbols
或者,可以使用pythonstr.encodeapi。导入 precis_i18n.codec注册precis编解码器名称的模块。现在你 可以对任何unicode字符串使用str.encode方法。结果 如果 不允许使用字符串。
>>> import precis_i18n.codec >>> 'Kevin'.encode('UsernameCasePreserved') b'Kevin' >>> '\u212Aevin'.encode('UsernameCasePreserved') b'Kevin' >>> '\uFF2Bevin'.encode('UsernameCasePreserved') b'Kevin' >>> '\u212Aevin'.encode('UsernameCaseMapped') b'kevin' >>> '\uFF2Bevin'.encode('OpaqueString') b'\xef\xbc\xabevin' >>> '\U0001F17Aevin'.encode('UsernameCasePreserved') Traceback (most recent call last): ... UnicodeEncodeError: 'UsernameCasePreserved' codec can't encode character '\U0001f17a' in position 0: DISALLOWED/symbols
支持的配置文件和编解码器
每个precis配置文件都有相应的编解码器名称。那CaseMapped variant将字符串转换为小写以实现 不区分大小写的比较。
- 保留用户名案例
- usernamecasemapped
- 不透明字符串
- 保留昵称case
- 昵称casemapped
CaseMapped配置文件根据最新的rfc使用unicodeToLower。上一个 这个包的版本使用Unicode默认大小写折叠。有casemapped变体 对于不同的情况转换。这些配置文件名称已弃用:
- usernamecasemapped:tolower
- 用户名casemapped:casefold
- 昵称casemapped:tolower
- 昵称casemapped:casefold
precis基字符串类也可用作编解码器:
- 标识类
- FreeformClass
用户部分和空格分隔的用户名
此实现中的用户名配置文件不允许空格。用户名 配置文件对应于RFC 8265中“用户部件”的定义。如果你想的话 允许应用程序用户名中有空格,必须先拆分字符串。
def enforce_app_username(name): profile = precis_i18n.get_profile('UsernameCasePreserved') userparts = [profile.enforce(userpart) for userpart in name.split(' ')] return ' '.join(userparts)
注意,这样构造的用户名可以在 单独的用户部分。
错误消息
如果字符串 是不允许的。reason字段指定错误的类型。
Reason | Explanation |
---|---|
DISALLOWED/arabic_indic | Arabic-Indic digits cannot be mixed with Extended Arabic-Indic Digits. (Context) |
DISALLOWED/bidi_rule | Right-to-left string cannot contain left-to-right characters due to the “Bidi” rule. (Context) |
DISALLOWED/controls | Control character is not allowed. |
DISALLOWED/empty | After applying the profile, the result cannot be empty. |
DISALLOWED/exceptions | Exception character is not allowed. |
DISALLOWED/extended_arabic_indic | Extended Arabic-Indic digits cannot be mixed with Arabic-Indic Digits. (Context) |
DISALLOWED/greek_keraia | Greek keraia must be followed by a Greek character. (Context) |
DISALLOWED/has_compat | Compatibility characters are not allowed. |
DISALLOWED/hebrew_punctuation | Hebrew punctuation geresh or gershayim must be preceded by Hebrew character. (Context) |
DISALLOWED/katakana_middle_dot | Katakana middle dot must be accompanied by a Hiragana, Katakana, or Han character. (Context) |
DISALLOWED/middle_dot | Middle dot must be surrounded by the letter ‘l’. (Context) |
DISALLOWED/not_idempotent | After reapplying the profile, the result is not stable. |
DISALLOWED/old_hangul_jamo | Conjoining Hangul Jamo is not allowed. |
DISALLOWED/other | Other character is not allowed. |
DISALLOWED/other_letter_digits | Non-traditional letter or digit is not allowed. |
DISALLOWED/precis_ignorable_properties | Default ignorable or non-character is not allowed. |
DISALLOWED/punctuation | Non-ASCII punctuation character is not allowed. |
DISALLOWED/spaces | Space character is not allowed. |
DISALLOWED/symbols | Non-ASCII symbol character is not allowed. |
DISALLOWED/unassigned | Unassigned unicode character is not allowed. |
DISALLOWED/zero_width_joiner | Zero width joiner must immediately follow a combining virama. (Context) |
DISALLOWED/zero_width_nonjoiner | Zero width non-joiner must immediately follow a combining virama, or appear where it breaks a cursive connection in a formally cursive script. (Context) |