2018-07-02 19:51:51 -04:00
|
|
|
from lemoncurry.middleware import ResponseException
|
2018-01-28 18:28:46 -05:00
|
|
|
from micropub.views import error
|
2018-06-12 00:57:53 -04:00
|
|
|
from .models import IndieAuthCode, Token
|
2017-12-17 17:51:06 -05:00
|
|
|
|
|
|
|
|
|
|
|
def auth(request):
|
|
|
|
if 'HTTP_AUTHORIZATION' in request.META:
|
|
|
|
auth = request.META.get('HTTP_AUTHORIZATION').split(' ')
|
|
|
|
if auth[0] != 'Bearer':
|
2018-07-02 19:51:51 -04:00
|
|
|
raise ResponseException(error.bad_req(
|
|
|
|
'auth type {0} not supported'.format(auth[0])
|
|
|
|
))
|
2017-12-17 17:51:06 -05:00
|
|
|
if len(auth) != 2:
|
2018-07-02 19:51:51 -04:00
|
|
|
raise ResponseException(error.bad_req(
|
2018-01-28 18:28:46 -05:00
|
|
|
'invalid Bearer auth format, must be Bearer <token>'
|
2018-07-02 19:51:51 -04:00
|
|
|
))
|
2017-12-17 17:51:06 -05:00
|
|
|
token = auth[1]
|
|
|
|
elif 'access_token' in request.POST:
|
|
|
|
token = request.POST.get('access_token')
|
|
|
|
elif 'access_token' in request.GET:
|
|
|
|
token = request.GET.get('access_token')
|
|
|
|
else:
|
2018-07-02 19:51:51 -04:00
|
|
|
raise ResponseException(error.unauthorized())
|
2017-12-17 17:51:06 -05:00
|
|
|
|
|
|
|
try:
|
2018-06-12 00:57:53 -04:00
|
|
|
token = Token.objects.get(pk=token)
|
|
|
|
except Token.DoesNotExist:
|
2018-07-02 19:51:51 -04:00
|
|
|
raise ResponseException(error.forbidden())
|
2017-12-17 17:51:06 -05:00
|
|
|
|
2018-06-12 00:57:53 -04:00
|
|
|
return token
|
2017-11-02 23:37:39 -04:00
|
|
|
|
|
|
|
|
2017-11-03 00:51:27 -04:00
|
|
|
def gen_auth_code(req):
|
2018-06-12 00:57:53 -04:00
|
|
|
code = IndieAuthCode()
|
|
|
|
code.user = req.user
|
|
|
|
code.client_id = req.POST['client_id']
|
|
|
|
code.redirect_uri = req.POST['redirect_uri']
|
|
|
|
code.response_type = req.POST.get('response_type', 'id')
|
2017-11-03 01:14:30 -04:00
|
|
|
if 'scope' in req.POST:
|
2018-06-12 00:57:53 -04:00
|
|
|
code.scope = ' '.join(req.POST.getlist('scope'))
|
|
|
|
code.save()
|
|
|
|
return code.id
|
2017-11-03 02:18:00 -04:00
|
|
|
|
|
|
|
|
|
|
|
def gen_token(code):
|
2018-06-12 00:57:53 -04:00
|
|
|
tok = Token()
|
|
|
|
tok.user = code.user
|
|
|
|
tok.client_id = code.client_id
|
|
|
|
tok.scope = code.scope
|
|
|
|
tok.save()
|
|
|
|
return tok.id
|