Skip to content

Commit

Permalink
cleanup - simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
dapolach committed Sep 10, 2024
1 parent 4e4936e commit a26f547
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -31,7 +31,7 @@ private Optional<SocialLoginOidcUserToKlabisOidcUserMapper> 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())
Expand All @@ -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<String> roles) {
DefaultOidcUser createAuthentication(OidcIdToken idToken, OidcUserInfo userInfo, ApplicationUser user, List<String> roles) {
Set<GrantedAuthority> authorities = roles.stream()
.map(roleName -> new SimpleGrantedAuthority(roleName))
.collect(Collectors.toSet());
Expand All @@ -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;
};
}

;

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ApplicationUser extends AbstractAggregateRoot<ApplicationUser> {
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<ApplicationGrant> globalGrants = EnumSet.noneOf(ApplicationGrant.class);
Expand Down Expand Up @@ -72,8 +72,8 @@ public String getPassword() {
return password;
}

public boolean isEnabled() {
return enabled;
public boolean isDisabled() {
return !enabled;
}

public Set<ApplicationGrant> getGlobalGrants() {
Expand Down

0 comments on commit a26f547

Please sign in to comment.