##// END OF EJS Templates
login: Fix recaptcha. #4279...
Martin Bornhold -
r1062:f920e065 default
parent child Browse files
Show More
@@ -18,6 +18,7 b''
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 import collections
21 22 import datetime
22 23 import formencode
23 24 import logging
@@ -47,6 +48,9 b' from rhodecode.translation import _'
47 48
48 49 log = logging.getLogger(__name__)
49 50
51 CaptchaData = collections.namedtuple(
52 'CaptchaData', 'active, private_key, public_key')
53
50 54
51 55 def _store_user_in_session(session, username, remember=False):
52 56 user = User.get_by_username(username, case_insensitive=True)
@@ -113,6 +117,14 b' class LoginView(object):'
113 117 'errors': {},
114 118 }
115 119
120 def _get_captcha_data(self):
121 settings = SettingsModel().get_all_settings()
122 private_key = settings.get('rhodecode_captcha_private_key')
123 public_key = settings.get('rhodecode_captcha_public_key')
124 active = bool(private_key)
125 return CaptchaData(
126 active=active, private_key=private_key, public_key=public_key)
127
116 128 @view_config(
117 129 route_name='login', request_method='GET',
118 130 renderer='rhodecode:templates/login.html')
@@ -192,10 +204,8 b' class LoginView(object):'
192 204 errors = errors or {}
193 205
194 206 settings = SettingsModel().get_all_settings()
195 captcha_public_key = settings.get('rhodecode_captcha_public_key')
196 captcha_private_key = settings.get('rhodecode_captcha_private_key')
197 captcha_active = bool(captcha_private_key)
198 207 register_message = settings.get('rhodecode_register_message') or ''
208 captcha = self._get_captcha_data()
199 209 auto_active = 'hg.register.auto_activate' in User.get_default_user()\
200 210 .AuthUser.permissions['global']
201 211
@@ -204,8 +214,8 b' class LoginView(object):'
204 214 'defaults': defaults,
205 215 'errors': errors,
206 216 'auto_active': auto_active,
207 'captcha_active': captcha_active,
208 'captcha_public_key': captcha_public_key,
217 'captcha_active': captcha.active,
218 'captcha_public_key': captcha.public_key,
209 219 'register_message': register_message,
210 220 })
211 221 return render_ctx
@@ -216,9 +226,7 b' class LoginView(object):'
216 226 route_name='register', request_method='POST',
217 227 renderer='rhodecode:templates/register.html')
218 228 def register_post(self):
219 captcha_private_key = SettingsModel().get_setting_by_name(
220 'rhodecode_captcha_private_key')
221 captcha_active = bool(captcha_private_key)
229 captcha = self._get_captcha_data()
222 230 auto_active = 'hg.register.auto_activate' in User.get_default_user()\
223 231 .AuthUser.permissions['global']
224 232
@@ -227,13 +235,13 b' class LoginView(object):'
227 235 form_result = register_form.to_python(self.request.params)
228 236 form_result['active'] = auto_active
229 237
230 if captcha_active:
238 if captcha.active:
231 239 response = submit(
232 240 self.request.params.get('recaptcha_challenge_field'),
233 241 self.request.params.get('recaptcha_response_field'),
234 private_key=captcha_private_key,
242 private_key=captcha.private_key,
235 243 remoteip=get_ip_addr(self.request.environ))
236 if captcha_active and not response.is_valid:
244 if captcha.active and not response.is_valid:
237 245 _value = form_result
238 246 _msg = _('bad captcha')
239 247 error_dict = {'recaptcha_field': _msg}
@@ -269,14 +277,11 b' class LoginView(object):'
269 277 route_name='reset_password', request_method=('GET', 'POST'),
270 278 renderer='rhodecode:templates/password_reset.html')
271 279 def password_reset(self):
272 settings = SettingsModel().get_all_settings()
273 captcha_private_key = settings.get('rhodecode_captcha_private_key')
274 captcha_active = bool(captcha_private_key)
275 captcha_public_key = settings.get('rhodecode_captcha_public_key')
280 captcha = self._get_captcha_data()
276 281
277 282 render_ctx = {
278 'captcha_active': captcha_active,
279 'captcha_public_key': captcha_public_key,
283 'captcha_active': captcha.active,
284 'captcha_public_key': captcha.public_key,
280 285 'defaults': {},
281 286 'errors': {},
282 287 }
@@ -292,13 +297,13 b' class LoginView(object):'
292 297 _('Password reset has been disabled.'),
293 298 queue='error')
294 299 return HTTPFound(self.request.route_path('reset_password'))
295 if captcha_active:
300 if captcha.active:
296 301 response = submit(
297 302 self.request.params.get('recaptcha_challenge_field'),
298 303 self.request.params.get('recaptcha_response_field'),
299 private_key=captcha_private_key,
304 private_key=captcha.private_key,
300 305 remoteip=get_ip_addr(self.request.environ))
301 if captcha_active and not response.is_valid:
306 if captcha.active and not response.is_valid:
302 307 _value = form_result
303 308 _msg = _('bad captcha')
304 309 error_dict = {'recaptcha_field': _msg}
General Comments 0
You need to be logged in to leave comments. Login now