cannot authenticate hashed password in Flask - mongodb

I'm building username/password authentication for a flask app. When the user signs up, I store the username and password in a mongodb document.
This is my User model:
class User():
def __init__(self, userid=None,username=None,password=None):
self.userid= userid
self.username = username
self.password = generate_password_hash(password)
def __repr__(self):
return '<User %r>' % self.username
def is_authenticated(self):
return True
def is_active(self):
return True
def is_anonymous(self):
return False
def get_id(self):
try:
return unicode(self.userid)
except NameError:
return unicode(self.userid)
I'm using the werkzeug library methods generate_password_hash and check_password_hash to generate/check the password, respectively.
This is my code for dumping user data into mongodb:
def db_dump(data):
k = {'username':data.username,'password':data.password}
db.users.insert(k,True)
And this is my code for retrieving user data by username:
def find_by_username(username):
data = self.db.users.find_one({'username': username})
user = User(userid=unicode(data['_id']),username=data['username'],password=data['password'])
return user
Here's my problem. Let's say I have a user 'jon' that has a password 'abcd'. When I run the code to check the password hash for a new user thru the console, it works fine:
>> check_password_hash(generate_password_hash('abcd'),'abcd')
True
However, after 'jon' has signed up and his info has been dumped into the database, on a subsequent login attempt in which I have to retrieve the hashed password from the database, the checking doesn't work:
>>check_password_hash(find_by_username('jon').password,'abcd')
False
Any idea as to why this is happening? After some googling my first instinct is that maybe I need to store or retrieve the hash password in mongodb in a different way, since it seems like it's being stored as a string. Any ideas?

I believe the issue is that you are double-hashing the password when you instantiate the User object in your find_by_username method. That method grabs the info from the DB which would contain the hashed password. Then, it creates a User object with that data. The User object calls generate_password_hash during its __init__ method. So, the password is being double-hashed and therefore failing the check_password_hash check.
You might consider moving the password hashing to a function that is called when the User object is saved to the DB. You'd want to detect if the password has changed or not so you aren't repeatedly rehashing the password. There are likely other ways to handle this as well, but in short, you might want to move the password hashing out of the __init__ call.

Related

bcrypt && bcryptjs compare password always return false in my case

I have a user model(sequelize for Postgres) have below comparePassword function:
In login controller, I am checking password to let user login as below:
But any user fail to login because of comparePassword always return "false". I created several new users successfully and then when try to login, it gave the same "false" compare results.
I had tried the two module (bcrypt and bcryptjs), while same results.
Checked the stackoverflow, and found similar issue by post title, while not the same.
I tried the compareSync, it also gave false compare result.
My question: The hash calculation should be the same when creating the password hash to store in the database and the later comparing Password. where is the pit? what's the mistakes in my User model?
Thanks in advance.
In similar situation I had placed the lowercase validation on password in the User Model. That was giving me always bcrypt compare false. When removing it everything ran as usual.

Authenticatee FrontendUser via PHP API call from Extbase

First of all im Using TYPO3 Version 8.7.
The current problem i'm facing regards authentication of FrontendUser (fe_user) stored on a given page (in this case pid 168).
Apparently i'm trying to authenticate user with given credentials sent by a mobile application. I'm able to parse the user data and perform an authentication:
// plain-text password
$password = 'XXX';
// salted user password hash
$saltedPassword = 'YYY';
// keeps status if plain-text password matches given salted user password hash
$success = FALSE;
if (\TYPO3\CMS\Saltedpasswords\Utility\SaltedPasswordsUtility::isUsageEnabled('FE')) {
$objSalt = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance($saltedPassword);
if (is_object($objSalt)) {
$success = $objSalt->checkPassword($password, $saltedPassword);
}
}
While debugging this code snippet, i recognized the password sent by the user via Request, which gets encrypted with the given Salt algorithm change every time i retry this request. I'm not sure how to get a correct authentication, if the password changes constantly.
The $objSalt object contains the right Hashing Method($pbkdf2-sha256$25000), the password stored in the Database starts with the same prefix, but the actual payload is different.
So What is the exact problem or whats the thing i'm missing in the above code to complete the authentication?
Thanks for your help
BR,
Martin
the password sent by the user via Request, which gets encrypted with the given Salt algorithm change every time i retry this request
Yes, that because the salt is changed every time.
You should retrieve the salting instance with:
$instance = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance($user['password']);

How to reset password in passport-local strategy in Sails.js

Passport.js provides authentication framework in Node.js. It only deals with Authentication.
Now I would like to enable password reset. Since there is no password field in User model, only passports, how can I reset password in passport-local strategy? I assume that user needs to generate a new password and call something to override the existing hash of the old password. What methods are those and where can I find them?
When the user selects to reset his/her password, what you can do is send an email to the user with a link containing a token associated with the user. Once the user clicks on the link, you validate the user based on the token & email and then show the reset password HTML. Once user enters the new password, in the backend code, you set the password on the User object after hashing and then save it. You can set the token as null too.
A sample code with base64 will be as shown below
user.salt = new Buffer(crypto.randomBytes(16).toString('base64'), 'base64');
user.password = user.hashPassword('newPassword');
user.token = undefined;
user.save(...)
The hashPassword method is as given.
UserSchema.methods.hashPassword = function(password) {
if (this.salt && password) {
return crypto.pbkdf2Sync(password, this.salt, 10000, 64).toString('base64');
} else {
return password;
}
};
The above code is auto-generated with Yeoman

