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 ;
2021import lombok .NoArgsConstructor ;
2122import lombok .extern .slf4j .Slf4j ;
2223import org .springframework .beans .factory .annotation .Autowired ;
24+ import org .springframework .context .annotation .Conditional ;
2325import org .springframework .security .core .GrantedAuthority ;
2426import org .springframework .security .core .authority .SimpleGrantedAuthority ;
2527import org .springframework .security .oauth2 .client .userinfo .DefaultOAuth2UserService ;
3234import org .springframework .transaction .annotation .Transactional ;
3335
3436@ Service
37+ @ Conditional (OAuthClientsCondition .class )
3538@ Slf4j
3639@ AllArgsConstructor
3740@ NoArgsConstructor
@@ -49,18 +52,25 @@ public class OauthUserService implements OAuth2UserService<OAuth2UserRequest, OA
4952 @ Transactional
5053 @ Override
5154 public OAuth2User loadUser (OAuth2UserRequest userRequest ) throws OAuth2AuthenticationException {
55+ log .debug ("attempt to loadUser" );
5256 OAuth2User oAuth2User = oAuth2UserService .loadUser (userRequest );
5357 User user = null ;
5458
5559 // user exists in database by email
56- final String userNameAttributeName = userRequest .getClientRegistration ().getProviderDetails ()
60+ String userNameAttributeName = userRequest .getClientRegistration ().getProviderDetails ()
5761 .getUserInfoEndpoint ().getUserNameAttributeName ();
62+ if (userRequest .getClientRegistration ().getClientName ().equalsIgnoreCase ("github" )) {
63+ log .debug ("set the userNameAttributeName to login" );
64+ userNameAttributeName = "login" ;
65+ }
66+
67+ log .debug ("userNameAttributeName {}" , userNameAttributeName );
5868 final var attrs = oAuth2User .getAttributes ();
5969 final var email = (String ) attrs .get ("email" );
60- final var username = (String ) attrs .get (userNameAttributeName );
70+
71+ var username = attrs .get (userNameAttributeName ).toString ();
6172 final var registrationId = userRequest .getClientRegistration ().getClientId ();
62- final var oauth2PlaceholderEmail =
63- "generated-" + username + registrationId + "@noemail.invalid" ;
73+ final var oauth2PlaceholderEmail = "generated-" + username + registrationId + "@noemail.invalid" ;
6474
6575 Supplier <User > signup = () -> {
6676 try {
@@ -90,12 +100,10 @@ else if (username != null && !username.isEmpty()) {
90100 throw new RuntimeException ("Error with user signup/signin" );
91101 }
92102
93- int userRole =
94- (user .getRole () == null || user .getRole ().getId () == null ) ? 0 : user .getRole ().getId ();
103+ int userRole = (user .getRole () == null || user .getRole ().getId () == null ) ? 0 : user .getRole ().getId ();
95104
96105 GrantedAuthority authority = new SimpleGrantedAuthority (URole .values ()[userRole ].toString ());
97- var attributes =
98- customAttribute (attrs , userNameAttributeName , user .getUserId (), registrationId );
106+ var attributes = customAttribute (attrs , userNameAttributeName , user .getUserId (), registrationId );
99107
100108 return new DefaultOAuth2User (Collections .singletonList (authority ), attributes ,
101109 userNameAttributeName );
0 commit comments