Compare commits

..

33 Commits
v0.12.0 ... api

Author SHA1 Message Date
Paweł Jastrzębski
5e48e2dcf5 Use POST to api token refresh 2022-08-29 08:44:17 +02:00
R. Miles McCain
b286c80754 Remove unneeded views 2022-08-28 15:07:05 -07:00
R. Miles McCain
c23f44d7b7 Merge commit '77cb1fb37c0da5bad39b3905f7a48cd3f176bac7' into api 2022-08-28 15:01:04 -07:00
Paweł Jastrzębski
b7f2e9cfe6 Remove basic option from api view 2022-08-28 13:35:22 +02:00
R. Miles McCain
77cb1fb37c Improve language 2022-08-27 14:52:02 -07:00
Paweł Jastrzębski
d9bbeea892 Remove basic option from API
For simplicity
2022-05-12 12:10:44 +02:00
Paweł Jastrzębski
ca97453c3e Return 400 if date format is invalid 2022-04-26 10:13:52 +02:00
Paweł Jastrzębski
b87b158aab Fix typo 2022-04-22 08:28:09 +02:00
Paweł Jastrzębski
4a6af18765 Add django-cors-headers 2022-04-14 19:41:14 +02:00
Paweł Jastrzębski
6d84f63130 Add API documentation to GUIDE.md 2022-01-05 10:27:14 +01:00
Paweł Jastrzębski
ba91ed561d Add uuid validation 2022-01-05 09:47:14 +01:00
Paweł Jastrzębski
2aaadfe81c Display api urls on service management page 2022-01-05 09:47:05 +01:00
Paweł Jastrzębski
7f60b3abff Rename minimal parameter to basic 2022-01-05 08:53:46 +01:00
Paweł Jastrzębski
069b218828 Move api token info to security tab 2022-01-04 08:53:00 +01:00
Paweł Jastrzębski
80647d960a Merge branch 'master' into api 2022-01-01 19:56:55 +01:00
Paweł Jastrzębski
364ef115c9 Merge branch 'api' of https://github.com/haaavk/shynet into api 2022-01-01 19:50:10 +01:00
Paweł Jastrzębski
bcf94147c9 Fix problem with whitespaces in copied token 2021-12-31 12:11:42 -05:00
Paweł Jastrzębski
66b841fd86 Move token to User model + add API setting view 2021-12-31 12:11:42 -05:00
Paweł Jastrzębski
d809ec82d9 Add uuid filter and service uuid filter 2021-12-31 12:11:42 -05:00
Paweł Jastrzębski
e577aa4997 Add minimal argument to get_core_stats 2021-12-31 12:11:42 -05:00
Paweł Jastrzębski
5966ea2f84 Add DashboardApiView 2021-12-31 12:11:42 -05:00
Paweł Jastrzębski
a7248cd54b Add ApiTokenRequiredMixin 2021-12-31 12:11:42 -05:00
Paweł Jastrzębski
1dec03c724 Add ApiToken to admin 2021-12-31 12:11:42 -05:00
Paweł Jastrzębski
ff6933b4de Add api app with ApiToken model 2021-12-31 12:11:42 -05:00
Paweł Jastrzębski
8302aedaa7 Fix problem with whitespaces in copied token 2021-11-17 11:46:35 +01:00
Paweł Jastrzębski
e2d438134a Merge branch 'master' into api 2021-11-17 11:24:34 +01:00
Paweł Jastrzębski
787ce1775f Move token to User model + add API setting view 2021-11-17 11:00:52 +01:00
Paweł Jastrzębski
d62d48c7b4 Add uuid filter and service uuid filter 2021-10-14 07:38:21 +02:00
Paweł Jastrzębski
2f8891a843 Add minimal argument to get_core_stats 2021-10-13 19:52:36 +02:00
Paweł Jastrzębski
a963694fd0 Add DashboardApiView 2021-10-13 19:21:52 +02:00
Paweł Jastrzębski
90b2896ded Add ApiTokenRequiredMixin 2021-10-13 16:01:31 +02:00
Paweł Jastrzębski
bec4b19366 Add ApiToken to admin 2021-10-11 12:37:01 +02:00
Paweł Jastrzębski
32adb64dc0 Add api app with ApiToken model 2021-10-11 11:33:18 +02:00
20 changed files with 219 additions and 10 deletions

View File

