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