0
I am developing a web application using the play 1.4 framework (didactic version) and I want to implement the password recovery functionality where the user will place his email and will be sent a temporary link to reset his password. My doubt, how to generate this temporary link send to completed email and validate the new password?
My MODEL works where already add public String token; Date tokenValidityDate; I know you will need: `package models;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import play.data.validation.MinSize;
import play.data.validation.Required;
import play.db.jpa.Model;
import sun.util.calendar.BaseCalendar.Date;
@Entity
public class Funcionario extends Model {
final static String chave = "0123456789abcdef";
private static final String ALGORITMO = "AES";
private static byte[] mensagemEncriptada;
private static byte[] mensagemDescriptada;
private static SecretKey key;
public String nome;
public String funcao;
public String nivelAcesso;
public String login;
@MinSize(4)
public String senha;
public String email;
public String token;
Date tokenValidityDate;
@Enumerated(EnumType.STRING)
public Status status;
public Funcionario() {
status = Status.ATIVO;
}
public boolean autenticar() throws Exception {
Funcionario u = Funcionario.find("login = ? and senha = ?", login, descriptografar(senha)).first();
if (u == null) {
return false;
} else {
return true;
}
}
public static String descriptografar(String mensagem) throws Exception {
key = new SecretKeySpec(chave.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
mensagemDescriptada = cipher.doFinal(mensagem.getBytes());
return StringUtils.trim(Base64.encodeBase64String(mensagemDescriptada));
}
}
` My html page to recover password:
<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<html>
<head>
<meta charset="ISO-8859-1">
<link rel="stylesheet" media="screen" href="@{'/public/stylesheets/login.css'}">
<title>Octopus</title>
</head>
<body background="/public/images/back.png">
<form action="@{Logins.logar}" method="post">
<input type="hidden" name="login.id" value="${u?.id}" />
<div class="login">
<p><center><small>Digite seu endereço de e-mail para redefinir a senha.</small></center></p></small>
<input type="text" placeholder="E-mail" name="funcionario.email" value="${flash['funcionario.email'] ? flash['funcionario.email'] : u?.email}" >
<span class="bg-danger">#{error 'login.email' /}</span>
<input type="submit" value="Enviar">
</div>
<div class="shadow"></div>
</form>
</body>
</html>
What I would do at my current level was what came to mind: When requesting the password exchange, the email would be passed, this email served to search which user is registered and send the link when generated. Discovered which user, someone would generate a token that would represent that user and serve as a parameter to a valid url. The requester would receive in the email the link (url+token), when accessing would be verified if this token is valid, if yes, search the user who is represented by the token, perform the password exchange and invalidates the token, so that this action can no longer be accessed.
– G. M4rc14L
exactly that, but in practice I have no idea how to implement, you have some similar example that can help me?
– Carlos Diego
I’ll take a moment to write a response debugging that "theory" to help you get a light in that tunnel.
– G. M4rc14L
opa, I am grateful
– Carlos Diego