@@ -208,6 +208,22 @@ In a single-page application, the page never reloads. (That's the entire point o
Fortunately, Shynet offers a simple method you can call from anywhere within your JavaScript to indicate that a new page has been loaded: `Shynet.newPageLoad()`. Add this method call to the code that handles routing in your app, and you'll be ready to go.
### API
All the information displayed on the dashboard can be obtained via API on url ```//shynet.example.com/api/v1/dashboard/```. By default this endpoint will return the full data from all services over the last last 30 days. The `Authentication` header should be set to use user's parsonal API token (```'Authorization: Token <user API token>'```).
There are 3 optional query parameters:
* `uuid` - to get data only from one service
* `startDate` - to set start date in format YYYY-MM-DD
* `endDate` - to set end date in format YYYY-MM-DD
Example in HTTPie:
```http get '//shynet.example.com/api/v1/dashboard/?uuid={{service_uuid}}&startDate=2021-01-01&endDate=2050-01-01' 'Authorization:Token {{user_api_token}}'```
Example in cURL:
```curl -H 'Authorization:Token {{user_api_token}}' '//shynet.example.com/api/v1/dashboard/?uuid={{service_uuid}}&startDate=2021-01-01&endDate=2050-01-01'```
---
## Troubleshooting

17
poetry.lock generated
View File

@@ -300,6 +300,17 @@ python3-openid = ">=3.0.8"
requests = "*"
requests-oauthlib = ">=0.3.0"
[[package]]
name = "django-cors-headers"
version = "3.11.0"
description = "django-cors-headers is a Django application for handling the server headers required for Cross-Origin Resource Sharing (CORS)."
category = "main"
optional = false
python-versions = ">=3.7"
[package.dependencies]
Django = ">=2.2"
[[package]]
name = "django-coverage-plugin"
version = "2.0.2"
@@ -949,7 +960,7 @@ multidict = ">=4.0"
[metadata]
lock-version = "1.1"
python-versions = "^3.8"
content-hash = "009655b041e17f83ac3f1b423241ee299b0e706274b5b77519d3c087a4a032f8"
content-hash = "9fa33a531809239cfa88d8d896484af7d8be765b2a0f4878ab5434d5af6b30d2"
[metadata.files]
aiohttp = [
@@ -1220,6 +1231,10 @@ django = [
django-allauth = [
{file = "django-allauth-0.45.0.tar.gz", hash = "sha256:6d46be0e1480316ccd45476db3aefb39db70e038d2a543112d314b76bb999a4e"},
]
django-cors-headers = [
{file = "django-cors-headers-3.11.0.tar.gz", hash = "sha256:eb98389bf7a2afc5d374806af4a9149697e3a6955b5a2dc2bf049f7d33647456"},
{file = "django_cors_headers-3.11.0-py3-none-any.whl", hash = "sha256:a22be2befd4069c4fc174f11cf067351df5c061a3a5f94a01650b4e928b0372b"},
]
django-coverage-plugin = [
{file = "django_coverage_plugin-2.0.2-py3-none-any.whl", hash = "sha256:4206c85ffba0301f83aecc38e5b01b1b9a4b45a545d9456a827e3fabea18d952"},
{file = "django_coverage_plugin-2.0.2.tar.gz", hash = "sha256:e91e3a0c8de2b3766a144cdd30dbbf7a79e5c532a5dcc1373ce7eaad83b358b3"},

View File

@@ -26,6 +26,7 @@ django-health-check = "^3.16.4"
django-npm = "^1.0.0"
python-dotenv = "^0.18.0"
django-debug-toolbar = "^3.2.1"
django-cors-headers = "^3.11.0"
[tool.poetry.dev-dependencies]
pytest-sugar = "^0.9.4"

0
shynet/api/__init__.py Normal file
View File

1
shynet/api/admin.py Normal file
View File

@@ -0,0 +1 @@
# from django.contrib import admin

6
shynet/api/apps.py Normal file
View File

@@ -0,0 +1,6 @@
from django.apps import AppConfig
class ApiConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'api'

View File

23
shynet/api/mixins.py Normal file
View File

@@ -0,0 +1,23 @@
from django.http import JsonResponse
from django.contrib.auth.models import AnonymousUser
from core.models import User
class ApiTokenRequiredMixin:
def _get_user_by_token(self, request):
token = request.headers.get('Authorization')
if not token or not token.startswith('Token '):
return AnonymousUser()
token = token.split(' ')[1]
user = User.objects.filter(api_token=token).first()
return user if user else AnonymousUser()
def dispatch(self, request, *args, **kwargs):
request.user = self._get_user_by_token(request)
if not request.user.is_authenticated:
return JsonResponse(data={}, status=403)
return super().dispatch(request, *args, **kwargs)

1
shynet/api/models.py Normal file
View File

@@ -0,0 +1 @@
# from django.db import models

3
shynet/api/tests.py Normal file
View File

@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

7
shynet/api/urls.py Normal file
View File

@@ -0,0 +1,7 @@
from django.urls import path
from . import views
urlpatterns = [
path("dashboard/", views.DashboardApiView.as_view(), name="services"),
]

60
shynet/api/views.py Normal file
View File

@@ -0,0 +1,60 @@
import uuid
from django.http import JsonResponse
from django.db.models import Q
from django.db.models.query import QuerySet
from django.views.generic import View
from dashboard.mixins import DateRangeMixin
from core.models import Service
from .mixins import ApiTokenRequiredMixin
def is_valid_uuid(value):
try:
uuid.UUID(value)
return True
except ValueError:
return False
class DashboardApiView(ApiTokenRequiredMixin, DateRangeMixin, View):
def get(self, request, *args, **kwargs):
services = Service.objects.filter(
Q(owner=request.user) | Q(collaborators__in=[request.user])
).distinct()
uuid = request.GET.get('uuid')
if uuid and is_valid_uuid(uuid):
services = services.filter(uuid=uuid)
try:
start = self.get_start_date()
end = self.get_end_date()
except ValueError:
return JsonResponse(status=400, data={'error': 'Invalid date format'})
services_data = [
{
'name': s.name,
'uuid': s.uuid,
'link': s.link,
'stats': s.get_core_stats(start, end),
}
for s in services
]
services_data = self._convert_querysets_to_lists(services_data)
return JsonResponse(data={'services': services_data})
def _convert_querysets_to_lists(self, services_data):
for service_data in services_data:
for key, value in service_data['stats'].items():
if isinstance(value, QuerySet):
service_data['stats'][key] = list(value)
for key, value in service_data['stats']['compare'].items():
if isinstance(value, QuerySet):
service_data['stats']['compare'][key] = list(value)
return services_data

View File

@@ -0,0 +1,24 @@
# Generated by Django 3.2.5 on 2021-11-17 07:17
import core.models
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('core', '0008_auto_20200628_1403'),
]
operations = [
migrations.AddField(
model_name='user',
name='api_token',
field=models.TextField(default=core.models._default_api_token, unique=True),
),
migrations.AlterField(
model_name='user',
name='id',
field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
]

View File

@@ -1,8 +1,9 @@
import ipaddress
import json
import re
import uuid
from secrets import token_urlsafe
from django.apps import apps
from django.conf import settings
from django.contrib.auth.models import AbstractUser
@@ -43,9 +44,14 @@ def _parse_network_list(networks: str):
return [ipaddress.ip_network(network.strip()) for network in networks.split(",")]
def _default_api_token():
return token_urlsafe(32)
class User(AbstractUser):
username = models.TextField(default=_default_uuid, unique=True)
email = models.EmailField(unique=True)
api_token = models.TextField(default=_default_api_token, unique=True)
def __str__(self):
return self.email
@@ -203,6 +209,7 @@ class Service(models.Model):
chart_data, chart_tooltip_format, chart_granularity = self._get_chart_data(
sessions, hits, start_time, end_time, tz_now
)
return {
"currently_online": currently_online,
"session_count": session_count,

View File

@@ -2,8 +2,8 @@
{% load i18n a17t_tags %}
{% block head_title %}{% trans "Change Password" %}{% endblock %}
{% block page_title %}{% trans "Change Password" %}{% endblock %}
{% block head_title %}{% trans "Change authentication info" %}{% endblock %}
{% block page_title %}{% trans "Change authentication info" %}{% endblock %}
{% block card %}
<form method="POST" action="{% url 'account_change_password' %}" class="password_change max-w-lg">
@@ -11,4 +11,17 @@
{{ form|a17t }}
<button type="submit" name="action" class="button ~urge !high">{% trans "Change Password" %}</button>
</form>
<hr class="sep">
<div>
<p class="label mb-1">Personal API token</p>
<div class="flex justify-between">
<span class='chip ~info !normal'>{{request.user.api_token}}</span>
<form method="POST" action="{% url 'dashboard:api_token_refresh' %}">
{% csrf_token %}
<button type="submit" name="action" class="button ~neutral @high">{% trans "Refresh token" %}</button>
</form>
</div>
<p class="support mt-1">To learn more about the API, see our <a href="https://github.com/milesmcc/shynet/blob/master/GUIDE.md#api">API guide</a>.</p>
</div>
</div>
{% endblock %}

View File

@@ -30,5 +30,20 @@
</div>
</div>
</form>
<hr class="sep h-4">
<h5>API</h5>
<div class="card ~neutral !low content">
<p>Shynet provides a simple API that you can use to pull data programmatically. You can access this data via this URL:</p>
<code class="text-sm">{{script_protocol}}{{request.get_host}}{% url 'api:services' %}?uuid={{object.uuid}}</code>
<p>
There are 2 optional query parameters:
<ul>
<li><code class="text-sm">startDate</code> &mdash; to set the start date (in format YYYY-MM-DD)</li>
<li><code class="text-sm">endDate</code> &mdash; to set the end date (in format YYYY-MM-DD)</li>
</ul>
</p>
<p>Example using cURL:</p>
<code class="text-sm">curl -H 'Authorization: Token {{request.user.api_token}}' '{{script_protocol}}{{request.get_host}}{% url 'api:services' %}?uuid={{object.uuid}}&startDate=2021-01-01&endDate=2050-01-01'</code>
</div>
</div>
{% endblock %}

View File

@@ -1,6 +1,4 @@
from django.contrib import admin
from django.urls import include, path
from django.views.generic import RedirectView
from django.urls import path
from . import views
@@ -28,4 +26,9 @@ urlpatterns = [
views.ServiceSessionView.as_view(),
name="service_session",
),
path(
"api-token-refresh/",
views.RefreshApiTokenView.as_view(),
name="api_token_refresh",
),
]

View File

@@ -3,8 +3,7 @@ from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.messages.views import SuccessMessageMixin
from django.core.cache import cache
from django.db.models import Q
from django.shortcuts import get_object_or_404, reverse
from django.utils import timezone
from django.shortcuts import get_object_or_404, reverse, redirect
from django.views.generic import (
CreateView,
DeleteView,
@@ -12,11 +11,12 @@ from django.views.generic import (
ListView,
TemplateView,
UpdateView,
View,
)
from rules.contrib.views import PermissionRequiredMixin
from analytics.models import Session
from core.models import Service
from core.models import Service, _default_api_token
from .forms import ServiceForm
from .mixins import DateRangeMixin
@@ -155,3 +155,10 @@ class ServiceSessionView(LoginRequiredMixin, PermissionRequiredMixin, DetailView
data = super().get_context_data(**kwargs)
data["object"] = get_object_or_404(Service, pk=self.kwargs.get("pk"))
return data
class RefreshApiTokenView(LoginRequiredMixin, View):
def post(self, request):
request.user.api_token = _default_api_token()
request.user.save()
return redirect('account_change_password')

View File

@@ -59,16 +59,19 @@ INSTALLED_APPS = [
"core",
"dashboard.apps.DashboardConfig",
"analytics",
"api",
"allauth",
"allauth.account",
"allauth.socialaccount",
"debug_toolbar",
"corsheaders",
]
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"corsheaders.middleware.CorsMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
@@ -370,3 +373,6 @@ DASHBOARD_PAGE_SIZE = int(os.getenv("DASHBOARD_PAGE_SIZE", "5"))
USE_RELATIVE_MAX_IN_BAR_VISUALIZATION = (
os.getenv("USE_RELATIVE_MAX_IN_BAR_VISUALIZATION", "True") == "True"
)
CORS_ALLOW_ALL_ORIGINS = True
CORS_ALLOW_METHODS = ["GET", "OPTIONS"]

View File

@@ -25,4 +25,5 @@ urlpatterns = [
path("dashboard/", include(("dashboard.urls", "dashboard"), namespace="dashboard")),
path("healthz/", include("health_check.urls")),
path("", include(("core.urls", "core"), namespace="core")),
path("api/v1/", include(("api.urls", "api"), namespace="api")),
]