Skip to content
This repository was archived by the owner on Oct 2, 2024. It is now read-only.

Commit 80eee9f

Browse files
committed
Fully support auth server customization
1 parent 3f56483 commit 80eee9f

File tree

2 files changed

+80
-4
lines changed

2 files changed

+80
-4
lines changed

src/onedrivesdk/auth_provider.py

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ class AuthProvider(AuthProviderBase):
3939
AUTH_SERVER_URL = "https://login.live.com/oauth20_authorize.srf"
4040
AUTH_TOKEN_URL = "https://login.live.com/oauth20_token.srf"
4141

42-
def __init__(self, http_provider, client_id=None, scopes=None, access_token=None, session_type=None, loop=None):
42+
def __init__(self, http_provider, client_id=None, scopes=None, access_token=None, session_type=None, loop=None,
43+
auth_server_url=None, auth_token_url=None):
4344
"""Initialize the authentication provider for authenticating
4445
requests sent to OneDrive
4546
@@ -63,13 +64,19 @@ def __init__(self, http_provider, client_id=None, scopes=None, access_token=None
6364
loop to use for all async requests. If none is provided,
6465
asyncio.get_event_loop() will be called. If using Python
6566
3.3 or below this does not need to be specified
67+
auth_server_url (str): URL where OAuth authentication can be performed. If
68+
None, defaults to OAuth for Microsoft Account.
69+
auth_token_url (str): URL where OAuth token can be redeemed. If None,
70+
defaults to OAuth for Microsoft Account.
6671
"""
6772
self._http_provider = http_provider
6873
self._client_id = client_id
6974
self._scopes = scopes
7075
self._access_token = access_token
7176
self._session_type = Session if session_type is None else session_type
7277
self._session = None
78+
self._auth_server_url = self.AUTH_SERVER_URL if auth_server_url is None else auth_server_url
79+
self._auth_token_url = self.AUTH_TOKEN_URL if auth_token_url is None else auth_token_url
7380

7481
if sys.version_info >= (3, 4, 0):
7582
import asyncio
@@ -117,6 +124,34 @@ def access_token(self):
117124
def access_token(self, value):
118125
self._access_token = value
119126

127+
@property
128+
def auth_server_url(self):
129+
"""Gets and sets the authorization server url for the
130+
AuthProvider
131+
132+
Returns:
133+
str: Auth server url
134+
"""
135+
return self._auth_server_url
136+
137+
@auth_server_url.setter
138+
def auth_server_url(self, value):
139+
self._auth_server_url = value
140+
141+
@property
142+
def auth_token_url(self):
143+
"""Gets and sets the authorization token url for the
144+
AuthProvider
145+
146+
Returns:
147+
str: The auth token url
148+
"""
149+
return self._auth_token_url
150+
151+
@auth_token_url.setter
152+
def auth_token_url(self, value):
153+
self._auth_token_url = value
154+
120155
def get_auth_url(self, redirect_uri):
121156
"""Build the auth url using the params provided
122157
and the auth_provider
@@ -132,7 +167,7 @@ def get_auth_url(self, redirect_uri):
132167
"response_type": "code",
133168
"redirect_uri": redirect_uri
134169
}
135-
return "{}?{}".format(self.AUTH_SERVER_URL, urlencode(params))
170+
return "{}?{}".format(self._auth_server_url, urlencode(params))
136171

137172
def authenticate(self, code, redirect_uri, client_secret=None, resource=None):
138173
"""Takes in a code string, gets the access token,
@@ -165,7 +200,7 @@ def authenticate(self, code, redirect_uri, client_secret=None, resource=None):
165200
headers = {"Content-Type": "application/x-www-form-urlencoded"}
166201
response = self._http_provider.send(method="POST",
167202
headers=headers,
168-
url=self.AUTH_TOKEN_URL,
203+
url=self._auth_token_url,
169204
data=params)
170205

171206
rcont = json.loads(response.content)
@@ -174,7 +209,7 @@ def authenticate(self, code, redirect_uri, client_secret=None, resource=None):
174209
rcont["scope"],
175210
rcont["access_token"],
176211
self.client_id,
177-
self.AUTH_TOKEN_URL,
212+
self._auth_token_url,
178213
redirect_uri,
179214
rcont["refresh_token"] if "refresh_token" in rcont else None,
180215
client_secret)

src/onedrivesdk/extensions/onedrivesdk_helper.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@
44

55

66
def get_default_client(client_id, scopes):
7+
"""Deprecated. Proxy of :method:`get_consumer_client()`.
8+
Get a client using the default HttpProvider and
9+
AuthProvider classes.
10+
11+
Args:
12+
client_id (str): The client id for your application
13+
scopes (list of str): The scopes required for your
14+
application
15+
16+
Returns:
17+
:class:`OneDriveClient<onedrivesdk.requests.one_drive_client.OneDriveClient>`:
18+
A OneDriveClient for making OneDrive requests.
19+
"""
20+
return get_consumer_client(client_id, scopes)
21+
22+
def get_consumer_client(client_id, scopes):
723
"""Get a client using the default HttpProvider and
824
AuthProvider classes
925
@@ -23,3 +39,28 @@ def get_default_client(client_id, scopes):
2339
return OneDriveClient("https://api.onedrive.com/v1.0/",
2440
auth_provider,
2541
http_provider)
42+
43+
44+
def get_business_client(client_id, scopes, base_url):
45+
"""Get a client using the default HttpProvider and
46+
AuthProvider classes
47+
48+
Args:
49+
client_id (str): The client id for your application
50+
scopes (list of str): The scopes required for your
51+
application
52+
base_url (str): Base URL of OneDrive for Business tenant.
53+
For example, "https://my-sharepoint.contoso.com/v1.0/"
54+
55+
Returns:
56+
:class:`OneDriveClient<onedrivesdk.requests.one_drive_client.OneDriveClient>`:
57+
A OneDriveClient for making OneDrive requests.
58+
"""
59+
http_provider = HttpProvider()
60+
auth_provider = AuthProvider(http_provider=http_provider,
61+
client_id=client_id,
62+
scopes=scopes)
63+
return OneDriveClient(base_url,
64+
auth_provider,
65+
http_provider)
66+

0 commit comments

Comments
 (0)