This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
authentik/passbook/root/monitoring.py

24 lines
787 B
Python
Raw Normal View History

2019-11-08 11:23:51 +00:00
"""Metrics view"""
from base64 import b64encode
2019-11-08 11:24:02 +00:00
2019-11-08 11:23:51 +00:00
from django.conf import settings
2019-11-08 11:24:02 +00:00
from django.http import Http404, HttpRequest, HttpResponse
2019-11-08 11:23:51 +00:00
from django.views import View
from django_prometheus.exports import ExportToDjangoView
2019-11-08 11:24:02 +00:00
2019-11-08 11:23:51 +00:00
class MetricsView(View):
"""Wrapper around ExportToDjangoView, using http-basic auth"""
def get(self, request: HttpRequest) -> HttpResponse:
"""Check for HTTP-Basic auth"""
auth_header = request.META.get('HTTP_AUTHORIZATION', '')
token_type, _, credentials = auth_header.partition(' ')
creds = f"monitor:{settings.SECRET_KEY}"
expected = b64encode(str.encode(creds)).decode()
if token_type != 'Basic' or credentials != expected:
raise Http404
return ExportToDjangoView(request)