|
| 1 | +from shopify import session_token |
| 2 | +from test.test_helper import TestCase |
| 3 | +from datetime import datetime, timedelta |
| 4 | + |
| 5 | +import jwt |
| 6 | +import sys |
| 7 | + |
| 8 | +if sys.version_info[0] < 3: # Backwards compatibility for python < v3.0.0 |
| 9 | + import time |
| 10 | + |
| 11 | + |
| 12 | +def timestamp(date): |
| 13 | + return time.mktime(date.timetuple()) if sys.version_info[0] < 3 else date.timestamp() |
| 14 | + |
| 15 | + |
| 16 | +class TestSessionTokenGetDecodedSessionToken(TestCase): |
| 17 | + @classmethod |
| 18 | + def setUpClass(self): |
| 19 | + self.secret = "API Secret" |
| 20 | + self.api_key = "API key" |
| 21 | + |
| 22 | + @classmethod |
| 23 | + def setUp(self): |
| 24 | + current_time = datetime.now() |
| 25 | + self.payload = { |
| 26 | + "iss": "https://test-shop.myshopify.com/admin", |
| 27 | + "dest": "https://test-shop.myshopify.com", |
| 28 | + "aud": self.api_key, |
| 29 | + "sub": "1", |
| 30 | + "exp": timestamp((current_time + timedelta(0, 60))), |
| 31 | + "nbf": timestamp(current_time), |
| 32 | + "iat": timestamp(current_time), |
| 33 | + "jti": "4321", |
| 34 | + "sid": "abc123", |
| 35 | + } |
| 36 | + |
| 37 | + @classmethod |
| 38 | + def build_auth_header(self): |
| 39 | + mock_session_token = jwt.encode(self.payload, self.secret, algorithm="HS256") |
| 40 | + return "Bearer {session_token}".format(session_token=mock_session_token) |
| 41 | + |
| 42 | + def test_raises_if_token_authentication_header_is_not_bearer(self): |
| 43 | + authorization_header = "Bad auth header" |
| 44 | + |
| 45 | + with self.assertRaises(session_token.TokenAuthenticationError) as cm: |
| 46 | + session_token.decode_from_header(authorization_header, api_key=self.api_key, secret=self.secret) |
| 47 | + |
| 48 | + self.assertEqual("The HTTP_AUTHORIZATION_HEADER provided does not contain a Bearer token", str(cm.exception)) |
| 49 | + |
| 50 | + def test_raises_jwt_error_if_session_token_is_expired(self): |
| 51 | + self.payload["exp"] = timestamp((datetime.now() + timedelta(0, -10))) |
| 52 | + |
| 53 | + with self.assertRaises(session_token.SessionTokenError) as cm: |
| 54 | + session_token.decode_from_header(self.build_auth_header(), api_key=self.api_key, secret=self.secret) |
| 55 | + |
| 56 | + self.assertEqual("Signature has expired", str(cm.exception)) |
| 57 | + |
| 58 | + def test_raises_jwt_error_if_invalid_alg(self): |
| 59 | + bad_session_token = jwt.encode(self.payload, None, algorithm="none") |
| 60 | + invalid_header = "Bearer {session_token}".format(session_token=bad_session_token) |
| 61 | + |
| 62 | + with self.assertRaises(session_token.SessionTokenError) as cm: |
| 63 | + session_token.decode_from_header(invalid_header, api_key=self.api_key, secret=self.secret) |
| 64 | + |
| 65 | + self.assertEqual("The specified alg value is not allowed", str(cm.exception)) |
| 66 | + |
| 67 | + def test_raises_jwt_error_if_invalid_signature(self): |
| 68 | + bad_session_token = jwt.encode(self.payload, "bad_secret", algorithm="HS256") |
| 69 | + invalid_header = "Bearer {session_token}".format(session_token=bad_session_token) |
| 70 | + |
| 71 | + with self.assertRaises(session_token.SessionTokenError) as cm: |
| 72 | + session_token.decode_from_header(invalid_header, api_key=self.api_key, secret=self.secret) |
| 73 | + |
| 74 | + self.assertEqual("Signature verification failed", str(cm.exception)) |
| 75 | + |
| 76 | + def test_raises_if_aud_doesnt_match_api_key(self): |
| 77 | + self.payload["aud"] = "bad audience" |
| 78 | + |
| 79 | + with self.assertRaises(session_token.SessionTokenError) as cm: |
| 80 | + session_token.decode_from_header(self.build_auth_header(), api_key=self.api_key, secret=self.secret) |
| 81 | + |
| 82 | + self.assertEqual("Invalid audience", str(cm.exception)) |
| 83 | + |
| 84 | + def test_raises_if_issuer_hostname_is_invalid(self): |
| 85 | + self.payload["iss"] = "bad_shop_hostname" |
| 86 | + |
| 87 | + with self.assertRaises(session_token.InvalidIssuerError) as cm: |
| 88 | + session_token.decode_from_header(self.build_auth_header(), api_key=self.api_key, secret=self.secret) |
| 89 | + |
| 90 | + self.assertEqual("Invalid issuer", str(cm.exception)) |
| 91 | + |
| 92 | + def test_raises_if_iss_and_dest_dont_match(self): |
| 93 | + self.payload["dest"] = "bad_shop.myshopify.com" |
| 94 | + |
| 95 | + with self.assertRaises(session_token.MismatchedHostsError) as cm: |
| 96 | + session_token.decode_from_header(self.build_auth_header(), api_key=self.api_key, secret=self.secret) |
| 97 | + |
| 98 | + self.assertEqual("The issuer and destination do not match", str(cm.exception)) |
| 99 | + |
| 100 | + def test_returns_decoded_payload(self): |
| 101 | + decoded_payload = session_token.decode_from_header( |
| 102 | + self.build_auth_header(), api_key=self.api_key, secret=self.secret |
| 103 | + ) |
| 104 | + |
| 105 | + self.assertEqual(self.payload, decoded_payload) |
0 commit comments