comparison rhodecode/lib/auth_modules/auth_container.py @ 4116:ffd45b185016 rhodecode-2.2.5-gpl

Imported some of the GPLv3'd changes from RhodeCode v2.2.5. This imports changes between changesets 21af6c4eab3d and 6177597791c2 in RhodeCode's original repository, including only changes to Python files and HTML. RhodeCode clearly licensed its changes to these files under GPLv3 in their /LICENSE file, which states the following: The Python code and integrated HTML are licensed under the GPLv3 license. (See: https://code.rhodecode.com/rhodecode/files/v2.2.5/LICENSE or http://web.archive.org/web/20140512193334/https://code.rhodecode.com/rhodecode/files/f3b123159901f15426d18e3dc395e8369f70ebe0/LICENSE for an online copy of that LICENSE file) Conservancy reviewed these changes and confirmed that they can be licensed as a whole to the Kallithea project under GPLv3-only. While some of the contents committed herein are clearly licensed GPLv3-or-later, on the whole we must assume the are GPLv3-only, since the statement above from RhodeCode indicates that they intend GPLv3-only as their license, per GPLv3ยง14 and other relevant sections of GPLv3.
author Bradley M. Kuhn <bkuhn@sfconservancy.org>
date Wed, 02 Jul 2014 19:03:13 -0400
parents
children 7e5f8c12a3fc
comparison
equal deleted inserted replaced
4115:8b7294a804a0 4116:ffd45b185016
1 # -*- coding: utf-8 -*-
2 # This program is free software: you can redistribute it and/or modify
3 # it under the terms of the GNU General Public License as published by
4 # the Free Software Foundation, either version 3 of the License, or
5 # (at your option) any later version.
6 #
7 # This program is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 # GNU General Public License for more details.
11 #
12 # You should have received a copy of the GNU General Public License
13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
14 """
15 rhodecode.lib.auth_modules.auth_container
16 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17
18 RhodeCode container based authentication plugin
19
20 :created_on: Created on Nov 17, 2012
21 :author: marcink
22 :copyright: (c) 2013 RhodeCode GmbH.
23 :license: GPLv3, see LICENSE for more details.
24 """
25
26 import logging
27 from rhodecode.lib import auth_modules
28 from rhodecode.lib.utils2 import str2bool, safe_unicode
29 from rhodecode.lib.compat import hybrid_property
30 from rhodecode.model.db import User
31
32 log = logging.getLogger(__name__)
33
34
35 class RhodeCodeAuthPlugin(auth_modules.RhodeCodeExternalAuthPlugin):
36 def __init__(self):
37 pass
38
39 @hybrid_property
40 def name(self):
41 return "container"
42
43 @hybrid_property
44 def is_container_auth(self):
45 return True
46
47 def settings(self):
48
49 settings = [
50 {
51 "name": "header",
52 "validator": self.validators.UnicodeString(strip=True, not_empty=True),
53 "type": "string",
54 "description": "Header to extract the user from",
55 "default": "REMOTE_USER",
56 "formname": "Header"
57 },
58 {
59 "name": "fallback_header",
60 "validator": self.validators.UnicodeString(strip=True),
61 "type": "string",
62 "description": "Header to extract the user from when main one fails",
63 "default": "HTTP_X_FORWARDED_USER",
64 "formname": "Fallback header"
65 },
66 {
67 "name": "clean_username",
68 "validator": self.validators.StringBoolean(if_missing=False),
69 "type": "bool",
70 "description": "Perform cleaning of user, if passed user has @ in username "
71 "then first part before @ is taken. "
72 "If there's \\ in the username only the part after \\ is taken",
73 "default": "True",
74 "formname": "Clean username"
75 },
76 ]
77 return settings
78
79 def use_fake_password(self):
80 return True
81
82 def user_activation_state(self):
83 def_user_perms = User.get_default_user().AuthUser.permissions['global']
84 return 'hg.extern_activate.auto' in def_user_perms
85
86 def _clean_username(self, username):
87 # Removing realm and domain from username
88 username = username.partition('@')[0]
89 username = username.rpartition('\\')[2]
90 return username
91
92 def _get_username(self, environ, settings):
93 username = None
94 environ = environ or {}
95 if not environ:
96 log.debug('got empty environ: %s' % environ)
97
98 settings = settings or {}
99 if settings.get('header'):
100 header = settings.get('header')
101 username = environ.get(header)
102 log.debug('extracted %s:%s' % (header, username))
103
104 # fallback mode
105 if not username and settings.get('fallback_header'):
106 header = settings.get('fallback_header')
107 username = environ.get(header)
108 log.debug('extracted %s:%s' % (header, username))
109
110 if username and str2bool(settings.get('clean_username')):
111 log.debug('Received username %s from container' % username)
112 username = self._clean_username(username)
113 log.debug('New cleanup user is: %s' % username)
114 return username
115
116 def get_user(self, username=None, **kwargs):
117 """
118 Helper method for user fetching in plugins, by default it's using
119 simple fetch by username, but this method can be custimized in plugins
120 eg. container auth plugin to fetch user by environ params
121 :param username: username if given to fetch
122 :param kwargs: extra arguments needed for user fetching.
123 """
124 environ = kwargs.get('environ') or {}
125 settings = kwargs.get('settings') or {}
126 username = self._get_username(environ, settings)
127 # we got the username, so use default method now
128 return super(RhodeCodeAuthPlugin, self).get_user(username)
129
130 def auth(self, userobj, username, password, settings, **kwargs):
131 """
132 Get's the container_auth username (or email). It tries to get username
133 from REMOTE_USER if this plugin is enabled, if that fails
134 it tries to get username from HTTP_X_FORWARDED_USER if fallback header
135 is set. clean_username extracts the username from this data if it's
136 having @ in it.
137 Return None on failure. On success, return a dictionary of the form:
138
139 see: RhodeCodeAuthPluginBase.auth_func_attrs
140
141 :param userobj:
142 :param username:
143 :param password:
144 :param settings:
145 :param kwargs:
146 """
147 environ = kwargs.get('environ')
148 if not environ:
149 log.debug('Empty environ data skipping...')
150 return None
151
152 if not userobj:
153 userobj = self.get_user('', environ=environ, settings=settings)
154
155 # we don't care passed username/password for container auth plugins.
156 # only way to log in is using environ
157 username = None
158 if userobj:
159 username = getattr(userobj, 'username')
160
161 if not username:
162 # we don't have any objects in DB user doesn't exist extrac username
163 # from environ based on the settings
164 username = self._get_username(environ, settings)
165
166 # if cannot fetch username, it's a no-go for this plugin to proceed
167 if not username:
168 return None
169
170 # old attrs fetched from RhodeCode database
171 admin = getattr(userobj, 'admin', False)
172 active = getattr(userobj, 'active', True)
173 email = getattr(userobj, 'email', '')
174 firstname = getattr(userobj, 'firstname', '')
175 lastname = getattr(userobj, 'lastname', '')
176 extern_type = getattr(userobj, 'extern_type', '')
177
178 user_attrs = {
179 'username': username,
180 'firstname': safe_unicode(firstname or username),
181 'lastname': safe_unicode(lastname or ''),
182 'groups': [],
183 'email': email or '',
184 'admin': admin or False,
185 'active': active,
186 'active_from_extern': True,
187 'extern_name': username,
188 'extern_type': extern_type,
189 }
190
191 log.info('user `%s` authenticated correctly' % user_attrs['username'])
192 return user_attrs