diff --git a/README.md b/README.md index ce8c6be..bf09a1f 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,8 @@ In order to updated data in NetBox you need a NetBox API token. * auth * secrets * users +* Both v1 (legacy) and v2 tokens (NetBox 4.5+, `nbt_` prefix) are supported. + The correct authorization header (`Token` or `Bearer`) is detected automatically. A short description can be found [here](https://docs.netbox.dev/en/stable/integrations/rest-api/#authentication) diff --git a/module/netbox/config.py b/module/netbox/config.py index 7bf00f1..a4f1c89 100644 --- a/module/netbox/config.py +++ b/module/netbox/config.py @@ -26,7 +26,8 @@ def __init__(self): ConfigOption("api_token", str, description="""Requires an NetBox API token with full permissions on all objects except - 'auth', 'secrets' and 'users' + 'auth', 'secrets' and 'users'. + Both v1 (legacy) and v2 (NetBox 4.5+, nbt_ prefix) tokens are supported. """, config_example="XYZ", mandatory=True, diff --git a/module/netbox/connection.py b/module/netbox/connection.py index 02ba53b..591228a 100644 --- a/module/netbox/connection.py +++ b/module/netbox/connection.py @@ -152,8 +152,10 @@ def create_session(self) -> requests.Session: requests.Session: session handler of new NetBox session """ + token = self.settings.api_token + keyword = "Bearer" if token.startswith("nbt_") else "Token" header = { - "Authorization": f"Token {self.settings.api_token}", + "Authorization": f"{keyword} {token}", "User-Agent": f"netbox-sync/{__version__}", "Content-Type": "application/json" } diff --git a/settings-example.ini b/settings-example.ini index ec2d5f1..b67a46c 100644 --- a/settings-example.ini +++ b/settings-example.ini @@ -44,7 +44,8 @@ [netbox] ; Requires an NetBox API token with full permissions on all objects except 'auth', -; 'secrets' and 'users' +; 'secrets' and 'users'. Both v1 (legacy) and v2 (NetBox 4.5+, nbt_ prefix) tokens are +; supported. api_token = XYZ ; Requires a hostname or IP which points to your NetBox instance