|
| 1 | +package io.github.patternknife.securityhelper.oauth2.api.config.security.response.auth.authentication; |
| 2 | + |
| 3 | + |
| 4 | +import io.github.patternknife.securityhelper.oauth2.api.config.security.response.error.dto.KnifeErrorMessages; |
| 5 | +import io.github.patternknife.securityhelper.oauth2.api.config.security.response.error.exception.KnifeOauth2AuthenticationException; |
| 6 | +import io.github.patternknife.securityhelper.oauth2.api.config.security.message.DefaultSecurityUserExceptionMessage; |
| 7 | +import io.github.patternknife.securityhelper.oauth2.api.config.security.message.ISecurityUserExceptionMessageService; |
| 8 | +import jakarta.servlet.http.HttpServletRequest; |
| 9 | +import jakarta.servlet.http.HttpServletResponse; |
| 10 | +import lombok.RequiredArgsConstructor; |
| 11 | +import org.springframework.http.converter.HttpMessageConverter; |
| 12 | +import org.springframework.http.server.ServletServerHttpResponse; |
| 13 | +import org.springframework.security.core.Authentication; |
| 14 | +import org.springframework.security.oauth2.core.AuthorizationGrantType; |
| 15 | +import org.springframework.security.oauth2.core.OAuth2AccessToken; |
| 16 | +import org.springframework.security.oauth2.core.OAuth2RefreshToken; |
| 17 | +import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse; |
| 18 | +import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames; |
| 19 | +import org.springframework.security.oauth2.core.http.converter.OAuth2AccessTokenResponseHttpMessageConverter; |
| 20 | +import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AccessTokenAuthenticationToken; |
| 21 | +import org.springframework.security.web.authentication.AuthenticationSuccessHandler; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.time.Instant; |
| 25 | +import java.time.temporal.ChronoUnit; |
| 26 | +import java.util.Map; |
| 27 | + |
| 28 | +@RequiredArgsConstructor |
| 29 | +public class DefaultApiAuthenticationSuccessHandlerImpl implements AuthenticationSuccessHandler { |
| 30 | + |
| 31 | + private final HttpMessageConverter<OAuth2AccessTokenResponse> accessTokenHttpResponseConverter = |
| 32 | + new OAuth2AccessTokenResponseHttpMessageConverter(); |
| 33 | + |
| 34 | + private final ISecurityUserExceptionMessageService iSecurityUserExceptionMessageService; |
| 35 | + |
| 36 | + @Override |
| 37 | + public void onAuthenticationSuccess(final HttpServletRequest request, final HttpServletResponse response, final Authentication authentication) throws IOException { |
| 38 | + |
| 39 | + final OAuth2AccessTokenAuthenticationToken accessTokenAuthentication = (OAuth2AccessTokenAuthenticationToken) authentication; |
| 40 | + |
| 41 | + OAuth2AccessToken accessToken = accessTokenAuthentication.getAccessToken(); |
| 42 | + OAuth2RefreshToken refreshToken = accessTokenAuthentication.getRefreshToken(); |
| 43 | + Map<String, Object> additionalParameters = accessTokenAuthentication.getAdditionalParameters(); |
| 44 | + |
| 45 | + if (((String) additionalParameters.get("grant_type")).equals(AuthorizationGrantType.PASSWORD.getValue()) |
| 46 | + || ((String) additionalParameters.get("grant_type")).equals(OAuth2ParameterNames.CODE)) { |
| 47 | + OAuth2AccessTokenResponse.Builder builder = OAuth2AccessTokenResponse.withToken(accessToken.getTokenValue()) |
| 48 | + .tokenType(accessToken.getTokenType()) |
| 49 | + .scopes(accessToken.getScopes()); |
| 50 | + if (accessToken.getExpiresAt() != null) { |
| 51 | + builder.expiresIn(ChronoUnit.SECONDS.between(Instant.now(), accessToken.getExpiresAt())); |
| 52 | + } |
| 53 | + if (refreshToken != null) { |
| 54 | + builder.refreshToken(refreshToken.getTokenValue()); |
| 55 | + } |
| 56 | + OAuth2AccessTokenResponse accessTokenResponse = builder.build(); |
| 57 | + ServletServerHttpResponse httpResponse = new ServletServerHttpResponse(response); |
| 58 | + this.accessTokenHttpResponseConverter.write(accessTokenResponse, null, httpResponse); |
| 59 | + |
| 60 | + } else if (((String) additionalParameters.get("grant_type")).equals(AuthorizationGrantType.REFRESH_TOKEN.getValue())) { |
| 61 | + OAuth2AccessTokenResponse.Builder builder = OAuth2AccessTokenResponse.withToken(accessToken.getTokenValue()) |
| 62 | + .tokenType(accessToken.getTokenType()) |
| 63 | + .scopes(accessToken.getScopes()); |
| 64 | + if (refreshToken.getExpiresAt() != null) { |
| 65 | + builder.expiresIn(ChronoUnit.SECONDS.between(Instant.now(), refreshToken.getExpiresAt())); |
| 66 | + } |
| 67 | + if (refreshToken != null) { |
| 68 | + builder.refreshToken(refreshToken.getTokenValue()); |
| 69 | + } |
| 70 | + OAuth2AccessTokenResponse accessTokenResponse = builder.build(); |
| 71 | + ServletServerHttpResponse httpResponse = new ServletServerHttpResponse(response); |
| 72 | + this.accessTokenHttpResponseConverter.write(accessTokenResponse, null, httpResponse); |
| 73 | + |
| 74 | + } else if (((String) additionalParameters.get("grant_type")).equals(AuthorizationGrantType.AUTHORIZATION_CODE.getValue())) { |
| 75 | + // Authorization Code만 JSON으로 응답 |
| 76 | + String code = (String) additionalParameters.get("authorization_code"); |
| 77 | + |
| 78 | + // JSON 응답 생성 (authorization_code만 포함) |
| 79 | + String jsonResponse = String.format("{\"code\":\"%s\"}", code); |
| 80 | + |
| 81 | + // JSON 응답 전송 |
| 82 | + response.setContentType("application/json"); |
| 83 | + response.setCharacterEncoding("UTF-8"); |
| 84 | + response.getWriter().write(jsonResponse); |
| 85 | + |
| 86 | + } else { |
| 87 | + throw new KnifeOauth2AuthenticationException(KnifeErrorMessages.builder() |
| 88 | + .message("Wrong grant type from Req : " + (String) additionalParameters.get("grant_type")) |
| 89 | + .userMessage(iSecurityUserExceptionMessageService.getUserMessage(DefaultSecurityUserExceptionMessage.AUTHENTICATION_WRONG_GRANT_TYPE)) |
| 90 | + .build()); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments