Skip to content

Commit 4183de2

Browse files
author
Alejandro Casanovas
committed
if scopes are not set and there is a token stored in the backend then those will be loaded and used in case of a refresh operation
1 parent 4ec79dc commit 4183de2

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

O365/account.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ def is_authenticated(self) -> bool:
7676
token = self.con.token_backend.get_access_token(username=self.con.current_username)
7777
if token is None:
7878
# try to load the token from the backend, although it was previously loaded
79-
self.con.token_backend.load_token()
79+
if self.con.token_backend.load_token() is False:
80+
return False
8081

8182
return not self.con.token_backend.token_is_expired(username=self.con.current_username, refresh_token=True)
8283

O365/connection.py

+14
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,11 @@ def get_naive_session(self):
646646

647647
return naive_session
648648

649+
def _set_scopes_from_token(self):
650+
""" This method will set the connection scopes from the scopes set in the token stored in the token backend"""
651+
if self.scopes is None:
652+
self.scopes = self.token_backend.get_token_scopes(username=self.current_username)
653+
649654
def refresh_token(self) -> bool:
650655
"""
651656
Refresh the OAuth authorization token.
@@ -669,6 +674,15 @@ def refresh_token(self) -> bool:
669674
if should_rt is True:
670675
# The backend has checked that we can refresh the token
671676
log.debug('Refreshing access token')
677+
678+
if self.scopes is None:
679+
# This method will set the connection scopes from the scopes set in the token stored
680+
# in the token backend
681+
self.scopes = self.token_backend.get_token_scopes(
682+
username=self.current_username,
683+
remove_reserved=True
684+
)
685+
672686
result = self.msal_client.acquire_token_silent_with_error(
673687
scopes=self.scopes,
674688
account=self.msal_client.get_accounts(username=self.current_username)[0]

O365/utils/token.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
log = logging.getLogger(__name__)
1212

1313

14+
RESERVED_SCOPES = {'profile', 'openid', 'offline_access'}
15+
16+
1417
class CryptographyManagerType(Protocol):
1518
def encrypt(self, data: str) -> bytes: ...
1619
def decrypt(self, data: bytes) -> str: ...
@@ -136,15 +139,21 @@ def get_refresh_token(self, *, username: Optional[str] = None) -> Optional[dict]
136139
))
137140
return results[0] if results else None
138141

139-
def get_token_scopes(self, *, username: Optional[str] = None) -> Optional[list]:
142+
def get_token_scopes(self, *, username: Optional[str] = None,
143+
remove_reserved: bool = False) -> Optional[list]:
140144
"""
141145
Retrieve the scopes the access token has permissions on
142146
:param str username: The username from which retrieve the refresh token
147+
:param bool remove_reserved: if True RESERVED_SCOPES will be removed from the list
143148
"""
144149
access_token = self.get_access_token(username=username)
145150
if access_token:
146151
scopes_str = access_token.get('target')
147-
return scopes_str.split(' ') if scopes_str else None
152+
if scopes_str:
153+
scopes = scopes_str.split(' ')
154+
if remove_reserved:
155+
scopes = [scope for scope in scopes if scope not in RESERVED_SCOPES]
156+
return scopes
148157
return None
149158

150159
def add(self, event, **kwargs) -> None:

0 commit comments

Comments
 (0)