Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pymongo/asynchronous/auth_oidc.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@
def _get_authenticator(
credentials: MongoCredential, address: tuple[str, int]
) -> _OIDCAuthenticator:
if credentials.cache.data:
return credentials.cache.data

# Extract values.
principal_name = credentials.username
properties = credentials.mechanism_properties
Expand All @@ -70,6 +67,9 @@ def _get_authenticator(
f"Refusing to connect to {address[0]}, which is not in authOIDCAllowedHosts: {allowed_hosts}"
)

if credentials.cache.data:
return credentials.cache.data

# Get or create the cache data.
credentials.cache.data = _OIDCAuthenticator(username=principal_name, properties=properties)
return credentials.cache.data
Expand Down
6 changes: 3 additions & 3 deletions pymongo/synchronous/auth_oidc.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@
def _get_authenticator(
credentials: MongoCredential, address: tuple[str, int]
) -> _OIDCAuthenticator:
if credentials.cache.data:
return credentials.cache.data

# Extract values.
principal_name = credentials.username
properties = credentials.mechanism_properties
Expand All @@ -70,6 +67,9 @@ def _get_authenticator(
f"Refusing to connect to {address[0]}, which is not in authOIDCAllowedHosts: {allowed_hosts}"
)

if credentials.cache.data:
return credentials.cache.data

# Get or create the cache data.
credentials.cache.data = _OIDCAuthenticator(username=principal_name, properties=properties)
return credentials.cache.data
Expand Down
20 changes: 20 additions & 0 deletions test/asynchronous/test_auth_oidc.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,26 @@ async def fail_point(self, command_args):
await client.close()


class TestOIDCAllowedHostsCache(unittest.TestCase):
class HumanCallback(OIDCCallback):
def fetch(self, context):
return OIDCCallbackResult(access_token="token")

def test_allowed_hosts_checked_before_cached_authenticator_reuse(self):
props = {
"OIDC_HUMAN_CALLBACK": self.HumanCallback(),
"ALLOWED_HOSTS": ["good.example.com"],
}
extra = {"authmechanismproperties": props}
credentials = _build_credentials_tuple("MONGODB-OIDC", None, "user", None, extra, "test")

authenticator = _get_authenticator(credentials, ("good.example.com", 27017))
self.assertIs(authenticator, credentials.cache.data)

with self.assertRaisesRegex(ConfigurationError, "evil.example.com"):
_get_authenticator(credentials, ("evil.example.com", 27017))


class TestAuthOIDCHuman(OIDCTestBase):
uri: str

Expand Down
20 changes: 20 additions & 0 deletions test/test_auth_oidc.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,26 @@ def fail_point(self, command_args):
client.close()


class TestOIDCAllowedHostsCache(unittest.TestCase):
class HumanCallback(OIDCCallback):
def fetch(self, context):
return OIDCCallbackResult(access_token="token")

def test_allowed_hosts_checked_before_cached_authenticator_reuse(self):
props = {
"OIDC_HUMAN_CALLBACK": self.HumanCallback(),
"ALLOWED_HOSTS": ["good.example.com"],
}
extra = {"authmechanismproperties": props}
credentials = _build_credentials_tuple("MONGODB-OIDC", None, "user", None, extra, "test")

authenticator = _get_authenticator(credentials, ("good.example.com", 27017))
self.assertIs(authenticator, credentials.cache.data)

with self.assertRaisesRegex(ConfigurationError, "evil.example.com"):
_get_authenticator(credentials, ("evil.example.com", 27017))


class TestAuthOIDCHuman(OIDCTestBase):
uri: str

Expand Down
Loading