Skip to content

Commit 10aed85

Browse files
committed
Added http basic authentication to results resources
1 parent ad9ede1 commit 10aed85

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

codespeed/auth.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import logging
2+
from functools import wraps
3+
from django.contrib.auth import authenticate
4+
from django.http import HttpResponse, HttpResponseForbidden
5+
from base64 import b64decode
6+
7+
8+
def basic_auth_required(realm='default'):
9+
def _helper(func):
10+
@wraps(func)
11+
def _decorator(request, *args, **kwargs):
12+
if 'HTTP_AUTHORIZATION' in request.META:
13+
http_auth = request.META['HTTP_AUTHORIZATION']
14+
authmeth, auth = http_auth.split(' ', 1)
15+
if authmeth.lower() == 'basic':
16+
authb = b64decode(auth.strip())
17+
auth = authb.decode()
18+
username, password = auth.split(':', 1)
19+
user = authenticate(username=username, password=password)
20+
if user is not None:
21+
logging.info(
22+
'Authentication succeeded for {}'.format(username))
23+
return func(request, *args, **kwargs)
24+
else:
25+
return HttpResponseForbidden()
26+
res = HttpResponse()
27+
res.status_code = 401
28+
res.reason_phrase = 'Unauthorized'
29+
res['WWW-Authenticate'] = 'Basic realm="{}"'.format(realm)
30+
return res
31+
return _decorator
32+
33+
return _helper

codespeed/views.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from django.views.decorators.csrf import csrf_exempt
1515
from django.template import RequestContext
1616
from django.conf import settings
17+
from .auth import basic_auth_required
1718

1819
from .models import (Environment, Report, Project, Revision, Result,
1920
Executable, Benchmark, Branch)
@@ -697,6 +698,7 @@ def displaylogs(request):
697698

698699
@csrf_exempt
699700
@require_POST
701+
@basic_auth_required('results')
700702
def add_result(request):
701703
response, error = save_result(request.POST)
702704
if error:
@@ -710,6 +712,7 @@ def add_result(request):
710712

711713
@csrf_exempt
712714
@require_POST
715+
@basic_auth_required('results')
713716
def add_json_results(request):
714717
if not request.POST.get('json'):
715718
return HttpResponseBadRequest("No key 'json' in POST payload")

0 commit comments

Comments
 (0)