Commit | Line | Data |
---|---|---|
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/>. | |
f339b76a | 16 | from mediagoblin.plugins.basic_auth import forms as auth_forms |
3bcdc490 | 17 | from mediagoblin.plugins.basic_auth import tools as auth_tools |
5adb906a | 18 | from mediagoblin.auth.tools import create_basic_user |
ee355966 | 19 | from mediagoblin.db.models import User |
ee355966 RE |
20 | from mediagoblin.tools import pluginapi |
21 | from sqlalchemy import or_ | |
22 | ||
23 | ||
ee355966 | 24 | def setup_plugin(): |
ac0bc6a1 | 25 | config = pluginapi.get_config('mediagoblin.plugins.basic_auth') |
ee355966 RE |
26 | |
27 | ||
b1e02e0a RE |
28 | def get_user(**kwargs): |
29 | username = kwargs.pop('username', None) | |
30 | if username: | |
31 | user = User.query.filter( | |
32 | or_( | |
33 | User.username == username, | |
34 | User.email == username, | |
35 | )).first() | |
36 | return user | |
ee355966 RE |
37 | |
38 | ||
39 | def create_user(registration_form): | |
b1e02e0a | 40 | user = get_user(username=registration_form.username.data) |
94d77e1f | 41 | if not user and 'password' in registration_form: |
5adb906a | 42 | user = create_basic_user(registration_form) |
3b8c733b | 43 | user.pw_hash = gen_password_hash( |
c94316bf | 44 | registration_form.password.data) |
c94316bf | 45 | user.save() |
ee355966 RE |
46 | return user |
47 | ||
48 | ||
ee355966 RE |
49 | def get_login_form(request): |
50 | return auth_forms.LoginForm(request.form) | |
51 | ||
52 | ||
53 | def get_registration_form(request): | |
54 | return auth_forms.RegistrationForm(request.form) | |
55 | ||
56 | ||
3b8c733b | 57 | def gen_password_hash(raw_pass, extra_salt=None): |
3bcdc490 | 58 | return auth_tools.bcrypt_gen_password_hash(raw_pass, extra_salt) |
9c2c9be7 RE |
59 | |
60 | ||
3b8c733b | 61 | def check_password(raw_pass, stored_hash, extra_salt=None): |
3bcdc490 | 62 | return auth_tools.bcrypt_check_password(raw_pass, stored_hash, extra_salt) |
b194f29f RE |
63 | |
64 | ||
744f1c83 RE |
65 | def auth(): |
66 | return True | |
67 | ||
68 | ||
c94316bf RE |
69 | def append_to_global_context(context): |
70 | context['pass_auth'] = True | |
71 | return context | |
72 | ||
73 | ||
ee355966 RE |
74 | hooks = { |
75 | 'setup': setup_plugin, | |
f65615ea | 76 | 'authentication': auth, |
ee355966 RE |
77 | 'auth_get_user': get_user, |
78 | 'auth_create_user': create_user, | |
ee355966 RE |
79 | 'auth_get_login_form': get_login_form, |
80 | 'auth_get_registration_form': get_registration_form, | |
9c2c9be7 | 81 | 'auth_gen_password_hash': gen_password_hash, |
b194f29f | 82 | 'auth_check_password': check_password, |
3bcdc490 | 83 | 'auth_fake_login_attempt': auth_tools.fake_login_attempt, |
c94316bf | 84 | 'template_global_context': append_to_global_context, |
ee355966 | 85 | } |