def _acquire_token(self):
     parsed = urlparse(self.path)
     code = parse_qs(parsed.query)["code"][0]
     state = parse_qs(parsed.query)["state"][0]
     cookie = Cookie.SimpleCookie(self.headers["Cookie"])
     if state != cookie["auth_state"].value:
         raise ValueError("state does not match")
     auth_context = AuthenticationContext(authority_url)
     return auth_context.acquire_token_with_authorization_code(
         code, REDIRECT_URI, RESOURCE, sample_parameters["clientId"], sample_parameters["clientSecret"]
     )
 def _acquire_token(self):
     parsed = urlparse(self.path)
     code = parse_qs(parsed.query)['code'][0]
     state = parse_qs(parsed.query)['state'][0]
     cookie = Cookie.SimpleCookie(self.headers["Cookie"])
     if state != cookie['auth_state'].value:
         raise ValueError('state does not match')
     ### Main logic begins
     auth_context = AuthenticationContext(authority_url, api_version=None)
     return auth_context.acquire_token_with_authorization_code(
         code,
         REDIRECT_URI,
         RESOURCE,
         sample_parameters['clientId'],
         sample_parameters['clientSecret'])
 def do_GET(self):
     if self.path == '/':
         self.send_response(307)
         login_url = 'http://localhost:{}/login'.format(PORT)
         self.send_header('Location', login_url)
         self.end_headers()
     elif self.path == '/login':
         auth_state = (''.join(random.SystemRandom()
                               .choice(string.ascii_uppercase + string.digits)
                               for _ in range(48)))
         cookie = Cookie.SimpleCookie()
         cookie['auth_state'] = auth_state
         authorization_url = TEMPLATE_AUTHZ_URL.format(
             sample_parameters['tenant'],
             sample_parameters['clientId'],
             REDIRECT_URI,
             auth_state,
             RESOURCE)
         self.send_response(307)
         self.send_header('Set-Cookie', cookie.output(header=''))
         self.send_header('Location', authorization_url)
         self.end_headers()
     elif self.path.startswith('/getAToken'):
         is_ok = True
         try:
             token_response = self._acquire_token()
             message = 'response: ' + json.dumps(token_response)
             #Later, if the access token is expired it can be refreshed.
             auth_context = AuthenticationContext(authority_url, api_version=None)
             token_response = auth_context.acquire_token_with_refresh_token(
                 token_response['refreshToken'],
                 sample_parameters['clientId'],
                 RESOURCE,
                 sample_parameters['clientSecret'])
             message = (message + '*** And here is the refresh response:' +
                        json.dumps(token_response))
         except ValueError as exp:
             message = str(exp)
             is_ok = False
         self._send_response(message, is_ok)
 def do_GET(self):
     if self.path == "/":
         self.send_response(307)
         login_url = "http://localhost:{}/login".format(PORT)
         self.send_header("Location", login_url)
         self.end_headers()
     elif self.path == "/login":
         auth_state = "".join(
             random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(48)
         )
         cookie = Cookie.SimpleCookie()
         cookie["auth_state"] = auth_state
         authorization_url = TEMPLATE_AUTHZ_URL.format(
             sample_parameters["tenant"], sample_parameters["clientId"], REDIRECT_URI, auth_state, RESOURCE
         )
         self.send_response(307)
         self.send_header("Set-Cookie", cookie.output(header=""))
         self.send_header("Location", authorization_url)
         self.end_headers()
     elif self.path.startswith("/getAToken"):
         is_ok = True
         try:
             token_response = self._acquire_token()
             message = "response: " + json.dumps(token_response)
             # Later, if the access token is expired it can be refreshed.
             auth_context = AuthenticationContext(authority_url)
             token_response = auth_context.acquire_token_with_refresh_token(
                 token_response["refreshToken"],
                 sample_parameters["clientId"],
                 RESOURCE,
                 sample_parameters["clientSecret"],
             )
             message = message + "*** And here is the refresh response:" + json.dumps(token_response)
         except ValueError as exp:
             message = str(exp)
             is_ok = False
         self._send_response(message, is_ok)