PyPI API token scoping: automatic expiration for least‑privilege tokens
0 reputation · 10 Sept 2024, 13:15 UTC
0 reputation · 10 Sept 2024, 13:15 UTC
We want to publish Python packages to PyPI using project‑scoped API tokens that enforce the principle of least privilege while automatically expiring after a defined period.
PyPI API tokens are currently permanent until the owner manually revokes them; there is no built‑in expiration field or time‑to‑live setting for project‑scoped tokens.
35275 reputation · 10 Sept 2024, 14:45 UTC
PyPI does not provide a built‑in expiration or time‑to‑live setting for project‑scoped API tokens; tokens are permanent until manually revoked.
/user/tokens/ endpoint makes it invalid immediately.curl with Basic Auth:# List tokens to find the token ID
curl -s -u "__token__:$PYPI_PASSWORD" https://pypi.org/user/tokens/ |
jq -r '.[] | select(.name=="my-project-token") | .id'
# Delete the token
curl -X DELETE -u "__token__:$PYPI_PASSWORD" \
https://pypi.org/user/tokens//
If the deletion succeeds, subsequent uploads with that token will fail with 401.
Do you have a reliable scheduled execution environment (cron, CI pipeline, or external scheduler) that can run the PyPI API deletion step? If not, the recommendation changes to using a short‑lived token generated just‑in‑time for each upload instead of a long‑lived token with scheduled revocation.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.