Run Black over the whole codebase
This commit is contained in:
parent
cd990e4e2f
commit
2e7d12b3e6
109 changed files with 1539 additions and 1209 deletions
|
@ -12,120 +12,114 @@ from urllib.parse import urlencode, urljoin, urlunparse, urlparse
|
|||
from .. import tokens
|
||||
from ..models import IndieAuthCode
|
||||
|
||||
breadcrumbs.add('lemonauth:indie', parent='home:index')
|
||||
breadcrumbs.add("lemonauth:indie", parent="home:index")
|
||||
|
||||
|
||||
def canonical(url):
|
||||
if '//' not in url:
|
||||
url = '//' + url
|
||||
if "//" not in url:
|
||||
url = "//" + url
|
||||
(scheme, netloc, path, params, query, fragment) = urlparse(url)
|
||||
if not scheme or scheme == 'http':
|
||||
scheme = 'https'
|
||||
if not scheme or scheme == "http":
|
||||
scheme = "https"
|
||||
if not path:
|
||||
path = '/'
|
||||
path = "/"
|
||||
return urlunparse((scheme, netloc, path, params, query, fragment))
|
||||
|
||||
|
||||
@method_decorator(csrf_exempt, name='dispatch')
|
||||
@method_decorator(csrf_exempt, name="dispatch")
|
||||
class IndieView(TemplateView):
|
||||
template_name = 'lemonauth/indie.html'
|
||||
required_params = ('client_id', 'redirect_uri')
|
||||
template_name = "lemonauth/indie.html"
|
||||
required_params = ("client_id", "redirect_uri")
|
||||
|
||||
@method_decorator(login_required)
|
||||
@method_decorator(render_to(template_name))
|
||||
def get(self, request):
|
||||
params = request.GET.dict()
|
||||
params.setdefault('response_type', 'id')
|
||||
params.setdefault("response_type", "id")
|
||||
|
||||
for param in self.required_params:
|
||||
if param not in params:
|
||||
return utils.bad_req(
|
||||
'parameter {0} is required'.format(param)
|
||||
)
|
||||
return utils.bad_req("parameter {0} is required".format(param))
|
||||
|
||||
me = request.user.full_url
|
||||
if 'me' in params:
|
||||
param_me = canonical(params['me'])
|
||||
if "me" in params:
|
||||
param_me = canonical(params["me"])
|
||||
if me != param_me:
|
||||
return utils.forbid(
|
||||
'you are logged in as {}, not as {}'.format(me, param_me)
|
||||
"you are logged in as {}, not as {}".format(me, param_me)
|
||||
)
|
||||
|
||||
redirect_uri = urljoin(params['client_id'], params['redirect_uri'])
|
||||
redirect_uri = urljoin(params["client_id"], params["redirect_uri"])
|
||||
|
||||
type = params['response_type']
|
||||
if type not in ('id', 'code'):
|
||||
return utils.bad_req(
|
||||
'unknown response_type: {0}'.format(type)
|
||||
)
|
||||
type = params["response_type"]
|
||||
if type not in ("id", "code"):
|
||||
return utils.bad_req("unknown response_type: {0}".format(type))
|
||||
|
||||
scopes = ()
|
||||
if type == 'code':
|
||||
if 'scope' not in params:
|
||||
return utils.bad_req(
|
||||
'scopes required for code type'
|
||||
)
|
||||
scopes = params['scope'].split(' ')
|
||||
if type == "code":
|
||||
if "scope" not in params:
|
||||
return utils.bad_req("scopes required for code type")
|
||||
scopes = params["scope"].split(" ")
|
||||
|
||||
client = requests.mf2(params['client_id'])
|
||||
rels = (client.to_dict()['rel-urls']
|
||||
.get(redirect_uri, {})
|
||||
.get('rels', ()))
|
||||
verified = 'redirect_uri' in rels
|
||||
client = requests.mf2(params["client_id"])
|
||||
rels = client.to_dict()["rel-urls"].get(redirect_uri, {}).get("rels", ())
|
||||
verified = "redirect_uri" in rels
|
||||
|
||||
try:
|
||||
app = client.to_dict(filter_by_type='h-x-app')[0]['properties']
|
||||
app = client.to_dict(filter_by_type="h-x-app")[0]["properties"]
|
||||
except IndexError:
|
||||
app = None
|
||||
|
||||
return {
|
||||
'app': app,
|
||||
'me': me,
|
||||
'redirect_uri': redirect_uri,
|
||||
'verified': verified,
|
||||
'params': params,
|
||||
'scopes': scopes,
|
||||
'title': 'indieauth from {client_id}'.format(**params),
|
||||
"app": app,
|
||||
"me": me,
|
||||
"redirect_uri": redirect_uri,
|
||||
"verified": verified,
|
||||
"params": params,
|
||||
"scopes": scopes,
|
||||
"title": "indieauth from {client_id}".format(**params),
|
||||
}
|
||||
|
||||
def post(self, request):
|
||||
post = request.POST.dict()
|
||||
try:
|
||||
code = IndieAuthCode.objects.get(pk=post.get('code'))
|
||||
code = IndieAuthCode.objects.get(pk=post.get("code"))
|
||||
except IndieAuthCode.DoesNotExist:
|
||||
# if anything at all goes wrong when decoding the auth code, bail
|
||||
# out immediately.
|
||||
return utils.forbid('invalid auth code')
|
||||
return utils.forbid("invalid auth code")
|
||||
code.delete()
|
||||
if code.expired:
|
||||
return utils.forbid('invalid auth code')
|
||||
return utils.forbid("invalid auth code")
|
||||
|
||||
if code.response_type != 'id':
|
||||
return utils.bad_req(
|
||||
'this endpoint only supports response_type=id'
|
||||
)
|
||||
if code.client_id != post.get('client_id'):
|
||||
return utils.forbid('client id did not match')
|
||||
if code.redirect_uri != post.get('redirect_uri'):
|
||||
return utils.forbid('redirect uri did not match')
|
||||
if code.response_type != "id":
|
||||
return utils.bad_req("this endpoint only supports response_type=id")
|
||||
if code.client_id != post.get("client_id"):
|
||||
return utils.forbid("client id did not match")
|
||||
if code.redirect_uri != post.get("redirect_uri"):
|
||||
return utils.forbid("redirect uri did not match")
|
||||
|
||||
# If we got here, it's valid! Yay!
|
||||
return utils.choose_type(request, {'me': code.me}, {
|
||||
'application/x-www-form-urlencoded': utils.form_encoded_response,
|
||||
'application/json': JsonResponse,
|
||||
})
|
||||
return utils.choose_type(
|
||||
request,
|
||||
{"me": code.me},
|
||||
{
|
||||
"application/x-www-form-urlencoded": utils.form_encoded_response,
|
||||
"application/json": JsonResponse,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@require_POST
|
||||
def approve(request):
|
||||
params = {
|
||||
'me': urljoin(utils.origin(request), request.user.url),
|
||||
'code': tokens.gen_auth_code(request),
|
||||
"me": urljoin(utils.origin(request), request.user.url),
|
||||
"code": tokens.gen_auth_code(request),
|
||||
}
|
||||
if 'state' in request.POST:
|
||||
params['state'] = request.POST['state']
|
||||
if "state" in request.POST:
|
||||
params["state"] = request.POST["state"]
|
||||
|
||||
uri = request.POST['redirect_uri']
|
||||
sep = '&' if '?' in uri else '?'
|
||||
uri = request.POST["redirect_uri"]
|
||||
sep = "&" if "?" in uri else "?"
|
||||
return redirect(uri + sep + urlencode(params))
|
||||
|
|
|
@ -2,11 +2,11 @@ import django.contrib.auth.views
|
|||
from otp_agents.forms import OTPAuthenticationForm
|
||||
from lemoncurry import breadcrumbs
|
||||
|
||||
breadcrumbs.add(route='lemonauth:login', label='log in', parent='home:index')
|
||||
breadcrumbs.add(route="lemonauth:login", label="log in", parent="home:index")
|
||||
|
||||
login = django.contrib.auth.views.LoginView.as_view(
|
||||
authentication_form=OTPAuthenticationForm,
|
||||
extra_context={'title': 'log in'},
|
||||
template_name='lemonauth/login.html',
|
||||
extra_context={"title": "log in"},
|
||||
template_name="lemonauth/login.html",
|
||||
redirect_authenticated_user=True,
|
||||
)
|
||||
|
|
|
@ -7,41 +7,42 @@ from ..models import IndieAuthCode
|
|||
from lemoncurry import utils
|
||||
|
||||
|
||||
@method_decorator(csrf_exempt, name='dispatch')
|
||||
@method_decorator(csrf_exempt, name="dispatch")
|
||||
class TokenView(View):
|
||||
def get(self, req):
|
||||
token = tokens.auth(req)
|
||||
res = {
|
||||
'me': token.me,
|
||||
'client_id': token.client_id,
|
||||
'scope': token.scope,
|
||||
"me": token.me,
|
||||
"client_id": token.client_id,
|
||||
"scope": token.scope,
|
||||
}
|
||||
return utils.choose_type(req, res)
|
||||
|
||||
def post(self, req):
|
||||
post = req.POST
|
||||
try:
|
||||
code = IndieAuthCode.objects.get(pk=post.get('code'))
|
||||
code = IndieAuthCode.objects.get(pk=post.get("code"))
|
||||
except IndieAuthCode.DoesNotExist:
|
||||
return utils.forbid('invalid auth code')
|
||||
return utils.forbid("invalid auth code")
|
||||
code.delete()
|
||||
if code.expired:
|
||||
return utils.forbid('invalid auth code')
|
||||
return utils.forbid("invalid auth code")
|
||||
|
||||
if code.response_type != 'code':
|
||||
return utils.bad_req(
|
||||
'this endpoint only supports response_type=code'
|
||||
)
|
||||
if 'client_id' in post and code.client_id != post['client_id']:
|
||||
return utils.forbid('client id did not match')
|
||||
if code.redirect_uri != post.get('redirect_uri'):
|
||||
return utils.forbid('redirect uri did not match')
|
||||
if code.response_type != "code":
|
||||
return utils.bad_req("this endpoint only supports response_type=code")
|
||||
if "client_id" in post and code.client_id != post["client_id"]:
|
||||
return utils.forbid("client id did not match")
|
||||
if code.redirect_uri != post.get("redirect_uri"):
|
||||
return utils.forbid("redirect uri did not match")
|
||||
|
||||
if 'me' in post and code.me != post['me']:
|
||||
return utils.forbid('me did not match')
|
||||
if "me" in post and code.me != post["me"]:
|
||||
return utils.forbid("me did not match")
|
||||
|
||||
return utils.choose_type(req, {
|
||||
'access_token': tokens.gen_token(code),
|
||||
'me': code.me,
|
||||
'scope': code.scope,
|
||||
})
|
||||
return utils.choose_type(
|
||||
req,
|
||||
{
|
||||
"access_token": tokens.gen_token(code),
|
||||
"me": code.me,
|
||||
"scope": code.scope,
|
||||
},
|
||||
)
|
||||
|
|
|
@ -20,15 +20,15 @@ class Client:
|
|||
self.id = client_id
|
||||
self.count = 0
|
||||
self.scopes = set()
|
||||
apps = mf2(self.id).to_dict(filter_by_type='h-x-app')
|
||||
apps = mf2(self.id).to_dict(filter_by_type="h-x-app")
|
||||
try:
|
||||
self.app = apps[0]['properties']
|
||||
self.app = apps[0]["properties"]
|
||||
except IndexError:
|
||||
self.app = None
|
||||
|
||||
|
||||
class TokensListView(LoginRequiredMixin, TemplateView):
|
||||
template_name = 'lemonauth/tokens.html'
|
||||
template_name = "lemonauth/tokens.html"
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
|
@ -36,6 +36,6 @@ class TokensListView(LoginRequiredMixin, TemplateView):
|
|||
for token in self.request.user.token_set.all():
|
||||
client = clients[token.client_id]
|
||||
client.count += 1
|
||||
client.scopes |= set(token.scope.split(' '))
|
||||
context.update({'clients': clients, 'title': 'tokens'})
|
||||
client.scopes |= set(token.scope.split(" "))
|
||||
context.update({"clients": clients, "title": "tokens"})
|
||||
return context
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue