mennanov
mennanov

Reputation: 1245

nginx and uWSGI obtain authentication data in Python

I need to obtain authentication data in my tiny Python app (just one script) from nginx + uWSGI. The authentication must be implemented at the web-server side (nginx, basic auth in nginx.conf for example) not in the Python app and this is important! So, no middleware or similar will help...

I suppose uWSGI have to send this data to my Python app via its API or smth..

How can i get this data in my Python script? I need smth like

$_SERVER['PHP_AUTH_USER']

in php.

uWSGI confing:

[uwsgi]

socket = /tmp/uwsgi.sock
processes = 4
master = true
module = myapp
harakiri = 30
cache = false
daemonize = /var/log/uwsgi.log

nginx config:

server {
listen       8080;
server_name  _;


location / {
    include        uwsgi_params;
    uwsgi_pass     unix:///tmp/uwsgi.sock;
uwsgi_cache off;    
}

}

Thank you!

Upvotes: 1

Views: 4293

Answers (2)

roberto
roberto

Reputation: 12953

try adding

uwsgi_param REMOTE_USER $remote_user;

in the nginx config

You will find username in environ['REMOTE_USER']

Upvotes: 2

Loïc Faure-Lacroix
Loïc Faure-Lacroix

Reputation: 13600

I was more expecting the entry point of your app with uwsgi. In the docs there is this link which might help you out.

http://projects.unbit.it/uwsgi/wiki/Quickstart

This is the simplest setup.

def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return "Hello World"

start_response is the variable to create a new response following the wsgi protocole. while env is where all the datas goes. In other words $_GET, $_POST and so on should be contained in that variable at some point.

The variable that you are looking for should be named as it it is in nginx. habitually it is called HTTP_AUTHENTICATION, in order to see everything, you should dump to your terminal or use a debugger to inspect that variable.

Upvotes: 0

Related Questions