2018-07-02 20:03:35 -04:00
|
|
|
from micropub import error
|
2018-06-12 00:57:53 -04:00
|
|
|
from .models import IndieAuthCode, Token
|
2017-12-17 17:51:06 -05:00
|
|
|
|
|
|
|
|
2018-07-02 20:03:35 -04:00
|
|
|
def auth(request) -> Token:
|
2023-08-10 02:52:37 -04:00
|
|
|
if "HTTP_AUTHORIZATION" in request.META:
|
|
|
|
auth = request.META.get("HTTP_AUTHORIZATION").split(" ")
|
|
|
|
if auth[0] != "Bearer":
|
|
|
|
raise error.bad_req("auth type {0} not supported".format(auth[0]))
|
2017-12-17 17:51:06 -05:00
|
|
|
if len(auth) != 2:
|
2023-08-10 02:52:37 -04:00
|
|
|
raise error.bad_req("invalid Bearer auth format, must be Bearer <token>")
|
2017-12-17 17:51:06 -05:00
|
|
|
token = auth[1]
|
2023-08-10 02:52:37 -04:00
|
|
|
elif "access_token" in request.POST:
|
|
|
|
token = request.POST.get("access_token")
|
|
|
|
elif "access_token" in request.GET:
|
|
|
|
token = request.GET.get("access_token")
|
2017-12-17 17:51:06 -05:00
|
|
|
else:
|
2018-07-02 20:03:35 -04:00
|
|
|
raise 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 20:03:35 -04:00
|
|
|
raise 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
|
2023-08-10 02:52:37 -04:00
|
|
|
code.client_id = req.POST["client_id"]
|
|
|
|
code.redirect_uri = req.POST["redirect_uri"]
|
|
|
|
code.response_type = req.POST.get("response_type", "id")
|
|
|
|
if "scope" in req.POST:
|
|
|
|
code.scope = " ".join(req.POST.getlist("scope"))
|
2018-06-12 00:57:53 -04:00
|
|
|
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
|