##// END OF EJS Templates
authn: Renamed auth_container -> auth_headers
johbo -
r59:55904bf8 default
parent child Browse files
Show More
@@ -0,0 +1,223 b''
1 # -*- coding: utf-8 -*-
2
3 # Copyright (C) 2012-2016 RhodeCode GmbH
4 #
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
7 # (only), as published by the Free Software Foundation.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
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/>.
16 #
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
21 import colander
22 import logging
23
24 from sqlalchemy.ext.hybrid import hybrid_property
25
26 from rhodecode.authentication.base import RhodeCodeExternalAuthPlugin
27 from rhodecode.authentication.schema import AuthnPluginSettingsSchemaBase
28 from rhodecode.authentication.routes import AuthnPluginResourceBase
29 from rhodecode.lib.colander_utils import strip_whitespace
30 from rhodecode.lib.utils2 import str2bool, safe_unicode
31 from rhodecode.model.db import User
32 from rhodecode.translation import _
33
34
35 log = logging.getLogger(__name__)
36
37
38 def plugin_factory(plugin_id, *args, **kwds):
39 """
40 Factory function that is called during plugin discovery.
41 It returns the plugin instance.
42 """
43 plugin = RhodeCodeAuthPlugin(plugin_id)
44 return plugin
45
46
47 class HeadersAuthnResource(AuthnPluginResourceBase):
48 pass
49
50
51 class HeadersSettingsSchema(AuthnPluginSettingsSchemaBase):
52 header = colander.SchemaNode(
53 colander.String(),
54 default='REMOTE_USER',
55 description=_('Header to extract the user from'),
56 preparer=strip_whitespace,
57 title=_('Header'),
58 widget='string')
59 fallback_header = colander.SchemaNode(
60 colander.String(),
61 default='HTTP_X_FORWARDED_USER',
62 description=_('Header to extract the user from when main one fails'),
63 preparer=strip_whitespace,
64 title=_('Fallback header'),
65 widget='string')
66 clean_username = colander.SchemaNode(
67 colander.Boolean(),
68 default=True,
69 description=_('Perform cleaning of user, if passed user has @ in '
70 'username then first part before @ is taken. '
71 'If there\'s \\ in the username only the part after '
72 ' \\ is taken'),
73 missing=False,
74 title=_('Clean username'),
75 widget='bool')
76
77
78 class RhodeCodeAuthPlugin(RhodeCodeExternalAuthPlugin):
79
80 def includeme(self, config):
81 config.add_authn_plugin(self)
82 config.add_authn_resource(self.get_id(), HeadersAuthnResource(self))
83 config.add_view(
84 'rhodecode.authentication.views.AuthnPluginViewBase',
85 attr='settings_get',
86 request_method='GET',
87 route_name='auth_home',
88 context=HeadersAuthnResource)
89 config.add_view(
90 'rhodecode.authentication.views.AuthnPluginViewBase',
91 attr='settings_post',
92 request_method='POST',
93 route_name='auth_home',
94 context=HeadersAuthnResource)
95
96 def get_display_name(self):
97 return _('Headers')
98
99 def get_settings_schema(self):
100 return HeadersSettingsSchema()
101
102 @hybrid_property
103 def name(self):
104 return 'headers'
105
106 @hybrid_property
107 def is_container_auth(self):
108 return True
109
110 def use_fake_password(self):
111 return True
112
113 def user_activation_state(self):
114 def_user_perms = User.get_default_user().AuthUser.permissions['global']
115 return 'hg.extern_activate.auto' in def_user_perms
116
117 def _clean_username(self, username):
118 # Removing realm and domain from username
119 username = username.split('@')[0]
120 username = username.rsplit('\\')[-1]
121 return username
122
123 def _get_username(self, environ, settings):
124 username = None
125 environ = environ or {}
126 if not environ:
127 log.debug('got empty environ: %s' % environ)
128
129 settings = settings or {}
130 if settings.get('header'):
131 header = settings.get('header')
132 username = environ.get(header)
133 log.debug('extracted %s:%s' % (header, username))
134
135 # fallback mode
136 if not username and settings.get('fallback_header'):
137 header = settings.get('fallback_header')
138 username = environ.get(header)
139 log.debug('extracted %s:%s' % (header, username))
140
141 if username and str2bool(settings.get('clean_username')):
142 log.debug('Received username `%s` from headers' % username)
143 username = self._clean_username(username)
144 log.debug('New cleanup user is:%s' % username)
145 return username
146
147 def get_user(self, username=None, **kwargs):
148 """
149 Helper method for user fetching in plugins, by default it's using
150 simple fetch by username, but this method can be custimized in plugins
151 eg. headers auth plugin to fetch user by environ params
152 :param username: username if given to fetch
153 :param kwargs: extra arguments needed for user fetching.
154 """
155 environ = kwargs.get('environ') or {}
156 settings = kwargs.get('settings') or {}
157 username = self._get_username(environ, settings)
158 # we got the username, so use default method now
159 return super(RhodeCodeAuthPlugin, self).get_user(username)
160
161 def auth(self, userobj, username, password, settings, **kwargs):
162 """
163 Get's the headers_auth username (or email). It tries to get username
164 from REMOTE_USER if this plugin is enabled, if that fails
165 it tries to get username from HTTP_X_FORWARDED_USER if fallback header
166 is set. clean_username extracts the username from this data if it's
167 having @ in it.
168 Return None on failure. On success, return a dictionary of the form:
169
170 see: RhodeCodeAuthPluginBase.auth_func_attrs
171
172 :param userobj:
173 :param username:
174 :param password:
175 :param settings:
176 :param kwargs:
177 """
178 environ = kwargs.get('environ')
179 if not environ:
180 log.debug('Empty environ data skipping...')
181 return None
182
183 if not userobj:
184 userobj = self.get_user('', environ=environ, settings=settings)
185
186 # we don't care passed username/password for headers auth plugins.
187 # only way to log in is using environ
188 username = None
189 if userobj:
190 username = getattr(userobj, 'username')
191
192 if not username:
193 # we don't have any objects in DB user doesn't exist extrac username
194 # from environ based on the settings
195 username = self._get_username(environ, settings)
196
197 # if cannot fetch username, it's a no-go for this plugin to proceed
198 if not username:
199 return None
200
201 # old attrs fetched from RhodeCode database
202 admin = getattr(userobj, 'admin', False)
203 active = getattr(userobj, 'active', True)
204 email = getattr(userobj, 'email', '')
205 firstname = getattr(userobj, 'firstname', '')
206 lastname = getattr(userobj, 'lastname', '')
207 extern_type = getattr(userobj, 'extern_type', '')
208
209 user_attrs = {
210 'username': username,
211 'firstname': safe_unicode(firstname or username),
212 'lastname': safe_unicode(lastname or ''),
213 'groups': [],
214 'email': email or '',
215 'admin': admin or False,
216 'active': active,
217 'active_from_extern': True,
218 'extern_name': username,
219 'extern_type': extern_type,
220 }
221
222 log.info('user `%s` authenticated correctly' % user_attrs['username'])
223 return user_attrs
@@ -1,244 +1,244 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Import early to make sure things are patched up properly
4 4 from setuptools import setup, find_packages
5 5
6 6 import os
7 7 import sys
8 8 import platform
9 9
10 10 if sys.version_info < (2, 7):
11 11 raise Exception('RhodeCode requires Python 2.7 or later')
12 12
13 13
14 14 here = os.path.abspath(os.path.dirname(__file__))
15 15
16 16
17 17 def _get_meta_var(name, data, callback_handler=None):
18 18 import re
19 19 matches = re.compile(r'(?:%s)\s*=\s*(.*)' % name).search(data)
20 20 if matches:
21 21 if not callable(callback_handler):
22 22 callback_handler = lambda v: v
23 23
24 24 return callback_handler(eval(matches.groups()[0]))
25 25
26 26 _meta = open(os.path.join(here, 'rhodecode', '__init__.py'), 'rb')
27 27 _metadata = _meta.read()
28 28 _meta.close()
29 29
30 30 callback = lambda V: ('.'.join(map(str, V[:3])) + '.'.join(V[3:]))
31 31 __version__ = open(os.path.join('rhodecode', 'VERSION')).read().strip()
32 32 __license__ = _get_meta_var('__license__', _metadata)
33 33 __author__ = _get_meta_var('__author__', _metadata)
34 34 __url__ = _get_meta_var('__url__', _metadata)
35 35 # defines current platform
36 36 __platform__ = platform.system()
37 37
38 38 # Cygwin has different platform identifiers, but they all contain the
39 39 # term "CYGWIN"
40 40 is_windows = __platform__ == 'Windows' or 'CYGWIN' in __platform__
41 41
42 42 requirements = [
43 43 'Babel',
44 44 'Beaker',
45 45 'FormEncode',
46 46 'Mako',
47 47 'Markdown',
48 48 'MarkupSafe',
49 49 'MySQL-python',
50 50 'Paste',
51 51 'PasteDeploy',
52 52 'PasteScript',
53 53 'Pygments',
54 54 'Pylons',
55 55 'Pyro4',
56 56 'Routes',
57 57 'SQLAlchemy',
58 58 'Tempita',
59 59 'URLObject',
60 60 'WebError',
61 61 'WebHelpers',
62 62 'WebHelpers2',
63 63 'WebOb',
64 64 'WebTest',
65 65 'Whoosh',
66 66 'alembic',
67 67 'amqplib',
68 68 'anyjson',
69 69 'appenlight-client',
70 70 'authomatic',
71 71 'backport_ipaddress',
72 72 'celery',
73 73 'colander',
74 74 'decorator',
75 75 'docutils',
76 76 'gunicorn',
77 77 'infrae.cache',
78 78 'ipython',
79 79 'iso8601',
80 80 'kombu',
81 81 'msgpack-python',
82 82 'packaging',
83 83 'psycopg2',
84 84 'pycrypto',
85 85 'pycurl',
86 86 'pyparsing',
87 87 'pyramid',
88 88 'pyramid-debugtoolbar',
89 89 'pyramid-mako',
90 90 'pyramid-beaker',
91 91 'pysqlite',
92 92 'python-dateutil',
93 93 'python-ldap',
94 94 'python-memcached',
95 95 'python-pam',
96 96 'recaptcha-client',
97 97 'repoze.lru',
98 98 'requests',
99 99 'simplejson',
100 100 'waitress',
101 101 'zope.cachedescriptors',
102 102 ]
103 103
104 104 if is_windows:
105 105 pass
106 106 else:
107 107 requirements.append('psutil')
108 108 requirements.append('py-bcrypt')
109 109
110 110 test_requirements = [
111 111 'WebTest',
112 112 'configobj',
113 113 'cssselect',
114 114 'flake8',
115 115 'lxml',
116 116 'mock',
117 117 'pytest',
118 118 'pytest-cov',
119 119 'pytest-runner',
120 120 ]
121 121
122 122 setup_requirements = [
123 123 'PasteScript',
124 124 'pytest-runner',
125 125 ]
126 126
127 127 dependency_links = [
128 128 ]
129 129
130 130 classifiers = [
131 131 'Development Status :: 6 - Mature',
132 132 'Environment :: Web Environment',
133 133 'Framework :: Pylons',
134 134 'Intended Audience :: Developers',
135 135 'Operating System :: OS Independent',
136 136 'Programming Language :: Python',
137 137 'Programming Language :: Python :: 2.7',
138 138 ]
139 139
140 140
141 141 # additional files from project that goes somewhere in the filesystem
142 142 # relative to sys.prefix
143 143 data_files = []
144 144
145 145 # additional files that goes into package itself
146 146 package_data = {'rhodecode': ['i18n/*/LC_MESSAGES/*.mo', ], }
147 147
148 148 description = ('RhodeCode is a fast and powerful management tool '
149 149 'for Mercurial and GIT with a built in push/pull server, '
150 150 'full text search and code-review.')
151 151
152 152 keywords = ' '.join([
153 153 'rhodecode', 'rhodiumcode', 'mercurial', 'git', 'code review',
154 154 'repo groups', 'ldap', 'repository management', 'hgweb replacement',
155 155 'hgwebdir', 'gitweb replacement', 'serving hgweb',
156 156 ])
157 157
158 158 # long description
159 159 README_FILE = 'README.rst'
160 160 CHANGELOG_FILE = 'CHANGES.rst'
161 161 try:
162 162 long_description = open(README_FILE).read() + '\n\n' + \
163 163 open(CHANGELOG_FILE).read()
164 164
165 165 except IOError, err:
166 166 sys.stderr.write(
167 167 '[WARNING] Cannot find file specified as long_description (%s)\n or '
168 168 'changelog (%s) skipping that file' % (README_FILE, CHANGELOG_FILE)
169 169 )
170 170 long_description = description
171 171
172 172 # packages
173 173 packages = find_packages()
174 174
175 175 paster_commands = [
176 176 'make-config=rhodecode.lib.paster_commands.make_config:Command',
177 177 'setup-rhodecode=rhodecode.lib.paster_commands.setup_rhodecode:Command',
178 178 'update-repoinfo=rhodecode.lib.paster_commands.update_repoinfo:Command',
179 179 'cache-keys=rhodecode.lib.paster_commands.cache_keys:Command',
180 180 'ishell=rhodecode.lib.paster_commands.ishell:Command',
181 181 'upgrade-db=rhodecode.lib.dbmigrate:UpgradeDb',
182 182 'celeryd=rhodecode.lib.celerypylons.commands:CeleryDaemonCommand',
183 183 ]
184 184
185 185 setup(
186 186 name='rhodecode-enterprise-ce',
187 187 version=__version__,
188 188 description=description,
189 189 long_description=long_description,
190 190 keywords=keywords,
191 191 license=__license__,
192 192 author=__author__,
193 193 author_email='marcin@rhodecode.com',
194 194 dependency_links=dependency_links,
195 195 url=__url__,
196 196 install_requires=requirements,
197 197 tests_require=test_requirements,
198 198 classifiers=classifiers,
199 199 setup_requires=setup_requirements,
200 200 data_files=data_files,
201 201 packages=packages,
202 202 include_package_data=True,
203 203 package_data=package_data,
204 204 message_extractors={
205 205 'rhodecode': [
206 206 ('**.py', 'python', None),
207 207 ('templates/**.mako', 'mako', {'input_encoding': 'utf-8'}),
208 208 ('templates/**.html', 'mako', {'input_encoding': 'utf-8'}),
209 209 ('public/**', 'ignore', None),
210 210 ]
211 211 },
212 212 zip_safe=False,
213 213 paster_plugins=['PasteScript', 'Pylons'],
214 214 entry_points={
215 215 'enterprise.plugins1': [
216 'container=rhodecode.authentication.plugins.auth_container:plugin_factory',
217 216 'crowd=rhodecode.authentication.plugins.auth_crowd:plugin_factory',
217 'headers=rhodecode.authentication.plugins.auth_headers:plugin_factory',
218 218 'jasig_cas=rhodecode.authentication.plugins.auth_jasig_cas:plugin_factory',
219 219 'ldap=rhodecode.authentication.plugins.auth_ldap:plugin_factory',
220 220 'pam=rhodecode.authentication.plugins.auth_pam:plugin_factory',
221 221 'rhodecode=rhodecode.authentication.plugins.auth_rhodecode:plugin_factory',
222 222 ],
223 223 'paste.app_factory': [
224 224 'main=rhodecode.config.middleware:make_pyramid_app',
225 225 'pylons=rhodecode.config.middleware:make_app',
226 226 ],
227 227 'paste.app_install': [
228 228 'main=pylons.util:PylonsInstaller',
229 229 'pylons=pylons.util:PylonsInstaller',
230 230 ],
231 231 'paste.global_paster_command': paster_commands,
232 232 'pytest11': [
233 233 'pylons=rhodecode.tests.pylons_plugin',
234 234 'enterprise=rhodecode.tests.plugin',
235 235 ],
236 236 'console_scripts': [
237 237 'rcserver=rhodecode.rcserver:main',
238 238 ],
239 239 'beaker.backends': [
240 240 'memorylru_base=rhodecode.lib.memory_lru_debug:MemoryLRUNamespaceManagerBase',
241 241 'memorylru_debug=rhodecode.lib.memory_lru_debug:MemoryLRUNamespaceManagerDebug'
242 242 ]
243 243 },
244 244 )
1 NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now