Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
public class OAuth2SecurityConfiguration {

@Bean
public OAuth2LoginSuccessHandler oAuth2LoginSuccessHandler(OAuth2LoginProperties oAuth2LoginProperties) {
return new OAuth2LoginSuccessHandler(oAuth2LoginProperties);
public OAuth2LoginSuccessHandler oAuth2LoginSuccessHandler() {
return new OAuth2LoginSuccessHandler();
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package com.project.auth.config.auth.security;

import com.project.auth.application.auth.exception.UnsupportedOAuthProviderException;
import com.project.auth.config.auth.OAuth2LoginProperties;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import org.springframework.web.util.UriComponentsBuilder;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
Expand All @@ -16,10 +12,7 @@ public class OAuth2LoginSuccessHandler extends SimpleUrlAuthenticationSuccessHan

private static final String OAUTH_LOGIN_COMPLETION_PATH = "/api/v1/auth/oauth2/complete";

private final OAuth2LoginProperties oAuth2LoginProperties;

public OAuth2LoginSuccessHandler(OAuth2LoginProperties oAuth2LoginProperties) {
this.oAuth2LoginProperties = oAuth2LoginProperties;
public OAuth2LoginSuccessHandler() {
setAlwaysUseDefaultTargetUrl(true);
}

Expand All @@ -29,26 +22,8 @@ public void onAuthenticationSuccess(
HttpServletResponse response,
Authentication authentication
) throws IOException, ServletException {
OAuth2AuthenticationToken authenticationToken = (OAuth2AuthenticationToken) authentication;
setDefaultTargetUrl(
UriComponentsBuilder.fromPath(OAUTH_LOGIN_COMPLETION_PATH)
.queryParam("provider", resolveProvider(authenticationToken.getAuthorizedClientRegistrationId()))
.build()
.toUriString()
);
setDefaultTargetUrl(OAUTH_LOGIN_COMPLETION_PATH);
clearAuthenticationAttributes(request);
super.onAuthenticationSuccess(request, response, authentication);
}

private String resolveProvider(String registrationId) {
if (oAuth2LoginProperties.googleRegistrationId().equals(registrationId)) {
return "GOOGLE";
}

if (oAuth2LoginProperties.githubRegistrationId().equals(registrationId)) {
return "GITHUB";
}

throw new UnsupportedOAuthProviderException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.net.URI;
Expand Down Expand Up @@ -57,11 +56,10 @@ public ResponseEntity<Void> loginWithGithub() {
@Operation(hidden = true)
@GetMapping("/complete")
public ResponseEntity<ApiResult<LoginResponse>> completeOAuthLogin(
@RequestParam("provider") String provider,
Authentication authentication
) {
LoginResult loginResult = oAuthLoginUseCase.login(
oAuth2AuthenticationCommandMapper.toCommand(provider, authentication)
oAuth2AuthenticationCommandMapper.toCommand(authentication)
);
LoginResponse response = authPresentationMapper.toResponse(loginResult);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
@Component
public class OAuth2AuthenticationCommandMapper {

public OAuthLoginCommand toCommand(String provider, Authentication authentication) {
public OAuthLoginCommand toCommand(Authentication authentication) {
OAuth2AuthenticationToken authenticationToken = requireAuthenticationToken(authentication);
OidcUser oidcUser = requireOidcUser(authenticationToken.getPrincipal());

return new OAuthLoginCommand(
provider,
"KEYCLOAK",
oidcUser.getSubject(),
oidcUser.getEmail(),
resolveUserName(oidcUser)
Expand All @@ -39,14 +39,7 @@ private OidcUser requireOidcUser(Object principal) {
}

private String resolveUserName(OidcUser oidcUser) {
if (oidcUser.getFullName() != null && !oidcUser.getFullName().isBlank()) {
return oidcUser.getFullName();
}

if (oidcUser.getPreferredUsername() != null && !oidcUser.getPreferredUsername().isBlank()) {
return oidcUser.getPreferredUsername();
}

return oidcUser.getEmail();
String name = oidcUser.getFullName();
return (name != null && !name.isBlank()) ? name : oidcUser.getPreferredUsername();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ void completeOAuthLoginReturnsJwtResponse() {
"keycloak-google"
);

var response = controller.completeOAuthLogin("GOOGLE", authentication);
var response = controller.completeOAuthLogin(authentication);

assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(response.getBody()).isNotNull();
assertThat(response.getBody().success()).isTrue();
assertThat(response.getBody().data()).isNotNull();
assertThat(response.getBody().data().user().provider()).isEqualTo("GOOGLE");
assertThat(response.getBody().data().user().provider()).isEqualTo("KEYCLOAK");
assertThat(response.getBody().data().user().email()).isEqualTo("tester@example.com");
assertThat(response.getBody().data().token().issuer()).isEqualTo("project-auth-server");
}
Expand Down