precis-i18n:国际化用户名和密码

precis-i18n的Python项目详细描述


MIT licensedBuild Statuscodecov.io

如果希望应用程序接受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字段指定错误的类型。

ReasonExplanation
DISALLOWED/arabic_indicArabic-Indic digits cannot be mixed with Extended Arabic-Indic Digits. (Context)
DISALLOWED/bidi_ruleRight-to-left string cannot contain left-to-right characters due to the “Bidi” rule. (Context)
DISALLOWED/controlsControl character is not allowed.
DISALLOWED/emptyAfter applying the profile, the result cannot be empty.
DISALLOWED/exceptionsException character is not allowed.
DISALLOWED/extended_arabic_indicExtended Arabic-Indic digits cannot be mixed with Arabic-Indic Digits. (Context)
DISALLOWED/greek_keraiaGreek keraia must be followed by a Greek character. (Context)
DISALLOWED/has_compatCompatibility characters are not allowed.
DISALLOWED/hebrew_punctuationHebrew punctuation geresh or gershayim must be preceded by Hebrew character. (Context)
DISALLOWED/katakana_middle_dotKatakana middle dot must be accompanied by a Hiragana, Katakana, or Han character. (Context)
DISALLOWED/middle_dotMiddle dot must be surrounded by the letter ‘l’. (Context)
DISALLOWED/not_idempotentAfter reapplying the profile, the result is not stable.
DISALLOWED/old_hangul_jamoConjoining Hangul Jamo is not allowed.
DISALLOWED/otherOther character is not allowed.
DISALLOWED/other_letter_digitsNon-traditional letter or digit is not allowed.
DISALLOWED/precis_ignorable_propertiesDefault ignorable or non-character is not allowed.
DISALLOWED/punctuationNon-ASCII punctuation character is not allowed.
DISALLOWED/spacesSpace character is not allowed.
DISALLOWED/symbolsNon-ASCII symbol character is not allowed.
DISALLOWED/unassignedUnassigned unicode character is not allowed.
DISALLOWED/zero_width_joinerZero width joiner must immediately follow a combining virama. (Context)
DISALLOWED/zero_width_nonjoinerZero width non-joiner must immediately follow a combining virama, or appear where it breaks a cursive connection in a formally cursive script. (Context)

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
内存Java正在运行。jar heapdump错误   java如何在安卓画布中弯曲文本区域?   java如何在Gdx 安卓游戏编程中获得矩形的真实触碰位置?   找不到java Spring MVC控制器   在Java中使用双重检查锁定单例扩展类   java在高效的时间和内存中动态执行insert(索引、数据)、delete(索引)、getAt(索引)操作。   java 安卓 Toast和视图帮助   java协议缓冲区:从文件中读取所有序列化消息   java如何在Jackson中为参数化接口类型执行通用自定义反序列化程序   与简单的空检查相比,使用(平面)映射的java优势是什么?   异步方法seam中的java Get contextparam   jar使用相同的java运行时运行另一个java程序   java访问Spring批处理中的作业参数   java给定字符串为空或null   在h2数据库1.4中找不到java类“org.h2.fulltext.FullTextLucene”。*不适用于Lucene Core 4*   java Spring Boot在使用@enableSync时不响应任何请求   java错误:在bash上找不到或加载主类pj2   “返回对象”和“返回(对象)”之间的Java差异   java Android开发:如何使用onKeyUp?