Skip to main content

MIT licensed Build Status codecov.io

If you want your application to accept Unicode user names and passwords, you must be careful in how you validate and compare them. The PRECIS framework makes internationalized user names and passwords safer for use by applications. PRECIS profiles transform Unicode strings into a canonical form, suitable for comparison.

This module implements the PRECIS Framework as described in:

  • PRECIS Framework: Preparation, Enforcement, and Comparison of Internationalized Strings in Application Protocols (RFC 8264)

  • Preparation, Enforcement, and Comparison of Internationalized Strings Representing Usernames and Passwords (RFC 8265)

  • Preparation, Enforcement, and Comparison of Internationalized Strings Representing Nicknames (RFC 8266)

Requires Python 3.3 or later.

Usage

Use the get_profile function to obtain a profile object, then use its enforce method. The enforce method returns a Unicode string.

>>> 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

Alternatively, you can use the Python str.encode API. Import the precis_i18n.codec module to register the PRECIS codec names. Now you can use the str.encode method with any Unicode string. The result will be a UTF-8 encoded byte string or a UnicodeEncodeError if the string is disallowed.

>>> 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

Alternative Unicode Versions

The get_profile function uses whatever version of unicodedata is provided by the Python runtime. The Unicode version is usually tied to the major version of the Python runtime. Python 3.7.x uses Unicode 11.0. Python 3.6.x uses Unicode 10.0.

To use an alternative unicodedata implementation, pass the unicodedata keyword argument to get_profile.

For example, you could separately install version 12.0 of the unicodedata2 module from PyPI. Then, pass it to get_profile to retrieve a profile that uses Unicode 12.0.

>> import unicodedata2
>> from precis_i18n import get_profile
>> username = get_profile('UsernameCaseMapped', unicodedata=unicodedata2)
>> username.enforce('Kevin')
'kevin'

Supported Profiles and Codecs

Each PRECIS profile has a corresponding codec name. The CaseMapped variant converts the string to lower case for implementing case-insensitive comparison.

  • UsernameCasePreserved

  • UsernameCaseMapped

  • OpaqueString

  • NicknameCasePreserved

  • NicknameCaseMapped

The CaseMapped profiles use Unicode ToLower per the latest RFC. Previous verions of this package used Unicode Default Case Folding. There are CaseMapped variants for different case transformations. These profile names are deprecated:

  • UsernameCaseMapped:ToLower

  • UsernameCaseMapped:CaseFold

  • NicknameCaseMapped:ToLower

  • NicknameCaseMapped:CaseFold

The PRECIS base string classes are also available as codecs:

  • IdentifierClass

  • FreeFormClass

Userparts and Space Delimited Usernames

The Username profiles in this implementation do not allow spaces. The Username profiles correspond to the definition of “userparts” in RFC 8265. If you want to allow spaces in your application’s user names, you must split the string first.

def enforce_app_username(name):
    profile = precis_i18n.get_profile('UsernameCasePreserved')
    userparts = [profile.enforce(userpart) for userpart in name.split(' ')]
    return ' '.join(userparts)

Be aware that a username constructed this way can contain bidirectional text in the separate userparts.

Error Messages

A PRECIS profile raises a UnicodeEncodeError exception if a string is disallowed. The reason field specifies the kind of error.

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)

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

precis_i18n-1.0.2.tar.gz (65.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

precis_i18n-1.0.2-py3-none-any.whl (23.2 kB view details)

Uploaded Python 3

File details

Details for the file precis_i18n-1.0.2.tar.gz.

File metadata

  • Download URL: precis_i18n-1.0.2.tar.gz
  • Upload date:
  • Size: 65.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for precis_i18n-1.0.2.tar.gz
Algorithm Hash digest
SHA256 f51b350221be1cfe952af468d0d6e3a062fa35b283eb5e399ef66a75a905ff2f
MD5 0e55dec243ab761551c7a8a9f5f20560
BLAKE2b-256 4c8c053c9c7be6ad02503b4ccaf7be857af97af82ea5898120217d1d7cc7e887

See more details on using hashes here.

File details

Details for the file precis_i18n-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: precis_i18n-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 23.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for precis_i18n-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c7d5a1f5667a6e7cb85db0a5a4ec8e1090eaeb6e189e8131a5ee238492d60445
MD5 4055e4d2dc825b986b37fd5bb7a0ee74
BLAKE2b-256 13f53a7485a32dd4422a2a78de744bea4a00126b6001416c677b99c1a6e50e28

See more details on using hashes here.

Release history Release notifications | RSS feed

1.1.2

2 files

1.1.1

2 files

1.1.0

2 files

1.0.5

2 files

1.0.4

2 files

1.0.3

2 files

This release

1.0.2 This release

2 files

1.0.1

2 files

1.0.0

2 files

0.7.0

2 files

0.6.0

2 files

0.5.0

2 files

0.4.1

2 files

0.4.0

2 files

0.3.0

2 files

0.2.2

2 files

0.2.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page