2
2
import datetime
3
3
from code_graph import *
4
4
from typing import Optional
5
+ from functools import wraps
5
6
from falkordb import FalkorDB
6
7
from dotenv import load_dotenv
7
8
from urllib .parse import urlparse
17
18
format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' )
18
19
logger = logging .getLogger (__name__ )
19
20
21
+ # Function to verify the token
22
+ SECRET_TOKEN = os .getenv ('SECRET_TOKEN' )
23
+ def verify_token (token ):
24
+ return token == SECRET_TOKEN
25
+
26
+ # Decorator to protect routes with token authentication
27
+ def token_required (f ):
28
+ @wraps (f )
29
+ def decorated_function (* args , ** kwargs ):
30
+ token = request .headers .get ('Authorization' ) # Get token from header
31
+ if not token or not verify_token (token ):
32
+ return jsonify (message = "Unauthorized" ), 401
33
+ return f (* args , ** kwargs )
34
+ return decorated_function
35
+
20
36
def create_app ():
21
37
app = Flask (__name__ )
22
38
23
39
@app .route ('/graph_entities' , methods = ['GET' ])
40
+ @token_required # Apply token authentication decorator
24
41
def graph_entities ():
25
42
"""
26
43
Endpoint to fetch sub-graph entities from a given repository.
@@ -64,6 +81,7 @@ def graph_entities():
64
81
65
82
66
83
@app .route ('/get_neighbors' , methods = ['GET' ])
84
+ @token_required # Apply token authentication decorator
67
85
def get_neighbors ():
68
86
"""
69
87
Endpoint to get neighbors of a specific node in the graph.
@@ -117,6 +135,7 @@ def get_neighbors():
117
135
118
136
119
137
@app .route ('/process_repo' , methods = ['POST' ])
138
+ @token_required # Apply token authentication decorator
120
139
def process_repo ():
121
140
"""
122
141
Process a GitHub repository.
@@ -151,6 +170,7 @@ def process_repo():
151
170
return jsonify (response ), 200
152
171
153
172
@app .route ('/process_local_repo' , methods = ['POST' ])
173
+ @token_required # Apply token authentication decorator
154
174
def process_local_repo ():
155
175
# Get JSON data from the request
156
176
data = request .get_json ()
@@ -182,6 +202,7 @@ def process_local_repo():
182
202
return jsonify (response ), 200
183
203
184
204
@app .route ('/process_code_coverage' , methods = ['POST' ])
205
+ @token_required # Apply token authentication decorator
185
206
def process_code_coverage ():
186
207
"""
187
208
Endpoint to process code coverage data for a given repository.
@@ -217,6 +238,7 @@ def process_code_coverage():
217
238
218
239
219
240
@app .route ('/switch_commit' , methods = ['POST' ])
241
+ @token_required # Apply token authentication decorator
220
242
def switch_commit ():
221
243
"""
222
244
Endpoint to switch a repository to a specific commit.
@@ -250,6 +272,7 @@ def switch_commit():
250
272
return jsonify (response ), 200
251
273
252
274
@app .route ('/auto_complete' , methods = ['POST' ])
275
+ @token_required # Apply token authentication decorator
253
276
def auto_complete ():
254
277
"""
255
278
Endpoint to process auto-completion requests for a repository based on a prefix.
@@ -288,6 +311,7 @@ def auto_complete():
288
311
289
312
290
313
@app .route ('/list_repos' , methods = ['GET' ])
314
+ @token_required # Apply token authentication decorator
291
315
def list_repos ():
292
316
"""
293
317
Endpoint to list all available repositories.
@@ -309,6 +333,7 @@ def list_repos():
309
333
310
334
311
335
@app .route ('/list_commits' , methods = ['POST' ])
336
+ @token_required # Apply token authentication decorator
312
337
def list_commits ():
313
338
"""
314
339
Endpoint to list all commits of a specified repository.
@@ -346,6 +371,7 @@ def list_commits():
346
371
347
372
348
373
@app .route ('/repo_info' , methods = ['POST' ])
374
+ @token_required # Apply token authentication decorator
349
375
def repo_info ():
350
376
"""
351
377
Endpoint to retrieve information about a specific repository.
@@ -390,6 +416,7 @@ def repo_info():
390
416
return jsonify (response ), 200
391
417
392
418
@app .route ('/find_paths' , methods = ['POST' ])
419
+ @token_required # Apply token authentication decorator
393
420
def find_paths ():
394
421
"""
395
422
Finds all paths between a source node (src) and a destination node (dest) in the graph.
@@ -445,6 +472,7 @@ def find_paths():
445
472
446
473
447
474
@app .route ('/unreachable' , methods = ['POST' ])
475
+ @token_required # Apply token authentication decorator
448
476
def unreachable_entities ():
449
477
"""
450
478
Endpoint to retrieve unreachable entities in the graph.
@@ -478,6 +506,7 @@ def unreachable_entities():
478
506
return jsonify (response ), 200
479
507
480
508
@app .route ('/chat' , methods = ['POST' ])
509
+ @token_required # Apply token authentication decorator
481
510
def chat ():
482
511
# Get JSON data from the request
483
512
data = request .get_json ()
0 commit comments