Enforce Token Expiration Policy via REQUIRE_TOKEN_EXPIRATION Setting#2898
Enforce Token Expiration Policy via REQUIRE_TOKEN_EXPIRATION Setting#2898crivetimihai merged 1 commit intomainfrom
Conversation
crivetimihai
left a comment
There was a problem hiding this comment.
Thanks for this PR, @rakdutta. The enforcement logic is correct and test coverage is thorough (15 new test cases covering enabled/disabled x various expiry scenarios).
Negative expires_in_days is not guarded
A value like expires_in_days=-1 is truthy, so it passes the if expires_in_days: gate and produces expires_at in the past. The new check sees expires_at is not None and allows it. The UI's min="1" is the only defense; the API endpoint is unprotected. Consider adding a server-side if expires_in_days is not None and expires_in_days < 1: raise ValueError(...) guard.
HTML diff is ~95% formatter noise
Consider splitting the Prettier/formatting pass into a separate commit to make the functional changes (~20 lines) reviewable independently.
Good: Enforcement is correctly centralized in TokenCatalogService.create_token. Both API endpoints delegate to it, and session tokens (which use create_jwt_token() directly) are unaffected since they always include expiration. No migration needed — this is a runtime config setting.
|
@crivetimihai , the current implementation already validates negative
Validation Implementationmcpgateway/schemas.py (Line 6230) expires_in_days: Optional[int] = Field(default=None, ge=1, description="Expiry in days (must be >= 1 if specified)")The
API Examplecurl -X 'POST' \
'http://localhost:4444/tokens' \
-H 'accept: application/json' \
-H 'Authorization: Bearer $mytoken' \
-H 'Content-Type: application/json' \
-d '{
"name": "test_neg",
"expires_in_days": 0,
"is_active": true
}'Error Response{
"detail": [
{
"type": "greater_than_equal",
"loc": [
"body",
"expires_in_days"
],
"msg": "Input should be greater than or equal to 1",
"input": 0,
"ctx": {
"ge": 1
}
}
]
}UI BehaviorIf Expires in Days is set to UI Reference
```
|
2490f9d to
e065423
Compare
e065423 to
9b1af4e
Compare
|
Rebased onto current Changes kept:
Cleanup applied:
All tests pass (token catalog service, tokens router, admin). |
crivetimihai
left a comment
There was a problem hiding this comment.
LGTM. Rebased, cleaned up, and verified — all tests pass.
…tting Enforce the existing REQUIRE_TOKEN_EXPIRATION config setting at token creation time. Previously the setting only validated incoming tokens but allowed creation of tokens without expiration. - Add validation in TokenCatalogService.create_token() to reject tokens without expiration when REQUIRE_TOKEN_EXPIRATION=true - Pass require_token_expiration flag to admin UI template context - Add conditional required field indicator and helper text in the token creation form - Update existing tests to provide expires_in_days where needed - Add new test cases covering policy enabled/disabled scenarios, team tokens, and edge cases (zero expiry days) Closes #2836 Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
9b1af4e to
4f52530
Compare
|
Rebased onto
All 122 token catalog service tests pass. Coverage on |
…tting (#2898) Enforce the existing REQUIRE_TOKEN_EXPIRATION config setting at token creation time. Previously the setting only validated incoming tokens but allowed creation of tokens without expiration. - Add validation in TokenCatalogService.create_token() to reject tokens without expiration when REQUIRE_TOKEN_EXPIRATION=true - Pass require_token_expiration flag to admin UI template context - Add conditional required field indicator and helper text in the token creation form - Update existing tests to provide expires_in_days where needed - Add new test cases covering policy enabled/disabled scenarios, team tokens, and edge cases (zero expiry days) Closes #2836 Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
…tting (#2898) Enforce the existing REQUIRE_TOKEN_EXPIRATION config setting at token creation time. Previously the setting only validated incoming tokens but allowed creation of tokens without expiration. - Add validation in TokenCatalogService.create_token() to reject tokens without expiration when REQUIRE_TOKEN_EXPIRATION=true - Pass require_token_expiration flag to admin UI template context - Add conditional required field indicator and helper text in the token creation form - Update existing tests to provide expires_in_days where needed - Add new test cases covering policy enabled/disabled scenarios, team tokens, and edge cases (zero expiry days) Closes #2836 Signed-off-by: Mihai Criveti <crivetimihai@gmail.com> Signed-off-by: Vishu Bhatnagar <vishu.bhatnagar@ibm.com>
…tting (IBM#2898) Enforce the existing REQUIRE_TOKEN_EXPIRATION config setting at token creation time. Previously the setting only validated incoming tokens but allowed creation of tokens without expiration. - Add validation in TokenCatalogService.create_token() to reject tokens without expiration when REQUIRE_TOKEN_EXPIRATION=true - Pass require_token_expiration flag to admin UI template context - Add conditional required field indicator and helper text in the token creation form - Update existing tests to provide expires_in_days where needed - Add new test cases covering policy enabled/disabled scenarios, team tokens, and edge cases (zero expiry days) Closes IBM#2836 Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>

closes issue #2836
This PR introduces support for enforcing a server policy that requires API tokens to have an expiration date. The enforcement is implemented at the service layer, reflected in the Admin UI, and covered with unit tests to ensure consistent behavior.
Key Changes
Backend Enforcement
REQUIRE_TOKEN_EXPIRATIONbefore allowing token creation.Files Updated
token_catalog_service.pyAdmin UI Enhancements
Files Updated
admin.htmlConfiguration Exposure
require_token_expirationsetting through admin API.Files Updated
admin.pyBehavior Scenarios
REQUIRE_TOKEN_EXPIRATION = false
REQUIRE_TOKEN_EXPIRATION = true
Testing
Updated existing tests to include expiration where required.
Added new test cases covering:
Impact