Skip to content

Commit bdf5ac6

Browse files
committed
PYTHON-4747 Sync auth_oidc.py to master
1 parent 6fc4611 commit bdf5ac6

File tree

2 files changed

+38
-88
lines changed

2 files changed

+38
-88
lines changed

pymongo/auth_oidc.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2024-present MongoDB, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Re-import of synchronous AuthOIDC API for compatibility."""
16+
from __future__ import annotations
17+
18+
from pymongo.auth_oidc_shared import * # noqa: F403
19+
from pymongo.synchronous.auth_oidc import * # noqa: F403
20+
from pymongo.synchronous.auth_oidc import __doc__ as original_doc
21+
22+
__doc__ = original_doc
23+
__all__ = ["OIDCCallback", "OIDCCallbackContext", "OIDCCallbackResult", "OIDCIdPInfo"] # noqa: F405

pymongo/synchronous/auth_oidc.py

Lines changed: 15 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -15,75 +15,33 @@
1515
"""MONGODB-OIDC Authentication helpers."""
1616
from __future__ import annotations
1717

18-
import abc
19-
import os
2018
import threading
2119
import time
2220
from dataclasses import dataclass, field
2321
from typing import TYPE_CHECKING, Any, Mapping, MutableMapping, Optional, Union
24-
from urllib.parse import quote
2522

2623
import bson
2724
from bson.binary import Binary
28-
from pymongo._azure_helpers import _get_azure_response
2925
from pymongo._csot import remaining
30-
from pymongo._gcp_helpers import _get_gcp_response
26+
from pymongo.auth_oidc_shared import (
27+
CALLBACK_VERSION,
28+
HUMAN_CALLBACK_TIMEOUT_SECONDS,
29+
MACHINE_CALLBACK_TIMEOUT_SECONDS,
30+
TIME_BETWEEN_CALLS_SECONDS,
31+
OIDCCallback,
32+
OIDCCallbackContext,
33+
OIDCCallbackResult,
34+
OIDCIdPInfo,
35+
_OIDCProperties,
36+
)
3137
from pymongo.errors import ConfigurationError, OperationFailure
32-
from pymongo.helpers import _AUTHENTICATION_FAILURE_CODE
38+
from pymongo.helpers_shared import _AUTHENTICATION_FAILURE_CODE
3339

3440
if TYPE_CHECKING:
35-
from pymongo.auth import MongoCredential
36-
from pymongo.pool import Connection
41+
from pymongo.auth_shared import MongoCredential
42+
from pymongo.synchronous.pool import Connection
3743

38-
39-
@dataclass
40-
class OIDCIdPInfo:
41-
issuer: str
42-
clientId: Optional[str] = field(default=None)
43-
requestScopes: Optional[list[str]] = field(default=None)
44-
45-
46-
@dataclass
47-
class OIDCCallbackContext:
48-
timeout_seconds: float
49-
username: str
50-
version: int
51-
refresh_token: Optional[str] = field(default=None)
52-
idp_info: Optional[OIDCIdPInfo] = field(default=None)
53-
54-
55-
@dataclass
56-
class OIDCCallbackResult:
57-
access_token: str
58-
expires_in_seconds: Optional[float] = field(default=None)
59-
refresh_token: Optional[str] = field(default=None)
60-
61-
62-
class OIDCCallback(abc.ABC):
63-
"""A base class for defining OIDC callbacks."""
64-
65-
@abc.abstractmethod
66-
def fetch(self, context: OIDCCallbackContext) -> OIDCCallbackResult:
67-
"""Convert the given BSON value into our own type."""
68-
69-
70-
@dataclass
71-
class _OIDCProperties:
72-
callback: Optional[OIDCCallback] = field(default=None)
73-
human_callback: Optional[OIDCCallback] = field(default=None)
74-
environment: Optional[str] = field(default=None)
75-
allowed_hosts: list[str] = field(default_factory=list)
76-
token_resource: Optional[str] = field(default=None)
77-
username: str = ""
78-
79-
80-
"""Mechanism properties for MONGODB-OIDC authentication."""
81-
82-
TOKEN_BUFFER_MINUTES = 5
83-
HUMAN_CALLBACK_TIMEOUT_SECONDS = 5 * 60
84-
CALLBACK_VERSION = 1
85-
MACHINE_CALLBACK_TIMEOUT_SECONDS = 60
86-
TIME_BETWEEN_CALLS_SECONDS = 0.1
44+
_IS_SYNC = True
8745

8846

8947
def _get_authenticator(
@@ -115,37 +73,6 @@ def _get_authenticator(
11573
return credentials.cache.data
11674

11775

118-
class _OIDCTestCallback(OIDCCallback):
119-
def fetch(self, context: OIDCCallbackContext) -> OIDCCallbackResult:
120-
token_file = os.environ.get("OIDC_TOKEN_FILE")
121-
if not token_file:
122-
raise RuntimeError(
123-
'MONGODB-OIDC with an "test" provider requires "OIDC_TOKEN_FILE" to be set'
124-
)
125-
with open(token_file) as fid:
126-
return OIDCCallbackResult(access_token=fid.read().strip())
127-
128-
129-
class _OIDCAzureCallback(OIDCCallback):
130-
def __init__(self, token_resource: str) -> None:
131-
self.token_resource = quote(token_resource)
132-
133-
def fetch(self, context: OIDCCallbackContext) -> OIDCCallbackResult:
134-
resp = _get_azure_response(self.token_resource, context.username, context.timeout_seconds)
135-
return OIDCCallbackResult(
136-
access_token=resp["access_token"], expires_in_seconds=resp["expires_in"]
137-
)
138-
139-
140-
class _OIDCGCPCallback(OIDCCallback):
141-
def __init__(self, token_resource: str) -> None:
142-
self.token_resource = quote(token_resource)
143-
144-
def fetch(self, context: OIDCCallbackContext) -> OIDCCallbackResult:
145-
resp = _get_gcp_response(self.token_resource, context.timeout_seconds)
146-
return OIDCCallbackResult(access_token=resp["access_token"])
147-
148-
14976
@dataclass
15077
class _OIDCAuthenticator:
15178
username: str

0 commit comments

Comments
 (0)