-
Notifications
You must be signed in to change notification settings - Fork 412
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(master-api-key/roles): Add roles to master api key (#2436)
- Loading branch information
1 parent
54f3e48
commit a46295b
Showing
51 changed files
with
1,344 additions
and
566 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from contextlib import suppress | ||
|
||
from rest_framework import authentication, exceptions | ||
from rest_framework_api_key.permissions import KeyParser | ||
|
||
from api_keys.models import MasterAPIKey | ||
from api_keys.user import APIKeyUser | ||
|
||
key_parser = KeyParser() | ||
|
||
|
||
class MasterAPIKeyAuthentication(authentication.BaseAuthentication): | ||
def authenticate(self, request): | ||
key = key_parser.get(request) | ||
if not key: | ||
return None | ||
|
||
with suppress(MasterAPIKey.DoesNotExist): | ||
key = MasterAPIKey.objects.get_from_key(key) | ||
if not key.has_expired: | ||
return APIKeyUser(key), None | ||
|
||
raise exceptions.AuthenticationFailed("Valid Master API Key not found.") |
This file was deleted.
Oops, something went wrong.
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,18 @@ | ||
# Generated by Django 3.2.20 on 2023-07-14 03:38 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('api_keys', '0002_soft_delete_api_keys'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='masterapikey', | ||
name='is_admin', | ||
field=models.BooleanField(default=True), | ||
), | ||
] |
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,68 @@ | ||
import typing | ||
|
||
from django.db.models import QuerySet | ||
|
||
from organisations.models import Organisation | ||
from permissions.permission_service import ( | ||
get_permitted_environments_for_master_api_key, | ||
get_permitted_projects_for_master_api_key, | ||
is_master_api_key_environment_admin, | ||
is_master_api_key_project_admin, | ||
master_api_key_has_organisation_permission, | ||
) | ||
from users.abc import UserABC | ||
|
||
from .models import MasterAPIKey | ||
|
||
if typing.TYPE_CHECKING: | ||
from environments.models import Environment | ||
from projects.models import Project | ||
|
||
|
||
class APIKeyUser(UserABC): | ||
def __init__(self, key: MasterAPIKey): | ||
self.key = key | ||
|
||
@property | ||
def is_authenticated(self) -> bool: | ||
return True | ||
|
||
@property | ||
def is_master_api_key_user(self) -> bool: | ||
return True | ||
|
||
def belongs_to(self, organisation_id: int) -> bool: | ||
return self.key.organisation_id == organisation_id | ||
|
||
def is_project_admin(self, project: "Project") -> bool: | ||
return is_master_api_key_project_admin(self.key, project) | ||
|
||
def is_environment_admin(self, environment: "Environment") -> bool: | ||
return is_master_api_key_environment_admin(self.key, environment) | ||
|
||
def has_project_permission(self, permission: str, project: "Project") -> bool: | ||
return project in self.get_permitted_projects(permission) | ||
|
||
def has_environment_permission( | ||
self, permission: str, environment: "Environment" | ||
) -> bool: | ||
return environment in self.get_permitted_environments( | ||
permission, environment.project | ||
) | ||
|
||
def has_organisation_permission( | ||
self, organisation: Organisation, permission_key: str | ||
) -> bool: | ||
return master_api_key_has_organisation_permission( | ||
self.key, organisation, permission_key | ||
) | ||
|
||
def get_permitted_projects(self, permission_key: str) -> QuerySet["Project"]: | ||
return get_permitted_projects_for_master_api_key(self.key, permission_key) | ||
|
||
def get_permitted_environments( | ||
self, permission_key: str, project: "Project" | ||
) -> QuerySet["Environment"]: | ||
return get_permitted_environments_for_master_api_key( | ||
self.key, project, permission_key | ||
) |
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 |
---|---|---|
@@ -1,21 +1,12 @@ | ||
from app.settings.common import * # noqa | ||
from app.settings.common import REST_FRAMEWORK | ||
|
||
# We dont want to track tests | ||
ENABLE_TELEMETRY = False | ||
|
||
REST_FRAMEWORK = { | ||
"DEFAULT_PERMISSION_CLASSES": ["rest_framework.permissions.IsAuthenticated"], | ||
"DEFAULT_AUTHENTICATION_CLASSES": ( | ||
"rest_framework.authentication.TokenAuthentication", | ||
), | ||
"PAGE_SIZE": 10, | ||
"UNICODE_JSON": False, | ||
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.PageNumberPagination", | ||
"DEFAULT_THROTTLE_RATES": { | ||
"login": "100/min", | ||
"mfa_code": "5/min", | ||
"invite": "10/min", | ||
"signup": "100/min", | ||
}, | ||
"DEFAULT_FILTER_BACKENDS": ["django_filters.rest_framework.DjangoFilterBackend"], | ||
REST_FRAMEWORK["DEFAULT_THROTTLE_RATES"] = { | ||
"login": "100/min", | ||
"mfa_code": "5/min", | ||
"invite": "10/min", | ||
"signup": "100/min", | ||
} |
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 was deleted.
Oops, something went wrong.
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
Oops, something went wrong.
a46295b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
docs – ./docs
docs-flagsmith.vercel.app
docs-git-main-flagsmith.vercel.app
docs.bullet-train.io
docs.flagsmith.com
a46295b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
flagsmith-frontend-staging – ./frontend
flagsmith-frontend-staging-git-main-flagsmith.vercel.app
flagsmith-frontend-staging-flagsmith.vercel.app
flagsmith-staging-frontend.vercel.app
staging.flagsmith.com
a46295b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
flagsmith-frontend-preview – ./frontend
flagsmith-frontend-preview-git-main-flagsmith.vercel.app
flagsmith-frontend-production-native.vercel.app
flagsmith-frontend-preview-flagsmith.vercel.app