88import java .util .UUID ;
99import java .util .function .Supplier ;
1010
11+ import dev .findfirst .security .conditions .OAuthClientsCondition ;
1112import dev .findfirst .security .userauth .models .payload .request .SignupRequest ;
1213import dev .findfirst .users .exceptions .EmailAlreadyRegisteredException ;
1314import dev .findfirst .users .exceptions .UserNameTakenException ;
1819
1920import lombok .AllArgsConstructor ;
2021import lombok .NoArgsConstructor ;
22+ import lombok .RequiredArgsConstructor ;
2123import lombok .extern .slf4j .Slf4j ;
2224import org .springframework .beans .factory .annotation .Autowired ;
25+ import org .springframework .beans .factory .annotation .Qualifier ;
26+ import org .springframework .context .annotation .Conditional ;
27+ import org .springframework .context .annotation .Primary ;
2328import org .springframework .security .core .GrantedAuthority ;
2429import org .springframework .security .core .authority .SimpleGrantedAuthority ;
2530import org .springframework .security .oauth2 .client .userinfo .DefaultOAuth2UserService ;
3237import org .springframework .transaction .annotation .Transactional ;
3338
3439@ Service
40+ @ Conditional (OAuthClientsCondition .class )
3541@ Slf4j
36- @ AllArgsConstructor
37- @ NoArgsConstructor
42+ @ RequiredArgsConstructor
43+ @ Primary
3844public class OauthUserService implements OAuth2UserService <OAuth2UserRequest , OAuth2User > {
3945
40- @ Autowired
41- private UserRepo userRepo ;
46+ private final UserRepo userRepo ;
4247
43- @ Autowired
44- private UserManagementService ums ;
48+ private final UserManagementService ums ;
4549
46- @ Autowired ( required = false )
47- private DefaultOAuth2UserService oAuth2UserService ;
50+ @ Qualifier ( "defaultOauthService" )
51+ private final OAuth2UserService < OAuth2UserRequest , OAuth2User > delegate ;
4852
4953 @ Transactional
5054 @ Override
5155 public OAuth2User loadUser (OAuth2UserRequest userRequest ) throws OAuth2AuthenticationException {
5256 log .debug ("attempt to loadUser" );
53- OAuth2User oAuth2User = oAuth2UserService .loadUser (userRequest );
57+ OAuth2User oAuth2User = delegate .loadUser (userRequest );
5458 User user = null ;
5559
5660 // user exists in database by email
@@ -67,8 +71,7 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic
6771
6872 var username = attrs .get (userNameAttributeName ).toString ();
6973 final var registrationId = userRequest .getClientRegistration ().getClientId ();
70- final var oauth2PlaceholderEmail =
71- "generated-" + username + registrationId + "@noemail.invalid" ;
74+ final var oauth2PlaceholderEmail = "generated-" + username + registrationId + "@noemail.invalid" ;
7275
7376 Supplier <User > signup = () -> {
7477 try {
@@ -98,12 +101,10 @@ else if (username != null && !username.isEmpty()) {
98101 throw new RuntimeException ("Error with user signup/signin" );
99102 }
100103
101- int userRole =
102- (user .getRole () == null || user .getRole ().getId () == null ) ? 0 : user .getRole ().getId ();
104+ int userRole = (user .getRole () == null || user .getRole ().getId () == null ) ? 0 : user .getRole ().getId ();
103105
104106 GrantedAuthority authority = new SimpleGrantedAuthority (URole .values ()[userRole ].toString ());
105- var attributes =
106- customAttribute (attrs , userNameAttributeName , user .getUserId (), registrationId );
107+ var attributes = customAttribute (attrs , userNameAttributeName , user .getUserId (), registrationId );
107108
108109 return new DefaultOAuth2User (Collections .singletonList (authority ), attributes ,
109110 userNameAttributeName );
0 commit comments