-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6dce295
commit bde1d78
Showing
10 changed files
with
192 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
from time import time | ||
from typing import Callable | ||
|
||
from django.core.exceptions import ObjectDoesNotExist | ||
from django.urls import path | ||
from rest_framework.views import APIView | ||
from rest_framework.response import Response | ||
from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView, extend_schema | ||
|
||
from aw.model.api import APIKey | ||
from aw.utils.http import deny_request | ||
|
||
|
||
def endpoint_wrapper(func) -> Callable: | ||
def wrapper(request, *args, **kwargs): | ||
bad, deny = deny_request(request) | ||
if bad: | ||
return deny | ||
|
||
return func(request, *args, **kwargs) | ||
|
||
return wrapper | ||
|
||
|
||
class KeyRead(APIView): | ||
http_method_names = ['get'] | ||
|
||
@staticmethod | ||
@endpoint_wrapper | ||
@extend_schema( | ||
summary='Return a list of all existing API keys of the current user.', | ||
parameters=None, | ||
) | ||
def get(request): | ||
return Response({'tokens': [key.name for key in APIKey.objects.filter(user=request.user)]}) | ||
|
||
|
||
class KeyWrite(APIView): | ||
http_method_names = ['post', 'delete'] | ||
|
||
@staticmethod | ||
@endpoint_wrapper | ||
@extend_schema( | ||
summary='Create a new API key.', | ||
) | ||
def post(request): | ||
token, secret = APIKey.objects.create_key( | ||
name=f'{request.user}-{time()}', | ||
user=request.user, | ||
) | ||
return Response({'token': token, 'secret': secret}) | ||
|
||
@staticmethod | ||
@endpoint_wrapper | ||
@extend_schema( | ||
summary='Delete one of the existing API keys of the current user.', | ||
) | ||
def delete(request): | ||
req_key = request.query_params.get('token', None) | ||
|
||
if req_key is None: | ||
return Response({'msg': 'No API key provided'}) | ||
|
||
try: | ||
results = APIKey.objects.filter(user=request.user, name=req_key) | ||
if len(results) == 1: | ||
results.delete() | ||
return Response({'msg': f"API key '{req_key}' revoked"}) | ||
|
||
except ObjectDoesNotExist: | ||
pass | ||
|
||
return Response(data={'msg': f"API key '{req_key}' not found"}, status=404) | ||
|
||
|
||
urlpatterns_api = [ | ||
path('api/key', KeyRead.as_view()), | ||
path('api/key/<str:token>', KeyWrite.as_view()), | ||
path('api/_schema/', SpectacularAPIView.as_view(), name='_schema'), | ||
path('api', SpectacularSwaggerView.as_view(url_name='_schema'), name='swagger-ui'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from django.db import models | ||
from django.conf import settings | ||
from rest_framework_api_key.models import AbstractAPIKey | ||
|
||
|
||
class APIKey(AbstractAPIKey): | ||
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{% extends "rest_framework/base.html" %} | ||
{# https://github.com/encode/django-rest-framework/blob/master/rest_framework/templates/rest_framework/base.html #} | ||
{% load static %} | ||
{% block title %} | ||
Ansible-WebUI API | ||
{% endblock %} | ||
{% block meta %} | ||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> | ||
<meta name="robots" content="NONE,NOARCHIVE" /> | ||
|
||
<link rel="icon" type="image/svg" href="{% static 'img/ansible.svg' %}"> | ||
{% endblock %} | ||
{% block branding %} | ||
<a class='navbar-brand' rel="nofollow" href='/'> | ||
Ansible-WebUI | ||
</a> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from django.shortcuts import HttpResponse | ||
|
||
|
||
def deny_request(request) -> (bool, HttpResponse): | ||
if request.method not in ['GET', 'POST', 'PUT']: | ||
return True, HttpResponse(status=405) | ||
|
||
return False, None |