5353
5454class OnedriveAccessConfig (AccessConfig ):
5555 client_cred : str = Field (description = "Microsoft App client secret" )
56+ password : Optional [str ] = Field (description = "Service account password" , default = None )
5657
5758
5859class OnedriveConnectionConfig (ConnectionConfig ):
5960 client_id : str = Field (description = "Microsoft app client ID" )
60- user_pname : str = Field (description = "User principal name, usually is your Azure AD email." )
61+ user_pname : str = Field (
62+ description = "User principal name or service account, usually your Azure AD email."
63+ )
6164 tenant : str = Field (
6265 repr = False , description = "ID or domain name associated with your Azure AD instance"
6366 )
@@ -74,25 +77,50 @@ def get_drive(self) -> "Drive":
7477 drive = client .users [self .user_pname ].drive
7578 return drive
7679
77- @requires_dependencies (["msal" ], extras = "onedrive" )
80+ @requires_dependencies (["msal" , "requests" ], extras = "onedrive" )
7881 def get_token (self ):
7982 from msal import ConfidentialClientApplication
83+ from requests import post
84+
85+ if self .access_config .get_secret_value ().password :
86+ url = f"https://login.microsoftonline.com/{ self .tenant } /oauth2/v2.0/token"
87+ headers = {"Content-Type" : "application/x-www-form-urlencoded" }
88+ data = {
89+ "grant_type" : "password" ,
90+ "username" : self .user_pname ,
91+ "password" : self .access_config .get_secret_value ().password ,
92+ "client_id" : self .client_id ,
93+ "client_secret" : self .access_config .get_secret_value ().client_cred ,
94+ "scope" : "https://graph.microsoft.com/.default" ,
95+ }
96+ response = post (url , headers = headers , data = data )
97+ if response .status_code == 200 :
98+ return response .json ()
99+ else :
100+ raise SourceConnectionError (
101+ f"Oauth2 authentication failed with { response .status_code } : { response .text } "
102+ )
80103
81- try :
82- app = ConfidentialClientApplication (
83- authority = f"{ self .authority_url } /{ self .tenant } " ,
84- client_id = self .client_id ,
85- client_credential = self .access_config .get_secret_value ().client_cred ,
86- )
87- token = app .acquire_token_for_client (scopes = ["https://graph.microsoft.com/.default" ])
88- except ValueError as exc :
89- logger .error ("Couldn't set up credentials for OneDrive" )
90- raise exc
91- if "error" in token :
92- raise SourceConnectionNetworkError (
93- "failed to fetch token, {}: {}" .format (token ["error" ], token ["error_description" ])
94- )
95- return token
104+ else :
105+ try :
106+ app = ConfidentialClientApplication (
107+ authority = f"{ self .authority_url } /{ self .tenant } " ,
108+ client_id = self .client_id ,
109+ client_credential = self .access_config .get_secret_value ().client_cred ,
110+ )
111+ token = app .acquire_token_for_client (
112+ scopes = ["https://graph.microsoft.com/.default" ]
113+ )
114+ except ValueError as exc :
115+ logger .error ("Couldn't set up credentials." )
116+ raise exc
117+ if "error" in token :
118+ raise SourceConnectionNetworkError (
119+ "failed to fetch token, {}: {}" .format (
120+ token ["error" ], token ["error_description" ]
121+ )
122+ )
123+ return token
96124
97125 @requires_dependencies (["office365" ], extras = "onedrive" )
98126 def get_client (self ) -> "GraphClient" :
0 commit comments