aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/plugins/oauth/views.py
blob: 70be303968cc0d9afaeb225a3066c58ba8e41f5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# GNU MediaGoblin -- federated, autonomous media hosting
# Copyright (C) 2011, 2012 MediaGoblin contributors.  See AUTHORS.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import logging
import json

from webob import exc, Response
from urllib import urlencode
from uuid import uuid4
from datetime import datetime
from functools import wraps

from mediagoblin.tools import pluginapi
from mediagoblin.tools.response import render_to_response
from mediagoblin.decorators import require_active_login
from mediagoblin.messages import add_message, SUCCESS, ERROR
from mediagoblin.tools.translate import pass_to_ugettext as _
from mediagoblin.plugins.oauth.models import OAuthCode, OAuthToken

_log = logging.getLogger(__name__)


@require_active_login
def authorize(request):
    # TODO: Check if allowed

    # Client is allowed by the user
    if True or already_authorized:
        # Generate a code
        # Save the code, the client will later use it to obtain an access token
        # Redirect the user agent to the redirect_uri with the code

        if not 'redirect_uri' in request.GET:
            add_message(request, ERROR, _('No redirect_uri found'))

        code = OAuthCode()
        code.code = unicode(uuid4())
        code.user = request.user
        code.save()

        redirect_uri = ''.join([
            request.GET.get('redirect_uri'),
            '?',
            urlencode({'code': code.code})])

        _log.debug('Redirecting to {0}'.format(redirect_uri))

        return exc.HTTPFound(location=redirect_uri)
    else:
        # Show prompt to allow client to access data
        # - on accept: send the user agent back to the redirect_uri with the
        # code parameter
        # - on deny: send the user agent back to the redirect uri with error
        # information
        pass
    return render_to_response(request, 'oauth/base.html', {})


def access_token(request):
    if request.GET.get('code'):
        code = OAuthCode.query.filter(OAuthCode.code == request.GET.get('code'))\
                .first()

        if code:
            token = OAuthToken()
            token.token = unicode(uuid4())
            token.user = code.user
            token.save()

            access_token_data = {
                'access_token': token.token,
                'token_type': 'what_do_i_use_this_for',  # TODO
                'expires_in':
                (token.expires - datetime.now()).total_seconds(),
                'refresh_token': 'This should probably be safe'}
            return Response(json.dumps(access_token_data))

    error_data = {
        'error': 'Incorrect code'}
    return Response(json.dumps(error_data))