fixing the config section we pull things out of for basic_auth
[mediagoblin.git] / mediagoblin / plugins / basic_auth / __init__.py
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/>.
16 from mediagoblin.plugins.basic_auth import forms as auth_forms
17 from mediagoblin.plugins.basic_auth import tools as auth_tools
18 from mediagoblin.db.models import User
19 from mediagoblin.tools import pluginapi
20 from sqlalchemy import or_
21
22
23 def setup_plugin():
24 config = pluginapi.get_config('mediagoblin.plugins.basic_auth')
25
26
27 def get_user(**kwargs):
28 username = kwargs.pop('username', None)
29 if username:
30 user = User.query.filter(
31 or_(
32 User.username == username,
33 User.email == username,
34 )).first()
35 return user
36
37
38 def create_user(registration_form):
39 user = get_user(username=registration_form.username.data)
40 if not user and 'password' in registration_form:
41 user = User()
42 user.username = registration_form.username.data
43 user.email = registration_form.email.data
44 user.pw_hash = gen_password_hash(
45 registration_form.password.data)
46 user.save()
47 return user
48
49
50 def get_login_form(request):
51 return auth_forms.LoginForm(request.form)
52
53
54 def get_registration_form(request):
55 return auth_forms.RegistrationForm(request.form)
56
57
58 def gen_password_hash(raw_pass, extra_salt=None):
59 return auth_tools.bcrypt_gen_password_hash(raw_pass, extra_salt)
60
61
62 def check_password(raw_pass, stored_hash, extra_salt=None):
63 return auth_tools.bcrypt_check_password(raw_pass, stored_hash, extra_salt)
64
65
66 def auth():
67 return True
68
69
70 def append_to_global_context(context):
71 context['pass_auth'] = True
72 return context
73
74
75 def add_to_form_context(context):
76 context['pass_auth_link'] = True
77 return context
78
79
80 hooks = {
81 'setup': setup_plugin,
82 'authentication': auth,
83 'auth_get_user': get_user,
84 'auth_create_user': create_user,
85 'auth_get_login_form': get_login_form,
86 'auth_get_registration_form': get_registration_form,
87 'auth_gen_password_hash': gen_password_hash,
88 'auth_check_password': check_password,
89 'auth_fake_login_attempt': auth_tools.fake_login_attempt,
90 'template_global_context': append_to_global_context,
91 ('mediagoblin.plugins.openid.register',
92 'mediagoblin/auth/register.html'): add_to_form_context,
93 ('mediagoblin.plugins.openid.login',
94 'mediagoblin/auth/login.html'): add_to_form_context,
95 }