summaryrefslogtreecommitdiff
path: root/sleekxmpp/thirdparty/suelta/mechanisms/plain.py
blob: accae54a79dd4fb07ddb8d2dd5b71e4c08a112e8 (plain)
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
import sys

from sleekxmpp.thirdparty.suelta.util import bytes
from sleekxmpp.thirdparty.suelta.sasl import Mechanism, register_mechanism
from sleekxmpp.thirdparty.suelta.exceptions import SASLError, SASLCancelled


class PLAIN(Mechanism):

    """
    """

    def __init__(self, sasl, name):
        """
        """
        super(PLAIN, self).__init__(sasl, name)

        if not self.sasl.tls_active():
            if not self.sasl.sec_query(self, '-ENCRYPTION, PLAIN'):
                raise SASLCancelled(self.sasl, self)
        else:
            if not self.sasl.sec_query(self, '+ENCRYPTION, PLAIN'):
                raise SASLCancelled(self.sasl, self)

        self.check_values(['username', 'password'])

    def prep(self):
        """
        Prepare for processing by deleting the password if
        the user has not approved storing it in the clear.
        """
        if 'savepass' not in self.values:
            if self.sasl.sec_query(self, 'CLEAR-PASSWORD'):
                self.values['savepass'] = True

        if 'savepass' not in self.values:
            del self.values['password']

        return True

    def process(self, challenge=None):
        """
        Process a challenge request and return the response.

        :param challenge: A challenge issued by the server that
                          must be answered for authentication.
        """
        user = bytes(self.values['username'])
        password = bytes(self.values['password'])
        return b'\x00' + user + b'\x00' + password

    def okay(self):
        """
        Mutual authentication is not supported by PLAIN.

        :returns: ``True``
        """
        return True


register_mechanism('PLAIN', 5, PLAIN, use_hashes=False)