summaryrefslogtreecommitdiff
path: root/cauth/auth/password.py
blob: 25f89f431c7d1b6d86e8ad0842939ea32af22150 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/usr/bin/env python
#
# Copyright (C) 2015 eNovance SAS <licensing@enovance.com>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.


import crypt
import ldap
import logging
import requests

from basicauth import encode
try:
    import keystoneclient as kc
except ImportError:
    kc = None

from cauth.auth import base
from cauth.utils import transaction


"""password-based authentication plugins."""


class BasePasswordAuthPlugin(base.AuthProtocolPlugin):

    log = logging.getLogger("cauth.BasePasswordAuthPlugin")

    @classmethod
    def get_args(cls):
        return {'username': {'description': 'user login'},
                'password': {'description': 'user password'}}


class LocalUserAuthPlugin(BasePasswordAuthPlugin):
    """User authentication using the cauth config file.
    """

    _config_section = "users"
    log = logging.getLogger("cauth.LocalUserAuthPlugin")

    def authenticate(self, **auth_context):
        username = auth_context.get('username', '')
        password = auth_context.get('password', '')
        user = self.conf.get(username)
        if user:
            email = user.get('mail')
            if email and isinstance(email, bytes):
                email = email.decode('utf-8')
            salted_password = user.get('password')
            if salted_password == crypt.crypt(password, salted_password):
                return {'login': username,
                        'email': email,
                        'name': user.get('lastname'),
                        'ssh_keys': [],
                        'external_auth': {'domain': self.get_domain(),
                                          'external_id': username}}
            else:
                raise base.UnauthenticatedError("Bad password")
        raise base.UnauthenticatedError("User not found")

    def get_domain(self):
        return "CAUTH_CONF"


class LDAPAuthPlugin(BasePasswordAuthPlugin):
    """User authentication using an LDAP backend.
    """

    _config_section = "ldap"
    log = logging.getLogger("cauth.LDAPAuthPlugin")

    def get_domain(self):
        return self.conf['host']

    def authenticate(self, **auth_context):
        transactionID = transaction.ensure_tid(auth_context)
        username = auth_context.get('username', '')
        password = auth_context.get('password', '')
        self.tdebug("Initializing LDAP connection", transactionID)
        try:
            conn = ldap.initialize(self.conf['host'])
            conn.set_option(ldap.OPT_REFERRALS, 0)
        except ldap.LDAPError as e:
            if getattr(self, 'standalone', True):
                self.terror("Client unable to bind on LDAP: %s",
                            transactionID, e)
            raise base.UnauthenticatedError("LDAP error")
        if not password or not username:
            if getattr(self, 'standalone', True):
                self.terror("Client unable to bind on LDAP empty credentials",
                            transactionID)
            raise base.UnauthenticatedError("LDAP error")
        who = self.conf['dn'] % {'username': username}
        self.tdebug("attempting LDAP binding", transactionID)
        try:
            conn.simple_bind_s(who, password)
        except (ldap.INVALID_CREDENTIALS, ldap.SERVER_DOWN):
            if getattr(self, 'standalone', True):
                self.terror("Client unable to bind due to invalid credentials",
                            transactionID)
            raise base.UnauthenticatedError("LDAP error")

        basedn = self.conf.get('basedn', who)
        sfilter = self.conf.get('sfilter', '(cn=*)') % {'username': username}
        result = conn.search_s(basedn, ldap.SCOPE_SUBTREE, sfilter,
                               attrlist=[self.conf['sn'], self.conf['mail']])
        if len(result) == 1:
            user = result[0]  # user is a tuple
            email = user[1].get(self.conf['mail'], [None])[0]
            if email and isinstance(email, bytes):
                email = email.decode('utf-8')
            lastname = user[1].get(self.conf['sn'], [None])
            return {'login': username,
                    'email': email,
                    'name': lastname[0],
                    'ssh_keys': [],
                    'external_auth': {'domain': self.get_domain(),
                                      'external_id': who}}
        if getattr(self, 'standalone', True):
            self.terror("LDAP search returned %i result(s)",
                        transactionID, len(result))
        raise base.UnauthenticatedError("Unauthorized")


