Skip to content

Commit 17079f0

Browse files
committed
Refactoring
1 parent c2edeb6 commit 17079f0

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

msal/application.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -858,11 +858,9 @@ def acquire_token_interactive(self,
858858
domain_hint=domain_hint,
859859
claims_challenge=claims_challenge,
860860
)
861-
auth_code, state = obtain_auth_code(_port, auth_uri=auth_url)
861+
auth_code = obtain_auth_code(_port, auth_uri=auth_url, request_state=request_state)
862862
if not auth_code:
863863
raise TimeoutError("Server timed out")
864-
if request_state != state:
865-
return ValueError("State does not match")
866864
return self.acquire_token_by_authorization_code(
867865
auth_code, scopes, redirect_uri=redirect_uri,
868866
claims_challenge=claims_challenge)

msal/oauth2cli/authcode.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
logger = logging.getLogger(__name__)
2525

26-
def obtain_auth_code(listen_port, auth_uri=None):
26+
def obtain_auth_code(listen_port, auth_uri=None, text=None, request_state=None):
2727
"""This function will start a web server listening on http://localhost:port
2828
and then you need to open a browser on this device and visit your auth_uri.
2929
When interaction finishes, this function will return the auth code,
@@ -36,9 +36,18 @@ def obtain_auth_code(listen_port, auth_uri=None):
3636
:param auth_uri: If provided, this function will try to open a local browser.
3737
:return: Hang indefinitely, until it receives and then return the auth code.
3838
"""
39-
if auth_uri:
39+
if text:
40+
exit_hint = "Visit http://localhost:{p}?auth_code=exit to abort".format(p=listen_port)
41+
browse("http://localhost:{p}?{q}".format(
42+
p=listen_port, q=urlencode({
43+
"text": text,
44+
"link": auth_uri,
45+
"exit_hint": exit_hint,
46+
})))
47+
logger.warning(exit_hint)
48+
else:
4049
browse(auth_uri)
41-
server = AuthcodeRedirectServer(int(listen_port))
50+
server = AuthcodeRedirectServer(int(listen_port), request_state)
4251
return server.get_auth_code()
4352

4453

@@ -62,8 +71,9 @@ def do_GET(self):
6271
#assert self.path.startswith('/THE_PATH_REGISTERED_BY_THE_APP')
6372
qs = parse_qs(urlparse(self.path).query)
6473
if qs.get('code'): # Then store it into the server instance
74+
if self.server.state and self.server.state != qs.get('state', [None])[0]:
75+
raise ValueError("State does not match")
6576
self.server.auth_code = qs['code'][0]
66-
self.server.state = qs.get('state', [None])[0]
6777
self._send_full_response('Authentication complete. You can close this window')
6878
# NOTE: Don't do self.server.shutdown() here. It'll halt the server.
6979
elif qs.get('text') and qs.get('link'): # Then display a landing page
@@ -85,9 +95,9 @@ def _send_full_response(self, body, is_ok=True):
8595

8696
class AuthcodeRedirectServer(HTTPServer):
8797

88-
def __init__(self, port):
98+
def __init__(self, port, request_state):
8999
HTTPServer.__init__(self, ("", port), AuthCodeReceiver)
90-
self.state = None
100+
self.state = request_state
91101
self.auth_code = None
92102
self.timeout = 300
93103

@@ -100,10 +110,12 @@ def get_auth_code(self):
100110
self.handle_request()
101111
except ValueError:
102112
break
113+
except IOError: # Python 2 throws an IOError handle timeout closes server
114+
break
103115
finally:
104116
self.server_close()
105117

106-
return self.auth_code, self.state
118+
return self.auth_code
107119

108120
def handle_timeout(self):
109121
"""Break the request-handling loop by tearing down the server"""

0 commit comments

Comments
 (0)