Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure nginx to pass user info to wsgi/flask

Tags:

nginx

wsgi

I have an nginx http server which authenticates users and passes the authenticated request to a Flask app via wsgi. When I print the entire header from the flask app no user information is available.

Is it possible to get nginx to include the username in the request header?

Here is the server block with the authentication config...

server {
    listen 80;
    server_name  notes.example.org;
    return 301 https://$server_name$request_uri;   
}

server {


    # SSL configuration

    listen 443;
    listen [::]:443; 
    include snippets/ssl-example.org.conf;
    include snippets/ssl-params.conf;
    server_name  notes.example.org;

    location / {
        auth_basic "Restricted Content";
        auth_basic_user_file /path/to/passwd/file;
        include uwsgi_params;
        uwsgi_pass unix:/path/to/wsgi/socket;
    }


}

Request header as seen by the app running under wsgi...

Authorization: Basic **************==
Content-Length: 0
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
Dnt: 1
Host: notes.example.org
Upgrade-Insecure-Requests: 1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: en-US,en;q=0.8
Content-Type: 
Accept-Encoding: gzip, deflate, sdch, br
like image 901
Vic_RTP Avatar asked Oct 15 '25 15:10

Vic_RTP


1 Answers

Here you are using http basic authentication option, after a successful feedback given by nginx server, browser sends a base64 of username:password. just use python base64 module to get username & password,

>>> from base64 import b64decode
>>> authorization_header = "dXNlcm5hbWU6cGFzc3dvcmQ=" # value from flask request header
>>> details = b64decode("dXNlcm5hbWU6cGFzc3dvcmQ=")
>>> username, password = details.split(':')
>>> username
'username'
>>> password
'password'
>>> 
like image 124
Renjith Thankachan Avatar answered Oct 18 '25 00:10

Renjith Thankachan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!