Files
@ 226893a56a81
Branch filter:
Location: kallithea/kallithea/controllers/login.py - annotation
226893a56a81
10.8 KiB
text/x-python
auth: move IP check to AuthUser.make - it is more about accepting authentication than about permissions after authentication
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e 24c0d584ba86 d1addaf7a91e 1948ede028ef 1948ede028ef d1addaf7a91e d1addaf7a91e 1948ede028ef ad38f9f93b3b d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e 38d1c99cd000 d1addaf7a91e d1addaf7a91e d1addaf7a91e e9ac5698281d e9ac5698281d f629e9a0c376 d1addaf7a91e d1addaf7a91e af3539a458f6 d1addaf7a91e c5ff0bfefdf8 d1addaf7a91e 4c5c59b96adc 9daad8c50b37 f629e9a0c376 f629e9a0c376 d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e 38d1c99cd000 38d1c99cd000 38d1c99cd000 38d1c99cd000 38d1c99cd000 38d1c99cd000 38d1c99cd000 38d1c99cd000 38d1c99cd000 38d1c99cd000 38d1c99cd000 38d1c99cd000 38d1c99cd000 d1addaf7a91e d1addaf7a91e b537babcf966 ad131f703996 ad131f703996 ad131f703996 ad131f703996 b98f4431671c 8ee17ef21796 d1addaf7a91e d1addaf7a91e d1addaf7a91e 24b61c257aab d1addaf7a91e d1addaf7a91e d1addaf7a91e c5ff0bfefdf8 b24e015a4174 d69aa464f373 d1addaf7a91e d1addaf7a91e 78bba376a508 d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e c04c2734e32f c04c2734e32f d69aa464f373 d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e c5ff0bfefdf8 226893a56a81 077ba994ee03 8ee17ef21796 8f8ee972820f 8f8ee972820f 05dc948c9788 8f8ee972820f 05dc948c9788 d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e 2e3e1dacdbb7 2e3e1dacdbb7 d1addaf7a91e 9daad8c50b37 f373f182b756 d1addaf7a91e f373f182b756 d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e 44f7f73da4a6 d1addaf7a91e 3dcf1f82311a 44f7f73da4a6 d1addaf7a91e 86b1f3cfe836 d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e 38e418408c58 d1addaf7a91e d1addaf7a91e d9b78d8f1db3 d1addaf7a91e d69aa464f373 d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e c04c2734e32f c04c2734e32f d69aa464f373 d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e 9daad8c50b37 f373f182b756 d1addaf7a91e f373f182b756 d1addaf7a91e d1addaf7a91e f629e9a0c376 d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e 44f7f73da4a6 d1addaf7a91e 3dcf1f82311a 44f7f73da4a6 d1addaf7a91e 86b1f3cfe836 d1addaf7a91e d1addaf7a91e d1addaf7a91e f629e9a0c376 f629e9a0c376 d1addaf7a91e d9b78d8f1db3 d1addaf7a91e d69aa464f373 d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e c04c2734e32f c04c2734e32f d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e f629e9a0c376 f629e9a0c376 f629e9a0c376 f629e9a0c376 f629e9a0c376 f629e9a0c376 f629e9a0c376 f629e9a0c376 f629e9a0c376 f629e9a0c376 f629e9a0c376 f629e9a0c376 f629e9a0c376 d1addaf7a91e f629e9a0c376 f629e9a0c376 f629e9a0c376 f629e9a0c376 f629e9a0c376 f629e9a0c376 f629e9a0c376 f629e9a0c376 f629e9a0c376 f629e9a0c376 f629e9a0c376 f629e9a0c376 f629e9a0c376 f629e9a0c376 f629e9a0c376 f629e9a0c376 f629e9a0c376 f629e9a0c376 f629e9a0c376 f629e9a0c376 f629e9a0c376 f629e9a0c376 f629e9a0c376 f629e9a0c376 f629e9a0c376 d9b78d8f1db3 d1addaf7a91e d1addaf7a91e d1addaf7a91e d1addaf7a91e d9b78d8f1db3 0efca3ad8467 0efca3ad8467 0efca3ad8467 49f656a0ccdd 0efca3ad8467 0efca3ad8467 0efca3ad8467 0efca3ad8467 | # -*- coding: utf-8 -*-
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
kallithea.controllers.login
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Login controller for Kallithea
This file was forked by the Kallithea project in July 2014.
Original author and date, and relevant copyright and licensing information is below:
:created_on: Apr 22, 2010
:author: marcink
:copyright: (c) 2013 RhodeCode GmbH, and others.
:license: GPLv3, see LICENSE.md for more details.
"""
import logging
import re
import formencode
from formencode import htmlfill
from tg.i18n import ugettext as _
from tg import request, session, tmpl_context as c
from webob.exc import HTTPFound, HTTPBadRequest
import kallithea.lib.helpers as h
from kallithea.config.routing import url
from kallithea.lib.auth import AuthUser, HasPermissionAnyDecorator
from kallithea.lib.base import BaseController, log_in_user, render
from kallithea.lib.exceptions import UserCreationError
from kallithea.lib.utils2 import safe_str
from kallithea.model.db import User, Setting
from kallithea.model.forms import \
LoginForm, RegisterForm, PasswordResetRequestForm, PasswordResetConfirmationForm
from kallithea.model.user import UserModel
from kallithea.model.meta import Session
log = logging.getLogger(__name__)
class LoginController(BaseController):
def _validate_came_from(self, came_from,
_re=re.compile(r"/(?!/)[-!#$%&'()*+,./:;=?@_~0-9A-Za-z]*$")):
"""Return True if came_from is valid and can and should be used.
Determines if a URI reference is valid and relative to the origin;
or in RFC 3986 terms, whether it matches this production:
origin-relative-ref = path-absolute [ "?" query ] [ "#" fragment ]
with the exception that '%' escapes are not validated and '#' is
allowed inside the fragment part.
"""
return _re.match(came_from) is not None
def index(self):
c.came_from = safe_str(request.GET.get('came_from', ''))
if c.came_from:
if not self._validate_came_from(c.came_from):
log.error('Invalid came_from (not server-relative): %r', c.came_from)
raise HTTPBadRequest()
else:
c.came_from = url('home')
if request.POST:
# import Login Form validator class
login_form = LoginForm()()
try:
c.form_result = login_form.to_python(dict(request.POST))
# form checks for username/password, now we're authenticated
username = c.form_result['username']
user = User.get_by_username_or_email(username, case_insensitive=True)
except formencode.Invalid as errors:
defaults = errors.value
# remove password from filling in form again
defaults.pop('password', None)
return htmlfill.render(
render('/login.html'),
defaults=errors.value,
errors=errors.error_dict or {},
prefix_error=False,
encoding="UTF-8",
force_defaults=False)
except UserCreationError as e:
# container auth or other auth functions that create users on
# the fly can throw this exception signaling that there's issue
# with user creation, explanation should be provided in
# Exception itself
h.flash(e, 'error')
else:
auth_user = log_in_user(user, c.form_result['remember'], is_external_auth=False, ip_addr=request.ip_addr)
# TODO: handle auth_user is None as failed authentication?
raise HTTPFound(location=c.came_from)
else:
# redirect if already logged in
if not request.authuser.is_anonymous:
raise HTTPFound(location=c.came_from)
# continue to show login to default user
return render('/login.html')
@HasPermissionAnyDecorator('hg.admin', 'hg.register.auto_activate',
'hg.register.manual_activate')
def register(self):
def_user_perms = AuthUser(dbuser=User.get_default_user()).permissions['global']
c.auto_active = 'hg.register.auto_activate' in def_user_perms
settings = Setting.get_app_settings()
captcha_private_key = settings.get('captcha_private_key')
c.captcha_active = bool(captcha_private_key)
c.captcha_public_key = settings.get('captcha_public_key')
if request.POST:
register_form = RegisterForm()()
try:
form_result = register_form.to_python(dict(request.POST))
form_result['active'] = c.auto_active
if c.captcha_active:
from kallithea.lib.recaptcha import submit
response = submit(request.POST.get('g-recaptcha-response'),
private_key=captcha_private_key,
remoteip=request.ip_addr)
if not response.is_valid:
_value = form_result
_msg = _('Bad captcha')
error_dict = {'recaptcha_field': _msg}
raise formencode.Invalid(_msg, _value, None,
error_dict=error_dict)
UserModel().create_registration(form_result)
h.flash(_('You have successfully registered with %s') % (c.site_name or 'Kallithea'),
category='success')
Session().commit()
raise HTTPFound(location=url('login_home'))
except formencode.Invalid as errors:
return htmlfill.render(
render('/register.html'),
defaults=errors.value,
errors=errors.error_dict or {},
prefix_error=False,
encoding="UTF-8",
force_defaults=False)
except UserCreationError as e:
# container auth or other auth functions that create users on
# the fly can throw this exception signaling that there's issue
# with user creation, explanation should be provided in
# Exception itself
h.flash(e, 'error')
return render('/register.html')
def password_reset(self):
settings = Setting.get_app_settings()
captcha_private_key = settings.get('captcha_private_key')
c.captcha_active = bool(captcha_private_key)
c.captcha_public_key = settings.get('captcha_public_key')
if request.POST:
password_reset_form = PasswordResetRequestForm()()
try:
form_result = password_reset_form.to_python(dict(request.POST))
if c.captcha_active:
from kallithea.lib.recaptcha import submit
response = submit(request.POST.get('g-recaptcha-response'),
private_key=captcha_private_key,
remoteip=request.ip_addr)
if not response.is_valid:
_value = form_result
_msg = _('Bad captcha')
error_dict = {'recaptcha_field': _msg}
raise formencode.Invalid(_msg, _value, None,
error_dict=error_dict)
redirect_link = UserModel().send_reset_password_email(form_result)
h.flash(_('A password reset confirmation code has been sent'),
category='success')
raise HTTPFound(location=redirect_link)
except formencode.Invalid as errors:
return htmlfill.render(
render('/password_reset.html'),
defaults=errors.value,
errors=errors.error_dict or {},
prefix_error=False,
encoding="UTF-8",
force_defaults=False)
return render('/password_reset.html')
def password_reset_confirmation(self):
# This controller handles both GET and POST requests, though we
# only ever perform the actual password change on POST (since
# GET requests are not allowed to have side effects, and do not
# receive automatic CSRF protection).
# The template needs the email address outside of the form.
c.email = request.params.get('email')
if not request.POST:
return htmlfill.render(
render('/password_reset_confirmation.html'),
defaults=dict(request.params),
encoding='UTF-8')
form = PasswordResetConfirmationForm()()
try:
form_result = form.to_python(dict(request.POST))
except formencode.Invalid as errors:
return htmlfill.render(
render('/password_reset_confirmation.html'),
defaults=errors.value,
errors=errors.error_dict or {},
prefix_error=False,
encoding='UTF-8')
if not UserModel().verify_reset_password_token(
form_result['email'],
form_result['timestamp'],
form_result['token'],
):
return htmlfill.render(
render('/password_reset_confirmation.html'),
defaults=form_result,
errors={'token': _('Invalid password reset token')},
prefix_error=False,
encoding='UTF-8')
UserModel().reset_password(form_result['email'], form_result['password'])
h.flash(_('Successfully updated password'), category='success')
raise HTTPFound(location=url('login_home'))
def logout(self):
session.delete()
log.info('Logging out and deleting session for user')
raise HTTPFound(location=url('home'))
def authentication_token(self):
"""Return the CSRF protection token for the session - just like it
could have been screen scraped from a page with a form.
Only intended for testing but might also be useful for other kinds
of automation.
"""
return h.authentication_token()
|