Skip to content

Commit 976ffcb

Browse files
authored
Merge pull request #27 from digidotcom/rmoral/add_api_keys_support
Add support for authentication using API keys
2 parents 4940d6f + 668af9f commit 976ffcb

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

devicecloud/__init__.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
# License, v. 2.0. If a copy of the MPL was not distributed with this
33
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
44
#
5-
# Copyright (c) 2015-2018 Digi International Inc.
5+
# Copyright (c) 2015-2025 Digi International Inc.
66

77
import logging
88
import time
99
import json
1010

1111
from devicecloud.util import validate_type
12-
from requests.auth import HTTPBasicAuth
12+
from requests.auth import HTTPBasicAuth, AuthBase
1313
import requests
1414
from devicecloud.version import __version__
1515
import six
@@ -344,21 +344,35 @@ class DeviceCloud(object):
344344
if dc.has_valid_credentials():
345345
print list(dc.devicecore.get_devices())
346346
347+
It's also possible to authenticate using API keys::
348+
349+
dc = DeviceCloud(api_key_id='my-id', api_key_secret='my-secret')
350+
347351
From there, access to all of Device Clouds features are possible. In some cases, methods
348352
for quickly performing selected actions may be provided directly via the ``DeviceCloud`` object
349353
while advanced usage requires using functionality exposed through other interfaces.
350354
351355
"""
352356

353-
def __init__(self, username, password, base_url=None,
357+
def __init__(self, username=None, password=None,
358+
api_key_id=None, api_key_secret=None,
359+
base_url=None,
354360
throttle_retries=DEFAULT_THROTTLE_RETRIES,
355361
throttle_delay_init=DEFAULT_THROTTLE_DELAY_INIT,
356362
throttle_delay_max=DEFAULT_THROTTLE_DELAY_MAX,
357363
throttle_delay_backoff_coefficient=DEFAULT_THROTTLE_DELAY_BACKOFF_COEFFICIENT):
358364
if base_url is None:
359365
base_url = "https://devicecloud.digi.com"
366+
367+
if username and password:
368+
auth = HTTPBasicAuth(username, password)
369+
elif api_key_id and api_key_secret:
370+
auth = ApiKeyAuth(api_key_id, api_key_secret)
371+
else:
372+
raise ValueError("Must provide either username/password or api_key_id/api_key_secret")
373+
360374
self._conn = DeviceCloudConnection(
361-
auth=HTTPBasicAuth(username, password),
375+
auth=auth,
362376
base_url=base_url,
363377
throttle_retries=throttle_retries,
364378
throttle_delay_init=throttle_delay_init,
@@ -543,3 +557,16 @@ def get_web_service_stub(self):
543557
from devicecloud.ws import WebServiceStub
544558

545559
return WebServiceStub(self._conn, "/ws")
560+
561+
562+
class ApiKeyAuth(AuthBase):
563+
"""Custom Auth class for API key authentication."""
564+
565+
def __init__(self, api_key_id: str, api_key_secret: str):
566+
self.api_key_id = api_key_id
567+
self.api_key_secret = api_key_secret
568+
569+
def __call__(self, r):
570+
r.headers["X-API-KEY-ID"] = self.api_key_id
571+
r.headers["X-API-KEY-SECRET"] = self.api_key_secret
572+
return r

docs/index.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ quick example of what the API looks like::
4747

4848
dc = DeviceCloud('user', 'pass')
4949

50+
# You can also use API keys for the authentication
51+
# dc = DeviceCloud(api_key_id='my-id', api_key_secret='my-secret')
52+
5053
# show the MAC address of all devices that are currently connected
5154
#
5255
# This is done using Device Cloud DeviceCore functionality

0 commit comments

Comments
 (0)