1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package com.hack23.cia.service.impl.action.application;
20
21 import java.util.ArrayList;
22 import java.util.Collection;
23
24 import org.apache.commons.lang3.StringUtils;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.security.access.annotation.Secured;
29 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
30 import org.springframework.security.core.authority.SimpleGrantedAuthority;
31 import org.springframework.security.core.context.SecurityContextHolder;
32 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
33 import org.springframework.security.crypto.password.PasswordEncoder;
34 import org.springframework.stereotype.Service;
35 import org.springframework.transaction.annotation.Propagation;
36 import org.springframework.transaction.annotation.Transactional;
37
38 import com.hack23.cia.model.internal.application.system.impl.ApplicationEventGroup;
39 import com.hack23.cia.model.internal.application.system.impl.ApplicationOperationType;
40 import com.hack23.cia.model.internal.application.user.impl.UserAccount;
41 import com.hack23.cia.model.internal.application.user.impl.UserAccount_;
42 import com.hack23.cia.model.internal.application.user.impl.UserRole;
43 import com.hack23.cia.service.api.action.application.CreateApplicationEventRequest;
44 import com.hack23.cia.service.api.action.application.CreateApplicationEventResponse;
45 import com.hack23.cia.service.api.action.application.LoginRequest;
46 import com.hack23.cia.service.api.action.application.LoginResponse;
47 import com.hack23.cia.service.api.action.common.ServiceResponse.ServiceResult;
48 import com.hack23.cia.service.data.api.UserDAO;
49 import com.hack23.cia.service.impl.action.common.AbstractBusinessServiceImpl;
50 import com.hack23.cia.service.impl.action.common.BusinessService;
51 import com.warrenstrange.googleauth.GoogleAuthenticator;
52
53
54
55
56 @Service
57 @Transactional(propagation = Propagation.REQUIRED)
58 public final class LoginService extends AbstractBusinessServiceImpl<LoginRequest, LoginResponse>
59 implements BusinessService<LoginRequest, LoginResponse> {
60
61
62 private static final Logger LOGGER = LoggerFactory.getLogger(LoginService.class);
63
64
65 @Autowired
66 private UserDAO userDAO;
67
68
69 @Autowired
70 private BusinessService<CreateApplicationEventRequest, CreateApplicationEventResponse> createApplicationEventService;
71
72
73 private final PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
74
75
76
77
78 public LoginService() {
79 super(LoginRequest.class);
80 }
81
82 @Secured({ "ROLE_ANONYMOUS", "ROLE_USER", "ROLE_ADMIN" })
83 @Override
84 public LoginResponse processService(final LoginRequest serviceRequest) {
85
86 final CreateApplicationEventRequest eventRequest = new CreateApplicationEventRequest();
87 eventRequest.setEventGroup(ApplicationEventGroup.USER);
88 eventRequest.setApplicationOperation(ApplicationOperationType.AUTHENTICATION);
89 eventRequest.setActionName(LoginRequest.class.getSimpleName());
90 eventRequest.setSessionId(serviceRequest.getSessionId());
91 eventRequest.setElementId(serviceRequest.getEmail());
92
93 final UserAccount userExist = userDAO.findFirstByProperty(UserAccount_.email, serviceRequest.getEmail());
94
95 LoginResponse response;
96 if (userExist != null && verifyOtp(serviceRequest, userExist) && passwordEncoder.matches(
97 userExist.getUserId() + ".uuid" + serviceRequest.getUserpassword(), userExist.getUserpassword())) {
98
99 final Collection<SimpleGrantedAuthority> authorities = new ArrayList<>();
100
101 if (UserRole.ADMIN == userExist.getUserRole()) {
102 authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
103 } else if (UserRole.USER == userExist.getUserRole()) {
104 authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
105 }
106
107 eventRequest.setUserId(userExist.getUserId());
108
109 SecurityContextHolder.getContext().setAuthentication(
110 new UsernamePasswordAuthenticationToken(userExist, userExist.getUserpassword(), authorities));
111
112 userExist.setNumberOfVisits(userExist.getNumberOfVisits() + 1);
113 userDAO.persist(userExist);
114 response = new LoginResponse(ServiceResult.SUCCESS);
115
116 } else {
117 response = new LoginResponse(ServiceResult.FAILURE);
118 }
119 eventRequest.setApplicationMessage(response.getResult().toString());
120
121 createApplicationEventService.processService(eventRequest);
122 LOGGER.info("Event: {}", eventRequest);
123
124 return response;
125 }
126
127 private static boolean verifyOtp(final LoginRequest serviceRequest, final UserAccount userExist) {
128 boolean authorizedOtp = true;
129
130 if (userExist.getGoogleAuthKey() != null) {
131 final GoogleAuthenticator gAuth = new GoogleAuthenticator();
132
133 if (!StringUtils.isBlank(serviceRequest.getOtpCode())
134 && StringUtils.isNumeric(serviceRequest.getOtpCode())) {
135 authorizedOtp = gAuth.authorize(userExist.getGoogleAuthKey(),
136 Integer.parseInt(serviceRequest.getOtpCode()));
137 } else {
138 authorizedOtp = false;
139 }
140 }
141 return authorizedOtp;
142 }
143
144 }