Subversion Repositories SmartDukaan

Rev

Rev 24389 | Go to most recent revision | Details | 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;
10
 
11
import com.google.common.hash.Hashing;
12
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
13
import com.spice.profitmandi.common.util.Utils;
14
import com.spice.profitmandi.dao.entity.auth.AuthUser;
15
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
16
 
17
public class AuthServiceImpl implements AuthService {
18
 
19
	// private static final String RESET_PASSWORD_BODY = "Dear %s, your password has
20
	// been reset. Please click this <a href=\"%s\">link</a> to reset your
21
	// password.\n\nRegards\nSmartdukaan";
22
	private static final String RESET_PASSWORD_BODY = "Dear %s, your password has been reset to %s. Regards\nSmartdukaan";
23
	private static final String RESET_PASSWORD_SUBJECT = "Password Reset request";
24
 
25
	private static final Logger LOGGER = LogManager.getLogger(AuthServiceImpl.class);
26
 
27
	@Autowired
28
	AuthRepository authRepository;
29
 
30
	@Autowired
31
	JavaMailSender mailSender;
32
 
33
	private String getHash256(String originalString) {
34
		return Hashing.sha256().hashString(originalString, StandardCharsets.UTF_8).toString();
35
	}
36
 
37
	@Override
38
	public boolean authenticate(String emailOrMobile, String password) {
39
		return authRepository.authenticate(emailOrMobile, getHash256(password));
40
	}
41
 
42
	@Override
43
	public void resetPassword(String emailOrMobile) throws ProfitMandiBusinessException {
44
		AuthUser authUser = authRepository.selectByEmailOrMobile(emailOrMobile);
45
		String password = getRandomString();
46
		try {
47
			Utils.sendMailWithAttachments(mailSender, authUser.getEmailId(), null, RESET_PASSWORD_SUBJECT, String.format(RESET_PASSWORD_BODY, authUser.getFirstName(), password), null);
48
		} catch(Exception e) {
49
			throw new ProfitMandiBusinessException("Password Reset Email", emailOrMobile, "Could not send password reset mail. Password Could not be reset");
50
		}
51
		authUser.setPassword(getHash256(password));
52
		authRepository.persist(authUser);
53
	}
54
 
55
	@Override
56
	public void changePassword(String emailOrMobile, String oldPassword, String newPassword)
57
			throws ProfitMandiBusinessException {
58
		if (authRepository.authenticate(emailOrMobile, getHash256(oldPassword))) {
59
			AuthUser authUser = authRepository.selectByEmailOrMobile(emailOrMobile);
60
			authUser.setPassword(getHash256(newPassword));
61
			authRepository.persist(authUser);
62
		} else {
63
			throw new ProfitMandiBusinessException("Authentication", "Credentials", "Invalid email/mobile or password");
64
		}
65
	}
66
 
67
	@Override
68
	public void addAuthUser(AuthUser authUser) throws ProfitMandiBusinessException {
69
		authRepository.persist(authUser);
70
		this.resetPassword(authUser.getEmailId());
71
	}
72
 
73
	private String getRandomString() {
74
		int length = 10;
75
		boolean useLetters = true;
76
		boolean useNumbers = false;
77
		String generatedString = RandomStringUtils.random(length, useLetters, useNumbers);
78
		return generatedString;
79
	}
80
 
81
}