class ManageSFAuthPlugin(BasePasswordAuthPlugin):
    """User authentication using the ManageSF local db backend.
    """

    _config_section = "localdb"
    log = logging.getLogger("cauth.ManageSFAuthPlugin")

    def authenticate(self, **auth_context):
        transactionID = transaction.ensure_tid(auth_context)
        username = auth_context.get('username', '')
        password = auth_context.get('password', '')
        bind_url = self.conf['managesf_url'].rstrip('/') + '/bind'
        headers = {"Authorization": encode(username.encode('utf8'),
                                           password.encode('utf8'))}
        self.tdebug("Binding to managesf", transactionID)
        response = requests.get(bind_url, headers=headers)
        if response.status_code > 399:
            self.tdebug("localdb auth failed for user %s, reason: %s",
                        transactionID, username, response.status_code)
            raise base.UnauthenticatedError(
                "Unauthorized, %d" % response.status_code)
        infos = response.json()
        return {'login': username,
                'email': infos['email'],
                'name': infos['fullname'],
                'ssh_keys': [{'key': infos['sshkey']}, ],
                'external_auth': {'domain': self.get_domain(),
                                  # username is the primary key
                                  'external_id': username}}

    def get_domain(self):
        # Remove port number
        return ":".join(self.conf['managesf_url'].split(':')[:2])


class KeystoneAuthPlugin(BasePasswordAuthPlugin):
    """User authentication using the ManageSF local db backend.
    """

    _config_section = "keystone"
    log = logging.getLogger("cauth.KeystoneAuthPlugin")

    def get_domain(self):
        return self.conf['auth_url']

    def authenticate(self, **auth_context):
        """Authentication against a keystone server. We simply try to fetch an
        unscoped token."""
        transactionID = transaction.ensure_tid(auth_context)
        username = auth_context.get('username', '')
        password = auth_context.get('password', '')
        auth_url = self.conf['auth_url']
        if kc:
            self.tdebug('Connecting to keystone server', transactionID)
            try:
                client = kc.client.Client(auth_url=auth_url,
                                          username=username,
                                          password=password)
                if client.authenticate():
                    # TODO(mhu) keystone can store a user's e-mail, but with
                    # default keystone policies this info can only be fetched
                    # by an admin account. Either patch keystone to allow
                    # a user to fetch her own info, or add admin auth to this
                    # plugin in order to fetch the e-mail.
                    external_id = client.user_id or username
                    return {'login': username,
                            'email': '',
                            'name': username,
                            'ssh_keys': [],
                            'external_auth': {'domain': self.get_domain(),
                                              'external_id': external_id}}
            except kc.exceptions.Unauthorized:
                self.terror("keystone authentication failed for user %s",
                            transactionID, username)
                raise base.UnauthenticatedError("Unauthorized")
            except Exception:
                self.texception("Unknown error", transactionID)
                raise base.UnauthenticatedError("Unauthorized")
        else:
            msg = "keystone authentication not available on this server"
            raise base.UnauthenticatedError(msg)
        # every other case
        raise base.UnauthenticatedError("Unauthorized")


class PasswordAuthPlugin(BasePasswordAuthPlugin):
    """Generic password authentication, using all the specific plugins.
    """

    _config_section = None
    log = logging.getLogger("cauth.PasswordAuthPlugin")
    name = "password"

    def __init__(self, conf):
        self.plugins = []
        plugins_list = [LocalUserAuthPlugin,
                        ManageSFAuthPlugin,
                        LDAPAuthPlugin]
        if kc:
            plugins_list.append(KeystoneAuthPlugin)
        for plugin in plugins_list:
            try:
                pg_instance = plugin(conf)
                pg_instance.standalone = False
                self.plugins.append(pg_instance)
            except base.AuthProtocolNotAvailableError:
                # Just skip unavailable protocols
                pass

    def configure_plugin(self, conf):
        pass

    def get_domain(self):
        pass

    def authenticate(self, **auth_context):
        username = auth_context.get('username')
        transactionID = transaction.ensure_tid(auth_context)
        if not username:
            self.terror("Missing username", transactionID)
            raise base.UnauthenticatedError("Unauthorized")
        user = None
        errors = []
        for plugin in self.plugins:
            try:
                user = plugin.authenticate(**auth_context)
            except base.UnauthenticatedError as e:
                errors.append("[%s: %s]" % (plugin.name, e))
        if user:
            user['transactionID'] = transactionID
            return user
        self.tinfo("Password authentication failed for user %s: %s",
                   transactionID, username, ", ".join(errors))
        raise base.UnauthenticatedError('Password authentication failed')