You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
149 lines
4.4 KiB
149 lines
4.4 KiB
package api.menu.playa.helper;
|
|
|
|
import java.io.InputStream;
|
|
import java.security.Key;
|
|
import java.security.KeyFactory;
|
|
import java.security.PrivateKey;
|
|
import java.security.spec.PKCS8EncodedKeySpec;
|
|
import java.util.Base64;
|
|
import java.util.Collections;
|
|
import java.util.HashSet;
|
|
import java.util.Optional;
|
|
import java.util.Set;
|
|
|
|
import javax.crypto.Cipher;
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
import javax.enterprise.context.ApplicationScoped;
|
|
import javax.inject.Inject;
|
|
|
|
import org.eclipse.microprofile.jwt.Claims;
|
|
import org.eclipse.microprofile.jwt.JsonWebToken;
|
|
import org.jboss.logging.Logger;
|
|
import org.jose4j.jwk.JsonWebKey;
|
|
import org.jose4j.jws.JsonWebSignature;
|
|
import org.jose4j.keys.resolvers.JwksVerificationKeyResolver;
|
|
|
|
import api.menu.playa.dao.UsuarioDAO;
|
|
import api.menu.playa.enums.RolesEnum;
|
|
import api.menu.playa.exceptions.NegocioException;
|
|
import api.menu.playa.model.Usuario;
|
|
import api.menu.playa.util.TokenUtils;
|
|
import api.menu.playa.vo.RegistroVO;
|
|
import api.menu.playa.vo.UsuarioVO;
|
|
import io.smallrye.jwt.auth.principal.DefaultJWTParser;
|
|
import io.smallrye.jwt.build.Jwt;
|
|
import io.smallrye.jwt.build.JwtSignature;
|
|
|
|
@ApplicationScoped
|
|
public class LoginHelper {
|
|
|
|
@Inject
|
|
Logger logger;
|
|
|
|
@Inject
|
|
UsuarioDAO usuarioDAO;
|
|
|
|
@Inject
|
|
TokenService tokenService;
|
|
|
|
public void registrarUsuario(RegistroVO request) throws Exception {
|
|
|
|
Optional<Usuario> user = usuarioDAO.encontrarUsuario(request.getUser());
|
|
|
|
if (user.isPresent()) {
|
|
throw new NegocioException("El usuario ya existe", 2);
|
|
}
|
|
|
|
Usuario usuario = new Usuario();
|
|
|
|
usuario.setActivo(false);
|
|
usuario.setUser(request.getUser());
|
|
usuario.setPass(encript(request.getPass()));
|
|
usuario.setRol(RolesEnum.USER);
|
|
usuario.setNombre(request.getUser());
|
|
|
|
usuarioDAO.save(usuario);
|
|
}
|
|
|
|
public String validarUsuario(UsuarioVO request) throws Exception {
|
|
|
|
Optional<Usuario> user = usuarioDAO.encontrarUsuario(request.getUser());
|
|
|
|
logger.info("Usuario");
|
|
logger.info(user);
|
|
|
|
if (user.isEmpty()) {
|
|
throw new NegocioException("Usuario no existe en el sistema", 2);
|
|
}
|
|
|
|
String password = encript(request.getPass());
|
|
|
|
logger.info("Password encriptado");
|
|
logger.info(password);
|
|
|
|
if (Boolean.FALSE.equals(user.get().getActivo())) {
|
|
throw new NegocioException("Usuario no se encuentra activo", 2);
|
|
}
|
|
|
|
if (!password.equals(user.get().getPass())) {
|
|
throw new NegocioException("Usuario o contraseña es incorrecto", 2);
|
|
}
|
|
|
|
return tokenService.generate(user.get().getNombre(), user.get().getUser(), user.get().getFecha(), user.get().getRol());
|
|
//return TokenUtils.generateToken(user.get().getUser(), Collections.singleton(user.get().getRol()), 36000L, "https://vodorod.cl");
|
|
|
|
|
|
|
|
|
|
|
|
//return tokenGenerator(user.get().getUser(), Collections.singleton(user.get().getRol()), 36000L, "https://vodorod.cl");
|
|
}
|
|
|
|
private static String ENCRYPT_KEY = "clave-compartida-no-reveloar-nun";
|
|
|
|
private String encript(String text) throws Exception {
|
|
Key aesKey = new SecretKeySpec(ENCRYPT_KEY.getBytes(), "AES");
|
|
|
|
Cipher cipher = Cipher.getInstance("AES");
|
|
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
|
|
|
|
byte[] encrypted = cipher.doFinal(text.getBytes());
|
|
|
|
return Base64.getEncoder().encodeToString(encrypted);
|
|
}
|
|
|
|
private static String decrypt(String encrypted) throws Exception {
|
|
byte[] encryptedBytes = Base64.getDecoder().decode(encrypted.replace("\n", ""));
|
|
|
|
Key aesKey = new SecretKeySpec(ENCRYPT_KEY.getBytes(), "AES");
|
|
|
|
Cipher cipher = Cipher.getInstance("AES");
|
|
cipher.init(Cipher.DECRYPT_MODE, aesKey);
|
|
|
|
String decrypted = new String(cipher.doFinal(encryptedBytes));
|
|
|
|
return decrypted;
|
|
}
|
|
|
|
public int currentTimeInSecs() {
|
|
long currentTimeMS = System.currentTimeMillis();
|
|
return (int) (currentTimeMS / 1000);
|
|
}
|
|
|
|
public String tokenGenerator(String username, Set<RolesEnum> roles, Long duration, String issuer) throws Exception {
|
|
|
|
Set<String> groups = new HashSet<>();
|
|
for (RolesEnum role : roles) groups.add(role.toString());
|
|
|
|
return Jwt.claims()
|
|
.issuer(issuer)
|
|
.issuedAt(currentTimeInSecs())
|
|
.expiresAt(currentTimeInSecs() + duration)
|
|
.groups(groups)
|
|
.claim("nombre", username).jws()
|
|
.signWithSecret("issuer11111111111111111111111111");
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|