Skip to content

Commit

Permalink
Add token authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
aruhier committed May 29, 2017
1 parent 4444da0 commit 18358db
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,23 @@ from netboxapi import NetboxAPI
Initialize a new `NetboxAPI` object:

```python
netbox_api = NetboxAPI(url="netbox.example.com")

# or if you enabled the authentication
netbox_api = NetboxAPI(
url="netbox.example.com", username="user", password="password"
)

# or if you have generated a token
netbox_api = NetboxAPI(
url="netbox.example.com", token="token"
)

# but the following is useless, as the token will not be used
netbox_api = NetboxAPI(
url="netbox.example.com", username="user", password="password",
token="token"
)
```

Then use multiple available methods to interact with the api:
Expand Down
24 changes: 23 additions & 1 deletion netboxapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,28 @@
from urllib.parse import urljoin


class _HTTPTokenAuth(requests.auth.AuthBase):
"""HTTP Basic Authentication with token."""

def __init__(self, token):
self.token = token

def __eq__(self, other):
return self.token == getattr(other, 'token', None)

def __ne__(self, other):
return not self == other

def __call__(self, r):
r.headers["Authorization"] = "Token {}".format(self.token)
return r


class NetboxAPI():
def __init__(self, url, username=None, password=None):
def __init__(self, url, username=None, password=None, token=None):
self.username = username
self.password = password
self.token = token
self.url = url.rstrip('/')

self.session = requests.Session()
Expand Down Expand Up @@ -43,6 +61,10 @@ def _generic_http_method_request(self, method, url, **kwargs):
response = http_method(
url, auth=(self.username, self.password), **kwargs
)
elif self.token:
response = http_method(
url, auth=_HTTPTokenAuth(self.token), **kwargs
)
else:
response = http_method(url, **kwargs)

Expand Down
14 changes: 14 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import requests_mock

from netboxapi import NetboxAPI
from netboxapi.api import _HTTPTokenAuth


class TestNetboxAPI():
url = "http://localhost/"
login = "login"
password = "password"
token = "testing_token"

@pytest.fixture()
def prepared_api(self):
Expand All @@ -29,6 +31,10 @@ def test_get_loggedin(self, **kwargs):
prepared_api = NetboxAPI(self.url, self.login, self.password)
self._generic_test_http_method_request(prepared_api, "get")

def test_get_loggedin_token(self, **kwargs):
prepared_api = NetboxAPI(self.url, token=self.token)
self._generic_test_http_method_request(prepared_api, "get")

def test_post(self, prepared_api, **kwargs):
self._generic_test_http_method_request(prepared_api, "post")

Expand All @@ -51,3 +57,11 @@ def _generic_test_http_method_request(self, prepared_api, method):
m.register_uri(method, url, json=expected_json)
response = getattr(prepared_api, method)(url)
assert response == expected_json


class TestHTTPTokenAuth():
def test_eq(self):
assert _HTTPTokenAuth("test") == _HTTPTokenAuth("test")

def test_not_eq(self):
assert _HTTPTokenAuth("test") != _HTTPTokenAuth("test1")

0 comments on commit 18358db

Please sign in to comment.