aixtools 0.2.4__py3-none-any.whl → 0.2.6__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.

Potentially problematic release.


This version of aixtools might be problematic. Click here for more details.

aixtools/_version.py CHANGED
@@ -28,7 +28,7 @@ version_tuple: VERSION_TUPLE
28
28
  commit_id: COMMIT_ID
29
29
  __commit_id__: COMMIT_ID
30
30
 
31
- __version__ = version = '0.2.4'
32
- __version_tuple__ = version_tuple = (0, 2, 4)
31
+ __version__ = version = '0.2.6'
32
+ __version_tuple__ = version_tuple = (0, 2, 6)
33
33
 
34
34
  __commit_id__ = commit_id = None
aixtools/auth/auth.py CHANGED
@@ -2,21 +2,60 @@
2
2
  Module that manages OAuth2 functions for authentication
3
3
  """
4
4
 
5
+ import enum
5
6
  import logging
6
7
 
7
8
  import jwt
8
- from jwt import ExpiredSignatureError, InvalidAudienceError, InvalidIssuerError, PyJWKClient
9
+ from fastapi import HTTPException
10
+ from jwt import ExpiredSignatureError, InvalidAudienceError, InvalidIssuerError, InvalidSignatureError, PyJWKClient
9
11
 
10
12
  from aixtools.utils import config
11
13
 
12
14
  logger = logging.getLogger(__name__)
13
15
 
14
16
 
17
+ class AuthTokenErrorCode(str, enum.Enum):
18
+ """Enum for error codes returned by the AuthTokenError exception."""
19
+
20
+ TOKEN_EXPIRED = "Token expired"
21
+ INVALID_AUDIENCE = "Token not for expected audience"
22
+ INVALID_ISSUER = "Token not for expected issuer"
23
+ INVALID_SIGNATURE = "Token signature error"
24
+ INVALID_TOKEN = "Invalid token"
25
+ JWT_ERROR = "Generic JWT error"
26
+ MISSING_GROUPS_ERROR = "Missing authorized groups"
27
+ INVALID_TOKEN_SCOPE = "Token scope does not match configured scope"
28
+
29
+
15
30
  class AuthTokenError(Exception):
16
31
  """Exception raised for authentication token errors."""
17
32
 
33
+ def __init__(self, error_code: AuthTokenErrorCode, msg: str = None):
34
+ self.error_code = error_code
35
+ error_msg = error_code.value if msg is None else msg
36
+ super().__init__(error_msg)
37
+
38
+ def to_http_exception(self, required_scope: str = None, realm: str = "MCP") -> HTTPException:
39
+ """
40
+ Returns an HTTPException with 401 status for all AuthTokenErrorCode,
41
+ including MCP JSON body and WWW-Authenticate header.
42
+ """
43
+ status_code = 401
44
+ www_error = (
45
+ "insufficient_scope" if self.error_code == AuthTokenErrorCode.INVALID_TOKEN_SCOPE else "invalid_token"
46
+ )
47
+
48
+ header_value = f'Bearer realm="{realm}", error="{www_error}", error_description="{self.error_code.value}"'
49
+ if self.error_code == AuthTokenErrorCode.INVALID_TOKEN_SCOPE and required_scope:
50
+ header_value += f', scope="{required_scope}"'
51
+
52
+ detail = {"error": {"code": self.error_code.name, "message": self.error_code.value}}
53
+ if self.error_code == AuthTokenErrorCode.INVALID_TOKEN_SCOPE and required_scope:
54
+ detail["error"]["required_scope"] = required_scope
55
+
56
+ return HTTPException(status_code=status_code, detail=detail, headers={"WWW-Authenticate": header_value})
57
+
18
58
 
19
- # pylint: disable=too-few-public-methods
20
59
  class AccessTokenVerifier:
21
60
  """
22
61
  Verifies Microsoft SSO JWT token against the configured Tenant ID, Audience, API ID and Issuer URL.
@@ -25,7 +64,12 @@ class AccessTokenVerifier:
25
64
  def __init__(self):
26
65
  tenant_id = config.APP_TENANT_ID
27
66
  self.api_id = config.APP_API_ID
28
- self.issuer_url = config.APP_ISSUER_URL
67
+ self.issuer_url = f"https://sts.windows.net/{tenant_id}/"
68
+
69
+ self.authorized_groups = set(config.APP_AUTHORIZED_GROUPS.split(",")) if config.APP_AUTHORIZED_GROUPS else set()
70
+ if not self.authorized_groups:
71
+ logger.warning("No authorized groups configured")
72
+
29
73
  # Azure AD endpoints
30
74
  jwks_url = f"https://login.microsoftonline.com/{tenant_id}/discovery/v2.0/keys"
