2020-06-30 12:45:36 +00:00
|
|
|
import base64
|
|
|
|
import datetime
|
|
|
|
import functools
|
|
|
|
import json
|
|
|
|
from concurrent.futures.thread import ThreadPoolExecutor
|
|
|
|
from pprint import pprint
|
|
|
|
|
2021-05-26 03:27:55 +00:00
|
|
|
import six
|
|
|
|
|
2020-06-30 12:45:36 +00:00
|
|
|
from huaweicloudsdkcore.auth.credentials import Credentials
|
2021-04-30 03:29:34 +00:00
|
|
|
from huaweicloudsdkcore.exceptions.exceptions import SdkException, ApiValueError
|
2020-06-30 12:45:36 +00:00
|
|
|
from huaweicloudsdkcore.sdk_request import SdkRequest
|
2021-04-30 03:29:34 +00:00
|
|
|
from huaweicloudsdkcore.signer.signer import process_canonical_query_string
|
2020-06-30 12:45:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MeetingCredentials(Credentials):
|
2021-04-30 03:29:34 +00:00
|
|
|
def __init__(self, user_name, user_password):
|
2020-06-30 12:45:36 +00:00
|
|
|
self._token = None
|
|
|
|
self._last_token_date = None
|
2020-09-11 08:55:53 +00:00
|
|
|
|
2021-04-30 03:29:34 +00:00
|
|
|
if user_name is None or user_name == "":
|
|
|
|
raise ApiValueError("user_name can not be null.")
|
2020-09-11 08:55:53 +00:00
|
|
|
|
2021-04-30 03:29:34 +00:00
|
|
|
if user_password is None or user_password == "":
|
|
|
|
raise ApiValueError("user_password can not be null.")
|
2020-09-11 08:55:53 +00:00
|
|
|
|
2021-04-30 03:29:34 +00:00
|
|
|
self._user_name = user_name
|
|
|
|
self._user_password = user_password
|
2020-06-30 12:45:36 +00:00
|
|
|
|
|
|
|
def get_update_path_params(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def process_auth_request(self, request, http_client, executor=None):
|
|
|
|
if executor is None:
|
|
|
|
executor = ThreadPoolExecutor(max_workers=1)
|
|
|
|
future = executor.submit(self.process_request, request, http_client)
|
|
|
|
return future
|
|
|
|
|
|
|
|
def process_request(self, request, http_client):
|
|
|
|
now_time = datetime.datetime.now()
|
|
|
|
|
|
|
|
if self._token is None or self._last_token_date is None or (
|
|
|
|
now_time - self._last_token_date).days * 24 * 3600 + (
|
|
|
|
now_time - self._last_token_date).seconds > 12 * 60 * 60:
|
2021-05-26 03:27:55 +00:00
|
|
|
authorization = "Basic " + six.ensure_str(
|
|
|
|
base64.b64encode((self._user_name + ':' + self._user_password).encode('utf-8')))
|
2020-06-30 12:45:36 +00:00
|
|
|
|
2021-08-10 12:26:18 +00:00
|
|
|
body = {'account': self._user_name, 'clientType': 72}
|
2020-09-11 08:55:53 +00:00
|
|
|
sdk_request = SdkRequest('POST', 'https', request.host, [], '/v1/usg/acs/auth/account', [],
|
2020-06-30 12:45:36 +00:00
|
|
|
{'Authorization': authorization, 'Content-Type': 'application/json'},
|
2020-09-11 08:55:53 +00:00
|
|
|
json.dumps(body), [])
|
2020-06-30 12:45:36 +00:00
|
|
|
|
2020-09-11 08:55:53 +00:00
|
|
|
response = http_client.do_request_sync(sdk_request)
|
2020-06-30 12:45:36 +00:00
|
|
|
content = json.loads(response.content.decode())
|
|
|
|
self._token = content['accessToken']
|
|
|
|
self._last_token_date = datetime.datetime.now()
|
2021-05-26 03:27:55 +00:00
|
|
|
request.header_params["X-Access-Token"] = self._token
|
2021-04-30 03:29:34 +00:00
|
|
|
canonical_query_string = process_canonical_query_string(request)
|
|
|
|
request.uri = request.resource_path + "?" + canonical_query_string if canonical_query_string != "" else request.resource_path
|
2020-06-30 12:45:36 +00:00
|
|
|
return request
|
|
|
|
else:
|
2021-05-26 03:27:55 +00:00
|
|
|
request.header_params["X-Access-Token"] = self._token
|
2021-04-30 03:29:34 +00:00
|
|
|
canonical_query_string = process_canonical_query_string(request)
|
|
|
|
request.uri = request.resource_path + "?" + canonical_query_string if canonical_query_string != "" else request.resource_path
|
2020-06-30 12:45:36 +00:00
|
|
|
return request
|
|
|
|
|
|
|
|
|