commit 32f56e618b4822b621c50b35992b2cb299b342e4
Author: Jiri Trefil <badjokke@gmail.com>
Date:   Fri Apr 7 23:31:01 2023 +0200

    #10228 definice realmu v application properties souboru

diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/security/WebSecurityConfig.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/security/WebSecurityConfig.java
new file mode 100644
index 0000000..8dd7c03
--- /dev/null
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/security/WebSecurityConfig.java
@@ -0,0 +1,49 @@
+package cz.zcu.fav.kiv.antipatterndetectionapp.v2.security;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Bean;
+import org.springframework.security.authentication.AuthenticationManager;
+import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
+import org.springframework.security.config.annotation.web.builders.HttpSecurity;
+import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
+import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
+import org.springframework.security.config.http.SessionCreationPolicy;
+import org.springframework.security.core.userdetails.UserDetailsService;
+
+@EnableWebSecurity
+public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
+
+    @Autowired
+    private UserDetailsService userService;
+
+    @Autowired
+    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
+        auth.userDetailsService(userService);
+    }
+
+
+    @Bean
+    public UserDetailsService userDetailsService() {
+        return super.userDetailsService();
+    }
+
+    @Override
+    @Bean
+    public AuthenticationManager authenticationManagerBean() throws Exception {
+        return super.authenticationManagerBean();
+    }
+
+    @Override
+    protected void configure(HttpSecurity httpSecurity) throws Exception {
+        httpSecurity.csrf().disable()
+                .authorizeRequests().antMatchers("/v2/user/register","/v2/user/login").permitAll().
+                anyRequest().authenticated().and().
+                exceptionHandling().and().sessionManagement()
+                .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
+//        httpSecurity.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
+
+    }
+
+
+
+}
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/OAuthService.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/OAuthService.java
new file mode 100644
index 0000000..e9fa4fa
--- /dev/null
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/OAuthService.java
@@ -0,0 +1,10 @@
+package cz.zcu.fav.kiv.antipatterndetectionapp.v2.service;
+
+import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.User;
+import org.springframework.http.ResponseEntity;
+
+public interface OAuthService {
+    public ResponseEntity<String> authenticate(User user);
+    public ResponseEntity<String> loginUser(User user);
+
+}
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/OAuthServiceImpl.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/OAuthServiceImpl.java
new file mode 100644
index 0000000..c3fb40a
--- /dev/null
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/OAuthServiceImpl.java
@@ -0,0 +1,68 @@
+package cz.zcu.fav.kiv.antipatterndetectionapp.v2.service;
+
+import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.User;
+import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.RequestBuilder;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.http.ResponseEntity;
+import org.springframework.security.core.userdetails.UserDetails;
+import org.springframework.security.core.userdetails.UserDetailsService;
+import org.springframework.security.core.userdetails.UsernameNotFoundException;
+import org.springframework.stereotype.Service;
+import java.util.ArrayList;
+import java.util.HashMap;
+
+@Service
+public class OAuthServiceImpl implements OAuthService, UserDetailsService {
+
+
+    @Value("${auth.realm.authenticate}")
+    private String AUTH_URL_AUTH;
+
+    @Value("${auth.realm.login}")
+    private String AUTH_URL_LOGIN;
+
+    @Autowired
+    private UserService userService;
+
+    public ResponseEntity<String> authenticate(User user) {
+        final String userName = user.getName();
+        final String token = user.getToken();
+
+        if(userName == null || token == null) {
+            return null;
+        }
+
+        //HttpURLConnection con = RequestBuilder.createConnection(AUTH_URL);
+        HashMap<String, String> requestBody = new HashMap<>();
+
+        requestBody.put("name", userName);
+        requestBody.put("token", token);
+
+        ResponseEntity<String> response = RequestBuilder.sendRequestResponse(AUTH_URL_AUTH, requestBody);
+
+        return response;
+    }
+
+    public ResponseEntity<String> loginUser(User user) {
+        final String userName = user.getName();
+
+        if(userName == null) {
+            return null;
+        }
+        //HttpURLConnection con = RequestBuilder.createConnection(AUTH_URL);
+        HashMap<String, String> requestBody = new HashMap<>();
+
+        requestBody.put("name", userName);
+
+        ResponseEntity<String> response = RequestBuilder.sendRequestResponse(AUTH_URL_LOGIN, requestBody);
+
+        return response;
+    }
+
+    @Override
+    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
+        final User user = this.userService.getUserByName(s);
+        return new org.springframework.security.core.userdetails.User(user.getName(),user.getPassword(),new ArrayList<>());
+    }
+}
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/utils/ParamsBuilder.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/utils/ParamsBuilder.java
new file mode 100644
index 0000000..603354e
--- /dev/null
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/utils/ParamsBuilder.java
@@ -0,0 +1,44 @@
+package cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.util.HashMap;
+
+public class ParamsBuilder {
+
+    /**
+     * Method creates body of post request
+     * ie returns json representation of parameters
+     * @param parameters Key: value pair parameters
+     * @return String JSON representation
+     */
+    public static String createPostParams(HashMap<String,String> parameters) {
+        return JSONBuilder.buildJson(parameters);
+    }
+
+    /**
+     * Method creates get parameters from parameters
+     * @param params parameters
+     * @return String GET representation of parameters passed into the function
+     * @throws UnsupportedEncodingException if unknown encoding standard is passed as an argument to the function
+     */
+    public static String createGetParams(HashMap<String, String> params) throws UnsupportedEncodingException {
+        StringBuilder builder = new StringBuilder();
+        for (HashMap.Entry<String, String> entry : params.entrySet()) {
+            builder.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
+            builder.append("=");
+            builder.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
+            builder.append("&");
+        }
+
+        String resultString = builder.toString();
+        return resultString.length() > 0
+                ? resultString.substring(0, resultString.length() - 1)
+                : resultString;
+
+    }
+
+
+
+
+}
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/utils/RequestBuilder.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/utils/RequestBuilder.java
new file mode 100644
index 0000000..9ef1b2b
--- /dev/null
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/utils/RequestBuilder.java
@@ -0,0 +1,28 @@
+package cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils;
+
+import org.springframework.web.client.RestTemplate;
+
+import java.util.HashMap;
+import java.util.logging.Logger;
+
+import org.springframework.http.*;
+
+public class RequestBuilder {
+
+
+    private static Logger logger = Logger.getLogger(RequestBuilder.class.getName());
+
+
+    public static ResponseEntity<String> sendRequestResponse(String url, HashMap<String,String> body) {
+        RestTemplate restTemplate = new RestTemplate();
+        String json = JSONBuilder.buildJson(body);
+
+        HttpHeaders headers = new HttpHeaders();
+        headers.setContentType(MediaType.APPLICATION_JSON);
+        HttpEntity<String> entity = new HttpEntity<>(json, headers);
+        ResponseEntity<String> response = restTemplate.postForEntity(url, entity, String.class);
+
+        return response;
+    }
+
+}
