use of fr.univlorraine.ecandidat.utils.CustomException in project esup-ecandidat by EsupPortail.
the class PasswordHashServicePBKDF2 method validatePassword.
/**
* Validates a password using a hash.
*
* @param password
* the password to check
* @param correctHash
* the hash of the valid password
* @return true if the password is correct, false if not
*/
public boolean validatePassword(char[] password, String correctHash) throws CustomException {
try {
// Decode the hash into its parameters
String[] params = correctHash.split(":");
int iterations = Integer.parseInt(params[ITERATION_INDEX]);
byte[] salt = fromHex(params[SALT_INDEX]);
byte[] hash = fromHex(params[PBKDF2_INDEX]);
// Compute the hash of the provided password, using the same salt,
// iteration count, and hash length
byte[] testHash = pbkdf2(password, salt, iterations, hash.length);
// both hashes match.
return slowEquals(hash, testHash);
} catch (Exception e) {
throw new CustomException(e);
}
}
use of fr.univlorraine.ecandidat.utils.CustomException in project esup-ecandidat by EsupPortail.
the class PasswordHashServicePBKDF2 method createHash.
/**
* Returns a salted PBKDF2 hash of the password.
*
* @param password
* the password to hash
* @return a salted PBKDF2 hash of the password
* @throws CustomException
*/
public String createHash(char[] password) throws CustomException {
try {
// Generate a random salt
SecureRandom random = new SecureRandom();
byte[] salt = new byte[SALT_BYTE_SIZE];
random.nextBytes(salt);
// Hash the password
byte[] hash = pbkdf2(password, salt, PBKDF2_ITERATIONS, HASH_BYTE_SIZE);
return PBKDF2_ITERATIONS + ":" + toHex(salt) + ":" + toHex(hash);
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
throw new CustomException(e);
}
}
use of fr.univlorraine.ecandidat.utils.CustomException in project esup-ecandidat by EsupPortail.
the class CandidatController method saveCompteMinima.
/**
* Enregistre un compte à minima
* @param cptMin
* @return le compte enregistré
*/
private CompteMinima saveCompteMinima(CompteMinima cptMin) {
// Generateur de mot de passe
final PasswordHashService passwordHashUtils = PasswordHashService.getCurrentImplementation();
final Campagne campagne = campagneController.getCampagneActive();
if (campagne == null) {
Notification.show(applicationContext.getMessage("compteMinima.camp.error", null, UI.getCurrent().getLocale()), Type.ERROR_MESSAGE);
return null;
}
cptMin.setCampagne(campagne);
final String prefix = parametreController.getPrefixeNumDossCpt();
Integer sizeNumDossier = ConstanteUtils.GEN_SIZE;
if (prefix != null) {
sizeNumDossier = sizeNumDossier - prefix.length();
}
String numDossierGenere = passwordHashUtils.generateRandomPassword(sizeNumDossier, ConstanteUtils.GEN_NUM_DOSS);
while (isNumDossierExist(numDossierGenere)) {
numDossierGenere = passwordHashUtils.generateRandomPassword(sizeNumDossier, ConstanteUtils.GEN_NUM_DOSS);
}
if (prefix != null) {
numDossierGenere = prefix + numDossierGenere;
}
cptMin.setNumDossierOpiCptMin(numDossierGenere);
final String pwd = passwordHashUtils.generateRandomPassword(ConstanteUtils.GEN_SIZE, ConstanteUtils.GEN_PWD);
try {
cptMin.setPwdCptMin(passwordHashUtils.createHash(pwd));
cptMin.setTypGenCptMin(passwordHashUtils.getType());
} catch (final CustomException e) {
Notification.show(applicationContext.getMessage("compteMinima.pwd.error", null, UI.getCurrent().getLocale()), Type.ERROR_MESSAGE);
return null;
}
/* La date avant destruction */
LocalDateTime datValid = LocalDateTime.now();
final Integer nbJourToKeep = parametreController.getNbJourKeepCptMin();
datValid = datValid.plusDays(nbJourToKeep);
datValid = LocalDateTime.of(datValid.getYear(), datValid.getMonth(), datValid.getDayOfMonth(), 23, 0, 0);
cptMin.setDatFinValidCptMin(datValid);
try {
cptMin = saveBaseCompteMinima(cptMin, campagne);
} catch (final Exception ex) {
logger.error(applicationContext.getMessage("compteMinima.numdossier.error", null, UI.getCurrent().getLocale()) + " numDossier=" + numDossierGenere, ex);
Notification.show(applicationContext.getMessage("compteMinima.numdossier.error", null, UI.getCurrent().getLocale()), Type.ERROR_MESSAGE);
return null;
}
final CptMinMailBean mailBean = new CptMinMailBean(cptMin.getPrenomCptMin(), cptMin.getNomCptMin(), cptMin.getNumDossierOpiCptMin(), pwd, getLienValidation(numDossierGenere), campagneController.getLibelleCampagne(cptMin.getCampagne(), getCodLangueCptMin(cptMin)), formatterDate.format(cptMin.getDatFinValidCptMin()));
mailController.sendMailByCod(cptMin.getMailPersoCptMin(), NomenclatureUtils.MAIL_CPT_MIN, mailBean, null, getCodLangueCptMin(cptMin));
Notification.show(applicationContext.getMessage("compteMinima.create.success", null, UI.getCurrent().getLocale()), Type.WARNING_MESSAGE);
return cptMin;
}
use of fr.univlorraine.ecandidat.utils.CustomException in project esup-ecandidat by EsupPortail.
the class CandidatController method initPasswordOrActivationCode.
/**
* Initialise le pwd du compte
* @param eMail
* @return true si tout se passe bien
*/
public Boolean initPasswordOrActivationCode(final String eMail, final String mode) {
// Generateur de mot de passe
final PasswordHashService passwordHashUtils = PasswordHashService.getCurrentImplementation();
final CompteMinima cptMin = searchCptMinByEMail(eMail);
if (cptMin == null) {
Notification.show(applicationContext.getMessage("compteMinima.id.oublie.mail.err", null, UI.getCurrent().getLocale()), Type.WARNING_MESSAGE);
return false;
}
final String pwd = passwordHashUtils.generateRandomPassword(ConstanteUtils.GEN_SIZE, ConstanteUtils.GEN_PWD);
try {
cptMin.setPwdCptMin(passwordHashUtils.createHash(pwd));
cptMin.setTypGenCptMin(passwordHashUtils.getType());
} catch (final CustomException e) {
Notification.show(applicationContext.getMessage("compteMinima.pwd.error", null, UI.getCurrent().getLocale()), Type.ERROR_MESSAGE);
return false;
}
compteMinimaRepository.save(cptMin);
if (mode.equals(ConstanteUtils.FORGOT_MODE_ID_OUBLIE)) {
final CptMinMailBean mailBean = new CptMinMailBean(cptMin.getPrenomCptMin(), cptMin.getNomCptMin(), cptMin.getNumDossierOpiCptMin(), pwd, null, campagneController.getLibelleCampagne(cptMin.getCampagne(), getCodLangueCptMin(cptMin)), null);
mailController.sendMailByCod(cptMin.getMailPersoCptMin(), NomenclatureUtils.MAIL_CPT_MIN_ID_OUBLIE, mailBean, null, getCodLangueCptMin(cptMin));
Notification.show(applicationContext.getMessage("compteMinima.id.oublie.success", null, UI.getCurrent().getLocale()), Type.HUMANIZED_MESSAGE);
} else {
final CptMinMailBean mailBean = new CptMinMailBean(cptMin.getPrenomCptMin(), cptMin.getNomCptMin(), cptMin.getNumDossierOpiCptMin(), pwd, getLienValidation(cptMin.getNumDossierOpiCptMin()), campagneController.getLibelleCampagne(cptMin.getCampagne(), getCodLangueCptMin(cptMin)), formatterDate.format(cptMin.getDatFinValidCptMin()));
mailController.sendMailByCod(cptMin.getMailPersoCptMin(), NomenclatureUtils.MAIL_CPT_MIN, mailBean, null, getCodLangueCptMin(cptMin));
Notification.show(applicationContext.getMessage("compteMinima.code.oublie.success", null, UI.getCurrent().getLocale()), Type.HUMANIZED_MESSAGE);
}
return true;
}
use of fr.univlorraine.ecandidat.utils.CustomException in project esup-ecandidat by EsupPortail.
the class DroitProfilIndividuWindow method getIndividu.
/**
* Renvoi l'individu construit a partir du people Ldap
* @return l'individu
*/
protected Individu getIndividu() {
if (isModificationMode) {
return null;
} else {
final PeopleLdap people = grid.getSelectedItem();
final Individu individu = new Individu(people);
try {
individuController.validateIndividuBean(individu);
return individu;
} catch (final CustomException e) {
Notification.show(e.getMessage(), Notification.Type.WARNING_MESSAGE);
return null;
}
}
}
Aggregations