[PR #20862] [MERGED] Closes #20823: Validate Token expiration date on creation #16071

Closed
opened 2025-12-30 00:25:38 +01:00 by adam · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/netbox-community/netbox/pull/20862
Author: @pheus
Created: 11/21/2025
Status: Merged
Merged: 11/24/2025
Merged by: @jnovinger

Base: mainHead: 20823-prevent-token-creation-with-past-expiration-date


📝 Commits (1)

  • 8e4b3f9 feat(users): Validate token expiration date on creation

📊 Changes

2 files changed (+87 additions, -1 deletions)

View changed files

📝 netbox/users/models/tokens.py (+20 -0)
📝 netbox/users/tests/test_models.py (+67 -1)

📄 Description

Fixes: #20823

This PR adds model-level validation to prevent creating API tokens with an expiration date in the past, while still allowing updates to existing tokens (including already-expired ones).

Changes:

  • Updates Token.clean() to raise a ValidationError when creating a new token (pk is None) whose expires timestamp is already in the past.
  • Leaves updates to existing tokens unchanged so that expired tokens can still be modified as needed.
  • Adds tests in users/tests/test_models.py covering:
    • the Token.is_expired property for None, future, and past expiration times
    • creation of tokens with a past expiration date
    • updates to existing expired tokens

No database or API schema changes are introduced by this PR.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/netbox-community/netbox/pull/20862 **Author:** [@pheus](https://github.com/pheus) **Created:** 11/21/2025 **Status:** ✅ Merged **Merged:** 11/24/2025 **Merged by:** [@jnovinger](https://github.com/jnovinger) **Base:** `main` ← **Head:** `20823-prevent-token-creation-with-past-expiration-date` --- ### 📝 Commits (1) - [`8e4b3f9`](https://github.com/netbox-community/netbox/commit/8e4b3f9d911c5e6c1cd4317ba74c8f2d0b901bb7) feat(users): Validate token expiration date on creation ### 📊 Changes **2 files changed** (+87 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `netbox/users/models/tokens.py` (+20 -0) 📝 `netbox/users/tests/test_models.py` (+67 -1) </details> ### 📄 Description <!-- Thank you for your interest in contributing to NetBox! Please note that our contribution policy requires that a feature request or bug report be approved and assigned prior to opening a pull request. This helps avoid waste time and effort on a proposed change that we might not be able to accept. IF YOUR PULL REQUEST DOES NOT REFERENCE AN ISSUE WHICH HAS BEEN ASSIGNED TO YOU, IT WILL BE CLOSED AUTOMATICALLY. Please specify your assigned issue number on the line below. --> ### Fixes: #20823 <!-- Please include a summary of the proposed changes below. --> This PR adds model-level validation to prevent creating API tokens with an expiration date in the past, while still allowing updates to existing tokens (including already-expired ones). Changes: - Updates `Token.clean()` to raise a `ValidationError` when creating a new token (`pk is None`) whose `expires` timestamp is already in the past. - Leaves updates to existing tokens unchanged so that expired tokens can still be modified as needed. - Adds tests in `users/tests/test_models.py` covering: - the `Token.is_expired` property for `None`, future, and past expiration times - creation of tokens with a past expiration date - updates to existing expired tokens No database or API schema changes are introduced by this PR. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
adam added the pull-request label 2025-12-30 00:25:38 +01:00
adam closed this issue 2025-12-30 00:25:38 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#16071