feat(auth): Add OAuth2 authentication

This commit is contained in:
Björn Benouarets
2026-01-27 16:35:46 +01:00
commit 50c85e9b7f
36 changed files with 1599 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
import requests
from typing import Optional
from datetime import datetime#
import secnex.utils as utils
from secnex.kit.config.config import Config
from secnex.kit.models.application import Application
class ApplicationClient:
def __init__(self, config: Config) -> None:
self.config = config
def create(self, name: str, tenant_id: str, secret: str) -> tuple[Optional[str], int, bool]:
"""Create a new application"""
url = f"{self.config.host}/apps"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer 1234567890",
}
response = requests.post(url, headers=headers, json={"name": name, "tenant": tenant_id, "secret": secret})
result = response.json()
success = False
app_id = None
if "id" in result["body"]:
success = True
app_id = result["body"]["id"]
return app_id, response.status_code, success
def get_all(self) -> tuple[Optional[list[Application]], int, bool]:
"""Get all applications"""
url = f"{self.config.host}/apps"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer 1234567890",
}
response = requests.get(url, headers=headers)
result = response.json()
success = False
applications = []
if "applications" in result["body"]:
success = True
applications = [Application(**application) for application in result["body"]["applications"]]
return applications, response.status_code, success
def remove(self, id: str) -> tuple[bool, int]:
"""Remove an application"""
url = f"{self.config.host}/apps/{id}"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer 1234567890",
}
response = requests.delete(url, headers=headers)
success = response.status_code == 200
return success, response.status_code

View File

View File

@@ -0,0 +1,40 @@
import requests
from typing import Optional
from datetime import datetime#
from secnex.kit.config.config import Config
from secnex.kit.models.tenant import Tenant
class TenantClient:
def __init__(self, config: Config) -> None:
self.config = config
def create(self, name: str) -> tuple[Optional[str], int, bool]:
"""Create a new tenant"""
url = f"{self.config.host}/tenants"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer 1234567890",
}
response = requests.post(url, headers=headers, json={"name": name})
result = response.json()
success = False
tenant_id = None
if "id" in result["body"]:
success = True
tenant_id = result["body"]["id"]
return tenant_id, response.status_code, success
def get_all(self) -> list[Tenant]:
"""Get all tenants"""
url = f"{self.config.host}/tenants"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer 1234567890",
}
response = requests.get(url, headers=headers)
result = response.json()
tenants = result["body"]["tenants"]
return [Tenant(**tenant) for tenant in tenants]

View File

@@ -0,0 +1,38 @@
import requests
from typing import Optional
from datetime import datetime#
from secnex.kit.config.config import Config
from secnex.kit.models.user import User
class UserClient:
def __init__(self, config: Config) -> None:
self.config = config
def get_all(self) -> list[User]:
"""Get all users"""
url = f"{self.config.host}/users"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer 1234567890",
}
response = requests.get(url, headers=headers)
result = response.json()
users = result["body"]["users"]
return [User(**u) for u in users]
def add_user_to_tenant(self, tenant_id: str, user_id: str) -> tuple[bool, int]:
"""Add a user to a tenant"""
url = f"{self.config.host}/users/{user_id}/tenant"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer 1234567890",
}
data = {
"tenant": tenant_id,
}
response = requests.put(url, headers=headers, json=data)
success = response.status_code == 200
return success, response.status_code