Search code examples
pythonflaskauthenticationactive-directory

Enable autologin into flask app using active directory


I have made flask web app meant to be used within my organisation.

My problem is currently the login works by requesting a username and password from the user which are then authenticated by an LDAP server.

I would like to be able to autologin a user immediately he gets onto the site since that's how other company internal websites work(these others were made in C# and php)

My main problem is:

  • Is there a python package to get the client's windows username immediately he gets on to the site? and how would I use it

Requests-ntlm was once suggested but it has no documentation.

I have tried to look at several stack overflow questions but they do not address my problem eg link


Solution

  • It has taken me weeks to find the answer but Yes it is possible but you must configure the server where your app is hosted to allow this. In my case I was using IIS and enabled windows authentication. With this its surprisingly easy to get the active directory user name as it comes as part of the environment variables from the fastcgi server on iis:

    from flask import request
    username = request.environ.get('REMOTE_USER')
    

    See this question for more information:

    How to access Apache Basic Authentication user in Flask