conftest.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import json
  2. import time
  3. import urllib.request
  4. from io import BytesIO
  5. from unittest import mock
  6. import jwt
  7. import pytest
  8. from clean_python.oauth2 import TokenVerifierSettings
  9. @pytest.fixture
  10. def private_key():
  11. # this key was generated especially for this test suite; it has no other applications
  12. return {
  13. "p": "_PgJBxrGEy8I5KvY_nDRT9loaBPqHHn0AUiTa92zBrAX0qA8ZhV66pUkX2JehU3efduel4FOK2xx-W31p7kCLoaGsMtfKAPYC33KptCH9YXkeMQHq1jWfcRgAVXpdXc7M4pQxO8Dh2BU8qhtAzhpbP4tUPoLIGcTUGd-1ieDkqE", # NOQA
  14. "kty": "RSA",
  15. "q": "hT0USPCNN4o2PauND53ubh2G5uOHzY9mfPuEXZ1fiRihCe5Bng0K8Pzx5QpSAjUY2-FhHa8jK8ITERmwT3MQKJpmlm_1R8GnaNVPOj8BpAhDlMzgkVikEGj0Pd7x_wdSko7KscyG-ZVsMw_KiCZpC6hMiI60w9GG14MtXhRVWhM", # NOQA
  16. "d": "BNwTHorPcAMiDglxt5Ylz1jqQ67rYcnA0okvZxz0QPbLovuTM1WIaPIeGlqXNzB9NxXtZhHXtnhoSwPf2LxMmYWWgJLqhPQWRlqZhLhww0nGGUgk_b1gNnMQuuh2weLfPNUksddhDJHzW1pBiDQrhP0t064Pz_P8WtGUkBka5-Pb3pItaF_w4xDIhhTJS48kv5H-BrwK8Vlz-EofkmPgxXBvCwhVoXZihxEUVzc6X59e1UiymXr-3lbNeL-76Yb9JHJFjXh2o52v5eZDVT6ir-iUp7bBXTiZsFaBCUCfCjx3MiQkHNBNEV7Cr9DKvfGdK3r9IbkSAC1tiD4Y1oyZwQ", # NOQA
  17. "e": "AQAB",
  18. "use": "sig",
  19. "kid": "_Lfex-skFCKBZd0xMN5dZSAX7uoG6LMx3i2qHReqU0c",
  20. "qi": "GNhYuNdxd4NyRhzreW72PWXzj2oIkm0rIHrcNW9bpqK1fxrsbiVUEVUly-cqpD_-AjFOyCWcKWQxHG7J8LeP2vW3_U4TLx_jKD9cc7S65gb37El1ihOwNWbapRxToOhP2sZa0g3y9P-M_8hQcfKr1OFMQMnD9wj-sVNw9yJf3I4", # NOQA
  21. "dp": "xTs6BrEISEK-w1N9Dvy1JXWToroMKQGojiugzVQAVjGLkWvfS5RpzmZUAo52taZ911EZOHTXlqGpx1jFVGy5176JW2RlH5THqEX-b8tchcBL3yCv_hd4vHwUglYSfMRmgwvPZ4wXC0C_WqaYwA8Gm7UdbepWLIBRHbpjuOL8AaE", # NOQA
  22. "alg": "RS256",
  23. "dq": "C4_UTcwKBRLKSCm10PAce5O2XBzMcQsLkrbkspbwbl4jw0_Yg9WP6H-aogx2N1jSMmppWgETpT1vGCHJietrMIrNcip-914Xn-I6wMws4UYSTzxEFHjDq-TfpOrOxxmkkbEwZ6Ne5xOPUxMAuTXUEb3l_keb6g4pjFQGwM405d8", # NOQA
  24. "n": "g6k31kvFdTaCSxXhazC5JaVekYi836F0H_YLrDioQlwiegsGjUDYk5TM7z8iXwDIm0QZZgtoEBlEny8vXrt1WGMO8GGwnVNq0_ZAD3JYp-a_c0X7VM7I2Dze32zcy8mC4QhPedEbMVDzi1XrusGjNHWObkMKsLZ7RRlwdkgR4nRpzncou_2ZJLvc50C8tjd3juCpUMWXNsvDjoAenxoXs68SDK4h9QSjvaWaSHNRGYiYkGUvcL5rv3htbrHIUVAcBC9r0j5Ued1hBR9ND1KPxVJWnn8oRAxFrYIcQdaDFWnWdb5BY9pJQls9fHlt0PF9vXUm-GufWk0U8D4Lc8V78w", # NOQA
  25. }
  26. @pytest.fixture
  27. def public_key(private_key):
  28. keys = ("alg", "e", "kid", "kty", "n", "use")
  29. return {k: private_key[k] for k in keys}
  30. @pytest.fixture
  31. def jwk_patched(public_key):
  32. with mock.patch.object(urllib.request, "urlopen") as urlopen:
  33. urlopen.return_value.__enter__.return_value = BytesIO(
  34. json.dumps({"keys": [public_key]}).encode()
  35. )
  36. yield urlopen
  37. @pytest.fixture
  38. def token_generator(private_key):
  39. default_claims = {
  40. "sub": "foo",
  41. "username": "piet",
  42. "iss": "https://some/auth/server",
  43. "scope": "user",
  44. "token_use": "access",
  45. "exp": int(time.time()) + 3600,
  46. "iat": int(time.time()) - 3600,
  47. }
  48. def generate_token(**claim_overrides):
  49. claims = {**default_claims, **claim_overrides}
  50. claims = {k: v for (k, v) in claims.items() if v is not None}
  51. return jwt.encode(
  52. claims,
  53. key=jwt.PyJWK.from_dict(private_key).key,
  54. algorithm=private_key["alg"],
  55. headers={"kid": private_key["kid"]},
  56. )
  57. return generate_token
  58. @pytest.fixture
  59. def settings():
  60. # settings match the defaults in the token_generator fixture
  61. return TokenVerifierSettings(
  62. issuer="https://some/auth/server",
  63. scope="user",
  64. algorithms=["RS256"],
  65. admin_users=["foo"],
  66. )