3
3
from dataclasses import asdict
4
4
from typing import Any
5
5
6
+ import httpx
6
7
from azure .core .exceptions import (
7
8
ClientAuthenticationError ,
8
9
ServiceRequestError ,
12
13
from django .db .models import Model
13
14
from django .http import HttpResponseBadRequest , HttpResponseNotFound
14
15
from kiota_abstractions .api_error import APIError
16
+ from kiota_abstractions .request_information import RequestInformation
15
17
from kiota_authentication_azure .azure_identity_authentication_provider import (
16
18
AzureIdentityAuthenticationProvider ,
17
19
)
21
23
from msgraph .graph_request_adapter import GraphRequestAdapter , options
22
24
from msgraph .graph_service_client import GraphServiceClient
23
25
from msgraph_core import GraphClientFactory
26
+ from opentelemetry import trace
24
27
25
28
from authentik .enterprise .providers .microsoft_entra .models import MicrosoftEntraProvider
26
29
from authentik .events .utils import sanitize_item
27
30
from authentik .lib .sync .outgoing import HTTP_CONFLICT
28
- from authentik .lib .sync .outgoing .base import BaseOutgoingSyncClient
31
+ from authentik .lib .sync .outgoing .base import SAFE_METHODS , BaseOutgoingSyncClient
29
32
from authentik .lib .sync .outgoing .exceptions import (
30
33
BadRequestSyncException ,
34
+ DryRunRejected ,
31
35
NotFoundSyncException ,
32
36
ObjectExistsSyncException ,
33
37
StopSync ,
34
38
TransientSyncException ,
35
39
)
36
40
37
41
38
- def get_request_adapter (
39
- credentials : ClientSecretCredential , scopes : list [str ] | None = None
40
- ) -> GraphRequestAdapter :
41
- if scopes :
42
- auth_provider = AzureIdentityAuthenticationProvider (credentials = credentials , scopes = scopes )
43
- else :
44
- auth_provider = AzureIdentityAuthenticationProvider (credentials = credentials )
42
+ class AuthentikRequestAdapter (GraphRequestAdapter ):
43
+ def __init__ (self , auth_provider , provider : MicrosoftEntraProvider , client = None ):
44
+ super ().__init__ (auth_provider , client )
45
+ self ._provider = provider
45
46
46
- return GraphRequestAdapter (
47
- auth_provider = auth_provider ,
48
- client = GraphClientFactory .create_with_default_middleware (
49
- options = options , client = KiotaClientFactory .get_default_client ()
50
- ),
51
- )
47
+ async def get_http_response_message (
48
+ self ,
49
+ request_info : RequestInformation ,
50
+ parent_span : trace .Span ,
51
+ claims : str = "" ,
52
+ ) -> httpx .Response :
53
+ if self ._provider .dry_run and request_info .http_method .value .upper () not in SAFE_METHODS :
54
+ raise DryRunRejected (
55
+ url = request_info .url ,
56
+ method = request_info .http_method .value ,
57
+ body = request_info .content .decode ("utf-8" ),
58
+ )
59
+ return await super ().get_http_response_message (request_info , parent_span , claims = claims )
52
60
53
61
54
62
class MicrosoftEntraSyncClient [TModel : Model , TConnection : Model , TSchema : dict ](
@@ -63,9 +71,27 @@ def __init__(self, provider: MicrosoftEntraProvider) -> None:
63
71
self .credentials = provider .microsoft_credentials ()
64
72
self .__prefetch_domains ()
65
73
74
+ def get_request_adapter (
75
+ self , credentials : ClientSecretCredential , scopes : list [str ] | None = None
76
+ ) -> AuthentikRequestAdapter :
77
+ if scopes :
78
+ auth_provider = AzureIdentityAuthenticationProvider (
79
+ credentials = credentials , scopes = scopes
80
+ )
81
+ else :
82
+ auth_provider = AzureIdentityAuthenticationProvider (credentials = credentials )
83
+
84
+ return AuthentikRequestAdapter (
85
+ auth_provider = auth_provider ,
86
+ provider = self .provider ,
87
+ client = GraphClientFactory .create_with_default_middleware (
88
+ options = options , client = KiotaClientFactory .get_default_client ()
89
+ ),
90
+ )
91
+
66
92
@property
67
93
def client (self ):
68
- return GraphServiceClient (request_adapter = get_request_adapter (** self .credentials ))
94
+ return GraphServiceClient (request_adapter = self . get_request_adapter (** self .credentials ))
69
95
70
96
def _request [T ](self , request : Coroutine [Any , Any , T ]) -> T :
71
97
try :
0 commit comments