Python ubinascii.a2b_base64() Examples

The following are 5 code examples of ubinascii.a2b_base64(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module ubinascii , or try the search function .
Example #1
Source File: upload.py    From uPyLoader with MIT License 6 votes vote down vote up
def _upload():
    suc = False
    with open("file_name.py", "wb") as f:
        while True:
            d = _read_timeout(3)
            if not d or d[0] != "#":
                x = sys.stdout.write("#2")
                break
            cnt = int(d[1:3])
            if cnt == 0:
                suc = True
                break
            d = _read_timeout(cnt)
            if d:
                x = f.write(a2b_base64(d))
                x = sys.stdout.write("#1")
            else:
                x = sys.stdout.write("#3")
                break
    x = sys.stdout.write("#0" if suc else "#4") 
Example #2
Source File: main.py    From xbee-micropython with MIT License 6 votes vote down vote up
def generate_sas_token(uri: str, key: str, policy_name=None, expiry: int = 36000) -> str:
    """
    Create an Azure SAS token.
    :param uri: URI/URL/Host Name to connect to with the token.
    :param key: The key.
    :param policy_name: Not sure what it is right now, defaults to None.
    :param expiry: How long until the token expires. defaults to one hour.
    :return: An SAS token to be used with Azure.
    """
    ttl = time() + expiry + 946684800
    sign_key = "{uri}\n{ttl}".format(uri=quote_plus(uri), ttl=int(ttl))
    signature = b64encode(hmac_digest(b64decode(key), sign_key.encode())).rstrip(b'\n')

    rawtoken = {
        'sr':  uri,
        'sig': signature,
        'se': str(int(ttl))
    }

    if policy_name is not None:
        rawtoken['skn'] = policy_name

    return 'SharedAccessSignature ' + urlencode(rawtoken) 
Example #3
Source File: example_basic_auth_deco.py    From picoweb with MIT License 6 votes vote down vote up
def require_auth(func):

    def auth(req, resp):
        auth = req.headers.get(b"Authorization")
        if not auth:
            yield from resp.awrite(
                'HTTP/1.0 401 NA\r\n'
                'WWW-Authenticate: Basic realm="Picoweb Realm"\r\n'
                '\r\n'
            )
            return

        auth = auth.split(None, 1)[1]
        auth = ubinascii.a2b_base64(auth).decode()
        req.username, req.passwd = auth.split(":", 1)
        yield from func(req, resp)

    return auth 
Example #4
Source File: __init__.py    From esp8266 with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def is_authorized(self, authorization):
        import ubinascii
        try:
            tmp = authorization.split()
            if tmp[0].lower() == "basic":
                str = ubinascii.a2b_base64(tmp[1].strip().encode()).decode()
                ra = str.split(':')
                auth_result = ra[0] == self._config['user'] and ra[1] == self._config['password']
                return auth_result, ra[0]
            else:
                raise BadRequestException(
                    "Unsupported authorization method: {}".format(tmp[0]))
        except Exception as e:
            raise BadRequestException(e) 
Example #5
Source File: example_basic_auth.py    From picoweb with MIT License 5 votes vote down vote up
def index(req, resp):
    if b"Authorization" not in req.headers:
        yield from resp.awrite(
            'HTTP/1.0 401 NA\r\n'
            'WWW-Authenticate: Basic realm="Picoweb Realm"\r\n'
            '\r\n'
        )
        return

    auth = req.headers[b"Authorization"].split(None, 1)[1]
    auth = ubinascii.a2b_base64(auth).decode()
    username, passwd = auth.split(":", 1)
    yield from picoweb.start_response(resp)
    yield from resp.awrite("You logged in with username: %s, password: %s" % (username, passwd))