2

I implemented Basic Authorization in Flask. (references this snippet) It works really well.

def check_auth(username):
    user_id = get_user_id(username)
    return user_id is not None

def authenticate():
    """Sends a 401 response that enables basic auth"""
     return Response(
        'Could not verify your access level for that URL.¥n'
        'You have to login with proper credentials',
        status=401,
        headers={'WWW-Authenticate': 'Basic realm="Test"'}
        )

def requires_auth(f):
    """Basic authorization decorator
    """
    @wraps(f)
    def decorated(*args, **kwargs):
        auth = request.authorization
        if not auth or not check_auth(auth.username):
            return authenticate()
        return f(*args, **kwargs)
    return decorated

The problem is that when the application is accessed via Javascript(e.g. postmessage username and password via JS), this basic authorization doesn't work.
If authorization failed, the application should return 401 response to users and stop the application , but the application return nothing to users and the application start to work without authorization.
How can I fix this?

Thanks in advance.

kinakomochi
  • 475
  • 4
  • 8
  • 14

0 Answers0