Implement a token endpoint - currently all tokens last forever and can't be revoked, but I can add revocation later without too much trouble

This commit is contained in:
Danielle McLean 2017-11-03 17:18:00 +11:00
parent 9add6be8e4
commit 179f5753ed
Signed by untrusted user: 00dani
GPG key ID: 5A5D2D1AFF12EEC5
5 changed files with 85 additions and 9 deletions

View file

@ -1,7 +1,7 @@
import json
from accept_types import get_best_match
from django.conf import settings
from django.http import HttpResponse
from django.http import HttpResponse, JsonResponse
from django.http import HttpResponseForbidden, HttpResponseBadRequest
from os.path import join
from shorturls import default_converter as converter
@ -28,14 +28,6 @@ def uri(request):
return origin(request) + request.path
def choose_type(request, content, reps):
accept = request.META.get('HTTP_ACCEPT', '*/*')
type = get_best_match(accept, reps.keys())
if type:
return reps[type](content)
return HttpResponse(status=406)
def form_encoded_response(content):
return HttpResponse(
urlencode(content),
@ -43,6 +35,20 @@ def form_encoded_response(content):
)
REPS = {
'application/x-www-form-urlencoded': form_encoded_response,
'application/json': JsonResponse,
}
def choose_type(request, content, reps=REPS):
accept = request.META.get('HTTP_ACCEPT', '*/*')
type = get_best_match(accept, reps.keys())
if type:
return reps[type](content)
return HttpResponse(status=406)
def shortlink(obj):
prefix = ShortURL(None).get_prefix(obj)
tinyid = converter.from_decimal(obj.pk)