Skip to content

Commit 6efccb0

Browse files
committed
Demote Device Flow into subclass PublicClientApplication, for now
1 parent 16278c8 commit 6efccb0

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

msal/application.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,15 @@ def acquire_token_silent(
276276
logger.debug(
277277
"Refresh failed. {error}: {error_description}".format(**response))
278278

279+
280+
class PublicClientApplication(ClientApplication): # browser app or mobile app
281+
282+
def __init__(self, client_id, client_credential=None, **kwargs):
283+
if client_credential is not None:
284+
raise ValueError("Public Client should not possess credentials")
285+
super(PublicClientApplication, self).__init__(
286+
client_id, client_credential=None, **kwargs)
287+
279288
def initiate_device_flow(self, scopes=None, **kwargs):
280289
return self.client.initiate_device_flow(
281290
scope=decorate_scope(scopes or [], self.client_id),
@@ -297,8 +306,6 @@ def acquire_token_by_device_flow(self, flow, **kwargs):
297306
# service seemingly need both device_code and code parameter.
298307
**kwargs)
299308

300-
class PublicClientApplication(ClientApplication): # browser app or mobile app
301-
302309
def acquire_token_by_username_password(
303310
self, username, password, scopes=None, **kwargs):
304311
"""Gets a token for a given resource via user credentails."""

tests/test_application.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,24 @@ def test_username_password(self):
105105
self.assertLoosely(result)
106106
self.assertCacheWorks(result)
107107

108+
def test_device_flow(self):
109+
self.app = PublicClientApplication(
110+
CONFIG["client_id"], authority=CONFIG["authority"])
111+
flow = self.app.initiate_device_flow(scopes=CONFIG.get("scope"))
112+
logging.warn(flow["message"])
113+
114+
duration = 30
115+
logging.warn("We will wait up to %d seconds for you to sign in" % duration)
116+
flow["expires_at"] = time.time() + duration # Shorten the time for quick test
117+
result = self.app.acquire_token_by_device_flow(flow)
118+
self.assertLoosely(
119+
result,
120+
assertion=lambda: self.assertIn('access_token', result),
121+
skippable_errors=self.app.client.DEVICE_FLOW_RETRIABLE_ERRORS)
122+
123+
if "access_token" in result:
124+
self.assertCacheWorks(result)
125+
108126

109127
@unittest.skipUnless("client_id" in CONFIG, "client_id missing")
110128
class TestClientApplication(Oauth2TestCase):
@@ -136,19 +154,3 @@ def test_auth_code(self):
136154
error_description=result.get("error_description")))
137155
self.assertCacheWorks(result)
138156

139-
def test_device_flow(self):
140-
flow = self.app.initiate_device_flow(scopes=CONFIG.get("scope"))
141-
logging.warn(flow["message"])
142-
143-
duration = 30
144-
logging.warn("We will wait up to %d seconds for you to sign in" % duration)
145-
flow["expires_at"] = time.time() + duration # Shorten the time for quick test
146-
result = self.app.acquire_token_by_device_flow(flow)
147-
self.assertLoosely(
148-
result,
149-
assertion=lambda: self.assertIn('access_token', result),
150-
skippable_errors=self.app.client.DEVICE_FLOW_RETRIABLE_ERRORS)
151-
152-
if "access_token" in result:
153-
self.assertCacheWorks(result)
154-

0 commit comments

Comments
 (0)