Commit 16021169 authored by Jie Yuan's avatar Jie Yuan
Browse files

add b2access user info extractor

parent 7a81bc54
package svmon.endpoints;
import java.security.Principal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.view.RedirectView;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import svmon.model.UserInfo;
import svmon.payload.ApiResponse;
import svmon.payload.JwtAuthenticationResponse;
import svmon.payload.LoginRequest;
import svmon.payload.SignUpRequest;
import svmon.payload.UserSummary;
import svmon.repository.UserInfoJpaRepository;
import svmon.security.JwtTokenProvider;
import svmon.utils.ScheduledTasks;
......@@ -48,6 +46,15 @@ public class AAILoginService {
@Autowired
JwtTokenProvider tokenProvider;
@Autowired
ScheduledTasks scheduled;
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);
/** @RequestMapping("/")
public ResponseEntity<?> authenticateUser(Principal currentUser ) {
......@@ -104,24 +111,33 @@ public class AAILoginService {
RedirectView redirectV = new RedirectView("https://svmon-dev.scc.kit.edu:443/login");
return redirectV;
}
System.out.println(currentUser.getName());
String name=currentUser.getName();
//System.out.println(currentUser.getName());
OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) currentUser;
Authentication uauthentication = oAuth2Authentication.getUserAuthentication();
Map<String, String> details = new LinkedHashMap<>();
details = (Map<String, String>) uauthentication.getDetails();
for (String key : details.keySet()) {
if (details.get(key) instanceof String ) {
log.info("B2ACCESS user with" + key + " :" + details.get(key) + "at {}", dateFormat.format(new Date()));
}
}
String pwd="";
name=name.trim();
name=name.replaceAll(" ", ".");
UserInfo user = userRepository.findByUsername(name);
String email = details.get("email");
UserInfo user = userRepository.findByEmail(email);
if( user == null ) {
user = new UserInfo();
user.setUsername(name);
System.out.println("a new user with name "+name+" is created");
user.setUsername(details.get("name"));
//System.out.println("a new user with name "+name+" is created");
user.setAuthService("AAI");
user.setEmail(name+"@AAI");
user.setEmail(email);
user.setIsEnabled(true);
user.setRole("USER");
String pwd = this.generateRandomStr(20);
user.setPassword(passwordEncoder.encode(pwd));
userRepository.save(user);
log.info("B2ACCESSUser with " + email + " has been saved at {} ", dateFormat.format(new Date()));
}
......@@ -130,7 +146,7 @@ public class AAILoginService {
Authentication authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(
user.getUsername(),
pwd
user.getPassword()
)
);
......@@ -151,6 +167,18 @@ public class AAILoginService {
return redirectV;
}
private String generateRandomStr(int length) {
String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random random = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
int number = random.nextInt(base.length());
sb.append(base.charAt(number));
}
return sb.toString();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment