Subversion Repositories SmartDukaan

Rev

Rev 24330 | Rev 24479 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
24330 amit.gupta 1
package com.spice.profitmandi.service;
2
 
3
import java.nio.charset.StandardCharsets;
4
 
5
import org.apache.commons.lang.RandomStringUtils;
6
import org.apache.logging.log4j.LogManager;
7
import org.apache.logging.log4j.Logger;
8
import org.springframework.beans.factory.annotation.Autowired;
9
import org.springframework.mail.javamail.JavaMailSender;
24389 amit.gupta 10
import org.springframework.stereotype.Component;
24330 amit.gupta 11
 
12
import com.google.common.hash.Hashing;
13
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
14
import com.spice.profitmandi.common.util.Utils;
15
import com.spice.profitmandi.dao.entity.auth.AuthUser;
16
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
17
 
24389 amit.gupta 18
@Component
24330 amit.gupta 19
public class AuthServiceImpl implements AuthService {
20
 
21
	// private static final String RESET_PASSWORD_BODY = "Dear %s, your password has
22
	// been reset. Please click this <a href=\"%s\">link</a> to reset your
23
	// password.\n\nRegards\nSmartdukaan";
24
	private static final String RESET_PASSWORD_BODY = "Dear %s, your password has been reset to %s. Regards\nSmartdukaan";
25
	private static final String RESET_PASSWORD_SUBJECT = "Password Reset request";
26
 
27
	private static final Logger LOGGER = LogManager.getLogger(AuthServiceImpl.class);
28
 
29
	@Autowired
30
	AuthRepository authRepository;
31
 
32
	@Autowired
33
	JavaMailSender mailSender;
34
 
35
	private String getHash256(String originalString) {
36
		return Hashing.sha256().hashString(originalString, StandardCharsets.UTF_8).toString();
37
	}
38
 
39
	@Override
40
	public boolean authenticate(String emailOrMobile, String password) {
41
		return authRepository.authenticate(emailOrMobile, getHash256(password));
42
	}
43
 
44
	@Override
45
	public void resetPassword(String emailOrMobile) throws ProfitMandiBusinessException {
46
		AuthUser authUser = authRepository.selectByEmailOrMobile(emailOrMobile);
47
		String password = getRandomString();
48
		try {
24389 amit.gupta 49
			Utils.sendMailWithAttachments(mailSender, authUser.getEmailId(), null, RESET_PASSWORD_SUBJECT,
50
					String.format(RESET_PASSWORD_BODY, authUser.getFirstName(), password), null);
51
		} catch (Exception e) {
52
			throw new ProfitMandiBusinessException("Password Reset Email", emailOrMobile,
53
					"Could not send password reset mail. Password Could not be reset");
24330 amit.gupta 54
		}
55
		authUser.setPassword(getHash256(password));
56
		authRepository.persist(authUser);
57
	}
58
 
59
	@Override
60
	public void changePassword(String emailOrMobile, String oldPassword, String newPassword)
61
			throws ProfitMandiBusinessException {
62
		if (authRepository.authenticate(emailOrMobile, getHash256(oldPassword))) {
63
			AuthUser authUser = authRepository.selectByEmailOrMobile(emailOrMobile);
64
			authUser.setPassword(getHash256(newPassword));
65
			authRepository.persist(authUser);
66
		} else {
67
			throw new ProfitMandiBusinessException("Authentication", "Credentials", "Invalid email/mobile or password");
68
		}
69
	}
70
 
71
	@Override
72
	public void addAuthUser(AuthUser authUser) throws ProfitMandiBusinessException {
24389 amit.gupta 73
		try {
74
			authRepository.selectByEmailOrMobile(authUser.getEmailId());
75
		} catch(ProfitMandiBusinessException pbse) {
76
			try {
77
				authRepository.selectByEmailOrMobile(authUser.getMobileNumber());
78
			} catch(ProfitMandiBusinessException e) {
79
				authRepository.persist(authUser);
80
				this.resetPassword(authUser.getEmailId());
81
				return;
82
			}
83
			throw new ProfitMandiBusinessException("Mobile", authUser.getMobileNumber(), "Mobile number already exist");
84
		}	
85
		throw new ProfitMandiBusinessException("Email", authUser.getEmailId(), "Email Id already exist");
86
 
24330 amit.gupta 87
	}
88
 
89
	private String getRandomString() {
90
		int length = 10;
91
		boolean useLetters = true;
92
		boolean useNumbers = false;
93
		String generatedString = RandomStringUtils.random(length, useLetters, useNumbers);
94
		return generatedString;
95
	}
96
 
97
}