2017-10-25 20:35:57 -04:00
|
|
|
import mf2py
|
|
|
|
|
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
|
|
|
|
from django.http import HttpResponseForbidden, HttpResponseBadRequest
|
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-10-27 07:03:25 -04:00
|
|
|
from lemoncurry import breadcrumbs, utils
|
2017-10-29 01:16:27 -04:00
|
|
|
from urllib.parse import urlencode, urljoin, urlunparse, urlparse
|
|
|
|
|
|
|
|
from ..models import IndieAuthCode
|
2017-10-25 20:35:57 -04:00
|
|
|
|
|
|
|
breadcrumbs.add('lemonauth:indie', label='indieauth', parent='home:index')
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
return HttpResponseBadRequest(
|
|
|
|
'parameter {0} is required'.format(param),
|
|
|
|
content_type='text/plain',
|
|
|
|
)
|
|
|
|
|
2017-10-28 23:39:30 -04:00
|
|
|
me = canonical(params['me'])
|
|
|
|
user = urljoin(utils.origin(request), request.user.url)
|
|
|
|
if user != me:
|
2017-10-25 20:35:57 -04:00
|
|
|
return HttpResponseForbidden(
|
|
|
|
'you are logged in but not as {0}'.format(me),
|
|
|
|
content_type='text/plain',
|
|
|
|
)
|
|
|
|
|
2017-10-27 06:32:14 -04:00
|
|
|
client = mf2py.Parser(url=params['client_id'], html_parser='html5lib')
|
|
|
|
rels = (client.to_dict()['rel-urls']
|
|
|
|
.get(params['redirect_uri'], {})
|
|
|
|
.get('rels', ()))
|
2017-10-25 20:35:57 -04:00
|
|
|
if 'redirect_uri' not in rels:
|
|
|
|
return HttpResponseBadRequest(
|
|
|
|
'your redirect_uri is not published on your client_id page',
|
|
|
|
content_type='text/plain'
|
|
|
|
)
|
|
|
|
|
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-28 23:39:30 -04:00
|
|
|
return {'app': app, 'me': me, 'params': params, 'title': 'indieauth'}
|
|
|
|
|
2017-10-29 02:07:36 -04:00
|
|
|
def post(self, request):
|
|
|
|
post = request.POST.dict()
|
|
|
|
try:
|
|
|
|
code = IndieAuthCode.objects.get(
|
|
|
|
code=post['code'],
|
|
|
|
client_id=post['client_id'],
|
|
|
|
redirect_uri=post['redirect_uri']
|
|
|
|
)
|
|
|
|
except IndieAuthCode.DoesNotExist:
|
|
|
|
return HttpResponseForbidden(
|
|
|
|
'invalid auth code {0}'.format(post['code']),
|
|
|
|
content_type='text/plain'
|
|
|
|
)
|
|
|
|
code.delete()
|
|
|
|
return utils.choose_type(request, {'me': code.me}, {
|
|
|
|
'application/json': JsonResponse,
|
|
|
|
'application/x-www-form-urlencoded': utils.form_encoded_response,
|
|
|
|
})
|
|
|
|
|
2017-10-28 23:39:30 -04:00
|
|
|
|
|
|
|
@login_required
|
|
|
|
@require_POST
|
|
|
|
def approve(request):
|
2017-10-29 01:16:27 -04:00
|
|
|
post = request.POST.dict()
|
|
|
|
code = IndieAuthCode.objects.create_from_dict(post)
|
|
|
|
code.save()
|
|
|
|
params = {'code': code.code, 'me': code.me}
|
|
|
|
if 'state' in post:
|
|
|
|
params['state'] = post['state']
|
|
|
|
return redirect(code.redirect_uri + '?' + urlencode(params))
|