31
75
  self.jwks_client = PyJWKClient(
@@ -48,7 +92,7 @@ class AccessTokenVerifier:
48
92
  """
49
93
  try:
50
94
  signing_key = self.jwks_client.get_signing_key_from_jwt(token)
51
-
95
+ logger.info("Verifying JWT token")
52
96
  claims = jwt.decode(
53
97
  token,
54
98
  signing_key.key,
@@ -58,13 +102,48 @@ class AccessTokenVerifier:
58
102
  # ensure audience verification is carried out
59
103
  options={"verify_aud": True},
60
104
  )
105
+ logger.info("Verified JWT token")
61
106
  return claims
62
107
 
63
108
  except ExpiredSignatureError as e:
64
- raise AuthTokenError("Token expired") from e
109
+ raise AuthTokenError(AuthTokenErrorCode.TOKEN_EXPIRED) from e
65
110
  except InvalidAudienceError as e:
66
- raise AuthTokenError(f"Token not for expected audience: {e}") from e
111
+ raise AuthTokenError(AuthTokenErrorCode.INVALID_AUDIENCE) from e
67
112
  except InvalidIssuerError as e:
68
- raise AuthTokenError(f"Token not for expected issuer: {e}") from e
113
+ raise AuthTokenError(AuthTokenErrorCode.INVALID_ISSUER) from e
114
+ except InvalidSignatureError as e:
115
+ raise AuthTokenError(AuthTokenErrorCode.INVALID_SIGNATURE) from e
69
116
  except jwt.exceptions.PyJWTError as e:
70
- raise AuthTokenError(f"Invalid token: {e}") from e
117
+ raise AuthTokenError(AuthTokenErrorCode.JWT_ERROR) from e
118
+
119
+ def authorize_claims(self, claims: dict, expected_scope: str):
120
+ """
121
+ Authorize claims based on token scope, expected scope and authorized groups
122
+ claims: decoded JWT claims
123
+ expected_scope: expected scope for the token
124
+ Raises AuthTokenError if authorization fails.
125
+ """
126
+ logger.info("Checking JWT token claims")
127
+ if expected_scope:
128
+ token_scopes = claims.get("scp", "").split()
129
+ if expected_scope not in token_scopes:
130
+ logger.error("Expected token scope: %s, got: %s", expected_scope, token_scopes)
131
+ raise AuthTokenError(
132
+ AuthTokenErrorCode.INVALID_TOKEN_SCOPE,
133
+ f"Expected token scope: {expected_scope}, got: {token_scopes}",
134
+ )
135
+
136
+ if not self.authorized_groups:
137
+ logger.info("Authorized JWT token, no authorized groups configured")
138
+ return
139
+
140
+ groups = claims.get("groups", [])
141
+ if self.authorized_groups & set(groups):
142
+ logger.info("Authorized JWT token, against %s", groups)
143
+ return
144
+
145
+ logger.error("Could not find any group in JWT token, matching: %s", self.authorized_groups)
146
+ raise AuthTokenError(
147
+ AuthTokenErrorCode.MISSING_GROUPS_ERROR,
148
+ f"Could not find any group in JWT token, matching: {self.authorized_groups}",
149
+ )
aixtools/utils/config.py CHANGED
@@ -135,5 +135,6 @@ APP_CLIENT_ID = get_variable_env("APP_CLIENT_ID")
135
135
  # used for token audience check
136
136
  APP_API_ID = get_variable_env("APP_API_ID")
137
137
  APP_TENANT_ID = get_variable_env("APP_TENANT_ID")
138
- # used for token issuer check
139
- APP_ISSUER_URL = get_variable_env("APP_ISSUER_URL")
138
+
139
+ # used for token authorization check
140
+ APP_AUTHORIZED_GROUPS = get_variable_env("APP_AUTHORIZED_GROUPS", allow_empty=True)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: aixtools
3
- Version: 0.2.4
3
+ Version: 0.2.6
4
4
  Summary: Tools for AI exploration and debugging
5
5
  Requires-Python: >=3.11.2
6
6
  Description-Content-Type: text/markdown
@@ -1,5 +1,5 @@
1
1
  aixtools/__init__.py,sha256=9NGHm7LjsQmsvjTZvw6QFJexSvAU4bCoN_KBk9SCa00,260
2
- aixtools/_version.py,sha256=NRw4Jle4n9v_DD2wtplRqflGCvX8OU5eAjycYY0vY3Y,704
2
+ aixtools/_version.py,sha256=2Q6v117QPuRsVsIEaHT3nJJVx7xxa47FYOkmuhVbGAI,704
3
3
  aixtools/app.py,sha256=JzQ0nrv_bjDQokllIlGHOV0HEb-V8N6k_nGQH-TEsVU,5227
4
4
  aixtools/chainlit.md,sha256=yC37Ly57vjKyiIvK4oUvf4DYxZCwH7iocTlx7bLeGLU,761
5
5
  aixtools/context.py,sha256=I_MD40ZnvRm5WPKAKqBUAdXIf8YaurkYUUHSVVy-QvU,598
@@ -31,7 +31,7 @@ aixtools/agents/agent_batch.py,sha256=0Zu9yNCRPAQZPjXQ-dIUAmP1uGTVbxVt7xvnMpoJMj
31
31
  aixtools/agents/print_nodes.py,sha256=wVTngNfqM0As845WTRz6G3Rei_Gr3HuBlvu-G_eXuig,1665
32
32
  aixtools/agents/prompt.py,sha256=p9OYnyJ4-MyGXwHPrQeJBhZ2a3RV2HqhtdUUCrTMsAQ,3361
33
33
  aixtools/auth/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
34
- aixtools/auth/auth.py,sha256=aKYCKJRjSNrVZmIWN2h2p1zYqkhMLLBXBfk_Qy5NKik,2365
34
+ aixtools/auth/auth.py,sha256=4vNcljdpFzvIBXqjK7i3xAy-CC8Hc7ii11t-11z6ofY,6041
35
35
  aixtools/compliance/__init__.py,sha256=vnw0zEdySIJWvDAJ8DCRRaWmY_agEOz1qlpAdhmtiuo,191
36
36
  aixtools/compliance/private_data.py,sha256=OOM9mIp3_w0fNgj3VAEWBl7-jrPc19_Ls1pC5dfF5UY,5323
37
37
  aixtools/db/__init__.py,sha256=b8vRhme3egV-aUZbAntnOaDkSXB8UT0Xy5oqQhU_z0Q,399
@@ -79,7 +79,7 @@ aixtools/tools/doctor/mcp_tool_doctor.py,sha256=sX2q5GfNkmUYxnXrqMpeGIwGfeL1LpYJ
79
79
  aixtools/tools/doctor/tool_doctor.py,sha256=EY1pshjLGLD0j6cc1ZFtbc0G19I5IbOZwHFDqypE49Q,2661
80
80
  aixtools/tools/doctor/tool_recommendation.py,sha256=LYyVOSXdAorWiY4P-ucSA1vLlV5BTEfX4GzBXNE_X0M,1569
81
81
  aixtools/utils/__init__.py,sha256=xT6almZBQYMfj4h7Hq9QXDHyVXbOOTxqLsmJsxYYnSw,757
82
- aixtools/utils/config.py,sha256=t32731F53Cv1YYoX95wksoreE0Zn0B8UKyEiKWne4ec,5147
82
+ aixtools/utils/config.py,sha256=fCATakEfeASo6eFMh5Me_idwMQgiITS3LMYc2Z1bGzU,5187
83
83
  aixtools/utils/config_util.py,sha256=3Ya4Qqhj1RJ1qtTTykQ6iayf5uxlpigPXgEJlTi1wn4,2229
84
84
  aixtools/utils/enum_with_description.py,sha256=zjSzWxG74eR4x7dpmb74pLTYCWNSMvauHd7_9LpDYIw,1088
85
85
  aixtools/utils/files.py,sha256=8JnxwHJRJcjWCdFpjzWmo0po2fRg8esj4H7sOxElYXU,517
@@ -89,8 +89,8 @@ aixtools/utils/chainlit/cl_agent_show.py,sha256=vaRuowp4BRvhxEr5hw0zHEJ7iaSF_5bo
89
89
  aixtools/utils/chainlit/cl_utils.py,sha256=fxaxdkcZg6uHdM8uztxdPowg3a2f7VR7B26VPY4t-3c,5738
90
90
  aixtools/vault/__init__.py,sha256=fsr_NuX3GZ9WZ7dGfe0gp_5-z3URxAfwVRXw7Xyc0dU,141
91
91
  aixtools/vault/vault.py,sha256=9dZLWdZQk9qN_Q9Djkofw9LUKnJqnrX5H0fGusVLBhA,6037
92
- aixtools-0.2.4.dist-info/METADATA,sha256=EzZB-SOZLdj5QZDkk3YPu0PCipJOiAKT08xJNWKENfg,27229
93
- aixtools-0.2.4.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
94
- aixtools-0.2.4.dist-info/entry_points.txt,sha256=q8412TG4T0S8K0SKeWp2vkVPIDYQs0jNoHqcQ7qxOiA,155
95
- aixtools-0.2.4.dist-info/top_level.txt,sha256=wBn-rw9bCtxrR4AYEYgjilNCUVmKY0LWby9Zan2PRJM,9
96
- aixtools-0.2.4.dist-info/RECORD,,
92
+ aixtools-0.2.6.dist-info/METADATA,sha256=ks0BVYWEcWW3I7hKY4Gm-s669muCpKJX6QLuwSborj4,27229
93
+ aixtools-0.2.6.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
94
+ aixtools-0.2.6.dist-info/entry_points.txt,sha256=q8412TG4T0S8K0SKeWp2vkVPIDYQs0jNoHqcQ7qxOiA,155
95
+ aixtools-0.2.6.dist-info/top_level.txt,sha256=wBn-rw9bCtxrR4AYEYgjilNCUVmKY0LWby9Zan2PRJM,9
96
+ aixtools-0.2.6.dist-info/RECORD,,