Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically(python) authenticate username/password using OS users

I'm writing a python program which allow user to login to it. I don't want to implement my own authentication but would rather take advantage of the OS(linux) mechanism. That is, when the user is trying to sign in my app by inputing username/password pair(which should be a valid OS user), I need to authenticate the pair by the OS. How to do that ? It may need the subprocess module, yet I've tried with no luck.

like image 479
John Wang Avatar asked Sep 11 '11 13:09

John Wang


People also ask

How do I create a login authentication in Python?

Authenticating Registered Users (User Login) To authenticate registered users, you have to redirect them to your IDX page, passing “login” as the AUTH_ACTION . Update the server.py file with the code below: @app. route("/login/") def login(): access_token = request.

How do I authenticate a user logged in?

The process is fairly simple; users input their credentials on the website's login form. That information is then sent to the authentication server where the information is compared with all the user credentials on file. When a match is found, the system will authenticate users and grant them access to their accounts.


2 Answers

Try using PAM via Python PAM or similar

like image 62
Hasturkun Avatar answered Nov 11 '22 13:11

Hasturkun


That should be possible by having your script read the /etc/passwd and /etc/shadow files, which contain details about usernames and passwords on a Linux system. Do note that the script will have to have read access to the files, which depending on the situation may or may not be possible.

Here are two good articles explaining the format of those files, which should tell you everything you need to know in order to have your script read and understand them:

  • Understanding /etc/passwd File Format
  • Understanding /etc/shadow File Format

By the way, when it talks about encrypted password, it means that it has been encrypted using the DES algorithm. You'll probably need to use pyDes or another python implementation of the DES algorithm in order for your script to create an encrypted password that it can compare to the one in /etc/shadow.

like image 44
EdoDodo Avatar answered Nov 11 '22 13:11

EdoDodo