Skip to content

Commit 5af0cab

Browse files
committed
Allow a dynamic consent process using functions
This paves the way for more elegant alternatives for attaining a token using a consent URL. Example implementations would not be limited to using a web browser, a pyperclip copy, or a web browser combined with a custom redirect_uri and a temporary HTTP server on localhost. The default and current consent process function is O365.utils.consent.consent_input_token.
1 parent 6df9fb1 commit 5af0cab

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

O365/account.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from .connection import Connection, Protocol, MSGraphProtocol, MSOffice365Protocol
2-
from .utils import ME_RESOURCE
2+
from .utils import ME_RESOURCE, consent
33

44

55
class Account:
@@ -69,7 +69,7 @@ def is_authenticated(self):
6969

7070
return token is not None and not token.is_expired
7171

72-
def authenticate(self, *, scopes=None, **kwargs):
72+
def authenticate(self, *, scopes=None, handle_consent=consent.consent_input_token, **kwargs):
7373
""" Performs the oauth authentication flow using the console resulting in a stored token.
7474
It uses the credentials passed on instantiation
7575
@@ -92,10 +92,7 @@ def authenticate(self, *, scopes=None, **kwargs):
9292

9393
consent_url, _ = self.con.get_authorization_url(**kwargs)
9494

95-
print('Visit the following url to give consent:')
96-
print(consent_url)
97-
98-
token_url = input('Paste the authenticated url here:\n')
95+
token_url = handle_consent(consent_url)
9996

10097
if token_url:
10198
result = self.con.request_token(token_url, **kwargs) # no need to pass state as the session is the same

O365/utils/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
from .utils import OneDriveWellKnowFolderNames, Pagination, Query
77
from .token import BaseTokenBackend, Token, FileSystemTokenBackend, FirestoreBackend, AWSS3Backend, AWSSecretsBackend
88
from .windows_tz import IANA_TO_WIN, WIN_TO_IANA
9+
from . import consent

O365/utils/consent.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def consent_input_token(consent_url):
2+
print('Visit the following url to give consent:')
3+
print(consent_url)
4+
5+
return input('Paste the authenticated url here:\n')

0 commit comments

Comments
 (0)