destiny_sdk 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- destiny_sdk/__init__.py +23 -0
- destiny_sdk/auth.py +204 -0
- destiny_sdk/client.py +113 -0
- destiny_sdk/core.py +35 -0
- destiny_sdk/enhancements.py +363 -0
- destiny_sdk/identifiers.py +107 -0
- destiny_sdk/imports.py +253 -0
- destiny_sdk/py.typed +0 -0
- destiny_sdk/references.py +45 -0
- destiny_sdk/robots.py +337 -0
- destiny_sdk/visibility.py +24 -0
- destiny_sdk-0.1.0.dist-info/LICENSE +51 -0
- destiny_sdk-0.1.0.dist-info/METADATA +92 -0
- destiny_sdk-0.1.0.dist-info/RECORD +15 -0
- destiny_sdk-0.1.0.dist-info/WHEEL +4 -0
destiny_sdk/__init__.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"""The DESTINY SDK provides files for interacting with DESTINY repository."""
|
|
2
|
+
|
|
3
|
+
from . import (
|
|
4
|
+
auth,
|
|
5
|
+
client,
|
|
6
|
+
enhancements,
|
|
7
|
+
identifiers,
|
|
8
|
+
imports,
|
|
9
|
+
references,
|
|
10
|
+
robots,
|
|
11
|
+
visibility,
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
__all__ = [
|
|
15
|
+
"auth",
|
|
16
|
+
"client",
|
|
17
|
+
"enhancements",
|
|
18
|
+
"identifiers",
|
|
19
|
+
"imports",
|
|
20
|
+
"references",
|
|
21
|
+
"robots",
|
|
22
|
+
"visibility",
|
|
23
|
+
]
|
destiny_sdk/auth.py
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
"""HMAC authentication assistance methods."""
|
|
2
|
+
|
|
3
|
+
import hashlib
|
|
4
|
+
import hmac
|
|
5
|
+
import time
|
|
6
|
+
from typing import Protocol, Self
|
|
7
|
+
from uuid import UUID
|
|
8
|
+
|
|
9
|
+
from fastapi import HTTPException, Request, status
|
|
10
|
+
from pydantic import UUID4, BaseModel
|
|
11
|
+
|
|
12
|
+
FIVE_MINUTES = 60 * 5
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class AuthException(HTTPException):
|
|
16
|
+
"""
|
|
17
|
+
An exception related to HTTP authentication.
|
|
18
|
+
|
|
19
|
+
Raised by implementations of the AuthMethod protocol.
|
|
20
|
+
|
|
21
|
+
.. code-block:: python
|
|
22
|
+
|
|
23
|
+
raise AuthException(
|
|
24
|
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
25
|
+
detail="Unable to parse authentication token.",
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def create_signature(
|
|
32
|
+
secret_key: str, request_body: bytes, client_id: UUID4, timestamp: float
|
|
33
|
+
) -> str:
|
|
34
|
+
"""
|
|
35
|
+
Create an HMAC signature using SHA256.
|
|
36
|
+
|
|
37
|
+
:param secret_key: secret key with which to encrypt message
|
|
38
|
+
:type secret_key: bytes
|
|
39
|
+
:param request_body: request body to be encrypted
|
|
40
|
+
:type request_body: bytes
|
|
41
|
+
:param client_id: client id to include in hmac
|
|
42
|
+
:type: UUID4
|
|
43
|
+
:param timestamp: timestamp for when the request is sent
|
|
44
|
+
:type: float
|
|
45
|
+
:return: encrypted hexdigest of the request body with the secret key
|
|
46
|
+
:rtype: str
|
|
47
|
+
"""
|
|
48
|
+
timestamp_bytes = f"{timestamp}".encode()
|
|
49
|
+
signature_components = b":".join([request_body, client_id.bytes, timestamp_bytes])
|
|
50
|
+
return hmac.new(
|
|
51
|
+
secret_key.encode(), signature_components, hashlib.sha256
|
|
52
|
+
).hexdigest()
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class HMACAuthorizationHeaders(BaseModel):
|
|
56
|
+
"""
|
|
57
|
+
The HTTP authorization headers required for HMAC authentication.
|
|
58
|
+
|
|
59
|
+
Expects the following headers to be present in the request
|
|
60
|
+
|
|
61
|
+
- Authorization: Signature [request signature]
|
|
62
|
+
- X-Client-Id: [UUID]
|
|
63
|
+
- X-Request-Timestamp: [float]
|
|
64
|
+
"""
|
|
65
|
+
|
|
66
|
+
signature: str
|
|
67
|
+
client_id: UUID4
|
|
68
|
+
timestamp: float
|
|
69
|
+
|
|
70
|
+
@classmethod
|
|
71
|
+
def from_request(cls, request: Request) -> Self:
|
|
72
|
+
"""
|
|
73
|
+
Get the required headers for HMAC authentication.
|
|
74
|
+
|
|
75
|
+
:param request: The incoming request
|
|
76
|
+
:type request: Request
|
|
77
|
+
:raises AuthException: Authorization header is missing
|
|
78
|
+
:raises AuthException: Authorization type not supported
|
|
79
|
+
:raises AuthException: X-Client-Id header is missing
|
|
80
|
+
:raises AuthException: Client id format is invalid
|
|
81
|
+
:raises AuthException: X-Request-Timestamp header is missing
|
|
82
|
+
:raises AuthException: Request timestamp has expired
|
|
83
|
+
:return: Header values necessary for authenticating the request
|
|
84
|
+
:rtype: HMACAuthorizationHeaders
|
|
85
|
+
"""
|
|
86
|
+
signature_header = request.headers.get("Authorization")
|
|
87
|
+
|
|
88
|
+
if not signature_header:
|
|
89
|
+
raise AuthException(
|
|
90
|
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
91
|
+
detail="Authorization header missing.",
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
scheme, _, signature = signature_header.partition(" ")
|
|
95
|
+
|
|
96
|
+
if scheme != "Signature":
|
|
97
|
+
raise AuthException(
|
|
98
|
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
99
|
+
detail="Authorization type not supported.",
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
client_id = request.headers.get("X-Client-Id")
|
|
103
|
+
|
|
104
|
+
if not client_id:
|
|
105
|
+
raise AuthException(
|
|
106
|
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
107
|
+
detail="X-Client-Id header missing",
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
try:
|
|
111
|
+
UUID(client_id)
|
|
112
|
+
except (ValueError, TypeError) as exc:
|
|
113
|
+
raise AuthException(
|
|
114
|
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
115
|
+
detail="Invalid format for client id, expected UUID4.",
|
|
116
|
+
) from exc
|
|
117
|
+
|
|
118
|
+
timestamp = request.headers.get("X-Request-Timestamp")
|
|
119
|
+
|
|
120
|
+
if not timestamp:
|
|
121
|
+
raise AuthException(
|
|
122
|
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
123
|
+
detail="X-Request-Timestamp header missing",
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
if (time.time() - float(timestamp)) > FIVE_MINUTES:
|
|
127
|
+
raise AuthException(
|
|
128
|
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
129
|
+
detail="Request timestamp has expired.",
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
return cls(signature=signature, client_id=client_id, timestamp=timestamp)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
class HMACAuthMethod(Protocol):
|
|
136
|
+
"""
|
|
137
|
+
Protocol for HMAC auth methods, enforcing the implmentation of __call__().
|
|
138
|
+
|
|
139
|
+
This allows FastAPI to call class instances as depenedencies in FastAPI routes,
|
|
140
|
+
see https://fastapi.tiangolo.com/advanced/advanced-dependencies
|
|
141
|
+
|
|
142
|
+
.. code-block:: python
|
|
143
|
+
|
|
144
|
+
auth = HMACAuthMethod()
|
|
145
|
+
|
|
146
|
+
router = APIRouter(
|
|
147
|
+
prefix="/robots", tags=["robot"], dependencies=[Depends(auth)]
|
|
148
|
+
)
|
|
149
|
+
"""
|
|
150
|
+
|
|
151
|
+
async def __call__(self, request: Request) -> bool:
|
|
152
|
+
"""
|
|
153
|
+
Callable interface to allow use as a dependency.
|
|
154
|
+
|
|
155
|
+
:param request: The request to verify
|
|
156
|
+
:type request: Request
|
|
157
|
+
:raises NotImplementedError: __call__() method has not been implemented.
|
|
158
|
+
:return: True if authorization is successful.
|
|
159
|
+
:rtype: bool
|
|
160
|
+
"""
|
|
161
|
+
raise NotImplementedError
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
class HMACAuth(HMACAuthMethod):
|
|
165
|
+
"""Adds HMAC auth when used as a router or endpoint dependency."""
|
|
166
|
+
|
|
167
|
+
def __init__(self, secret_key: str) -> None:
|
|
168
|
+
"""Initialise HMAC auth with a given secret key."""
|
|
169
|
+
self.secret_key = secret_key
|
|
170
|
+
|
|
171
|
+
async def __call__(self, request: Request) -> bool:
|
|
172
|
+
"""Perform Authorization check."""
|
|
173
|
+
auth_headers = HMACAuthorizationHeaders.from_request(request)
|
|
174
|
+
request_body = await request.body()
|
|
175
|
+
expected_signature = create_signature(
|
|
176
|
+
self.secret_key,
|
|
177
|
+
request_body,
|
|
178
|
+
auth_headers.client_id,
|
|
179
|
+
auth_headers.timestamp,
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
if not hmac.compare_digest(auth_headers.signature, expected_signature):
|
|
183
|
+
raise AuthException(
|
|
184
|
+
status_code=status.HTTP_401_UNAUTHORIZED, detail="Signature is invalid."
|
|
185
|
+
)
|
|
186
|
+
|
|
187
|
+
return True
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
class BypassHMACAuth(HMACAuthMethod):
|
|
191
|
+
"""
|
|
192
|
+
A fake auth class that will always respond successfully.
|
|
193
|
+
|
|
194
|
+
Intended for use in local environments and for testing.
|
|
195
|
+
|
|
196
|
+
Not for production use!
|
|
197
|
+
"""
|
|
198
|
+
|
|
199
|
+
async def __call__(
|
|
200
|
+
self,
|
|
201
|
+
request: Request, # noqa: ARG002
|
|
202
|
+
) -> bool:
|
|
203
|
+
"""Bypass Authorization check."""
|
|
204
|
+
return True
|
destiny_sdk/client.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"""Send authenticated requests to Destiny Repository."""
|
|
2
|
+
|
|
3
|
+
import time
|
|
4
|
+
from collections.abc import Generator
|
|
5
|
+
|
|
6
|
+
import httpx
|
|
7
|
+
from pydantic import UUID4, HttpUrl
|
|
8
|
+
|
|
9
|
+
from destiny_sdk.robots import (
|
|
10
|
+
BatchEnhancementRequestRead,
|
|
11
|
+
BatchRobotResult,
|
|
12
|
+
EnhancementRequestRead,
|
|
13
|
+
RobotResult,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
from .auth import create_signature
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class HMACSigningAuth(httpx.Auth):
|
|
20
|
+
"""Client that adds an HMAC signature to a request."""
|
|
21
|
+
|
|
22
|
+
requires_request_body = True
|
|
23
|
+
|
|
24
|
+
def __init__(self, secret_key: str, client_id: UUID4) -> None:
|
|
25
|
+
"""
|
|
26
|
+
Initialize the client.
|
|
27
|
+
|
|
28
|
+
:param secret_key: the key to use when signing the request
|
|
29
|
+
:type secret_key: str
|
|
30
|
+
"""
|
|
31
|
+
self.secret_key = secret_key
|
|
32
|
+
self.client_id = client_id
|
|
33
|
+
|
|
34
|
+
def auth_flow(
|
|
35
|
+
self, request: httpx.Request
|
|
36
|
+
) -> Generator[httpx.Request, httpx.Response]:
|
|
37
|
+
"""
|
|
38
|
+
Add a signature to the given request.
|
|
39
|
+
|
|
40
|
+
:param request: request to be sent with signature
|
|
41
|
+
:type request: httpx.Request
|
|
42
|
+
:yield: Generator for Request with signature headers set
|
|
43
|
+
:rtype: Generator[httpx.Request, httpx.Response]
|
|
44
|
+
"""
|
|
45
|
+
timestamp = time.time()
|
|
46
|
+
signature = create_signature(
|
|
47
|
+
self.secret_key, request.content, self.client_id, timestamp
|
|
48
|
+
)
|
|
49
|
+
request.headers["Authorization"] = f"Signature {signature}"
|
|
50
|
+
request.headers["X-Client-Id"] = f"{self.client_id}"
|
|
51
|
+
request.headers["X-Request-Timestamp"] = f"{timestamp}"
|
|
52
|
+
yield request
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class Client:
|
|
56
|
+
"""
|
|
57
|
+
Client for interaction with the Destiny API.
|
|
58
|
+
|
|
59
|
+
Current implementation only supports robot results.
|
|
60
|
+
"""
|
|
61
|
+
|
|
62
|
+
def __init__(self, base_url: HttpUrl, secret_key: str, client_id: UUID4) -> None:
|
|
63
|
+
"""
|
|
64
|
+
Initialize the client.
|
|
65
|
+
|
|
66
|
+
:param base_url: The base URL for the Destiny Repository API.
|
|
67
|
+
:type base_url: HttpUrl
|
|
68
|
+
:param secret_key: The secret key for signing requests
|
|
69
|
+
:type auth_method: str
|
|
70
|
+
"""
|
|
71
|
+
self.session = httpx.Client(
|
|
72
|
+
base_url=str(base_url),
|
|
73
|
+
headers={"Content-Type": "application/json"},
|
|
74
|
+
auth=HMACSigningAuth(secret_key=secret_key, client_id=client_id),
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
def send_robot_result(self, robot_result: RobotResult) -> EnhancementRequestRead:
|
|
78
|
+
"""
|
|
79
|
+
Send a RobotResult to destiny repository.
|
|
80
|
+
|
|
81
|
+
Signs the request with the client's secret key.
|
|
82
|
+
|
|
83
|
+
:param robot_result: The Robot Result to send
|
|
84
|
+
:type robot_result: RobotResult
|
|
85
|
+
:return: The EnhancementRequestRead object from the response.
|
|
86
|
+
:rtype: EnhancementRequestRead
|
|
87
|
+
"""
|
|
88
|
+
response = self.session.post(
|
|
89
|
+
"/robot/enhancement/single/",
|
|
90
|
+
json=robot_result.model_dump(mode="json"),
|
|
91
|
+
)
|
|
92
|
+
response.raise_for_status()
|
|
93
|
+
return EnhancementRequestRead.model_validate(response.json())
|
|
94
|
+
|
|
95
|
+
def send_batch_robot_result(
|
|
96
|
+
self, batch_robot_result: BatchRobotResult
|
|
97
|
+
) -> BatchEnhancementRequestRead:
|
|
98
|
+
"""
|
|
99
|
+
Send a BatchRobotResult to destiny repository.
|
|
100
|
+
|
|
101
|
+
Signs the request with the client's secret key.
|
|
102
|
+
|
|
103
|
+
:param batch_robot_result: The Batch Robot Result to send
|
|
104
|
+
:type batch_robot_result: BatchRobotResult
|
|
105
|
+
:return: The BatchEnhancementRequestRead object from the response.
|
|
106
|
+
:rtype: BatchEnhancementRequestRead
|
|
107
|
+
"""
|
|
108
|
+
response = self.session.post(
|
|
109
|
+
"/robot/enhancement/batch/",
|
|
110
|
+
json=batch_robot_result.model_dump(mode="json"),
|
|
111
|
+
)
|
|
112
|
+
response.raise_for_status()
|
|
113
|
+
return BatchEnhancementRequestRead.model_validate(response.json())
|
destiny_sdk/core.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"""Core classes for the Destiny SDK, not exposed to package users."""
|
|
2
|
+
|
|
3
|
+
from typing import Self
|
|
4
|
+
|
|
5
|
+
from pydantic import BaseModel
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class _JsonlFileInputMixIn(BaseModel):
|
|
9
|
+
"""
|
|
10
|
+
A mixin class for models that are used at the top-level for entries in .jsonl files.
|
|
11
|
+
|
|
12
|
+
This class is used to define a common interface for file input models.
|
|
13
|
+
It is not intended to be used directly.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
def to_jsonl(self) -> str:
|
|
17
|
+
"""
|
|
18
|
+
Convert the model to a JSONL string.
|
|
19
|
+
|
|
20
|
+
:return: The JSONL string representation of the model.
|
|
21
|
+
:rtype: str
|
|
22
|
+
"""
|
|
23
|
+
return self.model_dump_json(exclude_none=True)
|
|
24
|
+
|
|
25
|
+
@classmethod
|
|
26
|
+
def from_jsonl(cls, jsonl: str) -> Self:
|
|
27
|
+
"""
|
|
28
|
+
Create an object from a JSONL string.
|
|
29
|
+
|
|
30
|
+
:param jsonl: The JSONL string to parse.
|
|
31
|
+
:type jsonl: str
|
|
32
|
+
:return: The created object.
|
|
33
|
+
:rtype: Self
|
|
34
|
+
"""
|
|
35
|
+
return cls.model_validate_json(jsonl)
|