middleware.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:irisett 作者: beebyte 项目源码 文件源码
def basic_auth_middleware_factory(app: web.Application, handler: Any) -> Callable:
    """Authentication.

    Uses HTTP basic auth to check that requests are including the required
    username and password.
    """
    async def middleware_handler(request: web.Request) -> web.Response:
        ok = False
        auth_token = request.headers.get('Authorization')
        if auth_token and auth_token.startswith('Basic '):
            auth_token = auth_token[6:]
            try:
                auth_bytes = base64.b64decode(auth_token)  # type: Optional[bytes]
            except binascii.Error:
                auth_bytes = None
            if auth_bytes:
                auth_str = auth_bytes.decode('utf-8', errors='ignore')
                if ':' in auth_str:
                    username, password = auth_str.split(':', 1)
                    if username == app['username'] and password == app['password']:
                        ok = True
        if not ok:
            log.msg('Unauthorized request: %s' % request, 'WEBMGMT')
            raise errors.MissingLogin('Unauthorized')
        return await handler(request)

    return middleware_handler


# noinspection PyUnusedLocal
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号