Skip to content

Commit 0fb6296

Browse files
authored
[TOOLSLIBS-325] Adds bearer token auth (#189)
* rm py2 dep warning * adds TEST_TOKEN * adds support for bearer token and airship class docstring * adds tests for bearer token, uses context manager for exception tests * rm python 2 deprecation warning * rm py2.7 from CI test runner * removes some python2 things
1 parent f59d714 commit 0fb6296

File tree

8 files changed

+59
-37
lines changed

8 files changed

+59
-37
lines changed

.github/workflows/test_runner.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
runs-on: ubuntu-latest
99
strategy:
1010
matrix:
11-
python-version: ["2.7", "3.6", "3.7", "3.8", "3.9"]
11+
python-version: ["3.6", "3.7", "3.8", "3.9"]
1212

1313
steps:
1414
- uses: actions/checkout@v2

.gitmodules

Lines changed: 0 additions & 3 deletions
This file was deleted.

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ repos:
88
- id: no-commit-to-branch
99
args: ["--branch", "master", "--branch", "main"]
1010
- repo: https://github.com/psf/black
11-
rev: 19.3b0
11+
rev: 21.12b0
1212
hooks:
1313
- id: black
1414
- repo: https://github.com/codespell-project/codespell
15-
rev: v1.16.0
15+
rev: v2.1.0
1616
hooks:
1717
- id: codespell

isort.cfg

Lines changed: 0 additions & 2 deletions
This file was deleted.

tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
TEST_KEY = "x" * 22
22
TEST_SECRET = "y" * 22
3+
TEST_TOKEN = "z" * 92

tests/core/test_airship.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import requests
77

88
import urbanairship as ua
9-
from tests import TEST_SECRET, TEST_KEY
9+
from tests import TEST_SECRET, TEST_KEY, TEST_TOKEN
1010

1111

1212
class TestAirshipCore(unittest.TestCase):
@@ -22,12 +22,8 @@ def test_airship_timeout(self):
2222
def test_airship_timeout_exception(self):
2323
timeout_str = "50"
2424

25-
try:
26-
airship_raises_value_error = ua.Airship(
27-
key=TEST_KEY, secret=TEST_SECRET, timeout=timeout_str
28-
)
29-
except ValueError as e:
30-
self.assertIsInstance(e, ValueError)
25+
with self.assertRaises(ValueError):
26+
ua.Airship(key=TEST_KEY, secret=TEST_SECRET, timeout=timeout_str)
3127

3228
def test_airship_location(self):
3329
location = "eu"
@@ -39,12 +35,25 @@ def test_airship_location(self):
3935
def test_airship_location_exception(self):
4036
invalid_location = "xx"
4137

42-
try:
43-
airship_rasises_value_error = ua.Airship(
44-
key=TEST_KEY, secret=TEST_SECRET, location=invalid_location
45-
)
46-
except ValueError as e:
47-
self.assertIsInstance(e, ValueError)
38+
with self.assertRaises(ValueError):
39+
ua.Airship(key=TEST_KEY, secret=TEST_SECRET, location=invalid_location)
40+
41+
def test_token_auth(self):
42+
test_airship = ua.Airship(key=TEST_KEY, token=TEST_TOKEN)
43+
44+
self.assertEqual(TEST_TOKEN, test_airship.token)
45+
46+
def test_no_secret_nor_token(self):
47+
with self.assertRaises(
48+
ValueError, msg="One of token or secret must be used, not both"
49+
):
50+
ua.Airship(key=TEST_KEY)
51+
52+
def test_both_secret_and_token(self):
53+
with self.assertRaises(
54+
ValueError, msg="Either token or secret must be included"
55+
):
56+
ua.Airship(key=TEST_KEY, secret=TEST_SECRET, token=TEST_TOKEN)
4857

4958

5059
class TestAirshipResponse(unittest.TestCase):

tox.ini

Lines changed: 0 additions & 7 deletions
This file was deleted.

urbanairship/core.py

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,37 @@ def get(self, endpoint):
7676

7777

7878
class Airship(object):
79-
def __init__(self, key, secret, location=None, timeout=None):
79+
def __init__(self, key, secret=None, token=None, location="us", timeout=None):
80+
"""Main client class for interacting with the Airship API.
81+
82+
:param key: [required] An Airship project key used to authenticate
83+
:param secret: [optional] The Airship-generated app or master secret for the
84+
provided key
85+
:param token: [optional] An Airship-generated bearer token for the provided key
86+
:param location: [optional] The Airship cloud site your project is associated
87+
with. Possible values: 'us', 'eu'. Defaults to 'us'.
88+
:param: timeout: [optional] An integer specifying the number of seconds used
89+
for a timeout threshold
90+
"""
8091
self.key = key
8192
self.secret = secret
93+
self.token = token
8294
self.location = location
8395
self.timeout = timeout
84-
85-
self.session = requests.Session()
86-
self.session.auth = (key, secret)
8796
self.urls = Urls(self.location)
8897

89-
if sys.version[0] == "2":
90-
warnings.warn(
91-
"Support for Python 2.x will be removed in urbanairship version 6.0",
92-
DeprecationWarning,
98+
if all([secret, token]):
99+
raise ValueError("One of token or secret must be used, not both")
100+
101+
self.session = requests.Session()
102+
if self.token:
103+
self.session.headers.update(
104+
{"X-UA-Appkey": key, "Authorization": f"Bearer {self.token}"}
93105
)
106+
elif self.secret:
107+
self.session.auth = (key, secret)
108+
else:
109+
raise ValueError("Either token or secret must be included")
94110

95111
@property
96112
def timeout(self):
@@ -128,10 +144,18 @@ def secret(self):
128144

129145
@secret.setter
130146
def secret(self, value):
131-
if not VALID_KEY.match(value):
147+
if isinstance(value, str) and not VALID_KEY.match(value):
132148
raise ValueError("secrets must be 22 characters")
133149
self._secret = value
134150

151+
@property
152+
def token(self):
153+
return self._token
154+
155+
@token.setter
156+
def token(self, value):
157+
self._token = value
158+
135159
def request(
136160
self,
137161
method,

0 commit comments

Comments
 (0)