Search code examples
pythonflaskbasic-authentication

How do I access authorization header value using flask in python 2.7?


I am trying to access the authorization value when calling the endpoint. Firstly I add the following argument that does show in Swagger as a field.

parser.add_argument('Authorization', type=str, required=True,
                help='A base-64 string in format "Basic username:password"', location='headers')

The Resource is defined like this:

from flask import request
parser.add_argument('Authorization', type=str, required=True,
                help='A base-64 string in format "Basic username:password"', location='headers')
@api.route("/authentication/")
@api.expect(parser)
class TestResource(Resource):
    @api.marshal_with(set_test_model)
    def post(self):
        
        val = request.authorization['username']
        if val is None:
            abort(400, 'Username is none')

Unfortunately I get an error saying:

val= request.authorization["username"]
TypeError: 'NoneType' object has no attribute '__getitem__'

Watching the traffic using Fiddler shows me that there is an Authorization header in the call. It tells me the following:

AUTHORIZATION Header is present: Basic dGVzdDp0ZXN0
Decoded Username:Password= test:test

Even though Fiddler does recognize the header I can't seem to catch it in python. Neither does request.headers contain the key "Authorization". Does anyone know how I get the Authorization value using flask?


Solution

  • The problem is that we are running in an older version of apache server which does not support authorization security header. That is why I never get any Authorization header in python.