|
@@ -4,26 +4,37 @@ import com.auth0.jwt.JWT;
|
|
|
import com.auth0.jwt.algorithms.Algorithm;
|
|
import com.auth0.jwt.algorithms.Algorithm;
|
|
|
import com.example.plantsforyou.appuser.AppUser;
|
|
import com.example.plantsforyou.appuser.AppUser;
|
|
|
import com.example.plantsforyou.appuser.LoginCredentials;
|
|
import com.example.plantsforyou.appuser.LoginCredentials;
|
|
|
|
|
+import com.example.plantsforyou.oAuth.oAuthService;
|
|
|
|
|
+import com.example.plantsforyou.registration.RegistrationService;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
-import lombok.NoArgsConstructor;
|
|
|
|
|
|
|
+import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Configurable;
|
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
|
import org.springframework.security.core.Authentication;
|
|
import org.springframework.security.core.Authentication;
|
|
|
import org.springframework.security.core.AuthenticationException;
|
|
import org.springframework.security.core.AuthenticationException;
|
|
|
import org.springframework.security.core.GrantedAuthority;
|
|
import org.springframework.security.core.GrantedAuthority;
|
|
|
|
|
+import org.springframework.security.core.userdetails.UserDetails;
|
|
|
|
|
+import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
|
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
|
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
|
|
-import org.springframework.web.bind.annotation.CrossOrigin;
|
|
|
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
+import org.springframework.web.context.WebApplicationContext;
|
|
|
|
|
+import org.springframework.web.context.support.SpringBeanAutowiringSupport;
|
|
|
|
|
+import org.springframework.web.context.support.WebApplicationContextUtils;
|
|
|
|
|
|
|
|
import javax.servlet.FilterChain;
|
|
import javax.servlet.FilterChain;
|
|
|
-import javax.servlet.ServletException;
|
|
|
|
|
|
|
+import javax.servlet.ServletContext;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.BufferedReader;
|
|
import java.io.BufferedReader;
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
|
|
+import java.security.GeneralSecurityException;
|
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
+import java.util.Optional;
|
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
|
|
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
|
|
@@ -33,6 +44,8 @@ public class CustomAuthenticationFilter extends UsernamePasswordAuthenticationFi
|
|
|
private final AuthenticationManager authenticationManager;
|
|
private final AuthenticationManager authenticationManager;
|
|
|
private final ObjectMapper objectMapper = new ObjectMapper();
|
|
private final ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
|
|
|
|
|
+ private oAuthService authService;
|
|
|
|
|
+
|
|
|
public CustomAuthenticationFilter(AuthenticationManager authenticationManager){
|
|
public CustomAuthenticationFilter(AuthenticationManager authenticationManager){
|
|
|
this.authenticationManager = authenticationManager;
|
|
this.authenticationManager = authenticationManager;
|
|
|
}
|
|
}
|
|
@@ -40,6 +53,11 @@ public class CustomAuthenticationFilter extends UsernamePasswordAuthenticationFi
|
|
|
@Override
|
|
@Override
|
|
|
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) {
|
|
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) {
|
|
|
log.info("New login request!");
|
|
log.info("New login request!");
|
|
|
|
|
+ if(authService == null){
|
|
|
|
|
+ ServletContext servletContext = request.getServletContext();
|
|
|
|
|
+ WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
|
|
|
|
|
+ authService = webApplicationContext.getBean(oAuthService.class);
|
|
|
|
|
+ }
|
|
|
try {
|
|
try {
|
|
|
BufferedReader reader = request.getReader();
|
|
BufferedReader reader = request.getReader();
|
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
@@ -51,9 +69,26 @@ public class CustomAuthenticationFilter extends UsernamePasswordAuthenticationFi
|
|
|
|
|
|
|
|
String email = credentials.getUsername();
|
|
String email = credentials.getUsername();
|
|
|
String password = credentials.getPassword();
|
|
String password = credentials.getPassword();
|
|
|
|
|
+ String oAuth = credentials.getOAuth();
|
|
|
log.info("Email is: {}", email);
|
|
log.info("Email is: {}", email);
|
|
|
log.info("Password is: {}", password);
|
|
log.info("Password is: {}", password);
|
|
|
- UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(email, password);
|
|
|
|
|
|
|
+ UsernamePasswordAuthenticationToken authenticationToken = null;
|
|
|
|
|
+ if(oAuth.equals("true")){
|
|
|
|
|
+ GoogleIdToken idToken = authService.validate(password);
|
|
|
|
|
+ if(idToken != null) {
|
|
|
|
|
+ GoogleIdToken.Payload payload = idToken.getPayload();
|
|
|
|
|
+ String payloadEmail = payload.getEmail();
|
|
|
|
|
+ String payloadName = (String) payload.get("name");
|
|
|
|
|
+ if(!authService.findByEmail(payloadEmail).isPresent()){
|
|
|
|
|
+ authService.singUpUser(payload);
|
|
|
|
|
+ }
|
|
|
|
|
+ AppUser user = authService.findByEmail(payloadEmail).get();
|
|
|
|
|
+ authenticationToken = new UsernamePasswordAuthenticationToken(user.getUsername(), "none");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ else{
|
|
|
|
|
+ authenticationToken = new UsernamePasswordAuthenticationToken(email, password);
|
|
|
|
|
+ }
|
|
|
return authenticationManager.authenticate(authenticationToken);
|
|
return authenticationManager.authenticate(authenticationToken);
|
|
|
}
|
|
}
|
|
|
catch (AuthenticationException e){
|
|
catch (AuthenticationException e){
|
|
@@ -61,7 +96,10 @@ public class CustomAuthenticationFilter extends UsernamePasswordAuthenticationFi
|
|
|
throw new IllegalStateException(e.getMessage());
|
|
throw new IllegalStateException(e.getMessage());
|
|
|
}
|
|
}
|
|
|
catch(IOException e){
|
|
catch(IOException e){
|
|
|
|
|
+ log.error(e.getMessage());
|
|
|
throw new IllegalArgumentException(e.getMessage());
|
|
throw new IllegalArgumentException(e.getMessage());
|
|
|
|
|
+ } catch (GeneralSecurityException e) {
|
|
|
|
|
+ throw new IllegalStateException(e.getMessage());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|