# 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 __init__(self, app_id, secret_app_key, access_token=None):

        HmacKeys.__init__(self, "", app_id, secret_app_key)
        self.app_id = app_id
        self.access_token = access_token

    def sign_string(self, string_to_sign):

        to_sign = self.digest(string_to_sign)
        return base64_url_encode(to_sign)

    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")


# 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 __init__(self, app_id, secret_app_key, access_token=None):

        HmacKeys.__init__(self, "", app_id, secret_app_key)
        self.app_id = app_id
        self.access_token = access_token

    def sign_string(self, string_to_sign):

        to_sign = self.digest(string_to_sign)
        return base64_url_encode(to_sign)

    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))