|
@@ -4,23 +4,25 @@ 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.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.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 javax.servlet.FilterChain;
|
|
import javax.servlet.FilterChain;
|
|
|
-import javax.servlet.ServletException;
|
|
|
|
|
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;
|
|
@@ -32,6 +34,8 @@ import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
|
|
|
public class CustomAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
|
|
public class CustomAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
|
|
|
private final AuthenticationManager authenticationManager;
|
|
private final AuthenticationManager authenticationManager;
|
|
|
private final ObjectMapper objectMapper = new ObjectMapper();
|
|
private final ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
+ private final oAuthService authService = new oAuthService();
|
|
|
|
|
+
|
|
|
|
|
|
|
|
public CustomAuthenticationFilter(AuthenticationManager authenticationManager){
|
|
public CustomAuthenticationFilter(AuthenticationManager authenticationManager){
|
|
|
this.authenticationManager = authenticationManager;
|
|
this.authenticationManager = authenticationManager;
|
|
@@ -51,9 +55,28 @@ public class CustomAuthenticationFilter extends UsernamePasswordAuthenticationFi
|
|
|
|
|
|
|
|
String email = credentials.getUsername();
|
|
String email = credentials.getUsername();
|
|
|
String password = credentials.getPassword();
|
|
String password = credentials.getPassword();
|
|
|
|
|
+ Boolean 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){
|
|
|
|
|
+ GoogleIdToken idToken = authService.validate(password);
|
|
|
|
|
+ if(idToken != null) {
|
|
|
|
|
+ GoogleIdToken.Payload payload = idToken.getPayload();
|
|
|
|
|
+ try {
|
|
|
|
|
+ UserDetails userDetails = authService.findByEmail(payload.getEmail());
|
|
|
|
|
+ authenticationToken = new UsernamePasswordAuthenticationToken(userDetails.getUsername(), null);
|
|
|
|
|
+ }
|
|
|
|
|
+ catch(UsernameNotFoundException e){
|
|
|
|
|
+ authService.singUpUser(payload);
|
|
|
|
|
+ authenticationToken = new UsernamePasswordAuthenticationToken(payload.getEmail(), null);
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ else{
|
|
|
|
|
+ authenticationToken = new UsernamePasswordAuthenticationToken(email, password);
|
|
|
|
|
+ }
|
|
|
return authenticationManager.authenticate(authenticationToken);
|
|
return authenticationManager.authenticate(authenticationToken);
|
|
|
}
|
|
}
|
|
|
catch (AuthenticationException e){
|
|
catch (AuthenticationException e){
|
|
@@ -62,6 +85,8 @@ public class CustomAuthenticationFilter extends UsernamePasswordAuthenticationFi
|
|
|
}
|
|
}
|
|
|
catch(IOException e){
|
|
catch(IOException e){
|
|
|
throw new IllegalArgumentException(e.getMessage());
|
|
throw new IllegalArgumentException(e.getMessage());
|
|
|
|
|
+ } catch (GeneralSecurityException e) {
|
|
|
|
|
+ throw new IllegalStateException(e.getMessage());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|