public JSONObject validatePasswordRecovery(String b64Token) throws Exception {
byte[] encryptedToken = Base64.decodeBase64(b64Token);
Token token = TokenEncryptor.decryptToken(serverKey, encryptedToken);
if( token instanceof PasswordRecoveryToken ){
PasswordRecoveryToken passwordRecoveryToken = (PasswordRecoveryToken)token;
// Check expiry time
Date expiry = passwordRecoveryToken.getExpiry();
if( null != expiry ){
Date now = new Date();
if( now.getTime() > expiry.getTime() ){
throw new TokenExpiredException("Token is expired");
}
}
// Check if user already exists
String emailAddress = passwordRecoveryToken.getEmailAddress();
if( null == emailAddress ) {
throw new Exception("Token does not specify an e-mail address");
}
JSONObject userDoc = null;
try {
logger.error("userRepository: "+userRepository);
userDoc = userRepository.getUserFromEmailAddress(emailAddress);
} catch(Exception e) {
logger.error("Error",e);
throw new Exception("There is no user associated with the e-mail address: "+emailAddress+
". You must first create a user account.",e);
}
// Verify that user document was not modified since the token was generated
{
String rev = userDoc.optString("_rev");
if( null == rev ){
throw new Exception("Revision not available from user document");
}
String tokenRev = passwordRecoveryToken.getVersion();
if( null == tokenRev ){
throw new Exception("Revision not provided in password recovery token");
}
if( false == tokenRev.equals( rev.substring(0, tokenRev.length()) ) ){
throw new UserUpdatedException("Password recovery token refers to an older version of the user document");
}
}
JSONObject result = new JSONObject();
result.put("valid", true);
result.put("emailAddress", passwordRecoveryToken.getEmailAddress());
result.put("name", userDoc.getString("name"));
return result;
} else {
throw new Exception("Unexpected token class: "+token.getClass().getName());
}