commit 1d732187654f229e3bded434cb55c518ac9ea6d7
Author: Jiri Trefil <badjokke@gmail.com>
Date:   Sat Apr 8 16:23:49 2023 +0200

    #10228 Implementování odhlášení uživatele a invalidace tokenu

diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/security/JwtAuthenticationFilter.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/security/JwtAuthenticationFilter.java
new file mode 100644
index 0000000..00ab902
--- /dev/null
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/security/JwtAuthenticationFilter.java
@@ -0,0 +1,55 @@
+package cz.zcu.fav.kiv.antipatterndetectionapp.v2.security;
+
+import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.User;
+import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.AuthProvider;
+import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.OAuthService;
+import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.OAuthServiceImpl;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.annotation.Order;
+import org.springframework.http.ResponseEntity;
+import org.springframework.security.authentication.AuthenticationProvider;
+import org.springframework.stereotype.Component;
+import org.springframework.web.filter.OncePerRequestFilter;
+
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.IOException;
+import java.util.Objects;
+
+@Component
+public class JwtAuthenticationFilter extends OncePerRequestFilter {
+
+
+
+    @Override
+    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
+        System.out.println("------------------DO FILTER INTERNAL---------------------------");
+
+        String authorizationHeader = request.getHeader("Authorization");
+        if(authorizationHeader == null || authorizationHeader.length() < 7) {
+            filterChain.doFilter(request, response);
+            return;
+        }
+        OAuthServiceImpl oAuthService = new OAuthServiceImpl();
+
+        String token = authorizationHeader.substring(7);
+
+        ResponseEntity<String> oAuthResponse =  oAuthService.authenticate(token);
+        response.setContentType("application/json");
+        response.setStatus(oAuthResponse.getStatusCodeValue());
+
+        BufferedWriter out = new BufferedWriter(response.getWriter());
+        out.write(Objects.requireNonNull(oAuthResponse.getBody()));
+        out.flush();
+        out.close();
+
+        filterChain.doFilter(request, response);
+
+    }
+
+
+}
\ No newline at end of file
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/AuthProvider.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/AuthProvider.java
new file mode 100644
index 0000000..5cc53ce
--- /dev/null
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/AuthProvider.java
@@ -0,0 +1,71 @@
+package cz.zcu.fav.kiv.antipatterndetectionapp.v2.service;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.web.client.RestTemplateBuilder;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpMethod;
+import org.springframework.http.ResponseEntity;
+import org.springframework.security.authentication.AuthenticationProvider;
+import org.springframework.security.authentication.BadCredentialsException;
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
+
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.AuthenticationException;
+import org.springframework.security.core.userdetails.UserDetails;
+import org.springframework.stereotype.Service;
+import org.springframework.web.client.RestTemplate;
+
+@Service
+public class AuthProvider implements AuthenticationProvider {
+
+    @Value("${auth.realm.authenticate}")
+    private String auth_url;
+
+    private RestTemplate template;
+
+    public AuthProvider(RestTemplate restTemplate){
+        this.template = restTemplate;
+    }
+
+
+    @Override
+    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
+
+        RestTemplate restTemplate = new RestTemplate();
+        String jwtToken = (String) authentication.getCredentials();
+
+        HttpHeaders headers = new HttpHeaders();
+        headers.setBearerAuth(jwtToken);
+        HttpEntity<String> entity = new HttpEntity<>(headers);
+
+        try {
+            ResponseEntity<UserDetails> responseEntity = restTemplate.exchange(
+                    auth_url,
+                    HttpMethod.POST,
+                    entity,
+                    UserDetails.class);
+
+            UserDetails userDetails = responseEntity.getBody();
+//            List<GrantedAuthority> authorities = new ArrayList<>();
+//            authorities.add(new SimpleGrantedAuthority(userDetails.getRole()));
+
+            if (userDetails != null && userDetails.getUsername() != null) {
+                return new UsernamePasswordAuthenticationToken(userDetails.getUsername(), jwtToken);
+            }
+
+            return new UsernamePasswordAuthenticationToken(null, null);
+
+        } catch (Exception e) {
+            throw new BadCredentialsException("Invalid JWT token");
+        }
+    }
+
+    @Override
+    public boolean supports(Class<?> authentication) {
+        return authentication.equals(UsernamePasswordAuthenticationToken.class);
+    }
+
+
+}
