| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# This code is part of Ansible, but is an independent component. |
| 4 |
# This particular file snippet, and this file snippet only, is BSD licensed. |
| 5 |
# Modules you write using this snippet, which is embedded dynamically by Ansible |
| 6 |
# still belong to the author of the module, and may assign their own license |
| 7 |
# to the complete work. |
| 8 |
|
| 9 |
# Copyright: (c) 2020, Andrew Klychkov (@Andersson007) <[email protected]> |
| 10 |
# |
| 11 |
# Simplified BSD License (see simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause) |
| 12 |
|
| 13 |
from __future__ import absolute_import, division, print_function |
| 14 |
|
| 15 |
__metaclass__ = type |
| 16 |
|
| 17 |
from stringprep import (in_table_a1, in_table_b1, in_table_c3, in_table_c4, |
| 18 |
in_table_c5, in_table_c6, in_table_c7, in_table_c8, |
| 19 |
in_table_c9, in_table_c12, in_table_c21_c22, |
| 20 |
in_table_d1, in_table_d2) |
| 21 |
from unicodedata import normalize |
| 22 |
|
| 23 |
|
| 24 |
def is_unicode_str(string): |
| 25 |
return True if isinstance(string, str) else False |
| 26 |
|
| 27 |
|
| 28 |
def mapping_profile(string): |
| 29 |
"""RFC4013 Mapping profile implementation.""" |
| 30 |
# Regarding RFC4013, |
| 31 |
# This profile specifies: |
| 32 |
# - non-ASCII space characters [StringPrep, C.1.2] that can be |
| 33 |
# mapped to SPACE (U+0020), and |
| 34 |
# - the "commonly mapped to nothing" characters [StringPrep, B.1] |
| 35 |
# that can be mapped to nothing. |
| 36 |
|
| 37 |
tmp = [] |
| 38 |
for c in string: |
| 39 |
# If not the "commonly mapped to nothing" |
| 40 |
if not in_table_b1(c): |
| 41 |
if in_table_c12(c): |
| 42 |
# map non-ASCII space characters |
| 43 |
# (that can be mapped) to Unicode space |
| 44 |
tmp.append(u' ') |
| 45 |
else: |
| 46 |
tmp.append(c) |
| 47 |
|
| 48 |
return u"".join(tmp) |
| 49 |
|
| 50 |
|
| 51 |
def is_ral_string(string): |
| 52 |
"""RFC3454 Check bidirectional category of the string""" |
| 53 |
# Regarding RFC3454, |
| 54 |
# Table D.1 lists the characters that belong |
| 55 |
# to Unicode bidirectional categories "R" and "AL". |
| 56 |
# If a string contains any RandALCat character, a RandALCat |
| 57 |
# character MUST be the first character of the string, and a |
| 58 |
# RandALCat character MUST be the last character of the string. |
| 59 |
if in_table_d1(string[0]): |
| 60 |
if not in_table_d1(string[-1]): |
| 61 |
raise ValueError('RFC3454: incorrect bidirectional RandALCat string.') |
| 62 |
return True |
| 63 |
return False |
| 64 |
|
| 65 |
|
| 66 |
def prohibited_output_profile(string): |
| 67 |
"""RFC4013 Prohibited output profile implementation.""" |
| 68 |
# Implements: |
| 69 |
# RFC4013, 2.3. Prohibited Output. |
| 70 |
# This profile specifies the following characters as prohibited input: |
| 71 |
# - Non-ASCII space characters [StringPrep, C.1.2] |
| 72 |
# - ASCII control characters [StringPrep, C.2.1] |
| 73 |
# - Non-ASCII control characters [StringPrep, C.2.2] |
| 74 |
# - Private Use characters [StringPrep, C.3] |
| 75 |
# - Non-character code points [StringPrep, C.4] |
| 76 |
# - Surrogate code points [StringPrep, C.5] |
| 77 |
# - Inappropriate for plain text characters [StringPrep, C.6] |
| 78 |
# - Inappropriate for canonical representation characters [StringPrep, C.7] |
| 79 |
# - Change display properties or deprecated characters [StringPrep, C.8] |
| 80 |
# - Tagging characters [StringPrep, C.9] |
| 81 |
# RFC4013, 2.4. Bidirectional Characters. |
| 82 |
# RFC4013, 2.5. Unassigned Code Points. |
| 83 |
|
| 84 |
# Determine how to handle bidirectional characters (RFC3454): |
| 85 |
if is_ral_string(string): |
| 86 |
# If a string contains any RandALCat characters, |
| 87 |
# The string MUST NOT contain any LCat character: |
| 88 |
is_prohibited_bidi_ch = in_table_d2 |
| 89 |
bidi_table = 'D.2' |
| 90 |
else: |
| 91 |
# Forbid RandALCat characters in LCat string: |
| 92 |
is_prohibited_bidi_ch = in_table_d1 |
| 93 |
bidi_table = 'D.1' |
| 94 |
|
| 95 |
RFC = 'RFC4013' |
| 96 |
for c in string: |
| 97 |
# RFC4013 2.3. Prohibited Output: |
| 98 |
if in_table_c12(c): |
| 99 |
raise ValueError('%s: prohibited non-ASCII space characters ' |
| 100 |
'that cannot be replaced (C.1.2).' % RFC) |
| 101 |
if in_table_c21_c22(c): |
| 102 |
raise ValueError('%s: prohibited control characters (C.2.1).' % RFC) |
| 103 |
if in_table_c3(c): |
| 104 |
raise ValueError('%s: prohibited private Use characters (C.3).' % RFC) |
| 105 |
if in_table_c4(c): |
| 106 |
raise ValueError('%s: prohibited non-character code points (C.4).' % RFC) |
| 107 |
if in_table_c5(c): |
| 108 |
raise ValueError('%s: prohibited surrogate code points (C.5).' % RFC) |
| 109 |
if in_table_c6(c): |
| 110 |
raise ValueError('%s: prohibited inappropriate for plain text ' |
| 111 |
'characters (C.6).' % RFC) |
| 112 |
if in_table_c7(c): |
| 113 |
raise ValueError('%s: prohibited inappropriate for canonical ' |
| 114 |
'representation characters (C.7).' % RFC) |
| 115 |
if in_table_c8(c): |
| 116 |
raise ValueError('%s: prohibited change display properties / ' |
| 117 |
'deprecated characters (C.8).' % RFC) |
| 118 |
if in_table_c9(c): |
| 119 |
raise ValueError('%s: prohibited tagging characters (C.9).' % RFC) |
| 120 |
|
| 121 |
# RFC4013, 2.4. Bidirectional Characters: |
| 122 |
if is_prohibited_bidi_ch(c): |
| 123 |
raise ValueError('%s: prohibited bidi characters (%s).' % (RFC, bidi_table)) |
| 124 |
|
| 125 |
# RFC4013, 2.5. Unassigned Code Points: |
| 126 |
if in_table_a1(c): |
| 127 |
raise ValueError('%s: prohibited unassigned code points (A.1).' % RFC) |
| 128 |
|
| 129 |
|
| 130 |
def saslprep(string): |
| 131 |
"""RFC4013 implementation. |
| 132 |
Implements "SASLprep" profile (RFC4013) of the "stringprep" algorithm (RFC3454) |
| 133 |
to prepare Unicode strings representing user names and passwords for comparison. |
| 134 |
Regarding the RFC4013, the "SASLprep" profile is intended to be used by |
| 135 |
Simple Authentication and Security Layer (SASL) mechanisms |
| 136 |
(such as PLAIN, CRAM-MD5, and DIGEST-MD5), as well as other protocols |
| 137 |
exchanging simple user names and/or passwords. |
| 138 |
|
| 139 |
Args: |
| 140 |
string (unicode string): Unicode string to validate and prepare. |
| 141 |
|
| 142 |
Returns: |
| 143 |
Prepared unicode string. |
| 144 |
""" |
| 145 |
# RFC4013: "The algorithm assumes all strings are |
| 146 |
# comprised of characters from the Unicode [Unicode] character set." |
| 147 |
# Validate the string is a Unicode string |
| 148 |
# (text_type is the string type if PY3 and unicode otherwise): |
| 149 |
if not is_unicode_str(string): |
| 150 |
raise TypeError('input must be of type %s, not %s' % (str, type(string))) |
| 151 |
|
| 152 |
# RFC4013: 2.1. Mapping. |
| 153 |
string = mapping_profile(string) |
| 154 |
|
| 155 |
# RFC4013: 2.2. Normalization. |
| 156 |
# "This profile specifies using Unicode normalization form KC." |
| 157 |
string = normalize('NFKC', string) |
| 158 |
if not string: |
| 159 |
return u'' |
| 160 |
|
| 161 |
# RFC4013: 2.3. Prohibited Output. |
| 162 |
# RFC4013: 2.4. Bidirectional Characters. |
| 163 |
# RFC4013: 2.5. Unassigned Code Points. |
| 164 |
prohibited_output_profile(string) |
| 165 |
|
| 166 |
return string |