aboutsummaryrefslogtreecommitdiff
path: root/newspipe/controllers/user.py
blob: e259940e40c4741305751b19de7f68bb11a92788 (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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import logging
from urllib.parse import urlparse

from werkzeug.security import check_password_hash
from werkzeug.security import generate_password_hash

from .abstract import AbstractController
from newspipe.models import User

logger = logging.getLogger(__name__)

# FOR LDAP
# Reference: session_app
import ldap3
from ldap3.core.exceptions import LDAPBindError, LDAPPasswordIsMandatoryError


class UserController(AbstractController):
    _db_cls = User
    _user_id_key = "id"

    def _handle_password(self, attrs):
        if attrs.get("password"):
            attrs["pwdhash"] = generate_password_hash(attrs.pop("password"))
        elif "password" in attrs:
            del attrs["password"]

    def check_password(self, user, password):
        return check_password_hash(user.pwdhash, password)

    def create(self, **attrs):
        self._handle_password(attrs)
        return super().create(**attrs)

    def update(self, filters, attrs):
        self._handle_password(attrs)
        return super().update(filters, attrs)


class LdapuserController:
    def check_password(self, user, password, config):
        this_uri = self.get_next_ldap_server(config)
        # return this_uri
        this_user = self.list_matching_users(
            server_uri=this_uri,
            bind_dn=config["LDAP_BIND_DN"],
            bind_pw=config["LDAP_BIND_PASSWORD"],
            user_base=config["LDAP_USER_BASE"],
            username=user,
            user_match_attrib=config["LDAP_USER_MATCH_ATTRIB"],
            _filter=config["LDAP_FILTER"] if "LDAP_FILTER" in config else "",
        )
        # list_matching_users always returns list, so if it contains <> 1 we are in trouble
        if len(this_user) != 1:
            print(
                f"WARNING: cannot determine unique user for {config['LDAP_USER_MATCH_ATTRIB']}={user} which returned {this_user}"
            )
            return False
        # logger does not work here+flask for some reason. Very sad!
        # now we have exactly one user, this_user[0]
        this_user = this_user[0]

        ldapuser = self.authenticated_user(
            server_uri=this_uri, user_dn=this_user, password=password
        )
        if ldapuser:
            return ldapuser
        # return str(config)
        # return this_user
        return False

    def get_next_ldap_server(self, config):
        # on first ldap_login attempt, cache this lookup result:
        if "LDAP_HOSTS" not in config:
            this_domain = urlparse(config["LDAP_URI"]).hostname
            config["LDAP_HOSTS"] = self.list_ldap_servers_for_domain(this_domain)
        else:
            # rotate them! So every ldap_login attempt will use the next ldap server in the list.
            this_list = config["LDAP_HOSTS"]
            a = this_list[0]
            this_list.append(a)
            this_list.pop(0)
            config["LDAP_HOSTS"] = this_list
        # construct a new, full uri.
        this_netloc = config["LDAP_HOSTS"][0]
        up = urlparse(config["LDAP_URI"])
        if up.port:
            this_netloc += f":{up.port}"
        this_uri = up._replace(netloc=this_netloc).geturl()
        return this_uri

    def list_matching_users(
        self,
        server_uri="",
        bind_dn="",
        bind_pw="",
        connection=None,
        user_base="",
        username="",
        user_match_attrib="",
        _filter="",
    ):
        search_filter = f"({user_match_attrib}={username})"
        if _filter:
            search_filter = f"(&{search_filter}{_filter})"
        if connection and isinstance(connection, ldap3.core.connection.Connection):
            conn = connection
        else:
            conn = self.get_ldap_connection(server_uri, bind_dn, bind_pw)
        conn.search(
            search_base=user_base, search_filter=search_filter, search_scope="SUBTREE"
        )
        print(f"DEBUG: search_base {user_base}")
        print(f"DEBUG: search_filter {search_filter}")
        result = []
        for i in conn.entries:
            result.append(i.entry_dn)
        print(f"DEBUG: result {result}")
        return result

    def get_ldap_connection(self, server_uri, bind_dn, bind_pw):
        server = ldap3.Server(server_uri)
        conn = ldap3.Connection(server, auto_bind=True, user=bind_dn, password=bind_pw)
        return conn

    def list_ldap_servers_for_domain(self, domain):
        # return list of hostnames from the _ldap._tcp.{domain} SRV lookup
        try:
            import dns
            import dns.resolver
        except:
            print("Need python3-dns or dnspython installed for dns lookups.")
            return [domain]
        namelist = []
        try:
            query = dns.resolver.query(f"_ldap._tcp.{domain}", "SRV")
        except dns.resolver.NXDOMAIN:
            # no records exist that match the request, so we were probably given a specific hostname, and an empty query will trigger the logic below that will add the original domain to the list.
            query = []
        for i in query:
            namelist.append(i.target.to_text().rstrip("."))
        if not len(namelist):
            namelist.append(domain)
        return namelist

    def ldap_login(self, username, password):
        # print(f"DEBUG: Trying user {username} with pw '{password}'")
        this_uri = self.get_next_ldap_server(app)
        # Perform the ldap interactions
        user = self.authenticated_user(
            server_uri=this_uri, user_dn=username, password=password
        )
        if user:
            return user
        else:
            return False
        return False

    def authenticated_user(self, server_uri, user_dn, password):
        print(f"server_uri: {server_uri}")
        print(f"user_dn: {user_dn}")
        try:
            conn = self.get_ldap_connection(server_uri, user_dn, password)
            return conn
        except LDAPBindError as e:
            if "invalidCredentials" in str(e):
                print("Invalid credentials.")
                return False
            else:
                raise e
        # except (LDAPPasswordIsMandatoryError, LDAPBindError):
        #   print("Either an ldap password is required, or we had another bind error.")
        #   return False
        return False
bgstack15