|
38 | 38 | from pymongo._azure_helpers import _get_azure_response
|
39 | 39 | from pymongo._gcp_helpers import _get_gcp_response
|
40 | 40 | from pymongo.auth_oidc_shared import _get_k8s_token
|
| 41 | +from pymongo.auth_shared import _build_credentials_tuple |
41 | 42 | from pymongo.cursor_shared import CursorType
|
42 | 43 | from pymongo.errors import AutoReconnect, ConfigurationError, OperationFailure
|
43 | 44 | from pymongo.hello import HelloCompat
|
44 | 45 | from pymongo.operations import InsertOne
|
45 |
| -from pymongo.synchronous.auth_oidc import OIDCCallback, OIDCCallbackContext, OIDCCallbackResult |
| 46 | +from pymongo.synchronous.auth_oidc import ( |
| 47 | + OIDCCallback, |
| 48 | + OIDCCallbackContext, |
| 49 | + OIDCCallbackResult, |
| 50 | + _get_authenticator, |
| 51 | +) |
46 | 52 | from pymongo.uri_parser import parse_uri
|
47 | 53 |
|
48 | 54 | ROOT = Path(__file__).parent.parent.resolve()
|
@@ -103,7 +109,6 @@ def fail_point(self, command_args):
|
103 | 109 | client.close()
|
104 | 110 |
|
105 | 111 |
|
106 |
| -@pytest.mark.auth_oidc |
107 | 112 | class TestAuthOIDCHuman(OIDCTestBase):
|
108 | 113 | uri: str
|
109 | 114 |
|
@@ -838,12 +843,35 @@ def test_2_4_invalid_client_configuration_with_callback(self):
|
838 | 843 | self.create_client(authmechanismproperties=props)
|
839 | 844 |
|
840 | 845 | def test_2_5_invalid_use_of_ALLOWED_HOSTS(self):
|
841 |
| - # Create an OIDC configured client with auth mechanism properties `{"ENVIRONMENT": "azure", "ALLOWED_HOSTS": []}`. |
842 |
| - props: Dict = {"ENVIRONMENT": "azure", "ALLOWED_HOSTS": []} |
| 846 | + # Create an OIDC configured client with auth mechanism properties `{"ENVIRONMENT": "test", "ALLOWED_HOSTS": []}`. |
| 847 | + props: Dict = {"ENVIRONMENT": "test", "ALLOWED_HOSTS": []} |
843 | 848 | # Assert it returns a client configuration error.
|
844 | 849 | with self.assertRaises(ConfigurationError):
|
845 | 850 | self.create_client(authmechanismproperties=props)
|
846 | 851 |
|
| 852 | + # Create an OIDC configured client with auth mechanism properties `{"OIDC_CALLBACK": "<my_callback>", "ALLOWED_HOSTS": []}`. |
| 853 | + props: Dict = {"OIDC_CALLBACK": self.create_request_cb(), "ALLOWED_HOSTS": []} |
| 854 | + # Assert it returns a client configuration error. |
| 855 | + with self.assertRaises(ConfigurationError): |
| 856 | + self.create_client(authmechanismproperties=props) |
| 857 | + |
| 858 | + def test_2_6_ALLOWED_HOSTS_defaults_ignored(self): |
| 859 | + # Create a MongoCredential for OIDC with a machine callback. |
| 860 | + props = {"OIDC_CALLBACK": self.create_request_cb()} |
| 861 | + extra = dict(authmechanismproperties=props) |
| 862 | + mongo_creds = _build_credentials_tuple("MONGODB-OIDC", None, "foo", None, extra, "test") |
| 863 | + # Assert that creating an authenticator for example.com does not result in an error. |
| 864 | + authenticator = _get_authenticator(mongo_creds, ("example.com", 30)) |
| 865 | + assert authenticator.properties.username == "foo" |
| 866 | + |
| 867 | + # Create a MongoCredential for OIDC with an ENVIRONMENT. |
| 868 | + props = {"ENVIRONMENT": "test"} |
| 869 | + extra = dict(authmechanismproperties=props) |
| 870 | + mongo_creds = _build_credentials_tuple("MONGODB-OIDC", None, None, None, extra, "test") |
| 871 | + # Assert that creating an authenticator for example.com does not result in an error. |
| 872 | + authenticator = _get_authenticator(mongo_creds, ("example.com", 30)) |
| 873 | + assert authenticator.properties.username == "" |
| 874 | + |
847 | 875 | def test_3_1_authentication_failure_with_cached_tokens_fetch_a_new_token_and_retry(self):
|
848 | 876 | # Create a MongoClient and an OIDC callback that implements the provider logic.
|
849 | 877 | client = self.create_client()
|
@@ -909,7 +937,7 @@ def test_3_3_unexpected_error_code_does_not_clear_cache(self):
|
909 | 937 | # Assert that the callback has been called once.
|
910 | 938 | self.assertEqual(self.request_called, 1)
|
911 | 939 |
|
912 |
| - def test_4_1_reauthentication_succeds(self): |
| 940 | + def test_4_1_reauthentication_succeeds(self): |
913 | 941 | # Create a ``MongoClient`` configured with a custom OIDC callback that
|
914 | 942 | # implements the provider logic.
|
915 | 943 | client = self.create_client()
|
|
0 commit comments