|
1 | | -package io.github.patternknife.securityhelper.oauth2.api.config.security.converter.auth.endpoint; |
2 | | - |
3 | | -import io.github.patternknife.securityhelper.oauth2.api.config.security.dao.KnifeAuthorizationConsentRepository; |
4 | | -import io.github.patternknife.securityhelper.oauth2.api.config.security.response.error.exception.KnifeOauth2AuthenticationException; |
5 | | -import io.github.patternknife.securityhelper.oauth2.api.config.security.serivce.persistence.authorization.OAuth2AuthorizationServiceImpl; |
6 | | -import io.github.patternknife.securityhelper.oauth2.api.config.util.RequestOAuth2Distiller; |
7 | | -import jakarta.servlet.http.HttpServletRequest; |
8 | | -import lombok.RequiredArgsConstructor; |
9 | | -import org.springframework.lang.Nullable; |
10 | | -import org.springframework.security.core.Authentication; |
11 | | -import org.springframework.security.core.context.SecurityContextHolder; |
12 | | -import org.springframework.security.oauth2.core.ClientAuthenticationMethod; |
13 | | -import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames; |
14 | | -import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AuthorizationCodeAuthenticationToken; |
15 | | -import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AuthorizationCodeRequestAuthenticationToken; |
16 | | -import org.springframework.security.oauth2.server.authorization.authentication.OAuth2ClientAuthenticationToken; |
17 | | -import org.springframework.security.oauth2.server.authorization.client.RegisteredClient; |
18 | | -import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository; |
19 | | -import org.springframework.security.web.authentication.AuthenticationConverter; |
20 | | -import org.springframework.util.MultiValueMap; |
21 | | -import org.springframework.util.StringUtils; |
22 | | - |
23 | | -import java.util.*; |
24 | | - |
25 | | -@RequiredArgsConstructor |
26 | | -public final class AuthorizationCodeRequestAuthenticationConverter implements AuthenticationConverter { |
27 | | - |
28 | | - private final RegisteredClientRepository registeredClientRepository; |
29 | | - private final KnifeAuthorizationConsentRepository knifeAuthorizationConsentRepository; |
30 | | - private final OAuth2AuthorizationServiceImpl oAuth2AuthorizationService; |
31 | | - |
32 | | - public void setClientAuthentication(String clientId) { |
33 | | - RegisteredClient registeredClient = registeredClientRepository.findByClientId(clientId); |
34 | | - if (registeredClient == null) { |
35 | | - throw new IllegalArgumentException("Invalid client ID"); |
36 | | - } |
37 | | - |
38 | | - OAuth2ClientAuthenticationToken clientAuthenticationToken = new OAuth2ClientAuthenticationToken( |
39 | | - registeredClient, |
40 | | - ClientAuthenticationMethod.CLIENT_SECRET_BASIC, |
41 | | - null |
42 | | - ); |
43 | | - |
44 | | - SecurityContextHolder.getContext().setAuthentication(clientAuthenticationToken); |
45 | | - } |
46 | | - |
47 | | - @Override |
48 | | - @Nullable |
49 | | - public Authentication convert(HttpServletRequest request) { |
50 | | - if ("POST".equalsIgnoreCase(request.getMethod())) { |
51 | | - // TODO: Authorization Consent |
52 | | - } else if ("GET".equalsIgnoreCase(request.getMethod())) { |
53 | | - MultiValueMap<String, String> parameters = RequestOAuth2Distiller.getAuthorizationCodeSecurityAdditionalParameters(request); |
54 | | - String code = parameters.getFirst(OAuth2ParameterNames.CODE); |
55 | | - |
56 | | - if (!StringUtils.hasText(code)) { |
57 | | - throw new KnifeOauth2AuthenticationException("Authorization code missing in GET request"); |
58 | | - } |
59 | | - |
60 | | - // 클라이언트 ID와 기타 필수 파라미터 처리 |
61 | | - String clientId = parameters.getFirst(OAuth2ParameterNames.CLIENT_ID); |
62 | | - if (!StringUtils.hasText(clientId)) { |
63 | | - throw new KnifeOauth2AuthenticationException("client_id missing"); |
64 | | - } |
65 | | - |
66 | | - // 클라이언트 인증 설정 |
67 | | - setClientAuthentication(clientId); |
68 | | - Authentication clientPrincipal = SecurityContextHolder.getContext().getAuthentication(); |
69 | | - |
70 | | - String redirectUri = parameters.getFirst(OAuth2ParameterNames.REDIRECT_URI); |
71 | | - if (!StringUtils.hasText(redirectUri)) { |
72 | | - throw new KnifeOauth2AuthenticationException("redirect_uri missing"); |
73 | | - } |
74 | | - |
75 | | - |
76 | | - RegisteredClient registeredClient = ((OAuth2ClientAuthenticationToken) clientPrincipal).getRegisteredClient(); |
77 | | - |
78 | | - // Check if the registered client is null |
79 | | - if (registeredClient == null) { |
80 | | - throw new KnifeOauth2AuthenticationException("Registered client is missing or invalid"); |
81 | | - } |
82 | | - // Check if the redirectUri is not in the registered redirect URIs |
83 | | - if (!registeredClient.getRedirectUris().contains(redirectUri)) { |
84 | | - throw new KnifeOauth2AuthenticationException("Invalid redirect_uri: " + redirectUri); |
85 | | - } |
86 | | - |
87 | | - |
88 | | - Set<String> requestedScopes = new HashSet<>(parameters.getOrDefault(OAuth2ParameterNames.SCOPE, Collections.emptyList())); |
89 | | - // Scopes from the request |
90 | | - Set<String> registeredScopes = registeredClient.getScopes(); // Scopes from the RegisteredClient |
91 | | - |
92 | | - if (!registeredScopes.containsAll(requestedScopes)) { |
93 | | - throw new KnifeOauth2AuthenticationException("Invalid scopes: " + requestedScopes + ". Allowed scopes: " + registeredScopes); |
94 | | - } |
95 | | - |
96 | | - Map<String, Object> additionalParameters = new HashMap<>(); |
97 | | - |
98 | | - parameters.forEach((key, value) -> { |
99 | | - additionalParameters.put(key, (value.size() == 1) ? value.get(0) : value.toArray(new String[0])); |
100 | | - }); |
101 | | - |
102 | | - return new OAuth2AuthorizationCodeAuthenticationToken( |
103 | | - code, |
104 | | - clientPrincipal, |
105 | | - redirectUri, |
106 | | - additionalParameters |
107 | | - ); |
108 | | - |
109 | | - } else { |
110 | | - throw new IllegalStateException("Unsupported HTTP method: " + request.getMethod()); |
111 | | - } |
112 | | - |
113 | | - return null; |
114 | | - // TODO: Authorization Consent |
115 | | - /* return new OAuth2AuthorizationCodeRequestAuthenticationToken( |
116 | | - parameters.getFirst(OAuth2ParameterNames.REDIRECT_URI), |
117 | | - clientId, |
118 | | - clientPrincipal, |
119 | | - redirectUri, |
120 | | - state, |
121 | | - scopes, |
122 | | - additionalParameters |
123 | | - );*/ |
124 | | - } |
125 | | -} |
126 | | - |
| 1 | +package io.github.patternknife.securityhelper.oauth2.api.config.security.converter.auth.endpoint; |
| 2 | + |
| 3 | +import io.github.patternknife.securityhelper.oauth2.api.config.security.dao.KnifeAuthorizationConsentRepository; |
| 4 | +import io.github.patternknife.securityhelper.oauth2.api.config.security.response.error.exception.KnifeOauth2AuthenticationException; |
| 5 | +import io.github.patternknife.securityhelper.oauth2.api.config.security.serivce.persistence.authorization.OAuth2AuthorizationServiceImpl; |
| 6 | +import io.github.patternknife.securityhelper.oauth2.api.config.util.RequestOAuth2Distiller; |
| 7 | +import jakarta.servlet.http.HttpServletRequest; |
| 8 | +import lombok.RequiredArgsConstructor; |
| 9 | +import org.springframework.lang.Nullable; |
| 10 | +import org.springframework.security.core.Authentication; |
| 11 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 12 | +import org.springframework.security.oauth2.core.ClientAuthenticationMethod; |
| 13 | +import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames; |
| 14 | +import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AuthorizationCodeAuthenticationToken; |
| 15 | +import org.springframework.security.oauth2.server.authorization.authentication.OAuth2ClientAuthenticationToken; |
| 16 | +import org.springframework.security.oauth2.server.authorization.client.RegisteredClient; |
| 17 | +import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository; |
| 18 | +import org.springframework.security.web.authentication.AuthenticationConverter; |
| 19 | +import org.springframework.util.MultiValueMap; |
| 20 | +import org.springframework.util.StringUtils; |
| 21 | + |
| 22 | +import java.util.*; |
| 23 | + |
| 24 | +@RequiredArgsConstructor |
| 25 | +public final class AuthorizationCodeAuthorizationRequestConverter implements AuthenticationConverter { |
| 26 | + |
| 27 | + private final RegisteredClientRepository registeredClientRepository; |
| 28 | + private final KnifeAuthorizationConsentRepository knifeAuthorizationConsentRepository; |
| 29 | + private final OAuth2AuthorizationServiceImpl oAuth2AuthorizationService; |
| 30 | + |
| 31 | + public void setClientAuthentication(String clientId) { |
| 32 | + RegisteredClient registeredClient = registeredClientRepository.findByClientId(clientId); |
| 33 | + if (registeredClient == null) { |
| 34 | + throw new IllegalArgumentException("Invalid client ID"); |
| 35 | + } |
| 36 | + |
| 37 | + OAuth2ClientAuthenticationToken clientAuthenticationToken = new OAuth2ClientAuthenticationToken( |
| 38 | + registeredClient, |
| 39 | + ClientAuthenticationMethod.CLIENT_SECRET_BASIC, |
| 40 | + null |
| 41 | + ); |
| 42 | + |
| 43 | + SecurityContextHolder.getContext().setAuthentication(clientAuthenticationToken); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + @Nullable |
| 48 | + public Authentication convert(HttpServletRequest request) { |
| 49 | + if ("POST".equalsIgnoreCase(request.getMethod())) { |
| 50 | + // TODO: Authorization Consent |
| 51 | + } else if ("GET".equalsIgnoreCase(request.getMethod())) { |
| 52 | + MultiValueMap<String, String> parameters = RequestOAuth2Distiller.getAuthorizationCodeSecurityAdditionalParameters(request); |
| 53 | + String code = parameters.getFirst(OAuth2ParameterNames.CODE); |
| 54 | + |
| 55 | + if (!StringUtils.hasText(code)) { |
| 56 | + throw new KnifeOauth2AuthenticationException("Authorization code missing in GET request"); |
| 57 | + } |
| 58 | + |
| 59 | + // 클라이언트 ID와 기타 필수 파라미터 처리 |
| 60 | + String clientId = parameters.getFirst(OAuth2ParameterNames.CLIENT_ID); |
| 61 | + if (!StringUtils.hasText(clientId)) { |
| 62 | + throw new KnifeOauth2AuthenticationException("client_id missing"); |
| 63 | + } |
| 64 | + |
| 65 | + // 클라이언트 인증 설정 |
| 66 | + setClientAuthentication(clientId); |
| 67 | + Authentication clientPrincipal = SecurityContextHolder.getContext().getAuthentication(); |
| 68 | + |
| 69 | + String redirectUri = parameters.getFirst(OAuth2ParameterNames.REDIRECT_URI); |
| 70 | + if (!StringUtils.hasText(redirectUri)) { |
| 71 | + throw new KnifeOauth2AuthenticationException("redirect_uri missing"); |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + RegisteredClient registeredClient = ((OAuth2ClientAuthenticationToken) clientPrincipal).getRegisteredClient(); |
| 76 | + |
| 77 | + // Check if the registered client is null |
| 78 | + if (registeredClient == null) { |
| 79 | + throw new KnifeOauth2AuthenticationException("Registered client is missing or invalid"); |
| 80 | + } |
| 81 | + // Check if the redirectUri is not in the registered redirect URIs |
| 82 | + if (!registeredClient.getRedirectUris().contains(redirectUri)) { |
| 83 | + throw new KnifeOauth2AuthenticationException("Invalid redirect_uri: " + redirectUri); |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + Set<String> requestedScopes = new HashSet<>(parameters.getOrDefault(OAuth2ParameterNames.SCOPE, Collections.emptyList())); |
| 88 | + // Scopes from the request |
| 89 | + Set<String> registeredScopes = registeredClient.getScopes(); // Scopes from the RegisteredClient |
| 90 | + |
| 91 | + if (!registeredScopes.containsAll(requestedScopes)) { |
| 92 | + throw new KnifeOauth2AuthenticationException("Invalid scopes: " + requestedScopes + ". Allowed scopes: " + registeredScopes); |
| 93 | + } |
| 94 | + |
| 95 | + Map<String, Object> additionalParameters = new HashMap<>(); |
| 96 | + |
| 97 | + parameters.forEach((key, value) -> { |
| 98 | + additionalParameters.put(key, (value.size() == 1) ? value.get(0) : value.toArray(new String[0])); |
| 99 | + }); |
| 100 | + |
| 101 | + return new OAuth2AuthorizationCodeAuthenticationToken( |
| 102 | + code, |
| 103 | + clientPrincipal, |
| 104 | + redirectUri, |
| 105 | + additionalParameters |
| 106 | + ); |
| 107 | + |
| 108 | + } else { |
| 109 | + throw new IllegalStateException("Unsupported HTTP method: " + request.getMethod()); |
| 110 | + } |
| 111 | + |
| 112 | + return null; |
| 113 | + // TODO: Authorization Consent |
| 114 | + /* return new OAuth2AuthorizationCodeRequestAuthenticationToken( |
| 115 | + parameters.getFirst(OAuth2ParameterNames.REDIRECT_URI), |
| 116 | + clientId, |
| 117 | + clientPrincipal, |
| 118 | + redirectUri, |
| 119 | + state, |
| 120 | + scopes, |
| 121 | + additionalParameters |
| 122 | + );*/ |
| 123 | + } |
| 124 | +} |
| 125 | + |
0 commit comments