You've already forked ansible-netbox-inventory
feat: bearer auth for netbox 4.5 v2 api tokens
v2 tokens (nbt_<key>.<secret>) are sent as Authorization: Bearer, legacy tokens keep the Token scheme; NetBox drops legacy token support in 4.7.
This commit is contained in:
@@ -66,6 +66,18 @@ def _extract_error_message(response: requests.Response) -> str:
|
||||
return f"NetBox API error: HTTP {response.status_code}"
|
||||
|
||||
|
||||
def auth_header(token: str) -> str:
|
||||
"""
|
||||
Build the Authorization header value for a NetBox API token.
|
||||
|
||||
Version 2 tokens (NetBox 4.5+, "nbt_<key>.<secret>") use the Bearer
|
||||
scheme, legacy tokens the Token scheme.
|
||||
"""
|
||||
if token.startswith("nbt_"):
|
||||
return f"Bearer {token}"
|
||||
return f"Token {token}"
|
||||
|
||||
|
||||
class NetBoxClient:
|
||||
"""Thin wrapper around the NetBox REST API."""
|
||||
|
||||
@@ -76,7 +88,7 @@ class NetBoxClient:
|
||||
self._session.verify = verify_ssl
|
||||
self._session.headers.update(
|
||||
{
|
||||
"Authorization": f"Token {token}",
|
||||
"Authorization": auth_header(token),
|
||||
"Accept": "application/json",
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user