Skip to content

Commit 1258f7d

Browse files
Rivet 741 sharepoint username password (#448)
1 parent 011694d commit 1258f7d

File tree

8 files changed

+58
-29
lines changed

8 files changed

+58
-29
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 0.7.1
2+
3+
### Features
4+
5+
* **Add `username password` authentication to Onedrive and Sharepoint**
6+
17
## 0.7.0
28

39
### Features

requirements/connectors/onedrive.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
msal
44
Office365-REST-Python-Client
5-
bs4
5+
requests

requirements/connectors/onedrive.txt

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
# This file was autogenerated by uv via the following command:
22
# uv pip compile ./connectors/onedrive.in --output-file ./connectors/onedrive.txt --no-strip-extras --python-version 3.9
3-
beautifulsoup4==4.13.3
4-
# via bs4
5-
bs4==0.0.2
6-
# via -r ./connectors/onedrive.in
73
certifi==2025.1.31
84
# via requests
95
cffi==1.17.1
@@ -30,14 +26,11 @@ pytz==2025.1
3026
# via office365-rest-python-client
3127
requests==2.32.3
3228
# via
29+
# -r ./connectors/onedrive.in
3330
# msal
3431
# office365-rest-python-client
35-
soupsieve==2.6
36-
# via beautifulsoup4
3732
typing-extensions==4.12.2
38-
# via
39-
# beautifulsoup4
40-
# office365-rest-python-client
33+
# via office365-rest-python-client
4134
urllib3==1.26.20
4235
# via
4336
# -c ./connectors/../common/constraints.txt

requirements/connectors/sharepoint.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

33
msal
44
Office365-REST-Python-Client
5+
requests

requirements/connectors/sharepoint.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pytz==2025.1
2626
# via office365-rest-python-client
2727
requests==2.32.3
2828
# via
29+
# -r ./connectors/sharepoint.in
2930
# msal
3031
# office365-rest-python-client
3132
typing-extensions==4.12.2

unstructured_ingest/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.7.0" # pragma: no cover
1+
__version__ = "0.7.1" # pragma: no cover

unstructured_ingest/processes/connectors/onedrive.py

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,14 @@
5353

5454
class 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

5859
class 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":

unstructured_ingest/processes/connectors/sharepoint.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class SharepointDownloader(OnedriveDownloader):
100100
connector_type: str = CONNECTOR_TYPE
101101

102102
@SourceConnectionNetworkError.wrap
103-
@requires_dependencies(["office365"], extras="onedrive")
103+
@requires_dependencies(["office365"], extras="sharepoint")
104104
def _fetch_file(self, file_data: FileData) -> DriveItem:
105105
from office365.runtime.client_request_exception import ClientRequestException
106106

0 commit comments

Comments
 (0)