| 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 | |
| 17 | import logging |
| 18 | |
| 19 | import six |
| 20 | |
| 21 | from werkzeug.exceptions import Unauthorized |
| 22 | |
| 23 | from mediagoblin.auth.tools import check_login_simple |
| 24 | from mediagoblin.plugins.api.tools import Auth |
| 25 | |
| 26 | _log = logging.getLogger(__name__) |
| 27 | |
| 28 | |
| 29 | def setup_http_api_auth(): |
| 30 | _log.info('Setting up HTTP API Auth...') |
| 31 | |
| 32 | |
| 33 | class HTTPAuth(Auth): |
| 34 | def trigger(self, request): |
| 35 | if request.authorization: |
| 36 | return True |
| 37 | |
| 38 | return False |
| 39 | |
| 40 | def __call__(self, request, *args, **kw): |
| 41 | _log.debug('Trying to authorize the user agent via HTTP Auth') |
| 42 | if not request.authorization: |
| 43 | return False |
| 44 | |
| 45 | user = check_login_simple(six.text_type(request.authorization['username']), |
| 46 | request.authorization['password']) |
| 47 | |
| 48 | if user: |
| 49 | request.user = user |
| 50 | return True |
| 51 | else: |
| 52 | raise Unauthorized() |
| 53 | |
| 54 | return False |
| 55 | |
| 56 | |
| 57 | |
| 58 | hooks = { |
| 59 | 'setup': setup_http_api_auth, |
| 60 | 'auth': HTTPAuth()} |