Skip to content
This repository has been archived by the owner on Sep 29, 2023. It is now read-only.

fromClient() function for passwordBone and some code refactoring #52

Merged
merged 3 commits into from
Apr 13, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions bones/passwordBone.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from server import utils
from server.bones import stringBone
from hashlib import sha256
import hmac
Expand Down Expand Up @@ -60,23 +61,40 @@ class passwordBone( stringBone ):
def isInvalid(self, value):
if not value:
return False
if len(value)<self.minPasswordLength:
return "Password to short"
# Run our passwort test suite

if len(value) < self.minPasswordLength:
return _("The entered password is to short - it requires at least {{length}} characters.",
length=self.minPasswordLength)

# Run our password test suite
testResults = []
for test in self.passwordTests:
testResults.append(test(value))
if sum(testResults)<self.passwordTestThreshold:
return("Your password isn't strong enough!")

if sum(testResults) < self.passwordTestThreshold:
return _("The entered password is too weak.")

return False

def fromClient( self, valuesCache, name, data ):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can simplify that. Check for "not value" first (and return "No value entered" directly), and then test for isInvalid (return err) else store value.
With the current code, err is always False if no value is set; so the expression "not value and not err" can be shortened

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I've fixed it now and hope this fits your recommendations.

value = data.get(name)
if not value:
return "No value entered"

err = self.isInvalid(value)
if err:
return err

valuesCache[name] = value

def serialize( self, valuesCache, name, entity ):
if valuesCache.get(name,None) and valuesCache[name] != "":
salt = ''.join( [ random.choice(string.ascii_lowercase + string.ascii_uppercase + string.digits) for x in range(self.saltLength) ] )
salt = utils.generateRandomString(self.saltLength)
passwd = pbkdf2( valuesCache[name][ : conf["viur.maxPasswordLength"] ], salt )
entity.set( name, passwd, self.indexed )
entity.set( "%s_salt" % name, salt, self.indexed )
return( entity )

return entity

def unserialize( self, valuesCache, name, values ):
return( {name: ""} )
return {name: ""}