moving forgot_password views back to gmg/auth and cleanup
[mediagoblin.git] / mediagoblin / plugins / basic_auth / __init__.py
CommitLineData
ee355966
RE
1# GNU MediaGoblin -- federated, autonomous media hosting
2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU Affero General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
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 Affero 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/>.
ee355966
RE
16import uuid
17
f339b76a
RE
18from mediagoblin.plugins.basic_auth import forms as auth_forms
19from mediagoblin.plugins.basic_auth import lib as auth_lib
ee355966 20from mediagoblin.db.models import User
ee355966
RE
21from mediagoblin.tools import pluginapi
22from sqlalchemy import or_
23
24
ee355966
RE
25def setup_plugin():
26 config = pluginapi.get_config('mediagoblin.pluginapi.basic_auth')
27
28
d54cf48a 29def check_login(user, password):
09ae2df4
RE
30 if user.pw_hash:
31 result = auth_lib.bcrypt_check_password(password, user.pw_hash)
32 if result:
33 return result
0bd654a3 34 return None
ee355966
RE
35
36
c94316bf 37def get_user(form):
569873d8
RE
38 if 'username' in form.data:
39 username = form.username.data
40 user = User.query.filter(
41 or_(
42 User.username == username,
43 User.email == username,
44 )).first()
45 return user
ee355966
RE
46
47
48def create_user(registration_form):
c94316bf 49 user = get_user(registration_form)
94d77e1f 50 if not user and 'password' in registration_form:
c94316bf 51 user = User()
569873d8
RE
52 user.username = registration_form.username.data
53 user.email = registration_form.email.data
c94316bf
RE
54 user.pw_hash = auth_lib.bcrypt_gen_password_hash(
55 registration_form.password.data)
56 user.verification_key = unicode(uuid.uuid4())
57 user.save()
ee355966
RE
58 return user
59
60
ee355966
RE
61def get_login_form(request):
62 return auth_forms.LoginForm(request.form)
63
64
65def get_registration_form(request):
66 return auth_forms.RegistrationForm(request.form)
67
68
9c2c9be7 69def gen_password_hash(raw_pass, extra_salt):
5b6923ab 70 return auth_lib.bcrypt_gen_password_hash(raw_pass, extra_salt)
9c2c9be7
RE
71
72
b194f29f
RE
73def check_password(raw_pass, stored_hash, extra_salt):
74 return auth_lib.bcrypt_check_password(raw_pass, stored_hash, extra_salt)
75
76
744f1c83
RE
77def auth():
78 return True
79
80
c94316bf
RE
81def append_to_global_context(context):
82 context['pass_auth'] = True
83 return context
84
85
86def add_to_form_context(context):
87 context['pass_auth_link'] = True
88 return context
89
90
ee355966
RE
91hooks = {
92 'setup': setup_plugin,
f65615ea 93 'authentication': auth,
ee355966
RE
94 'auth_check_login': check_login,
95 'auth_get_user': get_user,
96 'auth_create_user': create_user,
ee355966
RE
97 'auth_get_login_form': get_login_form,
98 'auth_get_registration_form': get_registration_form,
9c2c9be7 99 'auth_gen_password_hash': gen_password_hash,
b194f29f 100 'auth_check_password': check_password,
14efa7bd 101 'auth_fake_login_attempt': auth_lib.fake_login_attempt,
c94316bf
RE
102 'template_global_context': append_to_global_context,
103 ('mediagoblin.plugins.openid.register',
104 'mediagoblin/auth/register.html'): add_to_form_context,
105 ('mediagoblin.plugins.openid.login',
106 'mediagoblin/auth/login.html'): add_to_form_context,
ee355966 107}