|
1 | 1 | package io.github.patternhelloworld.securityhelper.oauth2.api.domain.traditionaloauth.service;
|
2 | 2 |
|
3 | 3 | import io.github.patternhelloworld.securityhelper.oauth2.api.config.logger.EasyPlusSecurityLogConfig;
|
4 |
| -import io.github.patternhelloworld.securityhelper.oauth2.api.config.security.response.error.dto.EasyPlusErrorMessages; |
5 |
| -import io.github.patternhelloworld.securityhelper.oauth2.api.config.security.response.error.exception.EasyPlusOauth2AuthenticationException; |
6 |
| - |
7 | 4 | import io.github.patternhelloworld.securityhelper.oauth2.api.config.security.message.DefaultSecurityUserExceptionMessage;
|
8 | 5 | import io.github.patternhelloworld.securityhelper.oauth2.api.config.security.message.ISecurityUserExceptionMessageService;
|
| 6 | +import io.github.patternhelloworld.securityhelper.oauth2.api.config.security.response.error.dto.EasyPlusErrorMessages; |
| 7 | +import io.github.patternhelloworld.securityhelper.oauth2.api.config.security.response.error.exception.EasyPlusOauth2AuthenticationException; |
9 | 8 | import io.github.patternhelloworld.securityhelper.oauth2.api.config.security.serivce.CommonOAuth2AuthorizationSaver;
|
10 | 9 | import io.github.patternhelloworld.securityhelper.oauth2.api.config.security.serivce.DefaultOauth2AuthenticationHashCheckService;
|
11 | 10 | import io.github.patternhelloworld.securityhelper.oauth2.api.config.security.serivce.persistence.authorization.OAuth2AuthorizationServiceImpl;
|
|
22 | 21 | import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
23 | 22 | import org.springframework.security.oauth2.core.AuthorizationGrantType;
|
24 | 23 | import org.springframework.security.oauth2.core.OAuth2AccessToken;
|
25 |
| -import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames; |
26 | 24 | import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
|
27 |
| -import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationCode; |
28 | 25 | import org.springframework.security.oauth2.server.authorization.OAuth2TokenType;
|
29 | 26 | import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
|
30 | 27 | import org.springframework.stereotype.Service;
|
@@ -104,11 +101,11 @@ public SpringSecurityTraditionalOauthDTO.TokenResponse createAccessToken(SpringS
|
104 | 101 | String.join(" ", registeredClient.getScopes()));
|
105 | 102 |
|
106 | 103 | } catch (UsernameNotFoundException e) {
|
107 |
| - throw new EasyPlusOauth2AuthenticationException(EasyPlusErrorMessages.builder().message(e.getMessage()).userMessage(e.getMessage()).build()); |
| 104 | + throw new EasyPlusOauth2AuthenticationException(EasyPlusErrorMessages.builder().message(e.getMessage()).userMessage(e.getMessage()).build(), e); |
108 | 105 | } catch (EasyPlusOauth2AuthenticationException e) {
|
109 | 106 | throw e;
|
110 | 107 | } catch (Exception e) {
|
111 |
| - throw new EasyPlusOauth2AuthenticationException(EasyPlusErrorMessages.builder().message(e.getMessage()).userMessage(iSecurityUserExceptionMessageService.getUserMessage(DefaultSecurityUserExceptionMessage.AUTHENTICATION_LOGIN_ERROR)).build()); |
| 108 | + throw new EasyPlusOauth2AuthenticationException(EasyPlusErrorMessages.builder().message(e.getMessage()).userMessage(iSecurityUserExceptionMessageService.getUserMessage(DefaultSecurityUserExceptionMessage.AUTHENTICATION_LOGIN_ERROR)).build(), e); |
112 | 109 | }
|
113 | 110 | }
|
114 | 111 |
|
@@ -145,60 +142,12 @@ public SpringSecurityTraditionalOauthDTO.TokenResponse refreshAccessToken(Spring
|
145 | 142 | refreshTokenRemainingSeconds,
|
146 | 143 | String.join(" ", registeredClient.getScopes()));
|
147 | 144 |
|
148 |
| - }catch (UsernameNotFoundException e){ |
149 |
| - throw new EasyPlusOauth2AuthenticationException(EasyPlusErrorMessages.builder().message(e.getMessage()).userMessage(e.getMessage()).build()); |
150 |
| - }catch (EasyPlusOauth2AuthenticationException e){ |
151 |
| - throw e; |
152 |
| - } catch (Exception e){ |
153 |
| - throw new EasyPlusOauth2AuthenticationException(EasyPlusErrorMessages.builder().message(e.getMessage()).userMessage(iSecurityUserExceptionMessageService.getUserMessage(DefaultSecurityUserExceptionMessage.AUTHENTICATION_LOGIN_ERROR)).build()); |
154 |
| - } |
155 |
| - } |
156 |
| - |
157 |
| - |
158 |
| - public SpringSecurityTraditionalOauthDTO.AuthorizationCodeResponse createAuthorizationCode(SpringSecurityTraditionalOauthDTO.AuthorizationCodeRequest authorizationCodeRequest, |
159 |
| - String authorizationHeader) throws EasyPlusOauth2AuthenticationException { |
160 |
| - try { |
161 |
| - |
162 |
| - BasicTokenResolver.BasicCredentials basicCredentials = BasicTokenResolver.parse(authorizationHeader) |
163 |
| - .orElseThrow(() -> new EasyPlusOauth2AuthenticationException( |
164 |
| - EasyPlusErrorMessages.builder() |
165 |
| - .message("Header parsing error (header : " + authorizationHeader + ")") |
166 |
| - .userMessage(iSecurityUserExceptionMessageService.getUserMessage(DefaultSecurityUserExceptionMessage.AUTHENTICATION_WRONG_CLIENT_ID_SECRET)) |
167 |
| - .build())); |
168 |
| - |
169 |
| - HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest(); |
170 |
| - |
171 |
| - // Registered Client 검증 |
172 |
| - @NotNull RegisteredClient registeredClient = registeredClientRepository.findByClientId(basicCredentials.getClientId()); |
173 |
| - oauth2AuthenticationHashCheckService.validateClientCredentials(basicCredentials.getClientSecret(), registeredClient); |
174 |
| - |
175 |
| - // UserDetails 로드 및 Username/Password 검증 |
176 |
| - @NotNull UserDetails userDetails = conditionalDetailsService.loadUserByUsername(authorizationCodeRequest.getUsername(), basicCredentials.getClientId()); |
177 |
| - oauth2AuthenticationHashCheckService.validateUsernamePassword(authorizationCodeRequest.getPassword(), userDetails); |
178 |
| - |
179 |
| - // Authorization Code 생성 및 저장 |
180 |
| - Map<String, Object> additionalParameters = RequestOAuth2Distiller.getTokenUsingSecurityAdditionalParameters(request); |
181 |
| - additionalParameters.put(OAuth2ParameterNames.GRANT_TYPE, AuthorizationGrantType.AUTHORIZATION_CODE.getValue()); |
182 |
| - @NotNull OAuth2Authorization oAuth2Authorization = commonOAuth2AuthorizationSaver.save(userDetails, AuthorizationGrantType.AUTHORIZATION_CODE, basicCredentials.getClientId(), additionalParameters, null); |
183 |
| - |
184 |
| - // Authorization Code 추출 |
185 |
| - String authorizationCode = oAuth2Authorization.getToken(OAuth2AuthorizationCode.class).getToken().getTokenValue(); |
186 |
| - |
187 |
| - // Authorization Code Response 반환 |
188 |
| - return new SpringSecurityTraditionalOauthDTO.AuthorizationCodeResponse(authorizationCode); |
189 |
| - |
190 | 145 | } catch (UsernameNotFoundException e) {
|
191 |
| - throw new EasyPlusOauth2AuthenticationException( |
192 |
| - EasyPlusErrorMessages.builder().message(e.getMessage()) |
193 |
| - .userMessage(e.getMessage()) |
194 |
| - .build()); |
| 146 | + throw new EasyPlusOauth2AuthenticationException(EasyPlusErrorMessages.builder().message(e.getMessage()).userMessage(e.getMessage()).build(), e); |
195 | 147 | } catch (EasyPlusOauth2AuthenticationException e) {
|
196 | 148 | throw e;
|
197 | 149 | } catch (Exception e) {
|
198 |
| - throw new EasyPlusOauth2AuthenticationException( |
199 |
| - EasyPlusErrorMessages.builder().message(e.getMessage()) |
200 |
| - .userMessage(iSecurityUserExceptionMessageService.getUserMessage(DefaultSecurityUserExceptionMessage.AUTHENTICATION_LOGIN_ERROR)) |
201 |
| - .build()); |
| 150 | + throw new EasyPlusOauth2AuthenticationException(EasyPlusErrorMessages.builder().message(e.getMessage()).userMessage(iSecurityUserExceptionMessageService.getUserMessage(DefaultSecurityUserExceptionMessage.AUTHENTICATION_LOGIN_ERROR)).build(), e); |
202 | 151 | }
|
203 | 152 | }
|
204 | 153 |
|
|
0 commit comments