2017-11-03 02:18:00 -04:00
|
|
|
from django.views import View
|
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
|
|
|
|
from .. import tokens
|
2018-06-12 00:57:53 -04:00
|
|
|
from ..models import IndieAuthCode
|
2017-11-03 02:18:00 -04:00
|
|
|
from lemoncurry import utils
|
|
|
|
|
|
|
|
|
|
|
|
@method_decorator(csrf_exempt, name='dispatch')
|
|
|
|
class TokenView(View):
|
|
|
|
def get(self, req):
|
2017-12-17 17:51:06 -05:00
|
|
|
token = tokens.auth(req)
|
2017-11-03 02:18:00 -04:00
|
|
|
res = {
|
2017-12-17 17:51:06 -05:00
|
|
|
'me': token.me,
|
2018-06-12 00:57:53 -04:00
|
|
|
'client_id': token.client_id,
|
2017-12-17 17:51:06 -05:00
|
|
|
'scope': token.scope,
|
2017-11-03 02:18:00 -04:00
|
|
|
}
|
|
|
|
return utils.choose_type(req, res)
|
|
|
|
|
|
|
|
def post(self, req):
|
|
|
|
post = req.POST
|
|
|
|
try:
|
2018-06-12 00:57:53 -04:00
|
|
|
code = IndieAuthCode.objects.get(pk=post.get('code'))
|
|
|
|
except IndieAuthCode.DoesNotExist:
|
|
|
|
return utils.forbid('invalid auth code')
|
|
|
|
code.delete()
|
|
|
|
if code.expired:
|
2017-11-03 02:18:00 -04:00
|
|
|
return utils.forbid('invalid auth code')
|
|
|
|
|
2018-06-12 00:57:53 -04:00
|
|
|
if code.response_type != 'code':
|
2017-11-03 02:18:00 -04:00
|
|
|
return utils.bad_req(
|
|
|
|
'this endpoint only supports response_type=code'
|
|
|
|
)
|
2018-06-22 23:43:15 -04:00
|
|
|
if 'client_id' in post and code.client_id != post['client_id']:
|
2017-11-03 02:18:00 -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-03 02:18:00 -04:00
|
|
|
return utils.forbid('redirect uri did not match')
|
|
|
|
|
2018-06-22 23:43:15 -04:00
|
|
|
if 'me' in post and code.me != post['me']:
|
2017-11-03 02:18:00 -04:00
|
|
|
return utils.forbid('me did not match')
|
|
|
|
|
|
|
|
return utils.choose_type(req, {
|
|
|
|
'access_token': tokens.gen_token(code),
|
2018-06-12 00:57:53 -04:00
|
|
|
'me': code.me,
|
|
|
|
'scope': code.scope,
|
2017-11-03 02:18:00 -04:00
|
|
|
})
|