From a26f547e7a9783af699d908e1f33c38876efe3ef Mon Sep 17 00:00:00 2001 From: David Polach Date: Tue, 10 Sep 2024 17:05:40 +0200 Subject: [PATCH] cleanup - simplification --- .../ApplicationUserDetailsService.java | 34 ++++++++++++++++ .../config/authserver/KlabisOidcUser.java | 7 ++-- .../UsersDetailsServiceConfiguration.java | 39 ------------------- ...cialLoginAuthenticationSuccessHandler.java | 3 ++ .../CustomOidcUserService.java | 12 +++--- .../domain/appusers/ApplicationUser.java | 6 +-- 6 files changed, 50 insertions(+), 51 deletions(-) create mode 100644 backend/src/main/java/club/klabis/config/authserver/ApplicationUserDetailsService.java delete mode 100644 backend/src/main/java/club/klabis/config/authserver/UsersDetailsServiceConfiguration.java diff --git a/backend/src/main/java/club/klabis/config/authserver/ApplicationUserDetailsService.java b/backend/src/main/java/club/klabis/config/authserver/ApplicationUserDetailsService.java new file mode 100644 index 0000000..0260917 --- /dev/null +++ b/backend/src/main/java/club/klabis/config/authserver/ApplicationUserDetailsService.java @@ -0,0 +1,34 @@ +package club.klabis.config.authserver; + +import club.klabis.domain.appusers.ApplicationUser; +import club.klabis.domain.appusers.ApplicationUsersRepository; +import org.springframework.security.core.userdetails.User; +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.Component; + +@Component +class ApplicationUserDetailsService implements UserDetailsService { + + private final ApplicationUsersRepository applicationUsersRepository; + + public ApplicationUserDetailsService(ApplicationUsersRepository applicationUsersRepository) { + this.applicationUsersRepository = applicationUsersRepository; + } + + @Override + public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { + return applicationUsersRepository.findByUserName(username) + .map(this::fromMember) + .orElseThrow(() -> new UsernameNotFoundException("User with username %s not found".formatted(username))); + } + + private UserDetails fromMember(ApplicationUser member) { + return User.withUsername(member.getUsername()) + .password(member.getPassword()) + .disabled(member.isDisabled()) + .build(); + } + +} diff --git a/backend/src/main/java/club/klabis/config/authserver/KlabisOidcUser.java b/backend/src/main/java/club/klabis/config/authserver/KlabisOidcUser.java index a0b9181..5a0472f 100644 --- a/backend/src/main/java/club/klabis/config/authserver/KlabisOidcUser.java +++ b/backend/src/main/java/club/klabis/config/authserver/KlabisOidcUser.java @@ -14,10 +14,9 @@ import java.util.HashSet; import java.util.UUID; -/** - * Object holding information published in OIDC token for Klabis app users - */ -public class KlabisOidcUser extends DefaultOidcUser implements OidcUser, UserDetails { +// Object holding data what we would like to publish into Klabis JWT tokens +// TODO: find way how to use it +class KlabisOidcUser extends DefaultOidcUser implements OidcUser, UserDetails { private UUID id; private String username; private boolean active; diff --git a/backend/src/main/java/club/klabis/config/authserver/UsersDetailsServiceConfiguration.java b/backend/src/main/java/club/klabis/config/authserver/UsersDetailsServiceConfiguration.java deleted file mode 100644 index 4c6e4ab..0000000 --- a/backend/src/main/java/club/klabis/config/authserver/UsersDetailsServiceConfiguration.java +++ /dev/null @@ -1,39 +0,0 @@ -package club.klabis.config.authserver; - -import club.klabis.domain.appusers.ApplicationUser; -import club.klabis.domain.appusers.ApplicationUsersRepository; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.security.core.userdetails.User; -import org.springframework.security.core.userdetails.UserDetails; -import org.springframework.security.core.userdetails.UserDetailsService; -import org.springframework.security.core.userdetails.UsernameNotFoundException; - -@Configuration -class UsersDetailsServiceConfiguration { - - private final ApplicationUsersRepository applicationUsersRepository; - - public UsersDetailsServiceConfiguration(ApplicationUsersRepository applicationUsersRepository) { - this.applicationUsersRepository = applicationUsersRepository; - } - - @Bean - public UserDetailsService userDetailsService() { - return new UserDetailsService() { - @Override - public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { - return applicationUsersRepository.findByUserName(username) - .map(this::fromMember) - .orElseThrow(() -> new UsernameNotFoundException("User with username %s not found".formatted(username))); - } - - private UserDetails fromMember(ApplicationUser member) { - return User.withUsername(member.getUsername()).password(member.getPassword()).build(); - } - }; - } - - - -} diff --git a/backend/src/main/java/club/klabis/config/authserver/sociallogin/SocialLoginAuthenticationSuccessHandler.java b/backend/src/main/java/club/klabis/config/authserver/sociallogin/SocialLoginAuthenticationSuccessHandler.java index c3a7c02..a5fa486 100644 --- a/backend/src/main/java/club/klabis/config/authserver/sociallogin/SocialLoginAuthenticationSuccessHandler.java +++ b/backend/src/main/java/club/klabis/config/authserver/sociallogin/SocialLoginAuthenticationSuccessHandler.java @@ -3,6 +3,7 @@ import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken; import org.springframework.security.oauth2.core.oidc.user.OidcUser; @@ -34,6 +35,8 @@ public void onAuthenticationSuccess( } else if (authentication.getPrincipal() instanceof OAuth2User) { this.oauth2UserHandler.accept((OAuth2User) authentication.getPrincipal()); } + } else if (authentication instanceof UsernamePasswordAuthenticationToken userpasstoken) { + System.out.println("Handling %s user (username+pass)".formatted(userpasstoken.getPrincipal())); } this.delegate.onAuthenticationSuccess(request, response, authentication); diff --git a/backend/src/main/java/club/klabis/config/authserver/socialloginsupport/CustomOidcUserService.java b/backend/src/main/java/club/klabis/config/authserver/socialloginsupport/CustomOidcUserService.java index 68233b0..78b33b7 100644 --- a/backend/src/main/java/club/klabis/config/authserver/socialloginsupport/CustomOidcUserService.java +++ b/backend/src/main/java/club/klabis/config/authserver/socialloginsupport/CustomOidcUserService.java @@ -1,6 +1,5 @@ package club.klabis.config.authserver.socialloginsupport; -import club.klabis.config.authserver.KlabisOidcUser; import club.klabis.domain.appusers.ApplicationUser; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; @@ -11,6 +10,7 @@ import org.springframework.security.oauth2.core.oidc.OidcIdToken; import org.springframework.security.oauth2.core.oidc.OidcUserInfo; import org.springframework.security.oauth2.core.oidc.StandardClaimNames; +import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser; import org.springframework.security.oauth2.core.oidc.user.OidcUser; import org.springframework.stereotype.Service; @@ -31,7 +31,7 @@ private Optional getMapperForRegistra } @Override - public KlabisOidcUser loadUser(OidcUserRequest userRequest) throws OAuth2AuthenticationException { + public DefaultOidcUser loadUser(OidcUserRequest userRequest) throws OAuth2AuthenticationException { OidcUser oidcUser = super.loadUser(userRequest); SocialLoginOidcUserToKlabisOidcUserMapper mapper = getMapperForRegistrationId(userRequest.getClientRegistration()) @@ -42,7 +42,7 @@ public KlabisOidcUser loadUser(OidcUserRequest userRequest) throws OAuth2Authent .orElseThrow(() -> new OAuth2AuthenticationException("User with subject %s (%s) not found!".formatted(oidcUser.getSubject(), mapper.getOAuthClientId()))); } - KlabisOidcUser createAuthentication(OidcIdToken idToken, OidcUserInfo userInfo, ApplicationUser user, List roles) { + DefaultOidcUser createAuthentication(OidcIdToken idToken, OidcUserInfo userInfo, ApplicationUser user, List roles) { Set authorities = roles.stream() .map(roleName -> new SimpleGrantedAuthority(roleName)) .collect(Collectors.toSet()); @@ -61,12 +61,14 @@ KlabisOidcUser createAuthentication(OidcIdToken idToken, OidcUserInfo userInfo, idToken.getTokenValue(), idToken.getIssuedAt(), idToken.getExpiresAt(), klabisClaims ); - KlabisOidcUser oidcUser = new KlabisOidcUser(authorities, customIdToken, userInfo); + DefaultOidcUser oidcUser = new DefaultOidcUser(authorities, customIdToken, userInfo); // oidcUser.setId(user.getId()); // oidcUser.setUsername(user.getUsername()); // oidcUser.setCreatedAt(user.getCreatedAt()); // oidcUser.setActive(user.isActive()); return oidcUser; - }; + } + + ; } \ No newline at end of file diff --git a/backend/src/main/java/club/klabis/domain/appusers/ApplicationUser.java b/backend/src/main/java/club/klabis/domain/appusers/ApplicationUser.java index c096f95..945c590 100644 --- a/backend/src/main/java/club/klabis/domain/appusers/ApplicationUser.java +++ b/backend/src/main/java/club/klabis/domain/appusers/ApplicationUser.java @@ -16,7 +16,7 @@ public class ApplicationUser extends AbstractAggregateRoot { private Integer memberId; private String username; private String password = "{noop}secret"; - private boolean enabled; + private boolean enabled = true; private String googleSubject; private String githubSubject; private Set globalGrants = EnumSet.noneOf(ApplicationGrant.class); @@ -72,8 +72,8 @@ public String getPassword() { return password; } - public boolean isEnabled() { - return enabled; + public boolean isDisabled() { + return !enabled; } public Set getGlobalGrants() {