def oauthAccessToken(self, auth_code): """ Summary: This function is Step3 of the OAUTH mechanism. Obtain access token by using auth_code. Returns: token (dict): Obtained from response payload. Contains access_token and refresh_token """ access_token = None headers = {'Content-Type': 'application/json'} path = "/oauth2/token" query = { "client_id": self.central_info["client_id"], "client_secret": self.central_info["client_secret"], "grant_type": "authorization_code", "code": auth_code } url = get_url(base_url=self.central_info["base_url"], path=path, query=query) try: req = Request(url=url, method="POST") resp = urlopen(req) if resp.code == 200: result = json.loads(resp.read().decode('utf8')) token = result return token except Exception as e: self.logger.error("Central Login Step3 failed.." " Unable to obtain access token!") raise e
def oauthCode(self, csrf_token, session_token): """ Summary: This function is Step2 of the OAUTH mechanism. Obtain authentication code using CSRF token and session token. Returns: auth_code (str): Obtained from response payload """ auth_code = None path = "/oauth2/authorize/central/api" query = { "client_id": self.central_info["client_id"], "response_type": "code", "scope": "all" } url = get_url(base_url=self.central_info["base_url"], path=path, query=query) customer_id = self.central_info["customer_id"] data = json.dumps({'customer_id': customer_id}).encode("utf-8") headers = { 'X-CSRF-TOKEN': csrf_token, 'Content-Type': 'application/json', 'Cookie': "session=" + session_token } try: s = requests.Session() req = requests.Request(method="POST", url=url, data=data, headers=headers) prepped = s.prepare_request(req) settings = s.merge_environment_settings(prepped.url, {}, None, self.ssl_verify, None) resp = s.send(prepped, **settings) if resp.status_code == 200: result = json.loads(resp.text) auth_code = result['auth_code'] return auth_code except Exception as e: self.logger.error("Central Login Step2 failed.." " Unable to obtain Auth code!") raise e
def refreshToken(self, old_token): """ Summary: This function refreshes existing token in Aruba Central Parameters: old_token (dict): A token dict containg "refresh_token" Returns: token (dict): Obtained from response payload. Contains renewed access_token and refresh_token """ token = None resp = '' try: if not self.validateRefreshTokenParams(): raise UserWarning("") if "refresh_token" not in old_token: raise UserWarning("refresh_token not present in the input " "token dict") path = "/oauth2/token" query = { "client_id": self.central_info["client_id"], "client_secret": self.central_info["client_secret"], "grant_type": "refresh_token", "refresh_token": old_token["refresh_token"] } url = get_url(base_url=self.central_info["base_url"], path=path, query=query) s = requests.Session() req = requests.Request(method="POST", url=url) prepped = s.prepare_request(req) settings = s.merge_environment_settings(prepped.url, {}, None, self.ssl_verify, None) resp = s.send(prepped, **settings) if resp.status_code == 200: result = json.loads(resp.text) token = result return token except Exception as err: self.logger.error("Unable to refresh token.. " "%s" % str(err))
def oauthLogin(self): """ Summary: This function is Step1 of the OAUTH mechanism. Aruba Central login is performed using username and password. Returns: csrf_token, session_token (tuple): Obtained from response headers """ csrf_token = None session_token = None base_url = self.central_info["base_url"] headers = {'Content-Type': 'application/json'} path = "/oauth2/authorize/central/api/login" query = {"client_id": self.central_info["client_id"]} url = get_url(base_url=self.central_info["base_url"], path=path, query=query) data = json.dumps({ "username": self.central_info["username"], "password": self.central_info["password"] }) data = data.encode("utf-8") try: s = requests.Session() req = requests.Request(method="POST", url=url, data=data, headers=headers) prepped = s.prepare_request(req) settings = s.merge_environment_settings(prepped.url, {}, None, self.ssl_verify, None) resp = s.send(prepped, **settings) if resp.status_code == 200: cookies = resp.cookies.get_dict() return cookies['csrftoken'], cookies['session'] except Exception as e: self.logger.error("Central Login Step1 failed.." " Unable to obtain CSRF token!") raise e
def oauthLogin(self): """ Summary: This function is Step1 of the OAUTH mechanism. Aruba Central login is performed using username and password. Returns: csrf_token, session_token (tuple): Obtained from response headers """ csrf_token = None session_token = None base_url = self.central_info["base_url"] headers = {'Content-Type': 'application/json'} path = "/oauth2/authorize/central/api/login" query = {"client_id": self.central_info["client_id"]} url = get_url(base_url=self.central_info["base_url"], path=path, query=query) data = json.dumps({ "username": self.central_info["username"], "password": self.central_info["password"] }) data = data.encode("utf-8") try: req = Request(url=url, data=data, headers=headers, method="POST") resp = urlopen(req) if resp.code == 200: cookie = resp.getheader("Set-Cookie") match = re.search( r"csrftoken=(.*); Secure; Path=/, " r"session=(.*); Secure; HttpOnly; Path=/", cookie) csrf_token = match.group(1) session_token = match.group(2) return csrf_token, session_token except Exception as e: self.logger.error("Central Login Step1 failed.." " Unable to obtain CSRF token!") raise e
def oauthAccessToken(self, auth_code): """ Summary: This function is Step3 of the OAUTH mechanism. Obtain access token by using auth_code. Returns: token (dict): Obtained from response payload. Contains access_token and refresh_token """ access_token = None headers = {'Content-Type': 'application/json'} path = "/oauth2/token" query = { "client_id": self.central_info["client_id"], "client_secret": self.central_info["client_secret"], "grant_type": "authorization_code", "code": auth_code } url = get_url(base_url=self.central_info["base_url"], path=path, query=query) try: s = requests.Session() req = requests.Request(method="POST", url=url) prepped = s.prepare_request(req) settings = s.merge_environment_settings(prepped.url, {}, None, self.ssl_verify, None) resp = s.send(prepped, **settings) if resp.status_code == 200: result = json.loads(resp.text) token = result return token except Exception as e: self.logger.error("Central Login Step3 failed.." " Unable to obtain access token!") raise e
def refreshToken(self, old_token): """ Summary: This function refreshes existing token in Aruba Central Parameters: old_token (dict): A token dict containg "refresh_token" Returns: token (dict): Obtained from response payload. Contains renewed access_token and refresh_token """ token = None resp = '' try: if not self.validateRefreshTokenParams(): raise UserWarning("") if "refresh_token" not in old_token: raise UserWarning("refresh_token not present in the input " "token dict") path = "/oauth2/token" query = { "client_id": self.central_info["client_id"], "client_secret": self.central_info["client_secret"], "grant_type": "refresh_token", "refresh_token": old_token["refresh_token"] } url = get_url(base_url=self.central_info["base_url"], path=path, query=query) req = Request(url, method="POST") resp = urlopen(req) if resp.code == 200: result = json.loads(resp.read().decode('utf8')) token = result return token except Exception as err: self.logger.error("Unable to refresh token.. " "%s" % str(err))
def oauthCode(self, csrf_token, session_token): """ Summary: This function is Step2 of the OAUTH mechanism. Obtain authentication code using CSRF token and session token. Returns: auth_code (str): Obtained from response payload """ auth_code = None path = "/oauth2/authorize/central/api" query = { "client_id": self.central_info["client_id"], "response_type": "code", "scope": "all" } url = get_url(base_url=self.central_info["base_url"], path=path, query=query) customer_id = self.central_info["customer_id"] data = json.dumps({'customer_id': customer_id}).encode("utf-8") headers = { 'X-CSRF-TOKEN': csrf_token, 'Content-Type': 'application/json', 'Cookie': "session=" + session_token } try: req = Request(url, data, headers, method="POST") resp = urlopen(req) if resp.code == 200: result = json.loads(resp.read().decode('utf8')) auth_code = result['auth_code'] return auth_code except Exception as e: self.logger.error("Central Login Step2 failed.." " Unable to obtain Auth code!") raise e