1 package org.openkilda.controller;
3 import org.springframework.beans.factory.annotation.Autowired;
4 import org.springframework.security.authentication.AuthenticationManager;
5 import org.springframework.security.authentication.BadCredentialsException;
6 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
7 import org.springframework.security.core.Authentication;
8 import org.springframework.security.core.context.SecurityContextHolder;
9 import org.springframework.security.core.userdetails.UsernameNotFoundException;
10 import org.springframework.stereotype.Controller;
11 import org.springframework.ui.Model;
12 import org.springframework.web.bind.annotation.RequestMapping;
13 import org.springframework.web.bind.annotation.RequestMethod;
14 import org.springframework.web.bind.annotation.RequestParam;
15 import org.springframework.web.servlet.ModelAndView;
17 import java.util.HashSet;
18 import java.util.List;
21 import javax.servlet.http.HttpServletRequest;
23 import org.apache.log4j.Logger;
48 private static final Logger LOGGER = Logger.getLogger(
LoginController.class);
51 private AuthenticationManager authenticationManager;
65 @RequestMapping(
value = {
"/",
"/login"})
66 public ModelAndView
login(
final HttpServletRequest request) {
76 @RequestMapping(
"/logout")
77 public ModelAndView
logout(final Model model) {
89 @RequestMapping(
value =
"/authenticate", method = RequestMethod.POST)
90 public ModelAndView
authenticate(@RequestParam("username") final String username,
91 @RequestParam("password") final String password, final HttpServletRequest request) {
92 LOGGER.info(
"[authenticate] - start");
95 UsernamePasswordAuthenticationToken token =
96 new UsernamePasswordAuthenticationToken(username, password);
98 token.setDetails(customWebAuthenticationDetails);
101 Authentication
authenticate = authenticationManager.authenticate(token);
105 populateUserInfo(userInfo, username);
107 SecurityContextHolder.getContext().setAuthentication(
authenticate);
108 userService.updateLoginDetail(username);
110 error =
"Invalid email or password";
112 "authenticate() Authentication failure with username{} and password{}");
116 LOGGER.error(
"2 FA Key not set for user: '" + username +
"'.");
117 modelAndView.addObject(
"username", username);
118 modelAndView.addObject(
"password", password);
121 modelAndView.addObject(
"key", secretKey);
122 userService.updateUser2FAKey(username, secretKey);
126 LOGGER.error(
"OTP required for user: '" + username +
"'.");
127 modelAndView.addObject(
"username", username);
128 modelAndView.addObject(
"password", password);
131 LOGGER.error(
"Authentication code is invalid for user: '" + username +
"'.");
132 error =
"Authentication code is invalid";
133 modelAndView.addObject(
"username", username);
134 modelAndView.addObject(
"password", password);
136 UserEntity userInfo = userService.getUserByUsername(username);
137 modelAndView.addObject(
"key", userInfo.
getTwoFaKey());
142 }
catch (UsernameNotFoundException | BadCredentialsException e) {
143 LOGGER.error(
"authenticate() Authentication failure", e);
144 error =
"Invalid email or password";
146 }
catch (Exception e) {
147 LOGGER.error(
"authenticate() Authentication failure", e);
148 error =
"Login Failed. Error: '" + e.getMessage() +
"'.";
153 modelAndView.addObject(
"error",
error);
165 private void populateUserInfo(
final UserInfo userInfo,
final String username) {
166 UserEntity user = userService.getUserByUsername(username);
167 Set<RoleEntity> roleEntities = user.
getRoles();
168 Set<String> roles =
new HashSet<String>();
169 Set<String> permissions =
new HashSet<String>();
171 roles.add(roleEntity.getName());
172 userInfo.
setRole(
"ROLE_ADMIN");
174 Set<PermissionEntity> permissionEntities = roleEntity.getPermissions();
176 if (permissionEntity.getStatusEntity().getStatusCode().equalsIgnoreCase(
Status.
ACTIVE.getCode())
177 && !permissionEntity.getIsAdminPermission()) {
178 permissions.add(permissionEntity.getName());
184 List<PermissionEntity> permissionEntities = permissionRepository.findAll();
185 for (PermissionEntity permissionEntity : permissionEntities) {
186 permissions.add(permissionEntity.getName());
ModelAndView error(final Model model)
boolean getIs2FaEnabled()
static final String TWO_FA_GENERATOR
static final String LOGOUT
static final String SESSION_OBJECT
static final String REDIRECT_LOGIN
void setUsername(final String username)
static String getBase32EncryptedKey()
ModelAndView validateAndRedirect(final HttpServletRequest request, final String viewName)
Set< RoleEntity > getRoles()
void setIs2FaEnabled(final Boolean is2FaEnabled)
UserInfo getLoggedInUser(final HttpServletRequest request)
static final String REDIRECT_HOME
static final String LOGIN
ModelAndView login(final HttpServletRequest request)
ModelAndView authenticate(@RequestParam("username") final String username, @RequestParam("password") final String password, final HttpServletRequest request)
ModelAndView logout(final Model model)
void setRoles(final Set< String > roles)
void setPermissions(final Set< String > permissions)
void setName(final String name)
void setUserId(final Long userId)
void setRole(final String role)