31a4fd95755d612d20d8c384b64c410420ab0a39
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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
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
28 PLUGIN_DIR
= os
.path
.dirname(__file__
)
32 config
= pluginapi
.get_config('mediagoblin.plugins.basic_auth')
35 ('mediagoblin.plugins.basic_auth.edit.pass',
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')]
45 pluginapi
.register_routes(routes
)
46 pluginapi
.register_template_path(os
.path
.join(PLUGIN_DIR
, 'templates'))
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',
53 'mediagoblin/plugins/basic_auth/create_account_link.html'})
56 def get_user(**kwargs
):
57 username
= kwargs
.pop('username', None)
59 user
= LocalUser
.query
.filter(
61 LocalUser
.username
== username
,
62 LocalUser
.email
== username
,
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
)
77 def get_login_form(request
):
78 return auth_forms
.LoginForm(request
.form
)
81 def get_registration_form(request
):
82 return auth_forms
.RegistrationForm(request
.form
)
85 def gen_password_hash(raw_pass
, extra_salt
=None):
86 return auth_tools
.bcrypt_gen_password_hash(raw_pass
, extra_salt
)
89 def check_password(raw_pass
, stored_hash
, extra_salt
=None):
91 return auth_tools
.bcrypt_check_password(raw_pass
,
92 stored_hash
, extra_salt
)
100 def append_to_global_context(context
):
101 context
['pass_auth'] = True
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'))