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