File tree Expand file tree Collapse file tree 2 files changed +23
-1
lines changed Expand file tree Collapse file tree 2 files changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -105,6 +105,9 @@ class Settings(BaseSettings):
105
105
MAX_PROGRAM_ARCHIVE_SIZE = 10_000_000 # 10 MB
106
106
MAX_DATA_ARCHIVE_SIZE = 10_000_000 # 10 MB
107
107
108
+ # hashlib.sha256(b"secret-token").hexdigest()
109
+ ALLOCATION_TOKEN_HASH = "151ba92f2eb90bce67e912af2f7a5c17d8654b3d29895b042107ea312a7eebda"
110
+
108
111
FAKE_DATA_PROGRAM : Optional [Path ] = None
109
112
BENCHMARK_FAKE_DATA_PROGRAM = Path (
110
113
abspath (join (__file__ , "../../examples/example_fastapi" ))
Original file line number Diff line number Diff line change 1
1
import binascii
2
2
import logging
3
3
import os .path
4
+ from hashlib import sha256
4
5
from string import Template
5
6
from typing import Awaitable , Optional
6
7
@@ -170,8 +171,26 @@ async def status_check_version(request: web.Request):
170
171
return web .HTTPForbidden (text = f"Outdated: version { current } < { reference } " )
171
172
172
173
174
+ def authenticate_api_request (request : web .Request ) -> bool :
175
+ """Authenticate an API request to update the VM allocations.
176
+ """
177
+ signature : str = request .headers .get ('X-Auth-Signature' )
178
+ # body: bytes = await request.read()
179
+ if not signature :
180
+ raise web .HTTPUnauthorized (text = "Authentication token is missing" )
181
+
182
+ # Use a simple authentication method: the hash of the signature should match the value in the settings
183
+ if sha256 (signature ).hexdigest () != settings .ALLOCATION_TOKEN_HASH :
184
+ raise web .HTTPUnauthorized (text = "Authentication token received is invalid" )
185
+
186
+ return True
187
+
188
+
189
+
173
190
async def update_allocations (request : web .Request ):
174
- # TODO: Add some form of authentication
191
+ if not authenticate_api_request (request ):
192
+ return web .HTTPUnauthorized (text = "Invalid authentication" )
193
+
175
194
try :
176
195
data = await request .json ()
177
196
allocation = Allocation (** data )
You can’t perform that action at this time.
0 commit comments