PyKubeGrader 0.2.18__tar.gz → 0.2.20__tar.gz
Sign up to get free protection for your applications and to get access to all the features.
- {pykubegrader-0.2.18/src/PyKubeGrader.egg-info → pykubegrader-0.2.20}/PKG-INFO +1 -1
- {pykubegrader-0.2.18 → pykubegrader-0.2.20/src/PyKubeGrader.egg-info}/PKG-INFO +1 -1
- pykubegrader-0.2.20/src/pykubegrader/tokens/validate_token.py +127 -0
- pykubegrader-0.2.18/src/pykubegrader/tokens/validate_token.py +0 -69
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/.coveragerc +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/.github/workflows/main.yml +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/.gitignore +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/.readthedocs.yml +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/AUTHORS.rst +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/CHANGELOG.rst +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/CONTRIBUTING.rst +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/LICENSE.txt +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/README.rst +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/docs/Makefile +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/docs/_static/Drexel_blue_Logo_square_Dark.png +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/docs/_static/Drexel_blue_Logo_square_Light.png +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/docs/_static/custom.css +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/docs/authors.rst +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/docs/changelog.rst +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/docs/conf.py +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/docs/contributing.rst +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/docs/index.rst +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/docs/license.rst +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/docs/readme.rst +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/docs/requirements.txt +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/examples/.responses.json +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/examples/true_false.ipynb +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/pyproject.toml +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/setup.cfg +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/setup.py +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/src/PyKubeGrader.egg-info/SOURCES.txt +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/src/PyKubeGrader.egg-info/dependency_links.txt +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/src/PyKubeGrader.egg-info/entry_points.txt +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/src/PyKubeGrader.egg-info/not-zip-safe +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/src/PyKubeGrader.egg-info/requires.txt +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/src/PyKubeGrader.egg-info/top_level.txt +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/src/pykubegrader/__init__.py +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/src/pykubegrader/build/__init__.py +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/src/pykubegrader/build/api_notebook_builder.py +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/src/pykubegrader/build/build_folder.py +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/src/pykubegrader/build/clean_folder.py +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/src/pykubegrader/graders/__init__.py +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/src/pykubegrader/graders/late_assignments.py +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/src/pykubegrader/initialize.py +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/src/pykubegrader/log_parser/__init__.py +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/src/pykubegrader/log_parser/parse.ipynb +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/src/pykubegrader/log_parser/parse.py +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/src/pykubegrader/submit/submit_assignment.py +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/src/pykubegrader/telemetry.py +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/src/pykubegrader/tokens/tokens.py +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/src/pykubegrader/utils.py +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/src/pykubegrader/validate.py +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/src/pykubegrader/widgets/__init__.py +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/src/pykubegrader/widgets/multiple_choice.py +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/src/pykubegrader/widgets/reading_question.py +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/src/pykubegrader/widgets/select_many.py +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/src/pykubegrader/widgets/student_info.py +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/src/pykubegrader/widgets/style.py +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/src/pykubegrader/widgets/true_false.py +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/src/pykubegrader/widgets/types_question.py +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/src/pykubegrader/widgets_base/__init__.py +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/src/pykubegrader/widgets_base/multi_select.py +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/src/pykubegrader/widgets_base/reading.py +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/src/pykubegrader/widgets_base/select.py +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/tests/conftest.py +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/tests/import_test.py +0 -0
- {pykubegrader-0.2.18 → pykubegrader-0.2.20}/tox.ini +0 -0
@@ -0,0 +1,127 @@
|
|
1
|
+
import os
|
2
|
+
import base64
|
3
|
+
import httpx
|
4
|
+
import asyncio
|
5
|
+
import nest_asyncio
|
6
|
+
|
7
|
+
# Apply nest_asyncio for environments like Jupyter
|
8
|
+
nest_asyncio.apply()
|
9
|
+
|
10
|
+
|
11
|
+
class TokenValidationError(Exception):
|
12
|
+
"""
|
13
|
+
Custom exception raised when the token validation fails.
|
14
|
+
"""
|
15
|
+
|
16
|
+
def __init__(self, message=None):
|
17
|
+
"""
|
18
|
+
Initialize the exception with an optional message.
|
19
|
+
|
20
|
+
Args:
|
21
|
+
message (str, optional): The error message to display. Defaults to None.
|
22
|
+
"""
|
23
|
+
super().__init__(message)
|
24
|
+
|
25
|
+
|
26
|
+
def get_credentials():
|
27
|
+
"""
|
28
|
+
Fetch the username and password from environment variables.
|
29
|
+
|
30
|
+
Returns:
|
31
|
+
dict: A dictionary containing 'username' and 'password'.
|
32
|
+
"""
|
33
|
+
username = os.getenv("user_name_student")
|
34
|
+
password = os.getenv("keys_student")
|
35
|
+
if not username or not password:
|
36
|
+
raise ValueError(
|
37
|
+
"Environment variables 'user_name_student' or 'keys_student' are not set."
|
38
|
+
)
|
39
|
+
return {"username": username, "password": password}
|
40
|
+
|
41
|
+
|
42
|
+
async def async_validate_token(token: str) -> None:
|
43
|
+
"""
|
44
|
+
Asynchronously validate a token by making a GET request to the validation endpoint.
|
45
|
+
|
46
|
+
Args:
|
47
|
+
token (str): The token to validate.
|
48
|
+
|
49
|
+
Raises:
|
50
|
+
TokenValidationError: If the token is invalid or if there is an error in the validation process.
|
51
|
+
|
52
|
+
Returns:
|
53
|
+
None: If the token is valid, the function will pass silently.
|
54
|
+
"""
|
55
|
+
# Fetch the endpoint URL
|
56
|
+
base_url = os.getenv("DB_URL")
|
57
|
+
if not base_url:
|
58
|
+
raise ValueError("Environment variable 'DB_URL' is not set.")
|
59
|
+
endpoint = f"{base_url}validate-token/{token}"
|
60
|
+
|
61
|
+
# Get credentials
|
62
|
+
credentials = get_credentials()
|
63
|
+
username = credentials["username"]
|
64
|
+
password = credentials["password"]
|
65
|
+
|
66
|
+
# Encode credentials for Basic Authentication
|
67
|
+
auth_header = (
|
68
|
+
f"Basic {base64.b64encode(f'{username}:{password}'.encode()).decode()}"
|
69
|
+
)
|
70
|
+
|
71
|
+
# Make the GET request
|
72
|
+
async with httpx.AsyncClient() as client:
|
73
|
+
try:
|
74
|
+
response = await client.get(
|
75
|
+
endpoint, headers={"Authorization": auth_header}, timeout=10
|
76
|
+
)
|
77
|
+
|
78
|
+
if response.status_code == 200:
|
79
|
+
# If the response is 200, the token is valid
|
80
|
+
return # Pass silently
|
81
|
+
elif response.status_code == 404:
|
82
|
+
# If the response is 404, the token is invalid
|
83
|
+
detail = response.json().get("detail", "Token not found")
|
84
|
+
raise TokenValidationError(detail)
|
85
|
+
else:
|
86
|
+
# Handle unexpected status codes
|
87
|
+
raise TokenValidationError(
|
88
|
+
f"Unexpected response code: {response.status_code}"
|
89
|
+
)
|
90
|
+
except httpx.RequestError as e:
|
91
|
+
raise TokenValidationError(f"Request failed: {e}")
|
92
|
+
except Exception as e:
|
93
|
+
raise TokenValidationError(f"An unexpected error occurred: {e}")
|
94
|
+
|
95
|
+
|
96
|
+
def validate_token(token: str) -> None:
|
97
|
+
"""
|
98
|
+
Synchronous wrapper for the `async_validate_token` function.
|
99
|
+
|
100
|
+
Args:
|
101
|
+
token (str): The token to validate.
|
102
|
+
|
103
|
+
Raises:
|
104
|
+
TokenValidationError: If the token is invalid or if there is an error in the validation process.
|
105
|
+
|
106
|
+
Returns:
|
107
|
+
None: If the token is valid, the function will pass silently.
|
108
|
+
"""
|
109
|
+
# Get the current event loop or create one
|
110
|
+
try:
|
111
|
+
loop = asyncio.get_event_loop()
|
112
|
+
except RuntimeError:
|
113
|
+
loop = asyncio.new_event_loop()
|
114
|
+
asyncio.set_event_loop(loop)
|
115
|
+
|
116
|
+
# Run the async function in the event loop
|
117
|
+
loop.run_until_complete(async_validate_token(token))
|
118
|
+
|
119
|
+
|
120
|
+
# Example usage
|
121
|
+
if __name__ == "__main__":
|
122
|
+
token = "test"
|
123
|
+
try:
|
124
|
+
validate_token(token)
|
125
|
+
print("Token is valid.")
|
126
|
+
except TokenValidationError as e:
|
127
|
+
print(f"Token validation failed: {e}")
|
@@ -1,69 +0,0 @@
|
|
1
|
-
import requests
|
2
|
-
import os
|
3
|
-
|
4
|
-
class TokenValidationError(Exception):
|
5
|
-
"""
|
6
|
-
Custom exception raised when the token validation fails.
|
7
|
-
"""
|
8
|
-
|
9
|
-
def __init__(self, message=None):
|
10
|
-
"""
|
11
|
-
Initialize the exception with an optional message.
|
12
|
-
|
13
|
-
Args:
|
14
|
-
message (str, optional): The error message to display. Defaults to None.
|
15
|
-
"""
|
16
|
-
super().__init__(message)
|
17
|
-
|
18
|
-
|
19
|
-
def validate_token(token = None):
|
20
|
-
"""
|
21
|
-
Validate a token by making a GET request to the validation endpoint.
|
22
|
-
|
23
|
-
Args:
|
24
|
-
token (str): The token to validate.
|
25
|
-
|
26
|
-
Raises:
|
27
|
-
TokenValidationError: If the token is invalid or if there is an error in the validation process.
|
28
|
-
|
29
|
-
Returns:
|
30
|
-
None: If the token is valid, the function will pass silently.
|
31
|
-
"""
|
32
|
-
endpoint = f"https://engr-131-api.eastus.cloudapp.azure.com/validate-token/{token}"
|
33
|
-
|
34
|
-
if token is not None:
|
35
|
-
os.environ["TOKEN"] = token
|
36
|
-
|
37
|
-
if token is None:
|
38
|
-
token = os.getenv("TOKEN", None)
|
39
|
-
|
40
|
-
if token is None:
|
41
|
-
raise TokenValidationError("No token provided")
|
42
|
-
|
43
|
-
try:
|
44
|
-
response = requests.get(endpoint, timeout=10)
|
45
|
-
|
46
|
-
if response.status_code == 200:
|
47
|
-
# If the response is 200, the token is valid
|
48
|
-
return # Pass silently
|
49
|
-
elif response.status_code == 404:
|
50
|
-
# If the response is 404, the token is invalid
|
51
|
-
raise TokenValidationError(response.json().get("detail", "Token not found"))
|
52
|
-
else:
|
53
|
-
# Handle unexpected status codes
|
54
|
-
raise TokenValidationError(
|
55
|
-
f"Unexpected response code: {response.status_code}"
|
56
|
-
)
|
57
|
-
except requests.RequestException as e:
|
58
|
-
# Raise an exception for connection errors or timeout
|
59
|
-
raise TokenValidationError(f"Request failed: {e}")
|
60
|
-
|
61
|
-
|
62
|
-
# Example usage
|
63
|
-
if __name__ == "__main__":
|
64
|
-
token = "test"
|
65
|
-
try:
|
66
|
-
validate_token(token)
|
67
|
-
print("Token is valid.")
|
68
|
-
except TokenValidationError as e:
|
69
|
-
print(f"Token validation failed: {e}")
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|