๐Ÿ“ฆ savankansagra / transportOne

๐Ÿ“„ OtpService.java ยท 151 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151package com.transport.services;

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import javax.mail.MessagingException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.transport.controller.DriverController;
import com.transport.payloads.UserLoginEmail;
import com.transport.payloads.UserRequestRegister;

@Service
public class OtpService {

	private static final Integer EXPIRE_MINS = 10;
	private LoadingCache<String, Integer> otpCache;
	Logger logger = LoggerFactory.getLogger(OtpService.class);
	
	@Autowired
	private EmailService emailService; 
	
	@Autowired
	private SmsSenderService smsSenderService;
	
	public OtpService() {
		super();
		otpCache = CacheBuilder.newBuilder()
				.expireAfterWrite(EXPIRE_MINS, TimeUnit.MINUTES)
				.build(new CacheLoader<String, Integer>() {
					@Override
					public Integer load(String key) throws Exception {
						return 0;
					}
				});
		
	}
	
	public int generateOtp(String key) {
		Random random = new Random();
		int otp = 100000 + random.nextInt(900000);
		otpCache.put(key, otp);
		logger.info("method=generateOtp | generated otp is:" + otp );
		return otp;
	}
	
	
	public void generateAndSendtoEmail(String emailAddress) {
		// generate otp.
		int otp = this.generateOtp(emailAddress);
		
		//send to email
		String to = emailAddress;
		String subject = "Verification OTP code";
		String message = "Verification OTP code is "+otp;
		try {
			emailService.sendOtpMessage(to, subject, message);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public void generateAndSendtoTelephoneNumber(String telephoneNumber) {
		//generate otp
		int otp = this.generateOtp(telephoneNumber);
				
		//send to mobile
		try {
			smsSenderService.sendSmsByService(telephoneNumber, otp);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	
	
	public void generateAndSendtoEmailAndSendtoMobile(UserRequestRegister userRequestRegister) {
		//generate otp
		int otp = this.generateOtp(userRequestRegister.getTelephoneNumber());
		
		// make async call to the sending email function.
		ExecutorService executor = Executors.newSingleThreadExecutor();
		Callable<Void> task = new Callable<Void>() {

			@Override
			public Void call() throws Exception {
				//send to email
				String to = userRequestRegister.getUserEmail();
				String subject = "Verification OTP code";
				String message = "Verification OTP code is "+otp;
				try {
					emailService.sendOtpMessage(to, subject, message);
				} catch (Exception e) {
					// TODO: handle exception
				}
				return null;
			}
			
		};
		executor.submit(task);
		
		
		// TODO : check authentication of sending OTP to mobile.
		/*
		//send to mobile		
		try {
			smsSenderService.sendSmsByService(userRequestRegister.getTelephoneNumber(), otp);
		} catch (Exception e) {
			e.printStackTrace();
		}
		*/
	}
	
	public void clearOtp(String key) {
		otpCache.invalidate(key);
	}

	
	public boolean isOtpValid(String userTelephoneNumber, Integer userOtp) {
		boolean successFlag = false;
			
		Integer systemOtp;
		try {
			systemOtp = otpCache.get(userTelephoneNumber);
		} catch (Exception e) {
			systemOtp = 0;
		}
			
		if(userOtp.intValue() == systemOtp.intValue()) {
			successFlag = true;
			this.clearOtp(userTelephoneNumber);
		}
			
		return successFlag;
	}	
	
	
}