From 43c4252df13eb868b4a2e4eaed8c9b477cf66533 Mon Sep 17 00:00:00 2001 From: djmil Date: Sun, 27 Aug 2023 16:56:50 +0200 Subject: [PATCH] SpringBoot: customUserDetails - remove unnecessary Custom authenticatio provider - cosmetic changes to ApiUserDetailsService - rename ShortHashService to ShortHashManager --- .../djmil/cordacheckers/ApiController.java | 7 ++- .../cordacheckers/ApiUserDetailsService.java | 28 ++++++------ .../CustomAuthenticationProvider.java | 45 ------------------- .../djmil/cordacheckers/SecurityConfig.java | 36 --------------- ...HashService.java => ShortHashManager.java} | 14 +++--- 5 files changed, 25 insertions(+), 105 deletions(-) delete mode 100644 backend/src/main/java/djmil/cordacheckers/CustomAuthenticationProvider.java rename backend/src/main/java/djmil/cordacheckers/{ApiUserShortHashService.java => ShortHashManager.java} (69%) diff --git a/backend/src/main/java/djmil/cordacheckers/ApiController.java b/backend/src/main/java/djmil/cordacheckers/ApiController.java index 6945ac5..44bc849 100644 --- a/backend/src/main/java/djmil/cordacheckers/ApiController.java +++ b/backend/src/main/java/djmil/cordacheckers/ApiController.java @@ -33,8 +33,11 @@ public class ApiController { */ @GetMapping("/api/activegames") public ResponseEntity dashboard(@AuthenticationPrincipal ApiUserDetails user) { - - return ResponseEntity.ok("{ \"ActiveGames\" : [\"game\", \"GAME\", \""+user.getShortHash()+ "\" ] }" ); + System.out.println("List of active games for " + + "user: " + user.getUsername() + + " with shortIdentityHash: " + user.getShortHash()); + + return ResponseEntity.ok("{ \"ActiveGames\" : [\"id_game1\", \"id_game2\"] }" ); } } \ No newline at end of file diff --git a/backend/src/main/java/djmil/cordacheckers/ApiUserDetailsService.java b/backend/src/main/java/djmil/cordacheckers/ApiUserDetailsService.java index 2d25300..af264b3 100644 --- a/backend/src/main/java/djmil/cordacheckers/ApiUserDetailsService.java +++ b/backend/src/main/java/djmil/cordacheckers/ApiUserDetailsService.java @@ -11,33 +11,31 @@ import org.springframework.stereotype.Service; public class ApiUserDetailsService implements UserDetailsService { private final PasswordEncoder encoder; - private final ApiUserShortHashService apiUserShortHash; + private final ShortHashManager shortHashManager; - public ApiUserDetailsService(PasswordEncoder encoder, ApiUserShortHashService apiUserShortHash) { + public ApiUserDetailsService( + PasswordEncoder encoder, + ShortHashManager shortHashManager) { this.encoder = encoder; - this.apiUserShortHash = apiUserShortHash; + this.shortHashManager = shortHashManager; } @Override public ApiUserDetails loadUserByUsername(String username) throws UsernameNotFoundException { - // Load user from database - // User user = userRepository.findByUsername(username); - - // if (user == null) { - // throw new UsernameNotFoundException("User not found"); - // } + String shortHash = shortHashManager.getShortHashBy(username); + if (shortHash == null) { + throw new UsernameNotFoundException("ShortHash for user '" + +username+ "' not found"); + } System.out.println("Load user "+username); - User.UserBuilder users = User.builder(); - - UserDetails alice = users + User.UserBuilder userBuilder = User.builder(); + UserDetails user = userBuilder .username(username) .password(encoder.encode("qaz123")) .build(); - ApiUserDetails apiUser = new ApiUserDetails(alice, apiUserShortHash.getShortHashBy(username)); - - return apiUser; + return new ApiUserDetails(user, shortHash); } } diff --git a/backend/src/main/java/djmil/cordacheckers/CustomAuthenticationProvider.java b/backend/src/main/java/djmil/cordacheckers/CustomAuthenticationProvider.java deleted file mode 100644 index 94cb707..0000000 --- a/backend/src/main/java/djmil/cordacheckers/CustomAuthenticationProvider.java +++ /dev/null @@ -1,45 +0,0 @@ -package djmil.cordacheckers; - -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.crypto.password.PasswordEncoder; -import org.springframework.stereotype.Service; - -@Service -public class CustomAuthenticationProvider implements AuthenticationProvider { - - private final PasswordEncoder encoder; - private final ApiUserDetailsService userDetailsService; - - public CustomAuthenticationProvider(PasswordEncoder encoder, ApiUserDetailsService userDetailsService) { - this.userDetailsService = userDetailsService; - this.encoder = encoder; - } - - @Override - public Authentication authenticate(Authentication authentication) throws AuthenticationException { - String username = authentication.getName(); - String password = authentication.getCredentials().toString(); - - ApiUserDetails user = userDetailsService.loadUserByUsername(username); - - return checkPassword(user, password); - } - - @Override - public boolean supports(Class aClass) { - return UsernamePasswordAuthenticationToken.class.isAssignableFrom(aClass); - } - - private Authentication checkPassword(ApiUserDetails user, String rawPassword) { - if (encoder.matches(rawPassword, user.getPassword())) { - return new UsernamePasswordAuthenticationToken(user, user.getPassword(), user.getAuthorities()); - } else { - throw new BadCredentialsException("Bad credentials"); - } - } - -} diff --git a/backend/src/main/java/djmil/cordacheckers/SecurityConfig.java b/backend/src/main/java/djmil/cordacheckers/SecurityConfig.java index 8982250..0112f40 100644 --- a/backend/src/main/java/djmil/cordacheckers/SecurityConfig.java +++ b/backend/src/main/java/djmil/cordacheckers/SecurityConfig.java @@ -2,15 +2,9 @@ package djmil.cordacheckers; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -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.core.userdetails.UserDetailsService; import org.springframework.security.crypto.factory.PasswordEncoderFactories; import org.springframework.security.crypto.password.PasswordEncoder; -import org.springframework.security.web.SecurityFilterChain; - -import static org.springframework.security.config.Customizer.withDefaults; @Configuration @EnableWebSecurity @@ -21,34 +15,4 @@ public class SecurityConfig { return PasswordEncoderFactories.createDelegatingPasswordEncoder(); } - public void configure(AuthenticationManagerBuilder auth, CustomAuthenticationProvider authenticationProvider) throws Exception { - auth.authenticationProvider(authenticationProvider); - } - - // @Bean - // SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception { - // http - // .authorizeRequests(authorizeRequests -> - // authorizeRequests.anyRequest().authenticated()); - // .formLogin(withDefaults()); - // return http.build(); - // } - - // @Bean - // UserDetailsService hardcodedCordaUsers(PasswordEncoder passwordEncoder) { - // User.UserBuilder users = User.builder(); - - // UserDetails alice = users - // .username("alice") - // .password(passwordEncoder.encode("qaz123")) - // .build(); - - // UserDetails bob = users - // .username("bob") - // .password(passwordEncoder.encode("qaz123")) - // .build(); - - // return new InMemoryUserDetailsManager(alice, bob); - // } - } diff --git a/backend/src/main/java/djmil/cordacheckers/ApiUserShortHashService.java b/backend/src/main/java/djmil/cordacheckers/ShortHashManager.java similarity index 69% rename from backend/src/main/java/djmil/cordacheckers/ApiUserShortHashService.java rename to backend/src/main/java/djmil/cordacheckers/ShortHashManager.java index ae0dd93..962f83f 100644 --- a/backend/src/main/java/djmil/cordacheckers/ApiUserShortHashService.java +++ b/backend/src/main/java/djmil/cordacheckers/ShortHashManager.java @@ -13,16 +13,16 @@ import djmil.cordacheckers.cordaclient.CordaClient; import djmil.cordacheckers.cordaclient.pojo.virtualNodes; @Service -public class ApiUserShortHashService { +public class ShortHashManager { static final Locale locale = Locale.getDefault(); - Map apiUserShortHashMap; + Map cnName2shortHash; - ApiUserShortHashService(CordaClient client) { - this.apiUserShortHashMap = setApiUserShortHashMap(client); + ShortHashManager(CordaClient client) { + this.cnName2shortHash = setCnName2shortHash(client); } - private static Map setApiUserShortHashMap(CordaClient client) { + private static Map setCnName2shortHash(CordaClient client) { Map map = new HashMap<>(); List vNodesList = client.getVirtualnode(); @@ -37,7 +37,7 @@ public class ApiUserShortHashService { } } catch (InvalidNameException e) { // TODO: logs - System.out.println("Unable to get ShorHash map for vNode: "+e.getExplanation()); + System.out.println("Unable to get ShorHash map for Corda virtual nodes: "+e.getExplanation()); e.printStackTrace(); } @@ -46,6 +46,6 @@ public class ApiUserShortHashService { } String getShortHashBy(String apiUserName) { - return this.apiUserShortHashMap.get(apiUserName.toLowerCase(locale)); + return this.cnName2shortHash.get(apiUserName.toLowerCase(locale)); } }