| 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 pkg_resources import resource_filename |
| 17 | import os |
| 18 | |
| 19 | from mediagoblin.plugins.basic_auth import forms as auth_forms |
| 20 | from mediagoblin.plugins.basic_auth import tools as auth_tools |
| 21 | from mediagoblin.auth.tools import create_basic_user |
| 22 | from mediagoblin.db.models import LocalUser |
| 23 | from mediagoblin.tools import pluginapi |
| 24 | from sqlalchemy import or_ |
| 25 | from mediagoblin.tools.staticdirect import PluginStatic |
| 26 | |
| 27 | |
| 28 | PLUGIN_DIR = os.path.dirname(__file__) |
| 29 | |
| 30 | |
| 31 | def setup_plugin(): |
| 32 | config = pluginapi.get_config('mediagoblin.plugins.basic_auth') |
| 33 | |
| 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 | |
| 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', |
| 51 | 'fp_head': 'mediagoblin/plugins/basic_auth/fp_head.html', |
| 52 | 'create_account': |
| 53 | 'mediagoblin/plugins/basic_auth/create_account_link.html'}) |
| 54 | |
| 55 | |
| 56 | def get_user(**kwargs): |
| 57 | username = kwargs.pop('username', None) |
| 58 | if username: |
| 59 | user = LocalUser.query.filter( |
| 60 | or_( |
| 61 | LocalUser.username == username, |
| 62 | LocalUser.email == username, |
| 63 | )).first() |
| 64 | return user |
| 65 | |
| 66 | |
| 67 | def create_user(registration_form): |
| 68 | user = get_user(username=registration_form.username.data) |
| 69 | if not user and 'password' in registration_form: |
| 70 | user = create_basic_user(registration_form) |
| 71 | user.pw_hash = gen_password_hash( |
| 72 | registration_form.password.data) |
| 73 | user.save() |
| 74 | return user |
| 75 | |
| 76 | |
| 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 | |
| 85 | def gen_password_hash(raw_pass, extra_salt=None): |
| 86 | return auth_tools.bcrypt_gen_password_hash(raw_pass, extra_salt) |
| 87 | |
| 88 | |
| 89 | def check_password(raw_pass, stored_hash, extra_salt=None): |
| 90 | if stored_hash: |
| 91 | return auth_tools.bcrypt_check_password(raw_pass, |
| 92 | stored_hash, extra_salt) |
| 93 | return None |
| 94 | |
| 95 | |
| 96 | def auth(): |
| 97 | return True |
| 98 | |
| 99 | |
| 100 | def append_to_global_context(context): |
| 101 | context['pass_auth'] = True |
| 102 | return context |
| 103 | |
| 104 | |
| 105 | hooks = { |
| 106 | 'setup': setup_plugin, |
| 107 | 'authentication': auth, |
| 108 | 'auth_get_user': get_user, |
| 109 | 'auth_create_user': create_user, |
| 110 | 'auth_get_login_form': get_login_form, |
| 111 | 'auth_get_registration_form': get_registration_form, |
| 112 | 'auth_gen_password_hash': gen_password_hash, |
| 113 | 'auth_check_password': check_password, |
| 114 | 'auth_fake_login_attempt': auth_tools.fake_login_attempt, |
| 115 | 'template_global_context': append_to_global_context, |
| 116 | 'static_setup': lambda: PluginStatic( |
| 117 | 'coreplugin_basic_auth', |
| 118 | resource_filename('mediagoblin.plugins.basic_auth', 'static')) |
| 119 | } |