diff --git a/netbox/netbox/api/authentication.py b/netbox/netbox/api/authentication.py index 65f198471..f5454135e 100644 --- a/netbox/netbox/api/authentication.py +++ b/netbox/netbox/api/authentication.py @@ -12,22 +12,26 @@ class TokenAuthentication(authentication.TokenAuthentication): model = Token def authenticate(self, request): - token_user, token = super().authenticate(request) + authenticationresult = super().authenticate(request) + if authenticationresult: + token_user, token = authenticationresult - # Verify source IP is allowed - if token.allowed_ips: - # Replace 'HTTP_X_REAL_IP' with the settings variable choosen in #8867 - if 'HTTP_X_REAL_IP' in request.META: - clientip = request.META['HTTP_X_REAL_IP'].split(",")[0].strip() - elif 'REMOTE_ADDR' in request.META: - clientip = request.META['REMOTE_ADDR'] - else: - raise exceptions.AuthenticationFailed(f"A HTTP header containing the SourceIP (HTTP_X_REAL_IP, REMOTE_ADDR) is missing from the request.") + # Verify source IP is allowed + if token.allowed_ips: + # Replace 'HTTP_X_REAL_IP' with the settings variable choosen in #8867 + if 'HTTP_X_REAL_IP' in request.META: + clientip = request.META['HTTP_X_REAL_IP'].split(",")[0].strip() + elif 'REMOTE_ADDR' in request.META: + clientip = request.META['REMOTE_ADDR'] + else: + raise exceptions.AuthenticationFailed(f"A HTTP header containing the SourceIP (HTTP_X_REAL_IP, REMOTE_ADDR) is missing from the request.") - if not token.validate_client_ip(clientip): - raise exceptions.AuthenticationFailed(f"Source IP {clientip} is not allowed to use this token.") + if not token.validate_client_ip(clientip): + raise exceptions.AuthenticationFailed(f"Source IP {clientip} is not allowed to use this token.") - return token_user, token + return token_user, token + else: + return None def authenticate_credentials(self, key): model = self.get_model() diff --git a/netbox/users/admin/__init__.py b/netbox/users/admin/__init__.py index ddd1f3d6c..88066fe13 100644 --- a/netbox/users/admin/__init__.py +++ b/netbox/users/admin/__init__.py @@ -62,8 +62,9 @@ class TokenAdmin(admin.ModelAdmin): ] def list_allowed_ips(self, obj): - return obj.allowed_ips - list_allowed_ips.empty_value_display = 'Any' + if obj.allowed_ips: + return obj.allowed_ips + return 'Any' list_allowed_ips.short_description = "Allowed IPs"