/*
 * The MIT License (MIT)
 * 
 * Copyright (c) 2013-2017 cFrost.sun(孙宾, SUN BIN) 
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * For more information, please visit the following website
 * 
 * https://eulerproject.io
 * https://github.com/euler-projects/euler-framework
 * https://cfrost.net
 */
package net.eulerframework.web.module.authentication.service;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.jwt.Jwt;
import org.springframework.stereotype.Service;

import net.eulerframework.common.email.ThreadSimpleMailSender;
import net.eulerframework.common.util.CommonUtils;
import net.eulerframework.common.util.jwt.InvalidJwtException;
import net.eulerframework.common.util.jwt.JwtEncryptor;
import net.eulerframework.web.config.WebConfig;
import net.eulerframework.web.module.authentication.entity.EulerUserEntity;
import net.eulerframework.web.module.authentication.exception.InvalidEmailResetTokenException;
import net.eulerframework.web.module.authentication.exception.InvalidSmsResetPinException;
import net.eulerframework.web.module.authentication.exception.UserNotFoundException;
import net.eulerframework.web.module.authentication.vo.UserResetJwtClaims;
import net.eulerframework.web.util.ServletUtils;

/**
 * @author cFrost
 *
 */
@Service("passwordService")
public class PasswordServiceImpl implements PasswordService {
    
    @Resource private PasswordEncoder passwordEncoder;
    @Resource private EulerUserEntityService eulerUserEntityService;
    @Resource private JwtEncryptor jwtEncryptor;
    
    @Autowired(required = false)
    private ThreadSimpleMailSender threadSimpleMailSender;

    @Override
    public PasswordEncoder getPasswordEncoder() {
        return this.passwordEncoder;
    }

    @Override
    public EulerUserEntityService getEulerUserEntityService() {
        return this.eulerUserEntityService;
    }

    @Override
    public void passwdResetSMSGen(String mobile) {
        // TODO Auto-generated method stub
    }

    @Override
    public void passwdResetEmailGen(String email) {
        EulerUserEntity user;
        try {
            user = this.eulerUserEntityService.loadUserByEmail(email);
        } catch (UserNotFoundException e) {
            CommonUtils.sleep(1);
            return;
        }
        
        UserResetJwtClaims claims = new UserResetJwtClaims(user, 30 * 60);
        Jwt jwt = this.jwtEncryptor.encode(claims);
        String token = jwt.getEncoded();
        String resetPasswordUrl = 
                WebConfig.getWebUrl() 
                + ServletUtils.getRequest().getContextPath() 
                + "/reset-password?type=EMAIL&token="
                + token;
        String content = "<p>You can use the following link to reset your password in 10 minutes:</p><p><a href=\"" + resetPasswordUrl + "\">" + resetPasswordUrl + "</a></p>";
        if(this.threadSimpleMailSender != null) {
            this.threadSimpleMailSender.send("Reset Passwowrd", content, email);
        } else {
            System.out.println(content);
        }
    }

    @Override
    public String analyzeUserIdFromSmsResetPin(String pin) throws InvalidSmsResetPinException {
        // TODO Auto-generated method stub
        throw new InvalidSmsResetPinException();
    }

    @Override
    public String analyzeUserIdFromEmailResetToken(String token) throws InvalidEmailResetTokenException {
        UserResetJwtClaims claims;
        try {
            claims = this.jwtEncryptor.decode(token, UserResetJwtClaims.class);
        } catch (InvalidJwtException e) {
            throw new InvalidEmailResetTokenException();
        }
        return claims.getUserId();
    }

}

@@ -2524,20 +2524,16 @@
 rvice %7B%0A
-    
 %0A    @Re
@@ -2530,32 +2530,36 @@
 %7B%0A%0A    @Resource
+%0A   
  private Passwor
@@ -2589,32 +2589,36 @@
 r;%0A    @Resource
+%0A   
  private EulerUs
@@ -2670,16 +2670,20 @@
 Resource
+%0A   
  private
@@ -2710,24 +2710,140 @@
 ryptor;%0A    
+@Resource%0A    private String resetPasswordEmailSubject;%0A    @Resource%0A    private String resetPasswordEmailContent;%0A
 %0A    @Autowi
@@ -3569,17 +3569,9 @@
   %7D%0A
-        
 %0A
+
     
@@ -3768,33 +3768,16 @@
 rdUrl = 
-%0A                
 WebConfi
@@ -3790,33 +3790,16 @@
 ebUrl() 
-%0A                
 + Servle
@@ -3834,17 +3834,16 @@
 xtPath()
- 
 %0A       
@@ -3888,32 +3888,16 @@
 &token=%22
-%0A               
  + token
@@ -3927,101 +3927,57 @@
 t = 
-%22%3Cp%3EYou can use the following link to reset your password in 10 minutes:%3C/p%3E%3Cp%3E%3Ca href=%5C%22%22 + 
+this.resetPasswordEmailContent.replaceAll(%22%5C%5C$%5C%5C%7B
 rese
@@ -3992,18 +3992,13 @@
 dUrl
- + %22%5C%22%3E%22 +
+%5C%5C%7D%22,
  res
@@ -4014,21 +4014,9 @@
 dUrl
- + %22%3C/a%3E%3C/p%3E%22
+)
 ;%0A  
@@ -4023,16 +4023,17 @@
       if
+ 
 (this.th
@@ -4113,25 +4113,38 @@
 end(
-%22R
+this.r
 eset
- 
 Passwo
-w
 rd
-%22
+EmailSubject
 , co

reset password email content