2017-10-28 23:39:30 -04:00
|
|
|
from annoying.decorators import render_to
|
2017-10-25 20:35:57 -04:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2017-10-29 02:07:36 -04:00
|
|
|
from django.http import JsonResponse
|
2017-10-29 01:16:27 -04:00
|
|
|
from django.shortcuts import redirect
|
2017-10-25 20:35:57 -04:00
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from django.views.generic import TemplateView
|
2017-10-29 02:07:36 -04:00
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
2017-10-28 23:39:30 -04:00
|
|
|
from django.views.decorators.http import require_POST
|
2017-11-09 17:17:32 -05:00
|
|
|
from lemoncurry import breadcrumbs, requests, utils
|
2017-10-29 01:16:27 -04:00
|
|
|
from urllib.parse import urlencode, urljoin, urlunparse, urlparse
|
|
|
|
|
2017-11-02 01:36:16 -04:00
|
|
|
from .. import tokens
|
2018-06-12 00:57:53 -04:00
|
|
|
from ..models import IndieAuthCode
|
2017-10-25 20:35:57 -04:00
|
|
|
|
2017-12-17 18:53:17 -05:00
|
|
|
breadcrumbs.add('lemonauth:indie', parent='home:index')
|
2017-10-25 20:35:57 -04:00
|
|
|
|
|
|
|
|
2017-10-28 23:39:30 -04:00
|
|
|
def canonical(url):
|
|
|
|
(scheme, loc, path, params, q, fragment) = urlparse(url)
|
|
|
|
if not path:
|
|
|
|
path = '/'
|
|
|
|
if not loc:
|
|
|
|
loc, path = path, ''
|
|
|
|
if not scheme:
|
|
|
|
scheme = 'https'
|
|
|
|
return urlunparse((scheme, loc, path, params, q, fragment))
|
|
|
|
|
|
|
|
|
2017-10-29 02:07:36 -04:00
|
|
|
@method_decorator(csrf_exempt, name='dispatch')
|
2017-10-25 20:35:57 -04:00
|
|
|
class IndieView(TemplateView):
|
|
|
|
template_name = 'lemonauth/indie.html'
|
|
|
|
required_params = ('me', 'client_id', 'redirect_uri')
|
|
|
|
|
|
|
|
@method_decorator(login_required)
|
2017-10-28 23:39:30 -04:00
|
|
|
@method_decorator(render_to(template_name))
|
2017-10-25 20:35:57 -04:00
|
|
|
def get(self, request):
|
2017-10-28 23:39:30 -04:00
|
|
|
params = request.GET.dict()
|
|
|
|
params.setdefault('response_type', 'id')
|
|
|
|
|
2017-10-25 20:35:57 -04:00
|
|
|
for param in self.required_params:
|
|
|
|
if param not in params:
|
2017-11-02 01:16:04 -04:00
|
|
|
return utils.bad_req(
|
|
|
|
'parameter {0} is required'.format(param)
|
2017-10-25 20:35:57 -04:00
|
|
|
)
|
|
|
|
|
2017-10-28 23:39:30 -04:00
|
|
|
me = canonical(params['me'])
|
|
|
|
user = urljoin(utils.origin(request), request.user.url)
|
|
|
|
if user != me:
|
2017-11-02 01:16:04 -04:00
|
|
|
return utils.forbid(
|
|
|
|
'you are logged in but not as {0}'.format(me)
|
2017-10-25 20:35:57 -04:00
|
|
|
)
|
|
|
|
|
2017-11-02 20:28:26 -04:00
|
|
|
redirect_uri = urljoin(params['client_id'], params['redirect_uri'])
|
|
|
|
|
2017-10-31 22:27:55 -04:00
|
|
|
type = params['response_type']
|
|
|
|
if type not in ('id', 'code'):
|
2017-11-02 01:16:04 -04:00
|
|
|
return utils.bad_req(
|
|
|
|
'unknown response_type: {0}'.format(type)
|
2017-10-31 22:27:55 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
scopes = ()
|
|
|
|
if type == 'code':
|
|
|
|
if 'scope' not in params:
|
2017-11-02 01:16:04 -04:00
|
|
|
return utils.bad_req(
|
|
|
|
'scopes required for code type'
|
2017-10-31 22:27:55 -04:00
|
|
|
)
|
|
|
|
scopes = params['scope'].split(' ')
|
|
|
|
|
2017-11-09 17:17:32 -05:00
|
|
|
client = requests.mf2(params['client_id'])
|
2017-10-27 06:32:14 -04:00
|
|
|
rels = (client.to_dict()['rel-urls']
|
2017-11-02 21:14:15 -04:00
|
|
|
.get(redirect_uri, {})
|
2017-10-27 06:32:14 -04:00
|
|
|
.get('rels', ()))
|
2017-10-29 04:15:29 -04:00
|
|
|
verified = 'redirect_uri' in rels
|
2017-10-25 20:35:57 -04:00
|
|
|
|
2017-10-27 06:32:14 -04:00
|
|
|
try:
|
|
|
|
app = client.to_dict(filter_by_type='h-x-app')[0]['properties']
|
|
|
|
except IndexError:
|
|
|
|
app = None
|
|
|
|
|
2017-10-29 04:15:29 -04:00
|
|
|
return {
|
|
|
|
'app': app,
|
|
|
|
'me': me,
|
2017-11-02 20:28:26 -04:00
|
|
|
'redirect_uri': redirect_uri,
|
2017-10-29 04:15:29 -04:00
|
|
|
'verified': verified,
|
|
|
|
'params': params,
|
2017-10-31 22:27:55 -04:00
|
|
|
'scopes': scopes,
|
2017-12-17 18:53:17 -05:00
|
|
|
'title': 'indieauth from {client_id}'.format(**params),
|
2017-10-29 04:15:29 -04:00
|
|
|
}
|
2017-10-28 23:39:30 -04:00
|
|
|
|
2017-10-29 02:07:36 -04:00
|
|
|
def post(self, request):
|
|
|
|
post = request.POST.dict()
|
|
|
|
try:
|
2018-06-12 00:57:53 -04:00
|
|
|
code = IndieAuthCode.objects.get(pk=post.get('code'))
|
|
|
|
except IndieAuthCode.DoesNotExist:
|
2017-11-02 01:36:16 -04:00
|
|
|
# if anything at all goes wrong when decoding the auth code, bail
|
|
|
|
# out immediately.
|
2017-11-02 01:16:04 -04:00
|
|
|
return utils.forbid('invalid auth code')
|
2018-06-12 00:57:53 -04:00
|
|
|
code.delete()
|
|
|
|
if code.expired:
|
|
|
|
return utils.forbid('invalid auth code')
|
2017-10-31 19:56:49 -04:00
|
|
|
|
2018-06-12 00:57:53 -04:00
|
|
|
if code.response_type != 'id':
|
2017-11-02 01:16:04 -04:00
|
|
|
return utils.bad_req(
|
|
|
|
'this endpoint only supports response_type=id'
|
|
|
|
)
|
2018-06-12 00:57:53 -04:00
|
|
|
if code.client_id != post.get('client_id'):
|
2017-11-02 01:16:04 -04:00
|
|
|
return utils.forbid('client id did not match')
|
2018-06-12 00:57:53 -04:00
|
|
|
if code.redirect_uri != post.get('redirect_uri'):
|
2017-11-02 01:16:04 -04:00
|
|
|
return utils.forbid('redirect uri did not match')
|
2017-10-31 19:56:49 -04:00
|
|
|
|
|
|
|
# If we got here, it's valid! Yay!
|
2018-06-12 00:57:53 -04:00
|
|
|
return utils.choose_type(request, {'me': code.me}, {
|
2017-10-29 02:07:36 -04:00
|
|
|
'application/x-www-form-urlencoded': utils.form_encoded_response,
|
2017-11-02 20:51:34 -04:00
|
|
|
'application/json': JsonResponse,
|
2017-10-29 02:07:36 -04:00
|
|
|
})
|
|
|
|
|
2017-10-28 23:39:30 -04:00
|
|
|
|
|
|
|
@login_required
|
|
|
|
@require_POST
|
|
|
|
def approve(request):
|
2017-11-03 01:14:30 -04:00
|
|
|
params = {
|
|
|
|
'me': urljoin(utils.origin(request), request.user.url),
|
|
|
|
'code': tokens.gen_auth_code(request),
|
|
|
|
}
|
|
|
|
if 'state' in request.POST:
|
|
|
|
params['state'] = request.POST['state']
|
|
|
|
|
|
|
|
uri = request.POST['redirect_uri']
|
2017-11-02 21:14:15 -04:00
|
|
|
sep = '&' if '?' in uri else '?'
|
|
|
|
return redirect(uri + sep + urlencode(params))
|