October 18, 2023

How to Implement Basic Authentication in Flask


 if not request.headers.get("Authorization"):                                                                                                                                                                   
            return jsonify({"error": "Need auth header"})
        auth_hdr = base64.urlsafe_b64decode(
            request.headers.get("authorization").replace("Basic ", "")
        ).decode()
        username, password = auth_hdr.split(":")
        with open("flatFileForPasswords") as fin: 
            reader = csv.reader(fin)
            for r in reader:
                if r[0] == username:
                    if r[1] != password:
                        return jsonify({"error": "Wrong auth info"})

For when I forget how to do this.

No comments:

Post a Comment