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