Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the password in django?

Tags:

django

I have a python library which I wrote for something else. So I import that to use in Django, problem I am facing is how to get the password.

mycustom_lib_function(username, password)

username I can pass in through request.user

But, I am not sure how to get the password. Can someone help me? Thanks.

like image 346
user1012451 Avatar asked Nov 29 '22 09:11

user1012451


2 Answers

The password in User is hashed, and so you cannot get it. Ask the user.

like image 116
Ignacio Vazquez-Abrams Avatar answered Dec 07 '22 01:12

Ignacio Vazquez-Abrams


You technically can store the password as plain-text but its not right from a security stand poit, see this answer, it is highly not recommended! django.contrib.auth.hashers has some good tools to use for passwords, see the official Django docs.

If you have an idea what the plain-text password could be, i.e. I have a globally stored default password in one of my applications that is stored in plain-text, as in the example below. To check if a user has their password set to the default one, you can use the check_password function that will return True if the plain-text matches the encoded password:

from django.contrib.auth.hashers import check_password
from django.contrib.auth.models import User
u = User.objects.all().first()
if check_password('the default password', u.password):
    print 'user password matches default password'
else:
    print 'user a set custom password'

Also see is_password_usable, and make_password functions.

like image 39
radtek Avatar answered Dec 06 '22 23:12

radtek