##// END OF EJS Templates
auth-plugins: remove dead code
marcink -
r1621:5c0e2921 default
parent child Browse files
Show More
@@ -1,658 +1,656 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2010-2017 RhodeCode GmbH
3 # Copyright (C) 2010-2017 RhodeCode GmbH
4 #
4 #
5 # This program is free software: you can redistribute it and/or modify
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
7 # (only), as published by the Free Software Foundation.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU Affero General Public License
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
16 #
17 # This program is dual-licensed. If you wish to learn more about the
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
20
21 """
21 """
22 Authentication modules
22 Authentication modules
23 """
23 """
24
24
25 import colander
25 import colander
26 import logging
26 import logging
27 import time
27 import time
28 import traceback
28 import traceback
29 import warnings
29 import warnings
30
30
31 from pyramid.threadlocal import get_current_registry
31 from pyramid.threadlocal import get_current_registry
32 from sqlalchemy.ext.hybrid import hybrid_property
32 from sqlalchemy.ext.hybrid import hybrid_property
33
33
34 from rhodecode.authentication.interface import IAuthnPluginRegistry
34 from rhodecode.authentication.interface import IAuthnPluginRegistry
35 from rhodecode.authentication.schema import AuthnPluginSettingsSchemaBase
35 from rhodecode.authentication.schema import AuthnPluginSettingsSchemaBase
36 from rhodecode.lib import caches
36 from rhodecode.lib import caches
37 from rhodecode.lib.auth import PasswordGenerator, _RhodeCodeCryptoBCrypt
37 from rhodecode.lib.auth import PasswordGenerator, _RhodeCodeCryptoBCrypt
38 from rhodecode.lib.utils2 import md5_safe, safe_int
38 from rhodecode.lib.utils2 import md5_safe, safe_int
39 from rhodecode.lib.utils2 import safe_str
39 from rhodecode.lib.utils2 import safe_str
40 from rhodecode.model.db import User
40 from rhodecode.model.db import User
41 from rhodecode.model.meta import Session
41 from rhodecode.model.meta import Session
42 from rhodecode.model.settings import SettingsModel
42 from rhodecode.model.settings import SettingsModel
43 from rhodecode.model.user import UserModel
43 from rhodecode.model.user import UserModel
44 from rhodecode.model.user_group import UserGroupModel
44 from rhodecode.model.user_group import UserGroupModel
45
45
46
46
47 log = logging.getLogger(__name__)
47 log = logging.getLogger(__name__)
48
48
49 # auth types that authenticate() function can receive
49 # auth types that authenticate() function can receive
50 VCS_TYPE = 'vcs'
50 VCS_TYPE = 'vcs'
51 HTTP_TYPE = 'http'
51 HTTP_TYPE = 'http'
52
52
53
53
54 class LazyFormencode(object):
54 class LazyFormencode(object):
55 def __init__(self, formencode_obj, *args, **kwargs):
55 def __init__(self, formencode_obj, *args, **kwargs):
56 self.formencode_obj = formencode_obj
56 self.formencode_obj = formencode_obj
57 self.args = args
57 self.args = args
58 self.kwargs = kwargs
58 self.kwargs = kwargs
59
59
60 def __call__(self, *args, **kwargs):
60 def __call__(self, *args, **kwargs):
61 from inspect import isfunction
61 from inspect import isfunction
62 formencode_obj = self.formencode_obj
62 formencode_obj = self.formencode_obj
63 if isfunction(formencode_obj):
63 if isfunction(formencode_obj):
64 # case we wrap validators into functions
64 # case we wrap validators into functions
65 formencode_obj = self.formencode_obj(*args, **kwargs)
65 formencode_obj = self.formencode_obj(*args, **kwargs)
66 return formencode_obj(*self.args, **self.kwargs)
66 return formencode_obj(*self.args, **self.kwargs)
67
67
68
68
69 class RhodeCodeAuthPluginBase(object):
69 class RhodeCodeAuthPluginBase(object):
70 # cache the authentication request for N amount of seconds. Some kind
70 # cache the authentication request for N amount of seconds. Some kind
71 # of authentication methods are very heavy and it's very efficient to cache
71 # of authentication methods are very heavy and it's very efficient to cache
72 # the result of a call. If it's set to None (default) cache is off
72 # the result of a call. If it's set to None (default) cache is off
73 AUTH_CACHE_TTL = None
73 AUTH_CACHE_TTL = None
74 AUTH_CACHE = {}
74 AUTH_CACHE = {}
75
75
76 auth_func_attrs = {
76 auth_func_attrs = {
77 "username": "unique username",
77 "username": "unique username",
78 "firstname": "first name",
78 "firstname": "first name",
79 "lastname": "last name",
79 "lastname": "last name",
80 "email": "email address",
80 "email": "email address",
81 "groups": '["list", "of", "groups"]',
81 "groups": '["list", "of", "groups"]',
82 "extern_name": "name in external source of record",
82 "extern_name": "name in external source of record",
83 "extern_type": "type of external source of record",
83 "extern_type": "type of external source of record",
84 "admin": 'True|False defines if user should be RhodeCode super admin',
84 "admin": 'True|False defines if user should be RhodeCode super admin',
85 "active":
85 "active":
86 'True|False defines active state of user internally for RhodeCode',
86 'True|False defines active state of user internally for RhodeCode',
87 "active_from_extern":
87 "active_from_extern":
88 "True|False\None, active state from the external auth, "
88 "True|False\None, active state from the external auth, "
89 "None means use definition from RhodeCode extern_type active value"
89 "None means use definition from RhodeCode extern_type active value"
90 }
90 }
91 # set on authenticate() method and via set_auth_type func.
91 # set on authenticate() method and via set_auth_type func.
92 auth_type = None
92 auth_type = None
93
93
94 # set on authenticate() method and via set_calling_scope_repo, this is a
94 # set on authenticate() method and via set_calling_scope_repo, this is a
95 # calling scope repository when doing authentication most likely on VCS
95 # calling scope repository when doing authentication most likely on VCS
96 # operations
96 # operations
97 acl_repo_name = None
97 acl_repo_name = None
98
98
99 # List of setting names to store encrypted. Plugins may override this list
99 # List of setting names to store encrypted. Plugins may override this list
100 # to store settings encrypted.
100 # to store settings encrypted.
101 _settings_encrypted = []
101 _settings_encrypted = []
102
102
103 # Mapping of python to DB settings model types. Plugins may override or
103 # Mapping of python to DB settings model types. Plugins may override or
104 # extend this mapping.
104 # extend this mapping.
105 _settings_type_map = {
105 _settings_type_map = {
106 colander.String: 'unicode',
106 colander.String: 'unicode',
107 colander.Integer: 'int',
107 colander.Integer: 'int',
108 colander.Boolean: 'bool',
108 colander.Boolean: 'bool',
109 colander.List: 'list',
109 colander.List: 'list',
110 }
110 }
111
111
112 def __init__(self, plugin_id):
112 def __init__(self, plugin_id):
113 self._plugin_id = plugin_id
113 self._plugin_id = plugin_id
114
114
115 def __str__(self):
115 def __str__(self):
116 return self.get_id()
116 return self.get_id()
117
117
118 def _get_setting_full_name(self, name):
118 def _get_setting_full_name(self, name):
119 """
119 """
120 Return the full setting name used for storing values in the database.
120 Return the full setting name used for storing values in the database.
121 """
121 """
122 # TODO: johbo: Using the name here is problematic. It would be good to
122 # TODO: johbo: Using the name here is problematic. It would be good to
123 # introduce either new models in the database to hold Plugin and
123 # introduce either new models in the database to hold Plugin and
124 # PluginSetting or to use the plugin id here.
124 # PluginSetting or to use the plugin id here.
125 return 'auth_{}_{}'.format(self.name, name)
125 return 'auth_{}_{}'.format(self.name, name)
126
126
127 def _get_setting_type(self, name):
127 def _get_setting_type(self, name):
128 """
128 """
129 Return the type of a setting. This type is defined by the SettingsModel
129 Return the type of a setting. This type is defined by the SettingsModel
130 and determines how the setting is stored in DB. Optionally the suffix
130 and determines how the setting is stored in DB. Optionally the suffix
131 `.encrypted` is appended to instruct SettingsModel to store it
131 `.encrypted` is appended to instruct SettingsModel to store it
132 encrypted.
132 encrypted.
133 """
133 """
134 schema_node = self.get_settings_schema().get(name)
134 schema_node = self.get_settings_schema().get(name)
135 db_type = self._settings_type_map.get(
135 db_type = self._settings_type_map.get(
136 type(schema_node.typ), 'unicode')
136 type(schema_node.typ), 'unicode')
137 if name in self._settings_encrypted:
137 if name in self._settings_encrypted:
138 db_type = '{}.encrypted'.format(db_type)
138 db_type = '{}.encrypted'.format(db_type)
139 return db_type
139 return db_type
140
140
141 def is_enabled(self):
141 def is_enabled(self):
142 """
142 """
143 Returns true if this plugin is enabled. An enabled plugin can be
143 Returns true if this plugin is enabled. An enabled plugin can be
144 configured in the admin interface but it is not consulted during
144 configured in the admin interface but it is not consulted during
145 authentication.
145 authentication.
146 """
146 """
147 auth_plugins = SettingsModel().get_auth_plugins()
147 auth_plugins = SettingsModel().get_auth_plugins()
148 return self.get_id() in auth_plugins
148 return self.get_id() in auth_plugins
149
149
150 def is_active(self):
150 def is_active(self):
151 """
151 """
152 Returns true if the plugin is activated. An activated plugin is
152 Returns true if the plugin is activated. An activated plugin is
153 consulted during authentication, assumed it is also enabled.
153 consulted during authentication, assumed it is also enabled.
154 """
154 """
155 return self.get_setting_by_name('enabled')
155 return self.get_setting_by_name('enabled')
156
156
157 def get_id(self):
157 def get_id(self):
158 """
158 """
159 Returns the plugin id.
159 Returns the plugin id.
160 """
160 """
161 return self._plugin_id
161 return self._plugin_id
162
162
163 def get_display_name(self):
163 def get_display_name(self):
164 """
164 """
165 Returns a translation string for displaying purposes.
165 Returns a translation string for displaying purposes.
166 """
166 """
167 raise NotImplementedError('Not implemented in base class')
167 raise NotImplementedError('Not implemented in base class')
168
168
169 def get_settings_schema(self):
169 def get_settings_schema(self):
170 """
170 """
171 Returns a colander schema, representing the plugin settings.
171 Returns a colander schema, representing the plugin settings.
172 """
172 """
173 return AuthnPluginSettingsSchemaBase()
173 return AuthnPluginSettingsSchemaBase()
174
174
175 def get_setting_by_name(self, name, default=None):
175 def get_setting_by_name(self, name, default=None):
176 """
176 """
177 Returns a plugin setting by name.
177 Returns a plugin setting by name.
178 """
178 """
179 full_name = self._get_setting_full_name(name)
179 full_name = self._get_setting_full_name(name)
180 db_setting = SettingsModel().get_setting_by_name(full_name)
180 db_setting = SettingsModel().get_setting_by_name(full_name)
181 return db_setting.app_settings_value if db_setting else default
181 return db_setting.app_settings_value if db_setting else default
182
182
183 def create_or_update_setting(self, name, value):
183 def create_or_update_setting(self, name, value):
184 """
184 """
185 Create or update a setting for this plugin in the persistent storage.
185 Create or update a setting for this plugin in the persistent storage.
186 """
186 """
187 full_name = self._get_setting_full_name(name)
187 full_name = self._get_setting_full_name(name)
188 type_ = self._get_setting_type(name)
188 type_ = self._get_setting_type(name)
189 db_setting = SettingsModel().create_or_update_setting(
189 db_setting = SettingsModel().create_or_update_setting(
190 full_name, value, type_)
190 full_name, value, type_)
191 return db_setting.app_settings_value
191 return db_setting.app_settings_value
192
192
193 def get_settings(self):
193 def get_settings(self):
194 """
194 """
195 Returns the plugin settings as dictionary.
195 Returns the plugin settings as dictionary.
196 """
196 """
197 settings = {}
197 settings = {}
198 for node in self.get_settings_schema():
198 for node in self.get_settings_schema():
199 settings[node.name] = self.get_setting_by_name(node.name)
199 settings[node.name] = self.get_setting_by_name(node.name)
200 return settings
200 return settings
201
201
202 @property
202 @property
203 def validators(self):
203 def validators(self):
204 """
204 """
205 Exposes RhodeCode validators modules
205 Exposes RhodeCode validators modules
206 """
206 """
207 # this is a hack to overcome issues with pylons threadlocals and
207 # this is a hack to overcome issues with pylons threadlocals and
208 # translator object _() not beein registered properly.
208 # translator object _() not beein registered properly.
209 class LazyCaller(object):
209 class LazyCaller(object):
210 def __init__(self, name):
210 def __init__(self, name):
211 self.validator_name = name
211 self.validator_name = name
212
212
213 def __call__(self, *args, **kwargs):
213 def __call__(self, *args, **kwargs):
214 from rhodecode.model import validators as v
214 from rhodecode.model import validators as v
215 obj = getattr(v, self.validator_name)
215 obj = getattr(v, self.validator_name)
216 # log.debug('Initializing lazy formencode object: %s', obj)
216 # log.debug('Initializing lazy formencode object: %s', obj)
217 return LazyFormencode(obj, *args, **kwargs)
217 return LazyFormencode(obj, *args, **kwargs)
218
218
219 class ProxyGet(object):
219 class ProxyGet(object):
220 def __getattribute__(self, name):
220 def __getattribute__(self, name):
221 return LazyCaller(name)
221 return LazyCaller(name)
222
222
223 return ProxyGet()
223 return ProxyGet()
224
224
225 @hybrid_property
225 @hybrid_property
226 def name(self):
226 def name(self):
227 """
227 """
228 Returns the name of this authentication plugin.
228 Returns the name of this authentication plugin.
229
229
230 :returns: string
230 :returns: string
231 """
231 """
232 raise NotImplementedError("Not implemented in base class")
232 raise NotImplementedError("Not implemented in base class")
233
233
234 def get_url_slug(self):
234 def get_url_slug(self):
235 """
235 """
236 Returns a slug which should be used when constructing URLs which refer
236 Returns a slug which should be used when constructing URLs which refer
237 to this plugin. By default it returns the plugin name. If the name is
237 to this plugin. By default it returns the plugin name. If the name is
238 not suitable for using it in an URL the plugin should override this
238 not suitable for using it in an URL the plugin should override this
239 method.
239 method.
240 """
240 """
241 return self.name
241 return self.name
242
242
243 @property
243 @property
244 def is_headers_auth(self):
244 def is_headers_auth(self):
245 """
245 """
246 Returns True if this authentication plugin uses HTTP headers as
246 Returns True if this authentication plugin uses HTTP headers as
247 authentication method.
247 authentication method.
248 """
248 """
249 return False
249 return False
250
250
251 @hybrid_property
251 @hybrid_property
252 def is_container_auth(self):
252 def is_container_auth(self):
253 """
253 """
254 Deprecated method that indicates if this authentication plugin uses
254 Deprecated method that indicates if this authentication plugin uses
255 HTTP headers as authentication method.
255 HTTP headers as authentication method.
256 """
256 """
257 warnings.warn(
257 warnings.warn(
258 'Use is_headers_auth instead.', category=DeprecationWarning)
258 'Use is_headers_auth instead.', category=DeprecationWarning)
259 return self.is_headers_auth
259 return self.is_headers_auth
260
260
261 @hybrid_property
261 @hybrid_property
262 def allows_creating_users(self):
262 def allows_creating_users(self):
263 """
263 """
264 Defines if Plugin allows users to be created on-the-fly when
264 Defines if Plugin allows users to be created on-the-fly when
265 authentication is called. Controls how external plugins should behave
265 authentication is called. Controls how external plugins should behave
266 in terms if they are allowed to create new users, or not. Base plugins
266 in terms if they are allowed to create new users, or not. Base plugins
267 should not be allowed to, but External ones should be !
267 should not be allowed to, but External ones should be !
268
268
269 :return: bool
269 :return: bool
270 """
270 """
271 return False
271 return False
272
272
273 def set_auth_type(self, auth_type):
273 def set_auth_type(self, auth_type):
274 self.auth_type = auth_type
274 self.auth_type = auth_type
275
275
276 def set_calling_scope_repo(self, acl_repo_name):
276 def set_calling_scope_repo(self, acl_repo_name):
277 self.acl_repo_name = acl_repo_name
277 self.acl_repo_name = acl_repo_name
278
278
279 def allows_authentication_from(
279 def allows_authentication_from(
280 self, user, allows_non_existing_user=True,
280 self, user, allows_non_existing_user=True,
281 allowed_auth_plugins=None, allowed_auth_sources=None):
281 allowed_auth_plugins=None, allowed_auth_sources=None):
282 """
282 """
283 Checks if this authentication module should accept a request for
283 Checks if this authentication module should accept a request for
284 the current user.
284 the current user.
285
285
286 :param user: user object fetched using plugin's get_user() method.
286 :param user: user object fetched using plugin's get_user() method.
287 :param allows_non_existing_user: if True, don't allow the
287 :param allows_non_existing_user: if True, don't allow the
288 user to be empty, meaning not existing in our database
288 user to be empty, meaning not existing in our database
289 :param allowed_auth_plugins: if provided, users extern_type will be
289 :param allowed_auth_plugins: if provided, users extern_type will be
290 checked against a list of provided extern types, which are plugin
290 checked against a list of provided extern types, which are plugin
291 auth_names in the end
291 auth_names in the end
292 :param allowed_auth_sources: authentication type allowed,
292 :param allowed_auth_sources: authentication type allowed,
293 `http` or `vcs` default is both.
293 `http` or `vcs` default is both.
294 defines if plugin will accept only http authentication vcs
294 defines if plugin will accept only http authentication vcs
295 authentication(git/hg) or both
295 authentication(git/hg) or both
296 :returns: boolean
296 :returns: boolean
297 """
297 """
298 if not user and not allows_non_existing_user:
298 if not user and not allows_non_existing_user:
299 log.debug('User is empty but plugin does not allow empty users,'
299 log.debug('User is empty but plugin does not allow empty users,'
300 'not allowed to authenticate')
300 'not allowed to authenticate')
301 return False
301 return False
302
302
303 expected_auth_plugins = allowed_auth_plugins or [self.name]
303 expected_auth_plugins = allowed_auth_plugins or [self.name]
304 if user and (user.extern_type and
304 if user and (user.extern_type and
305 user.extern_type not in expected_auth_plugins):
305 user.extern_type not in expected_auth_plugins):
306 log.debug(
306 log.debug(
307 'User `%s` is bound to `%s` auth type. Plugin allows only '
307 'User `%s` is bound to `%s` auth type. Plugin allows only '
308 '%s, skipping', user, user.extern_type, expected_auth_plugins)
308 '%s, skipping', user, user.extern_type, expected_auth_plugins)
309
309
310 return False
310 return False
311
311
312 # by default accept both
312 # by default accept both
313 expected_auth_from = allowed_auth_sources or [HTTP_TYPE, VCS_TYPE]
313 expected_auth_from = allowed_auth_sources or [HTTP_TYPE, VCS_TYPE]
314 if self.auth_type not in expected_auth_from:
314 if self.auth_type not in expected_auth_from:
315 log.debug('Current auth source is %s but plugin only allows %s',
315 log.debug('Current auth source is %s but plugin only allows %s',
316 self.auth_type, expected_auth_from)
316 self.auth_type, expected_auth_from)
317 return False
317 return False
318
318
319 return True
319 return True
320
320
321 def get_user(self, username=None, **kwargs):
321 def get_user(self, username=None, **kwargs):
322 """
322 """
323 Helper method for user fetching in plugins, by default it's using
323 Helper method for user fetching in plugins, by default it's using
324 simple fetch by username, but this method can be custimized in plugins
324 simple fetch by username, but this method can be custimized in plugins
325 eg. headers auth plugin to fetch user by environ params
325 eg. headers auth plugin to fetch user by environ params
326
326
327 :param username: username if given to fetch from database
327 :param username: username if given to fetch from database
328 :param kwargs: extra arguments needed for user fetching.
328 :param kwargs: extra arguments needed for user fetching.
329 """
329 """
330 user = None
330 user = None
331 log.debug(
331 log.debug(
332 'Trying to fetch user `%s` from RhodeCode database', username)
332 'Trying to fetch user `%s` from RhodeCode database', username)
333 if username:
333 if username:
334 user = User.get_by_username(username)
334 user = User.get_by_username(username)
335 if not user:
335 if not user:
336 log.debug('User not found, fallback to fetch user in '
336 log.debug('User not found, fallback to fetch user in '
337 'case insensitive mode')
337 'case insensitive mode')
338 user = User.get_by_username(username, case_insensitive=True)
338 user = User.get_by_username(username, case_insensitive=True)
339 else:
339 else:
340 log.debug('provided username:`%s` is empty skipping...', username)
340 log.debug('provided username:`%s` is empty skipping...', username)
341 if not user:
341 if not user:
342 log.debug('User `%s` not found in database', username)
342 log.debug('User `%s` not found in database', username)
343 else:
343 else:
344 log.debug('Got DB user:%s', user)
344 log.debug('Got DB user:%s', user)
345 return user
345 return user
346
346
347 def user_activation_state(self):
347 def user_activation_state(self):
348 """
348 """
349 Defines user activation state when creating new users
349 Defines user activation state when creating new users
350
350
351 :returns: boolean
351 :returns: boolean
352 """
352 """
353 raise NotImplementedError("Not implemented in base class")
353 raise NotImplementedError("Not implemented in base class")
354
354
355 def auth(self, userobj, username, passwd, settings, **kwargs):
355 def auth(self, userobj, username, passwd, settings, **kwargs):
356 """
356 """
357 Given a user object (which may be null), username, a plaintext
357 Given a user object (which may be null), username, a plaintext
358 password, and a settings object (containing all the keys needed as
358 password, and a settings object (containing all the keys needed as
359 listed in settings()), authenticate this user's login attempt.
359 listed in settings()), authenticate this user's login attempt.
360
360
361 Return None on failure. On success, return a dictionary of the form:
361 Return None on failure. On success, return a dictionary of the form:
362
362
363 see: RhodeCodeAuthPluginBase.auth_func_attrs
363 see: RhodeCodeAuthPluginBase.auth_func_attrs
364 This is later validated for correctness
364 This is later validated for correctness
365 """
365 """
366 raise NotImplementedError("not implemented in base class")
366 raise NotImplementedError("not implemented in base class")
367
367
368 def _authenticate(self, userobj, username, passwd, settings, **kwargs):
368 def _authenticate(self, userobj, username, passwd, settings, **kwargs):
369 """
369 """
370 Wrapper to call self.auth() that validates call on it
370 Wrapper to call self.auth() that validates call on it
371
371
372 :param userobj: userobj
372 :param userobj: userobj
373 :param username: username
373 :param username: username
374 :param passwd: plaintext password
374 :param passwd: plaintext password
375 :param settings: plugin settings
375 :param settings: plugin settings
376 """
376 """
377 auth = self.auth(userobj, username, passwd, settings, **kwargs)
377 auth = self.auth(userobj, username, passwd, settings, **kwargs)
378 if auth:
378 if auth:
379 # check if hash should be migrated ?
379 # check if hash should be migrated ?
380 new_hash = auth.get('_hash_migrate')
380 new_hash = auth.get('_hash_migrate')
381 if new_hash:
381 if new_hash:
382 self._migrate_hash_to_bcrypt(username, passwd, new_hash)
382 self._migrate_hash_to_bcrypt(username, passwd, new_hash)
383 return self._validate_auth_return(auth)
383 return self._validate_auth_return(auth)
384 return auth
384 return auth
385
385
386 def _migrate_hash_to_bcrypt(self, username, password, new_hash):
386 def _migrate_hash_to_bcrypt(self, username, password, new_hash):
387 new_hash_cypher = _RhodeCodeCryptoBCrypt()
387 new_hash_cypher = _RhodeCodeCryptoBCrypt()
388 # extra checks, so make sure new hash is correct.
388 # extra checks, so make sure new hash is correct.
389 password_encoded = safe_str(password)
389 password_encoded = safe_str(password)
390 if new_hash and new_hash_cypher.hash_check(
390 if new_hash and new_hash_cypher.hash_check(
391 password_encoded, new_hash):
391 password_encoded, new_hash):
392 cur_user = User.get_by_username(username)
392 cur_user = User.get_by_username(username)
393 cur_user.password = new_hash
393 cur_user.password = new_hash
394 Session().add(cur_user)
394 Session().add(cur_user)
395 Session().flush()
395 Session().flush()
396 log.info('Migrated user %s hash to bcrypt', cur_user)
396 log.info('Migrated user %s hash to bcrypt', cur_user)
397
397
398 def _validate_auth_return(self, ret):
398 def _validate_auth_return(self, ret):
399 if not isinstance(ret, dict):
399 if not isinstance(ret, dict):
400 raise Exception('returned value from auth must be a dict')
400 raise Exception('returned value from auth must be a dict')
401 for k in self.auth_func_attrs:
401 for k in self.auth_func_attrs:
402 if k not in ret:
402 if k not in ret:
403 raise Exception('Missing %s attribute from returned data' % k)
403 raise Exception('Missing %s attribute from returned data' % k)
404 return ret
404 return ret
405
405
406
406
407 class RhodeCodeExternalAuthPlugin(RhodeCodeAuthPluginBase):
407 class RhodeCodeExternalAuthPlugin(RhodeCodeAuthPluginBase):
408
408
409 @hybrid_property
409 @hybrid_property
410 def allows_creating_users(self):
410 def allows_creating_users(self):
411 return True
411 return True
412
412
413 def use_fake_password(self):
413 def use_fake_password(self):
414 """
414 """
415 Return a boolean that indicates whether or not we should set the user's
415 Return a boolean that indicates whether or not we should set the user's
416 password to a random value when it is authenticated by this plugin.
416 password to a random value when it is authenticated by this plugin.
417 If your plugin provides authentication, then you will generally
417 If your plugin provides authentication, then you will generally
418 want this.
418 want this.
419
419
420 :returns: boolean
420 :returns: boolean
421 """
421 """
422 raise NotImplementedError("Not implemented in base class")
422 raise NotImplementedError("Not implemented in base class")
423
423
424 def _authenticate(self, userobj, username, passwd, settings, **kwargs):
424 def _authenticate(self, userobj, username, passwd, settings, **kwargs):
425 # at this point _authenticate calls plugin's `auth()` function
425 # at this point _authenticate calls plugin's `auth()` function
426 auth = super(RhodeCodeExternalAuthPlugin, self)._authenticate(
426 auth = super(RhodeCodeExternalAuthPlugin, self)._authenticate(
427 userobj, username, passwd, settings, **kwargs)
427 userobj, username, passwd, settings, **kwargs)
428 if auth:
428 if auth:
429 # maybe plugin will clean the username ?
429 # maybe plugin will clean the username ?
430 # we should use the return value
430 # we should use the return value
431 username = auth['username']
431 username = auth['username']
432
432
433 # if external source tells us that user is not active, we should
433 # if external source tells us that user is not active, we should
434 # skip rest of the process. This can prevent from creating users in
434 # skip rest of the process. This can prevent from creating users in
435 # RhodeCode when using external authentication, but if it's
435 # RhodeCode when using external authentication, but if it's
436 # inactive user we shouldn't create that user anyway
436 # inactive user we shouldn't create that user anyway
437 if auth['active_from_extern'] is False:
437 if auth['active_from_extern'] is False:
438 log.warning(
438 log.warning(
439 "User %s authenticated against %s, but is inactive",
439 "User %s authenticated against %s, but is inactive",
440 username, self.__module__)
440 username, self.__module__)
441 return None
441 return None
442
442
443 cur_user = User.get_by_username(username, case_insensitive=True)
443 cur_user = User.get_by_username(username, case_insensitive=True)
444 is_user_existing = cur_user is not None
444 is_user_existing = cur_user is not None
445
445
446 if is_user_existing:
446 if is_user_existing:
447 log.debug('Syncing user `%s` from '
447 log.debug('Syncing user `%s` from '
448 '`%s` plugin', username, self.name)
448 '`%s` plugin', username, self.name)
449 else:
449 else:
450 log.debug('Creating non existing user `%s` from '
450 log.debug('Creating non existing user `%s` from '
451 '`%s` plugin', username, self.name)
451 '`%s` plugin', username, self.name)
452
452
453 if self.allows_creating_users:
453 if self.allows_creating_users:
454 log.debug('Plugin `%s` allows to '
454 log.debug('Plugin `%s` allows to '
455 'create new users', self.name)
455 'create new users', self.name)
456 else:
456 else:
457 log.debug('Plugin `%s` does not allow to '
457 log.debug('Plugin `%s` does not allow to '
458 'create new users', self.name)
458 'create new users', self.name)
459
459
460 user_parameters = {
460 user_parameters = {
461 'username': username,
461 'username': username,
462 'email': auth["email"],
462 'email': auth["email"],
463 'firstname': auth["firstname"],
463 'firstname': auth["firstname"],
464 'lastname': auth["lastname"],
464 'lastname': auth["lastname"],
465 'active': auth["active"],
465 'active': auth["active"],
466 'admin': auth["admin"],
466 'admin': auth["admin"],
467 'extern_name': auth["extern_name"],
467 'extern_name': auth["extern_name"],
468 'extern_type': self.name,
468 'extern_type': self.name,
469 'plugin': self,
469 'plugin': self,
470 'allow_to_create_user': self.allows_creating_users,
470 'allow_to_create_user': self.allows_creating_users,
471 }
471 }
472
472
473 if not is_user_existing:
473 if not is_user_existing:
474 if self.use_fake_password():
474 if self.use_fake_password():
475 # Randomize the PW because we don't need it, but don't want
475 # Randomize the PW because we don't need it, but don't want
476 # them blank either
476 # them blank either
477 passwd = PasswordGenerator().gen_password(length=16)
477 passwd = PasswordGenerator().gen_password(length=16)
478 user_parameters['password'] = passwd
478 user_parameters['password'] = passwd
479 else:
479 else:
480 # Since the password is required by create_or_update method of
480 # Since the password is required by create_or_update method of
481 # UserModel, we need to set it explicitly.
481 # UserModel, we need to set it explicitly.
482 # The create_or_update method is smart and recognises the
482 # The create_or_update method is smart and recognises the
483 # password hashes as well.
483 # password hashes as well.
484 user_parameters['password'] = cur_user.password
484 user_parameters['password'] = cur_user.password
485
485
486 # we either create or update users, we also pass the flag
486 # we either create or update users, we also pass the flag
487 # that controls if this method can actually do that.
487 # that controls if this method can actually do that.
488 # raises NotAllowedToCreateUserError if it cannot, and we try to.
488 # raises NotAllowedToCreateUserError if it cannot, and we try to.
489 user = UserModel().create_or_update(**user_parameters)
489 user = UserModel().create_or_update(**user_parameters)
490 Session().flush()
490 Session().flush()
491 # enforce user is just in given groups, all of them has to be ones
491 # enforce user is just in given groups, all of them has to be ones
492 # created from plugins. We store this info in _group_data JSON
492 # created from plugins. We store this info in _group_data JSON
493 # field
493 # field
494 try:
494 try:
495 groups = auth['groups'] or []
495 groups = auth['groups'] or []
496 UserGroupModel().enforce_groups(user, groups, self.name)
496 UserGroupModel().enforce_groups(user, groups, self.name)
497 except Exception:
497 except Exception:
498 # for any reason group syncing fails, we should
498 # for any reason group syncing fails, we should
499 # proceed with login
499 # proceed with login
500 log.error(traceback.format_exc())
500 log.error(traceback.format_exc())
501 Session().commit()
501 Session().commit()
502 return auth
502 return auth
503
503
504
504
505 def loadplugin(plugin_id):
505 def loadplugin(plugin_id):
506 """
506 """
507 Loads and returns an instantiated authentication plugin.
507 Loads and returns an instantiated authentication plugin.
508 Returns the RhodeCodeAuthPluginBase subclass on success,
508 Returns the RhodeCodeAuthPluginBase subclass on success,
509 or None on failure.
509 or None on failure.
510 """
510 """
511 # TODO: Disusing pyramids thread locals to retrieve the registry.
511 # TODO: Disusing pyramids thread locals to retrieve the registry.
512 authn_registry = get_authn_registry()
512 authn_registry = get_authn_registry()
513 plugin = authn_registry.get_plugin(plugin_id)
513 plugin = authn_registry.get_plugin(plugin_id)
514 if plugin is None:
514 if plugin is None:
515 log.error('Authentication plugin not found: "%s"', plugin_id)
515 log.error('Authentication plugin not found: "%s"', plugin_id)
516 return plugin
516 return plugin
517
517
518
518
519 def get_authn_registry(registry=None):
519 def get_authn_registry(registry=None):
520 registry = registry or get_current_registry()
520 registry = registry or get_current_registry()
521 authn_registry = registry.getUtility(IAuthnPluginRegistry)
521 authn_registry = registry.getUtility(IAuthnPluginRegistry)
522 return authn_registry
522 return authn_registry
523
523
524
524
525 def get_auth_cache_manager(custom_ttl=None):
525 def get_auth_cache_manager(custom_ttl=None):
526 return caches.get_cache_manager(
526 return caches.get_cache_manager(
527 'auth_plugins', 'rhodecode.authentication', custom_ttl)
527 'auth_plugins', 'rhodecode.authentication', custom_ttl)
528
528
529
529
530 def authenticate(username, password, environ=None, auth_type=None,
530 def authenticate(username, password, environ=None, auth_type=None,
531 skip_missing=False, registry=None, acl_repo_name=None):
531 skip_missing=False, registry=None, acl_repo_name=None):
532 """
532 """
533 Authentication function used for access control,
533 Authentication function used for access control,
534 It tries to authenticate based on enabled authentication modules.
534 It tries to authenticate based on enabled authentication modules.
535
535
536 :param username: username can be empty for headers auth
536 :param username: username can be empty for headers auth
537 :param password: password can be empty for headers auth
537 :param password: password can be empty for headers auth
538 :param environ: environ headers passed for headers auth
538 :param environ: environ headers passed for headers auth
539 :param auth_type: type of authentication, either `HTTP_TYPE` or `VCS_TYPE`
539 :param auth_type: type of authentication, either `HTTP_TYPE` or `VCS_TYPE`
540 :param skip_missing: ignores plugins that are in db but not in environment
540 :param skip_missing: ignores plugins that are in db but not in environment
541 :returns: None if auth failed, plugin_user dict if auth is correct
541 :returns: None if auth failed, plugin_user dict if auth is correct
542 """
542 """
543 if not auth_type or auth_type not in [HTTP_TYPE, VCS_TYPE]:
543 if not auth_type or auth_type not in [HTTP_TYPE, VCS_TYPE]:
544 raise ValueError('auth type must be on of http, vcs got "%s" instead'
544 raise ValueError('auth type must be on of http, vcs got "%s" instead'
545 % auth_type)
545 % auth_type)
546 headers_only = environ and not (username and password)
546 headers_only = environ and not (username and password)
547
547
548 authn_registry = get_authn_registry(registry)
548 authn_registry = get_authn_registry(registry)
549 for plugin in authn_registry.get_plugins_for_authentication():
549 for plugin in authn_registry.get_plugins_for_authentication():
550 plugin.set_auth_type(auth_type)
550 plugin.set_auth_type(auth_type)
551 plugin.set_calling_scope_repo(acl_repo_name)
551 plugin.set_calling_scope_repo(acl_repo_name)
552 user = plugin.get_user(username)
553 display_user = user.username if user else username
554
552
555 if headers_only and not plugin.is_headers_auth:
553 if headers_only and not plugin.is_headers_auth:
556 log.debug('Auth type is for headers only and plugin `%s` is not '
554 log.debug('Auth type is for headers only and plugin `%s` is not '
557 'headers plugin, skipping...', plugin.get_id())
555 'headers plugin, skipping...', plugin.get_id())
558 continue
556 continue
559
557
560 # load plugin settings from RhodeCode database
558 # load plugin settings from RhodeCode database
561 plugin_settings = plugin.get_settings()
559 plugin_settings = plugin.get_settings()
562 log.debug('Plugin settings:%s', plugin_settings)
560 log.debug('Plugin settings:%s', plugin_settings)
563
561
564 log.debug('Trying authentication using ** %s **', plugin.get_id())
562 log.debug('Trying authentication using ** %s **', plugin.get_id())
565 # use plugin's method of user extraction.
563 # use plugin's method of user extraction.
566 user = plugin.get_user(username, environ=environ,
564 user = plugin.get_user(username, environ=environ,
567 settings=plugin_settings)
565 settings=plugin_settings)
568 display_user = user.username if user else username
566 display_user = user.username if user else username
569 log.debug(
567 log.debug(
570 'Plugin %s extracted user is `%s`', plugin.get_id(), display_user)
568 'Plugin %s extracted user is `%s`', plugin.get_id(), display_user)
571
569
572 if not plugin.allows_authentication_from(user):
570 if not plugin.allows_authentication_from(user):
573 log.debug('Plugin %s does not accept user `%s` for authentication',
571 log.debug('Plugin %s does not accept user `%s` for authentication',
574 plugin.get_id(), display_user)
572 plugin.get_id(), display_user)
575 continue
573 continue
576 else:
574 else:
577 log.debug('Plugin %s accepted user `%s` for authentication',
575 log.debug('Plugin %s accepted user `%s` for authentication',
578 plugin.get_id(), display_user)
576 plugin.get_id(), display_user)
579
577
580 log.info('Authenticating user `%s` using %s plugin',
578 log.info('Authenticating user `%s` using %s plugin',
581 display_user, plugin.get_id())
579 display_user, plugin.get_id())
582
580
583 _cache_ttl = 0
581 _cache_ttl = 0
584
582
585 if isinstance(plugin.AUTH_CACHE_TTL, (int, long)):
583 if isinstance(plugin.AUTH_CACHE_TTL, (int, long)):
586 # plugin cache set inside is more important than the settings value
584 # plugin cache set inside is more important than the settings value
587 _cache_ttl = plugin.AUTH_CACHE_TTL
585 _cache_ttl = plugin.AUTH_CACHE_TTL
588 elif plugin_settings.get('cache_ttl'):
586 elif plugin_settings.get('cache_ttl'):
589 _cache_ttl = safe_int(plugin_settings.get('cache_ttl'), 0)
587 _cache_ttl = safe_int(plugin_settings.get('cache_ttl'), 0)
590
588
591 plugin_cache_active = bool(_cache_ttl and _cache_ttl > 0)
589 plugin_cache_active = bool(_cache_ttl and _cache_ttl > 0)
592
590
593 # get instance of cache manager configured for a namespace
591 # get instance of cache manager configured for a namespace
594 cache_manager = get_auth_cache_manager(custom_ttl=_cache_ttl)
592 cache_manager = get_auth_cache_manager(custom_ttl=_cache_ttl)
595
593
596 log.debug('AUTH_CACHE_TTL for plugin `%s` active: %s (TTL: %s)',
594 log.debug('AUTH_CACHE_TTL for plugin `%s` active: %s (TTL: %s)',
597 plugin.get_id(), plugin_cache_active, _cache_ttl)
595 plugin.get_id(), plugin_cache_active, _cache_ttl)
598
596
599 # for environ based password can be empty, but then the validation is
597 # for environ based password can be empty, but then the validation is
600 # on the server that fills in the env data needed for authentication
598 # on the server that fills in the env data needed for authentication
601 _password_hash = md5_safe(plugin.name + username + (password or ''))
599 _password_hash = md5_safe(plugin.name + username + (password or ''))
602
600
603 # _authenticate is a wrapper for .auth() method of plugin.
601 # _authenticate is a wrapper for .auth() method of plugin.
604 # it checks if .auth() sends proper data.
602 # it checks if .auth() sends proper data.
605 # For RhodeCodeExternalAuthPlugin it also maps users to
603 # For RhodeCodeExternalAuthPlugin it also maps users to
606 # Database and maps the attributes returned from .auth()
604 # Database and maps the attributes returned from .auth()
607 # to RhodeCode database. If this function returns data
605 # to RhodeCode database. If this function returns data
608 # then auth is correct.
606 # then auth is correct.
609 start = time.time()
607 start = time.time()
610 log.debug('Running plugin `%s` _authenticate method', plugin.get_id())
608 log.debug('Running plugin `%s` _authenticate method', plugin.get_id())
611
609
612 def auth_func():
610 def auth_func():
613 """
611 """
614 This function is used internally in Cache of Beaker to calculate
612 This function is used internally in Cache of Beaker to calculate
615 Results
613 Results
616 """
614 """
617 return plugin._authenticate(
615 return plugin._authenticate(
618 user, username, password, plugin_settings,
616 user, username, password, plugin_settings,
619 environ=environ or {})
617 environ=environ or {})
620
618
621 if plugin_cache_active:
619 if plugin_cache_active:
622 plugin_user = cache_manager.get(
620 plugin_user = cache_manager.get(
623 _password_hash, createfunc=auth_func)
621 _password_hash, createfunc=auth_func)
624 else:
622 else:
625 plugin_user = auth_func()
623 plugin_user = auth_func()
626
624
627 auth_time = time.time() - start
625 auth_time = time.time() - start
628 log.debug('Authentication for plugin `%s` completed in %.3fs, '
626 log.debug('Authentication for plugin `%s` completed in %.3fs, '
629 'expiration time of fetched cache %.1fs.',
627 'expiration time of fetched cache %.1fs.',
630 plugin.get_id(), auth_time, _cache_ttl)
628 plugin.get_id(), auth_time, _cache_ttl)
631
629
632 log.debug('PLUGIN USER DATA: %s', plugin_user)
630 log.debug('PLUGIN USER DATA: %s', plugin_user)
633
631
634 if plugin_user:
632 if plugin_user:
635 log.debug('Plugin returned proper authentication data')
633 log.debug('Plugin returned proper authentication data')
636 return plugin_user
634 return plugin_user
637 # we failed to Auth because .auth() method didn't return proper user
635 # we failed to Auth because .auth() method didn't return proper user
638 log.debug("User `%s` failed to authenticate against %s",
636 log.debug("User `%s` failed to authenticate against %s",
639 display_user, plugin.get_id())
637 display_user, plugin.get_id())
640 return None
638 return None
641
639
642
640
643 def chop_at(s, sub, inclusive=False):
641 def chop_at(s, sub, inclusive=False):
644 """Truncate string ``s`` at the first occurrence of ``sub``.
642 """Truncate string ``s`` at the first occurrence of ``sub``.
645
643
646 If ``inclusive`` is true, truncate just after ``sub`` rather than at it.
644 If ``inclusive`` is true, truncate just after ``sub`` rather than at it.
647
645
648 >>> chop_at("plutocratic brats", "rat")
646 >>> chop_at("plutocratic brats", "rat")
649 'plutoc'
647 'plutoc'
650 >>> chop_at("plutocratic brats", "rat", True)
648 >>> chop_at("plutocratic brats", "rat", True)
651 'plutocrat'
649 'plutocrat'
652 """
650 """
653 pos = s.find(sub)
651 pos = s.find(sub)
654 if pos == -1:
652 if pos == -1:
655 return s
653 return s
656 if inclusive:
654 if inclusive:
657 return s[:pos+len(sub)]
655 return s[:pos+len(sub)]
658 return s[:pos]
656 return s[:pos]
General Comments 0
You need to be logged in to leave comments. Login now