Removed unused imports in httpapiauth
[mediagoblin.git] / mediagoblin / plugins / httpapiauth / __init__.py
CommitLineData
ac4c5aef
JW
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
17import logging
ac4c5aef 18
1422cab6 19from werkzeug.exceptions import Unauthorized
ac4c5aef
JW
20
21from mediagoblin.plugins.api.tools import Auth
22
23_log = logging.getLogger(__name__)
24
25
26def setup_http_api_auth():
27 _log.info('Setting up HTTP API Auth...')
28
29
30class HTTPAuth(Auth):
31 def trigger(self, request):
32 if request.authorization:
33 return True
34
35 return False
36
37 def __call__(self, request, *args, **kw):
38 _log.debug('Trying to authorize the user agent via HTTP Auth')
39 if not request.authorization:
40 return False
41
42 user = request.db.User.query.filter_by(
1eac751b 43 username=unicode(request.authorization['username'])).first()
ac4c5aef
JW
44
45 if user.check_login(request.authorization['password']):
46 request.user = user
47 return True
48 else:
49 raise Unauthorized()
50
51 return False
52
53
54
55hooks = {
56 'setup': setup_http_api_auth,
57 'auth': HTTPAuth()}