Bootstrap a grails app with dummy data when spring security facebook is involved

I've created a grails app that uses spring security to allow a user to authenticate via facebook, and I can successfully print out the facebook username onto one of the views, so thus far I don't have any issues.
My problem lies when trying to bootstrap my application with some sample data for my given facebook user, so I don't have to enter it every time the application starts up.
This is how I'm trying to bootstrap my own facebook account, I have the following in Bootstrap.groovy :
def adminRole = new AppRole(authority: 'ROLE_ADMIN').save(flush: true)
def userRole = new AppRole(authority: 'ROLE_USER').save(flush: true)
def testUser = new AppUser(username: 'facebook_563645402', enabled: true,
password: 'my-hashed-pw-here',
surveys: [jamies])
testUser.save(flush: true)
AppUserAppRole.create testUser, adminRole, true
For the record, I've added a hasMany for the surveys field mentioned above onto AppUser.
When I fire up the app and try to connect, I get the following error :
URI
/web/j_spring_security_facebook_check
Class
grails.validation.ValidationException
Message
Validation Error(s) occurred during save(): - Field error in object 'web.AppUser' on field 'username': rejected value [facebook_563645402]; codes [web.AppUser.username.unique.error.web.AppUser.username,web.AppUser.username.unique.error.username,web.AppUser.username.unique.error.java.lang.String,web.AppUser.username.unique.error,appUser.username.unique.error.web.AppUser.username,appUser.username.unique.error.username,appUser.username.unique.error.java.lang.String,appUser.username.unique.error,web.AppUser.username.unique.web.AppUser.username,web.AppUser.username.unique.username,web.AppUser.username.unique.java.lang.String,web.AppUser.username.unique,appUser.username.unique.web.AppUser.username,appUser.username.unique.username,appUser.username.unique.java.lang.String,appUser.username.unique,unique.web.AppUser.username,unique.username,unique.java.lang.String,unique]; arguments [username,class web.AppUser,facebook_563645402]; default message [Property [{0}] of class [{1}] with value [{2}] must be unique]
Which appears to complain about the username not being unique.
If by trying to bootstrap some data breaks the unique constraints on the facebook username, how can I possibly ever pre define any data for a user?
A quick Googling brings up a few suggestions (link1, Grails spring security bootstrap, but so far they haven't helped, any ideas?
EDIT:
Delving deeper into the error that grails reports, I can see that the root of the above error is located in DefaultFacebookAuthDao, line 135, which mentions the following :
AppUserDomainClazz.withTransaction {
appUser.save(flush: true, failOnError: true)
}
So, by authenticating, spring security attempts to save a user domain object...
EDIT 2 :
This is my Bootstrap.groovy
def testUser = new AppUser(username: 'facebook_563645402', enabled: true,
password: 'my-hashed-pw', surveys: [new Survey(1)])
testUser.save()
def fbUser = new FacebookUser(uid: 563645402)
fbUser.save(flush: true)
Both FacebookUser and AppUser were generated via the spring security facebook quickstart, with the only change being to add static hasMany = [surveys: Survey] to AppUser.
It looks like the data has already been predefined, otherwise there wouldn't be a unique constraint violation. Just check for the existence of the data and only create it if needed:
def adminRole = AppRole.findOrSaveByAuthority('ROLE_ADMIN')
def userRole = AppRole.findOrSaveByAuthority('ROLE_USER')
String username = 'facebook_563645402'
if (!AppUser.findByUsername(username)) {
def testUser = new AppUser(username: username, enabled: true,
password: 'my-hashed-pw-here')
testUser.addToSurveys(jamies)
testUser.save()
AppUserAppRole.create testUser, adminRole, true
}
Spring Security Facebook tries to create a new user with same username (facebook_563645402). Because it cannot find it by uid.
As I see from you code, you just create an user, with filled username, but it's not a facebook user, and uid field isn't filled. So, the plugin cannot find any facebook user and tries to create a new one, using 'facebook_563645402' username by default.
There two ways: or change username for user created in Bootstrap (if it's not a facebook user), or create a Facebook User also (will be used for authentication).

Get username from SecurityIdentifier

I am using Windows Authentication on a website where users create reports that are stored in a database. When I save a report, I want to know which user filled it out, so I have been storing the SecurityIdentifier of their WindowsIdentity in the database to identify which user filled out the report. Here is the code I use to get the SecurityIdentifier value for the current Windows user:
public static string GetCurrentUserSID()
{
IPrincipal princ = HttpContext.Current.User;
WindowsIdentity winId = princ.Identity as WindowsIdentity;
SecurityIdentifier si = winId.User;
string securityIdentifierValue = winId.User.Value;
return securityIdentifierValue;
}
Questions
Am I doing the right thing by storing the SecurityIdentifier in the database instead of username or some other value? What is the best practice to follow in this sort of situation?
How can I get the user’s username from the SecurityIdentifier value I have stored?
Should contain the username:
HttpContext.Current.User.Identity.Name