Show More
@@ -1,83 +1,87 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2012-2016 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | import logging |
|
22 | 22 | |
|
23 | 23 | from pyramid.exceptions import ConfigurationError |
|
24 | 24 | from zope.interface import implementer |
|
25 | 25 | |
|
26 | 26 | from rhodecode.authentication.interface import IAuthnPluginRegistry |
|
27 | 27 | from rhodecode.lib.utils2 import safe_str |
|
28 | 28 | from rhodecode.model.settings import SettingsModel |
|
29 | 29 | |
|
30 | 30 | log = logging.getLogger(__name__) |
|
31 | 31 | |
|
32 | 32 | |
|
33 | 33 | @implementer(IAuthnPluginRegistry) |
|
34 | 34 | class AuthenticationPluginRegistry(object): |
|
35 | 35 | |
|
36 | 36 | # INI settings key to set a fallback authentication plugin. |
|
37 | 37 | fallback_plugin_key = 'rhodecode.auth_plugin_fallback' |
|
38 | 38 | |
|
39 | 39 | def __init__(self, settings): |
|
40 | 40 | self._plugins = {} |
|
41 | 41 | self._fallback_plugin = settings.get(self.fallback_plugin_key, None) |
|
42 | 42 | |
|
43 | 43 | def add_authn_plugin(self, config, plugin): |
|
44 | 44 | plugin_id = plugin.get_id() |
|
45 | 45 | if plugin_id in self._plugins.keys(): |
|
46 | 46 | raise ConfigurationError( |
|
47 | 47 | 'Cannot register authentication plugin twice: "%s"', plugin_id) |
|
48 | 48 | else: |
|
49 | 49 | log.debug('Register authentication plugin: "%s"', plugin_id) |
|
50 | 50 | self._plugins[plugin_id] = plugin |
|
51 | 51 | |
|
52 | 52 | def get_plugins(self): |
|
53 | 53 | def sort_key(plugin): |
|
54 | 54 | return str.lower(safe_str(plugin.get_display_name())) |
|
55 | 55 | |
|
56 | 56 | return sorted(self._plugins.values(), key=sort_key) |
|
57 | 57 | |
|
58 | 58 | def get_plugin(self, plugin_id): |
|
59 | 59 | return self._plugins.get(plugin_id, None) |
|
60 | 60 | |
|
61 | 61 | def get_plugins_for_authentication(self): |
|
62 | 62 | """ |
|
63 | 63 | Returns a list of plugins which should be consulted when authenticating |
|
64 | 64 | a user. It only returns plugins which are enabled and active. |
|
65 | 65 | Additionally it includes the fallback plugin from the INI file, if |
|
66 | 66 | `rhodecode.auth_plugin_fallback` is set to a plugin ID. |
|
67 | 67 | """ |
|
68 | 68 | plugins = [] |
|
69 | 69 | |
|
70 | 70 | # Add all enabled and active plugins to the list. We iterate over the |
|
71 | 71 | # auth_plugins setting from DB beacuse it also represents the ordering. |
|
72 | 72 | enabled_plugins = SettingsModel().get_auth_plugins() |
|
73 | 73 | for plugin_id in enabled_plugins: |
|
74 | 74 | plugin = self.get_plugin(plugin_id) |
|
75 | 75 | if plugin is not None and plugin.is_active(): |
|
76 | 76 | plugins.append(plugin) |
|
77 | 77 | |
|
78 | 78 | # Add the fallback plugin from ini file. |
|
79 | if self._fallback_plugin: | |
|
80 | log.warn( | |
|
81 | 'Using fallback authentication plugin from INI file: "%s"', | |
|
82 | plugin.get_id()) | |
|
79 | 83 | plugin = self.get_plugin(self._fallback_plugin) |
|
80 | 84 | if plugin is not None and plugin not in plugins: |
|
81 | 85 | plugins.append(plugin) |
|
82 | 86 | |
|
83 | 87 | return plugins |
General Comments 0
You need to be logged in to leave comments.
Login now