added focus to form input
[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/>.
16import os
17import uuid
18
19import forms as auth_forms
5b6923ab 20import lib as auth_lib
ee355966 21from mediagoblin.db.models import User
ee355966
RE
22from mediagoblin.tools import pluginapi
23from sqlalchemy import or_
24
25
ee355966
RE
26def setup_plugin():
27 config = pluginapi.get_config('mediagoblin.pluginapi.basic_auth')
28
29
d54cf48a 30def check_login(user, password):
09ae2df4
RE
31 if user.pw_hash:
32 result = auth_lib.bcrypt_check_password(password, user.pw_hash)
33 if result:
34 return result
0bd654a3 35 return None
ee355966
RE
36
37
c94316bf 38def get_user(form):
569873d8
RE
39 if 'username' in form.data:
40 username = form.username.data
41 user = User.query.filter(
42 or_(
43 User.username == username,
44 User.email == username,
45 )).first()
46 return user
ee355966
RE
47
48
49def create_user(registration_form):
c94316bf 50 user = get_user(registration_form)
94d77e1f 51 if not user and 'password' in registration_form:
c94316bf 52 user = User()
569873d8
RE
53 user.username = registration_form.username.data
54 user.email = registration_form.email.data
c94316bf
RE
55 user.pw_hash = auth_lib.bcrypt_gen_password_hash(
56 registration_form.password.data)
57 user.verification_key = unicode(uuid.uuid4())
58 user.save()
ee355966
RE
59 return user
60
61
ee355966
RE
62def get_login_form(request):
63 return auth_forms.LoginForm(request.form)
64
65
66def get_registration_form(request):
67 return auth_forms.RegistrationForm(request.form)
68
69
9c2c9be7 70def gen_password_hash(raw_pass, extra_salt):
5b6923ab 71 return auth_lib.bcrypt_gen_password_hash(raw_pass, extra_salt)
9c2c9be7
RE
72
73
744f1c83
RE
74def auth():
75 return True
76
77
c94316bf
RE
78def append_to_global_context(context):
79 context['pass_auth'] = True
80 return context
81
82
83def add_to_form_context(context):
84 context['pass_auth_link'] = True
85 return context
86
87
ee355966
RE
88hooks = {
89 'setup': setup_plugin,
f65615ea 90 'authentication': auth,
ee355966
RE
91 'auth_check_login': check_login,
92 'auth_get_user': get_user,
93 'auth_create_user': create_user,
ee355966
RE
94 'auth_get_login_form': get_login_form,
95 'auth_get_registration_form': get_registration_form,
9c2c9be7 96 'auth_gen_password_hash': gen_password_hash,
14efa7bd 97 'auth_fake_login_attempt': auth_lib.fake_login_attempt,
c94316bf
RE
98 'template_global_context': append_to_global_context,
99 ('mediagoblin.plugins.openid.register',
100 'mediagoblin/auth/register.html'): add_to_form_context,
101 ('mediagoblin.plugins.openid.login',
102 'mediagoblin/auth/login.html'): add_to_form_context,
ee355966 103}