Search in sources :

Example 1 with CustomException

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);
    }
}
Also used : CustomException(fr.univlorraine.ecandidat.utils.CustomException) CustomException(fr.univlorraine.ecandidat.utils.CustomException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 2 with CustomException

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);
    }
}
Also used : SecureRandom(java.security.SecureRandom) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) CustomException(fr.univlorraine.ecandidat.utils.CustomException)

Example 3 with CustomException

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;
}
Also used : LocalDateTime(java.time.LocalDateTime) CptMinMailBean(fr.univlorraine.ecandidat.utils.bean.mail.CptMinMailBean) PasswordHashService(fr.univlorraine.ecandidat.services.security.PasswordHashService) CustomException(fr.univlorraine.ecandidat.utils.CustomException) Campagne(fr.univlorraine.ecandidat.entities.ecandidat.Campagne) CustomException(fr.univlorraine.ecandidat.utils.CustomException) SiScolException(fr.univlorraine.ecandidat.services.siscol.SiScolException)

Example 4 with CustomException

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;
}
Also used : CompteMinima(fr.univlorraine.ecandidat.entities.ecandidat.CompteMinima) CptMinMailBean(fr.univlorraine.ecandidat.utils.bean.mail.CptMinMailBean) PasswordHashService(fr.univlorraine.ecandidat.services.security.PasswordHashService) CustomException(fr.univlorraine.ecandidat.utils.CustomException)

Example 5 with CustomException

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;
        }
    }
}
Also used : PeopleLdap(fr.univlorraine.ecandidat.services.ldap.PeopleLdap) Individu(fr.univlorraine.ecandidat.entities.ecandidat.Individu) CustomException(fr.univlorraine.ecandidat.utils.CustomException)

Aggregations

CustomException (fr.univlorraine.ecandidat.utils.CustomException)7 Individu (fr.univlorraine.ecandidat.entities.ecandidat.Individu)2 PasswordHashService (fr.univlorraine.ecandidat.services.security.PasswordHashService)2 CptMinMailBean (fr.univlorraine.ecandidat.utils.bean.mail.CptMinMailBean)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)2 Campagne (fr.univlorraine.ecandidat.entities.ecandidat.Campagne)1 CompteMinima (fr.univlorraine.ecandidat.entities.ecandidat.CompteMinima)1 PreferenceInd (fr.univlorraine.ecandidat.entities.ecandidat.PreferenceInd)1 PeopleLdap (fr.univlorraine.ecandidat.services.ldap.PeopleLdap)1 SecurityUserGestionnaire (fr.univlorraine.ecandidat.services.security.SecurityUserGestionnaire)1 SiScolException (fr.univlorraine.ecandidat.services.siscol.SiScolException)1 SecureRandom (java.security.SecureRandom)1 LocalDateTime (java.time.LocalDateTime)1 ArrayList (java.util.ArrayList)1 ConstraintViolation (javax.validation.ConstraintViolation)1 Validator (javax.validation.Validator)1 ValidatorFactory (javax.validation.ValidatorFactory)1 GrantedAuthority (org.springframework.security.core.GrantedAuthority)1 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)1