Refactor to use password strength options

This commit is contained in:
Alasdair Smith 2018-10-04 12:00:33 +01:00
parent 1fe8aebf5b
commit 44c86b3769
2 changed files with 18 additions and 15 deletions

View file

@ -28,13 +28,25 @@ module.exports = AuthenticationManager =
else
callback null, null
setUserPassword: (user_id, password, callback = (error) ->) ->
validateEmail: (email) ->
if !email?.length
return { message: 'email not set' }
return null
validatePassword: (password) ->
if !password?
return { message: 'password not set' }
if (Settings.passwordStrengthOptions?.length?.max? and
Settings.passwordStrengthOptions?.length?.max < password.length)
return callback("password is too long")
return { message: 'password is too short' }
if (Settings.passwordStrengthOptions?.length?.min? and
Settings.passwordStrengthOptions?.length?.min > password.length)
return callback("password is too short")
return { message: "password is too short" }
return null
setUserPassword: (user_id, password, callback = (error) ->) ->
validation = validatePassword(password)
return callback(validation.message) if validation?
bcrypt.genSalt BCRYPT_ROUNDS, (error, salt) ->
return callback(error) if error?

View file

@ -13,20 +13,11 @@ settings = require "settings-sharelatex"
EmailHelper = require("../Helpers/EmailHelper")
module.exports = UserRegistrationHandler =
hasZeroLengths : (props) ->
hasZeroLength = false
props.forEach (prop) ->
if prop.length == 0
hasZeroLength = true
return hasZeroLength
isTooShort: (prop, length) ->
return prop.length < length
_registrationRequestIsValid : (body, callback)->
email = EmailHelper.parseEmail(body.email) or ''
password = body.password
if @hasZeroLengths([password, email]) or @isTooShort(password, 6)
invalidEmail = AuthenticationManager.validateEmail(email)
invalidPassword = AuthenticationManager.validatePassword(body.password)
if invalidEmail? or invalidPassword?
return false
else
return true