Show More
@@ -1,78 +1,83 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright (C) 2012-2016 RhodeCode GmbH |
|
3 | # Copyright (C) 2012-2016 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 | import logging |
|
21 | import logging | |
22 |
|
22 | |||
23 | from pyramid.exceptions import ConfigurationError |
|
23 | from pyramid.exceptions import ConfigurationError | |
24 | from zope.interface import implementer |
|
24 | from zope.interface import implementer | |
25 |
|
25 | |||
26 | from rhodecode.authentication.interface import IAuthnPluginRegistry |
|
26 | from rhodecode.authentication.interface import IAuthnPluginRegistry | |
27 | from rhodecode.lib.utils2 import safe_str |
|
27 | from rhodecode.lib.utils2 import safe_str | |
|
28 | from rhodecode.model.settings import SettingsModel | |||
28 |
|
29 | |||
29 | log = logging.getLogger(__name__) |
|
30 | log = logging.getLogger(__name__) | |
30 |
|
31 | |||
31 |
|
32 | |||
32 | @implementer(IAuthnPluginRegistry) |
|
33 | @implementer(IAuthnPluginRegistry) | |
33 | class AuthenticationPluginRegistry(object): |
|
34 | class AuthenticationPluginRegistry(object): | |
34 |
|
35 | |||
35 | # INI settings key to set a fallback authentication plugin. |
|
36 | # INI settings key to set a fallback authentication plugin. | |
36 | fallback_plugin_key = 'rhodecode.auth_plugin_fallback' |
|
37 | fallback_plugin_key = 'rhodecode.auth_plugin_fallback' | |
37 |
|
38 | |||
38 | def __init__(self, settings): |
|
39 | def __init__(self, settings): | |
39 | self._plugins = {} |
|
40 | self._plugins = {} | |
40 | self._fallback_plugin = settings.get(self.fallback_plugin_key, None) |
|
41 | self._fallback_plugin = settings.get(self.fallback_plugin_key, None) | |
41 |
|
42 | |||
42 | def add_authn_plugin(self, config, plugin): |
|
43 | def add_authn_plugin(self, config, plugin): | |
43 | plugin_id = plugin.get_id() |
|
44 | plugin_id = plugin.get_id() | |
44 | if plugin_id in self._plugins.keys(): |
|
45 | if plugin_id in self._plugins.keys(): | |
45 | raise ConfigurationError( |
|
46 | raise ConfigurationError( | |
46 | 'Cannot register authentication plugin twice: "%s"', plugin_id) |
|
47 | 'Cannot register authentication plugin twice: "%s"', plugin_id) | |
47 | else: |
|
48 | else: | |
48 | log.debug('Register authentication plugin: "%s"', plugin_id) |
|
49 | log.debug('Register authentication plugin: "%s"', plugin_id) | |
49 | self._plugins[plugin_id] = plugin |
|
50 | self._plugins[plugin_id] = plugin | |
50 |
|
51 | |||
51 | def get_plugins(self): |
|
52 | def get_plugins(self): | |
52 | def sort_key(plugin): |
|
53 | def sort_key(plugin): | |
53 | return str.lower(safe_str(plugin.get_display_name())) |
|
54 | return str.lower(safe_str(plugin.get_display_name())) | |
54 |
|
55 | |||
55 | return sorted(self._plugins.values(), key=sort_key) |
|
56 | return sorted(self._plugins.values(), key=sort_key) | |
56 |
|
57 | |||
57 | def get_plugin(self, plugin_id): |
|
58 | def get_plugin(self, plugin_id): | |
58 | return self._plugins.get(plugin_id, None) |
|
59 | return self._plugins.get(plugin_id, None) | |
59 |
|
60 | |||
60 | def get_plugins_for_authentication(self): |
|
61 | def get_plugins_for_authentication(self): | |
61 | """ |
|
62 | """ | |
62 | Returns a list of plugins which should be consulted when authenticating |
|
63 | Returns a list of plugins which should be consulted when authenticating | |
63 | a user. It only returns plugins which are enabled and active. |
|
64 | a user. It only returns plugins which are enabled and active. | |
64 | Additionally it includes the fallback plugin from the INI file, if |
|
65 | Additionally it includes the fallback plugin from the INI file, if | |
65 | `rhodecode.auth_plugin_fallback` is set to a plugin ID. |
|
66 | `rhodecode.auth_plugin_fallback` is set to a plugin ID. | |
66 | """ |
|
67 | """ | |
67 | plugins = [] |
|
68 | plugins = [] | |
68 | for plugin in self.get_plugins(): |
|
69 | ||
69 | if (self._fallback_plugin and |
|
70 | # Add all enabled and active plugins to the list. We iterate over the | |
70 | plugin.get_id() == self._fallback_plugin): |
|
71 | # auth_plugins setting from DB beacuse it also represents the ordering. | |
71 | log.warn( |
|
72 | enabled_plugins = SettingsModel().get_auth_plugins() | |
72 | 'Using fallback authentication plugin from INI file: "%s"', |
|
73 | for plugin_id in enabled_plugins: | |
73 |
|
|
74 | plugin = self.get_plugin(plugin_id) | |
|
75 | if plugin is not None and plugin.is_active(): | |||
74 | plugins.append(plugin) |
|
76 | plugins.append(plugin) | |
75 | elif plugin.is_enabled() and plugin.is_active(): |
|
77 | ||
|
78 | # Add the fallback plugin from ini file. | |||
|
79 | plugin = self.get_plugin(self._fallback_plugin) | |||
|
80 | if plugin is not None and plugin not in plugins: | |||
76 |
|
|
81 | plugins.append(plugin) | |
77 |
|
82 | |||
78 | return plugins |
|
83 | return plugins |
General Comments 0
You need to be logged in to leave comments.
Login now