Skip to content

Commit 5a4f5a8

Browse files
committed
Fix auth_app for modern Taiga versions (#227)
1 parent b258c1a commit 5a4f5a8

File tree

6 files changed

+19
-33
lines changed

6 files changed

+19
-33
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ Daniel Federschmidt <[email protected]>
2626
Robert Dyer <[email protected]>
2727
Patrick Szczepański <[email protected]>
2828
Hassen Ben Tanfous <[email protected]>
29+
Melissa Eckardt <[email protected]>

changes/227.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix auth_app method to work with Taiga versions >= 3.1.0

setup.cfg

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ install_requires =
2929
requests>2.11
3030
six>=1.9
3131
python-dateutil>=2.4
32-
pyjwkest>=1.0
3332
packages = taiga
3433
python_requires = >=3.7
3534
setup_requires =

taiga/client.py

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,19 @@ def auth(self, username, password):
161161
)
162162
self._init_resources()
163163

164-
def auth_app(self, app_id, app_secret, auth_code, state=""):
164+
def auth_app(self, app_id, auth_code, state):
165165
"""
166-
Authenticate an app
166+
Retrieve an application token.
167+
This only works once per token; in order to reset it, the auth code needs
168+
to be set again in the Taiga admin UI.
169+
170+
In order to use the token, initialize TaigaAPI with token_type="Application"
171+
and token="token from this function".
167172
168173
:param app_id: the app id
169-
:param app_secret: the app secret
170-
:param auth_code: the app auth code
174+
:param auth_code: app auth code as specified in Taiga
175+
:param state: state as specified in Taiga (any string; must not be empty)
176+
:return: token string
171177
"""
172178
headers = {"Content-type": "application/json"}
173179
payload = {"application": app_id, "auth_code": auth_code, "state": state}
@@ -180,31 +186,12 @@ def auth_app(self, app_id, app_secret, auth_code, state=""):
180186
raise exceptions.TaigaRestException(full_url, 400, "NETWORK ERROR", "POST")
181187
if response.status_code != 200:
182188
raise exceptions.TaigaRestException(full_url, response.status_code, response.text, "POST")
183-
cyphered_token = response.json().get("cyphered_token", "")
184-
if cyphered_token:
185-
from jwkest.jwe import JWE
186-
from jwkest.jwk import SYMKey
187-
188-
sym_key = SYMKey(key=app_secret, alg="A128KW")
189-
data, success = JWE().decrypt(cyphered_token, keys=[sym_key]), True
190-
if isinstance(data, tuple):
191-
data, success = data
192-
try:
193-
self.token = json.loads(data.decode("utf-8")).get("token", None)
194-
except ValueError: # pragma: no cover
195-
self.token = None
196-
if not success:
197-
self.token = None
198-
else:
199-
self.token = None
200-
201-
if self.token is None:
189+
token = response.json().get("token", None)
190+
191+
if token is None:
202192
raise exceptions.TaigaRestException(full_url, 400, "INVALID TOKEN", "POST")
203193

204-
self.raw_request = RequestMaker(
205-
"/api/v1", self.host, self.token, "Application", self.tls_verify, proxies=self.proxies
206-
)
207-
self._init_resources()
194+
return token
208195

209196
def refresh_token(self, token_refresh=""):
210197
"""
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"cyphered_token": "eyJhbGciOiJBMTI4S1ciLCJlbmMiOiJBMjU2R0NNIn0.9C2qwG_R0B22Qws6umB1gbqpLiH2rfVJbwgtJlxWBqPtYhhG-Ioc1g.RBBVW4k2k8t44aEo.VFsRiipfRMKXVGQxdGcnM6k.8uaF6FoQiPxX6wdFU2AyYA"
2+
"token": "eyJhcHBfdG9rZW5faWQiOjN9:1utpZt:fXS-ifJ6TGWQEy7IkkxemDPGM5jXTOYYn7heGf8MFWU"
33
}

tests/test_auth_app.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class TestAuthApp(unittest.TestCase):
1414
def test_auth_success(self, requests):
1515
requests.post.return_value = MockResponse(200, create_mock_json("tests/resources/auth_app_success.json"))
1616
api = TaigaAPI(host="host")
17-
api.auth_app("valid-app-id", "valid-app-secret", "valid-auth-code", "valid-state")
18-
self.assertEqual(api.token, "f4k3")
17+
token = api.auth_app("valid-app-id", "valid-auth-code", "valid-state")
18+
self.assertEqual(token, "eyJhcHBfdG9rZW5faWQiOjN9:1utpZt:fXS-ifJ6TGWQEy7IkkxemDPGM5jXTOYYn7heGf8MFWU")
1919

2020
@patch("taiga.client.requests")
2121
def test_auth_not_success(self, requests):
@@ -25,7 +25,6 @@ def test_auth_not_success(self, requests):
2525
taiga.exceptions.TaigaRestException,
2626
api.auth_app,
2727
"valid-app-id",
28-
"valid-app-secret",
2928
"valid-auth-code",
3029
"valid-state",
3130
)
@@ -38,7 +37,6 @@ def test_auth_connection_error(self, requests_post):
3837
taiga.exceptions.TaigaRestException,
3938
api.auth_app,
4039
"valid-app-id",
41-
"valid-app-pass",
4240
"valid-auth-code",
4341
"valid-state",
4442
)

0 commit comments

Comments
 (0)