# What this PR does - Adds [`mypy` static type checking](https://mypy-lang.org/) to our CI pipeline. Currently there is still a **ton** of errors being returned by the tool, as we'll need to fix pre-existing errors. I think we can slowly chip away at these errors in small PRs, doing them all in one large PR is likely very risky. - Also, this PR starts chipping away at one of the main type errors that we have which is accessing the `datetime` class (from the `datetime` library) or `timedelta` function on the `django.utils.timezone` module. Basically we should be instead accessing these two objects from the native `datetime` module. This makes sense because the [`__all__` attribute](https://github.com/django/django/blob/main/django/utils/timezone.py#L14-L30) in `django.utils.timezone` does not re-export `datetime` or `timedelta`. - splits `engine` dependencies out into `requirements.txt` and `requirements-dev.txt` ## Checklist - [ ] Unit, integration, and e2e (if applicable) tests updated (N/A) - [ ] Documentation added (or `pr:no public docs` PR label added if not required) (N/A) - [ ] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not required) (N/A)
28 lines
1 KiB
Python
28 lines
1 KiB
Python
import json
|
|
import logging
|
|
|
|
from django.views import View
|
|
from rest_framework import permissions
|
|
from rest_framework.authentication import get_authorization_header
|
|
from rest_framework.request import Request
|
|
|
|
from apps.auth_token.exceptions import InvalidToken
|
|
from apps.grafana_plugin.helpers.gcom import check_token
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class PluginTokenVerified(permissions.BasePermission):
|
|
# The grafana plugin can either use a token from gcom or one generated internally by oncall
|
|
# Tokens from gcom will be prefixed with gcom: otherwise they will be treated as local
|
|
def has_permission(self, request: Request, view: View) -> bool:
|
|
token_string = get_authorization_header(request).decode()
|
|
context = json.loads(request.headers.get("X-Instance-Context"))
|
|
try:
|
|
auth_token = check_token(token_string, context)
|
|
if auth_token:
|
|
return True
|
|
except InvalidToken:
|
|
logger.warning(f"Invalid token used: {context}")
|
|
|
|
return False
|