2

Let me preface this by saying I'm an engineer at a big company and am NOT a programmer so I'm sorry if I'm way off. This question has really been tough to google the answer which generally means I'm asking the question terribly. So I'll try to respond to users to work towards a solution if you'll let me.

I've also tagged multiple languages because I'm not sure if this is a javascript solution or a python solution. I apologize.

Basically, when a user fills in a form it runs a python cgi script that stores the information to a database. I want to automate the writing of the username so the user doesn't have to fill it in manually(not for security, just for ease of use)

My program is on an apache RedHat server and in /etc/httpd/conf I have put the following in my httpd.conf file:

<Directory /var/www/html/program>
##### LDAP Login #######################
AuthType Basic
AuthName "ProbeEng Webserver: Enter Your BIGCOMPANY Username and Password"
AuthBasicProvider ldap
AuthzLDAPAuthoritative off
AuthLDAPURL "ldap://ldap.city.bigcompany.com/ou=mtworkers,o=bigcompany.com?uid"

##### Require Authentication
Require valid-user

Order deny,allow
Allow from all

</Directory>

When users access my program they are required to put in their BIGCOMPANY username and password. How can I send that username through to my python cgi-script. The program they start the form on is also a python cgi-script that generates the html.

I tried the following:

try:
   username = getpass.getuser()

But that doesn't seem to work.

Is this possible while using a cgi-script, if so how do I access the user's username?

Thanks

1 Answer 1

3

If the webserver authenticated the user, typically the username will be in a header and will appear as an environment variable in os.environ in a Python CGI script. Usually, this header name is REMOTE_USER.

You can try printing all the environment variables, just to see what's there.

import os
for key, value in os.environ.items():
    print(key, value)

Once you know what key you're looking for (probably 'REMOTE_USER') just retrieve it like

import os
username = os.environ.get('REMOTE_USER')

Although, the CGI module is probably not how you want to be running this application. Every requests spawns a new process and, with any serious amount of traffic, could quickly become a burden on that server.

Sign up to request clarification or add additional context in comments.

2 Comments

Hey sytech, Thanks for you answer. First off, I'm definitely aware CGI is not current way to do things but I didn't know that when I started this app. All of my future apps are done in wsgi through flask. Looking at the environ variables there is no REMOTE_USER, only a REMOTE_ADDR with an ip address. Can I get the user from the IP address, or is that unrelated to the user's basic auth account?
I lied @sytech, it does have REMOTE_USER. I accidentally tested it on a page not being authenticated by my own webserver. Thank you so much for your help, I really appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.