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/>. | |
36f901fb | 16 | from pkg_resources import resource_filename |
aeae6cc2 RE |
17 | import os |
18 | ||
f339b76a | 19 | from mediagoblin.plugins.basic_auth import forms as auth_forms |
3bcdc490 | 20 | from mediagoblin.plugins.basic_auth import tools as auth_tools |
5adb906a | 21 | from mediagoblin.auth.tools import create_basic_user |
d88fcb03 | 22 | from mediagoblin.db.models import LocalUser |
ee355966 RE |
23 | from mediagoblin.tools import pluginapi |
24 | from sqlalchemy import or_ | |
36f901fb RE |
25 | from mediagoblin.tools.staticdirect import PluginStatic |
26 | ||
ee355966 | 27 | |
aeae6cc2 RE |
28 | PLUGIN_DIR = os.path.dirname(__file__) |
29 | ||
ee355966 | 30 | |
ee355966 | 31 | def setup_plugin(): |
ac0bc6a1 | 32 | config = pluginapi.get_config('mediagoblin.plugins.basic_auth') |
ee355966 | 33 | |
aeae6cc2 RE |
34 | routes = [ |
35 | ('mediagoblin.plugins.basic_auth.edit.pass', | |
36 | '/edit/password/', | |
37 | 'mediagoblin.plugins.basic_auth.views:change_pass'), | |
38 | ('mediagoblin.plugins.basic_auth.forgot_password', | |
39 | '/auth/forgot_password/', | |
40 | 'mediagoblin.plugins.basic_auth.views:forgot_password'), | |
41 | ('mediagoblin.plugins.basic_auth.verify_forgot_password', | |
42 | '/auth/forgot_password/verify/', | |
43 | 'mediagoblin.plugins.basic_auth.views:verify_forgot_password')] | |
44 | ||
45 | pluginapi.register_routes(routes) | |
46 | pluginapi.register_template_path(os.path.join(PLUGIN_DIR, 'templates')) | |
47 | ||
36f901fb RE |
48 | pluginapi.register_template_hooks( |
49 | {'edit_link': 'mediagoblin/plugins/basic_auth/edit_link.html', | |
50 | 'fp_link': 'mediagoblin/plugins/basic_auth/fp_link.html', | |
f66e4282 RE |
51 | 'fp_head': 'mediagoblin/plugins/basic_auth/fp_head.html', |
52 | 'create_account': | |
53 | 'mediagoblin/plugins/basic_auth/create_account_link.html'}) | |
36f901fb | 54 | |
ee355966 | 55 | |
b1e02e0a RE |
56 | def get_user(**kwargs): |
57 | username = kwargs.pop('username', None) | |
58 | if username: | |
d88fcb03 | 59 | user = LocalUser.query.filter( |
b1e02e0a | 60 | or_( |
d88fcb03 JT |
61 | LocalUser.username == username, |
62 | LocalUser.email == username, | |
b1e02e0a RE |
63 | )).first() |
64 | return user | |
ee355966 RE |
65 | |
66 | ||
67 | def create_user(registration_form): | |
b1e02e0a | 68 | user = get_user(username=registration_form.username.data) |
94d77e1f | 69 | if not user and 'password' in registration_form: |
5adb906a | 70 | user = create_basic_user(registration_form) |
3b8c733b | 71 | user.pw_hash = gen_password_hash( |
c94316bf | 72 | registration_form.password.data) |
c94316bf | 73 | user.save() |
ee355966 RE |
74 | return user |
75 | ||
76 | ||
ee355966 RE |
77 | def get_login_form(request): |
78 | return auth_forms.LoginForm(request.form) | |
79 | ||
80 | ||
81 | def get_registration_form(request): | |
82 | return auth_forms.RegistrationForm(request.form) | |
83 | ||
84 | ||
3b8c733b | 85 | def gen_password_hash(raw_pass, extra_salt=None): |
3bcdc490 | 86 | return auth_tools.bcrypt_gen_password_hash(raw_pass, extra_salt) |
9c2c9be7 RE |
87 | |
88 | ||
3b8c733b | 89 | def check_password(raw_pass, stored_hash, extra_salt=None): |
b3c4cbd5 RE |
90 | if stored_hash: |
91 | return auth_tools.bcrypt_check_password(raw_pass, | |
92 | stored_hash, extra_salt) | |
93 | return None | |
b194f29f RE |
94 | |
95 | ||
744f1c83 RE |
96 | def auth(): |
97 | return True | |
98 | ||
99 | ||
c94316bf RE |
100 | def append_to_global_context(context): |
101 | context['pass_auth'] = True | |
102 | return context | |
103 | ||
104 | ||
ee355966 RE |
105 | hooks = { |
106 | 'setup': setup_plugin, | |
f65615ea | 107 | 'authentication': auth, |
ee355966 RE |
108 | 'auth_get_user': get_user, |
109 | 'auth_create_user': create_user, | |
ee355966 RE |
110 | 'auth_get_login_form': get_login_form, |
111 | 'auth_get_registration_form': get_registration_form, | |
9c2c9be7 | 112 | 'auth_gen_password_hash': gen_password_hash, |
b194f29f | 113 | 'auth_check_password': check_password, |
3bcdc490 | 114 | 'auth_fake_login_attempt': auth_tools.fake_login_attempt, |
c94316bf | 115 | 'template_global_context': append_to_global_context, |
36f901fb RE |
116 | 'static_setup': lambda: PluginStatic( |
117 | 'coreplugin_basic_auth', | |
118 | resource_filename('mediagoblin.plugins.basic_auth', 'static')) | |
ee355966 | 119 | } |