# Path: qingcloud/misc/utils.py
# def get_utf8_value(value):
#     if sys.version < "3":
#         if isinstance(value, unicode):
#             return value.encode('utf-8')
#         if not isinstance(value, str):
#             value = str(value)
#         return value
#     else:
#         return str(value)
# 
# def get_ts(ts=None):
#     """ Get formatted time
#     """
#     if not ts:
#         ts = time.gmtime()
#     return time.strftime(ISO8601, ts)
# 
# def base64_url_decode(inp):
#     if sys.version > "3":
#         if isinstance(inp, bytes):
#             inp = inp.decode()
#         return base64.urlsafe_b64decode(inp + '=' * (4 - len(inp) % 4)).decode()
#     else:
#         return base64.urlsafe_b64decode(str(inp + '=' * (4 - len(inp) % 4)))
# 
# def base64_url_encode(inp):
#     if sys.version > "3":
#         if isinstance(inp, str):
#             inp = inp.encode()
#         return bytes.decode(base64.urlsafe_b64encode(inp).rstrip(b'='))
#     else:
#         return base64.urlsafe_b64encode(str(inp)).rstrip(b'=')


import sys
import hmac
import base64
import datetime
    import urllib.parse as urllib
    import urllib
from hashlib import sha1, sha256
from past.builtins import basestring
from qingcloud.misc.json_tool import json_dump, json_load
from qingcloud.misc.utils import get_utf8_value, get_ts, base64_url_decode,\
    base64_url_encode
    def extract_payload(self, payload, signature):

        expected_sig = self.sign_string(payload)
        if signature != expected_sig:
            return None

        return json_load(base64_url_decode(payload))

    def create_auth(self, access_info):
        """
        @param access_info: {user_id, access_token, action, zone, expires}
        @return {"payload":..., "signature": ...}
        """

        if "expires" not in access_info or not access_info["expires"]:
            raise Exception("expires must exist in access_info")

        payload = base64_url_encode(json_dump(access_info))
        signature = self.sign_string(payload)
        return {"payload": payload,
                "signature": signature}

    def add_auth(self, req, **kwargs):
        """ add authorize information for request
        """
        req.params['app_id'] = self.app_id
        if self.access_token:
            req.params['access_token'] = self.access_token
        req.params['signature_version'] = self.SignatureVersion
        req.params['version'] = self.APIVersion

# Path: qingcloud/misc/utils.py
# def get_utf8_value(value):
#     if sys.version < "3":
#         if isinstance(value, unicode):
#             return value.encode('utf-8')
#         if not isinstance(value, str):
#             value = str(value)
#         return value
#     else:
#         return str(value)
# 
# def get_ts(ts=None):
#     """ Get formatted time
#     """
#     if not ts:
#         ts = time.gmtime()
#     return time.strftime(ISO8601, ts)
# 
# def base64_url_decode(inp):
#     if sys.version > "3":
#         if isinstance(inp, bytes):
#             inp = inp.decode()
#         return base64.urlsafe_b64decode(inp + '=' * (4 - len(inp) % 4)).decode()
#     else:
#         return base64.urlsafe_b64decode(str(inp + '=' * (4 - len(inp) % 4)))
# 
# def base64_url_encode(inp):
#     if sys.version > "3":
#         if isinstance(inp, str):
#             inp = inp.encode()
#         return bytes.decode(base64.urlsafe_b64encode(inp).rstrip(b'='))
#     else:
#         return base64.urlsafe_b64encode(str(inp)).rstrip(b'=')


# Path: qingcloud/conn/auth.py
import sys
import hmac
import base64
import datetime
    import urllib.parse as urllib
    import urllib
from hashlib import sha1, sha256
from past.builtins import basestring
from qingcloud.misc.json_tool import json_dump, json_load
from qingcloud.misc.utils import get_utf8_value, get_ts, base64_url_decode,\
    base64_url_encode

    def extract_payload(self, payload, signature):

        expected_sig = self.sign_string(payload)
        if signature != expected_sig:
            return None

        return json_load(base64_url_decode(payload))

    def create_auth(self, access_info):
        """
        @param access_info: {user_id, access_token, action, zone, expires}
        @return {"payload":..., "signature": ...}
        """

        if "expires" not in access_info or not access_info["expires"]:
            raise Exception("expires must exist in access_info")

        payload = base64_url_encode(json_dump(access_info))
        signature = self.sign_string(payload)
        return {"payload": payload,
                "signature": signature}

    def add_auth(self, req, **kwargs):
        """ add authorize information for request
        """
        req.params['app_id'] = self.app_id
        if self.access_token:
            req.params['access_token'] = self.access_token
        req.params['signature_version'] = self.SignatureVersion
        req.params['version'] = self.APIVersion

        time_stamp = get_ts()