File: saslprep.py

package info (click to toggle)
python-asyncssh 2.21.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,464 kB
  • sloc: python: 40,306; makefile: 11
file content (118 lines) | stat: -rw-r--r-- 3,672 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# Copyright (c) 2013-2024 by Ron Frederick <ronf@timeheart.net> and others.
#
# This program and the accompanying materials are made available under
# the terms of the Eclipse Public License v2.0 which accompanies this
# distribution and is available at:
#
#     http://www.eclipse.org/legal/epl-2.0/
#
# This program may also be made available under the following secondary
# licenses when the conditions for such availability set forth in the
# Eclipse Public License v2.0 are satisfied:
#
#    GNU General Public License, Version 2.0, or any later versions of
#    that license
#
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
#
# Contributors:
#     Ron Frederick - initial implementation, API, and documentation

"""SASLprep implementation

   This module implements the stringprep algorithm defined in RFC 3454
   and the SASLprep profile of stringprep defined in RFC 4013. This
   profile is used to normalize usernames and passwords sent in the
   SSH protocol.

"""

# The stringprep module should not be flagged as deprecated
# pylint: disable=deprecated-module
import stringprep
# pylint: enable=deprecated-module
import unicodedata

from typing import Callable, Optional, Sequence
from typing_extensions import Literal


class SASLPrepError(ValueError):
    """Invalid data provided to saslprep"""


def _check_bidi(s: str) -> None:
    """Enforce bidirectional character check from RFC 3454 (stringprep)"""

    r_and_al_cat = False
    l_cat = False

    for c in s:
        if not r_and_al_cat and stringprep.in_table_d1(c):
            r_and_al_cat = True

        if not l_cat and stringprep.in_table_d2(c):
            l_cat = True

    if r_and_al_cat and l_cat:
        raise SASLPrepError('Both RandALCat and LCat characters present')

    if r_and_al_cat and not (stringprep.in_table_d1(s[0]) and
                             stringprep.in_table_d1(s[-1])):
        raise SASLPrepError('RandALCat character not at both start and end')


def _stringprep(s: str, check_unassigned: bool,
                mapping: Optional[Callable[[str], str]],
                normalization: Literal['NFC', 'NFD', 'NFKC', 'NFKD'],
                prohibited: Sequence[Callable[[str], bool]],
                bidi: bool) -> str:
    """Implement a stringprep profile as defined in RFC 3454"""

    if check_unassigned: # pragma: no branch
        for c in s:
            if stringprep.in_table_a1(c):
                raise SASLPrepError(f'Unassigned character: {c!r}')

    if mapping: # pragma: no branch
        s = mapping(s)

    if normalization: # pragma: no branch
        s = unicodedata.normalize(normalization, s)

    if prohibited: # pragma: no branch
        for c in s:
            for lookup in prohibited:
                if lookup(c):
                    raise SASLPrepError(f'Prohibited character: {c!r}')

    if bidi: # pragma: no branch
        _check_bidi(s)

    return s


def _map_saslprep(s: str) -> str:
    """Map stringprep table B.1 to nothing and C.1.2 to ASCII space"""

    r = []

    for c in s:
        if stringprep.in_table_c12(c):
            r.append(' ')
        elif not stringprep.in_table_b1(c):
            r.append(c)

    return ''.join(r)


def saslprep(s: str) -> str:
    """Implement SASLprep profile defined in RFC 4013"""

    prohibited = (stringprep.in_table_c12, stringprep.in_table_c21_c22,
                  stringprep.in_table_c3, stringprep.in_table_c4,
                  stringprep.in_table_c5, stringprep.in_table_c6,
                  stringprep.in_table_c7, stringprep.in_table_c8,
                  stringprep.in_table_c9)

    return _stringprep(s, True, _map_saslprep, 'NFKC', prohibited, True)