|
1 | 1 | package com.example.config; |
2 | 2 |
|
| 3 | +import com.example.security.dpop.DPoPHttpHeadersConverter; |
3 | 4 | import com.example.security.dpop.DPoPProofBuilder; |
4 | | -import com.example.security.dpop.client.DPoPAccessTokenResponseClient; |
5 | | -import com.example.security.dpop.client.DefaultDPoPAccessTokenResponseClient; |
6 | | -import com.example.security.dpop.request.DPoPClientCredentialsGrantRequest; |
7 | | -import com.example.security.dpop.request.DPoPOClientCredentialsGrantRequestEntityConverter; |
8 | 5 | import com.example.service.AuthorizationDetailsJwtClientParametersConverter; |
9 | 6 | import com.example.service.HelseIDClientCredentialTokenService; |
10 | 7 | import com.example.service.HelseIDDPoPClientCredentialTokenService; |
11 | 8 | import java.time.Duration; |
12 | 9 | import lombok.extern.slf4j.Slf4j; |
13 | | -import org.springframework.boot.autoconfigure.security.oauth2.client.ClientsConfiguredCondition; |
| 10 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 11 | +import org.springframework.boot.autoconfigure.security.oauth2.client.ConditionalOnOAuth2ClientRegistrationProperties; |
14 | 12 | import org.springframework.boot.context.properties.EnableConfigurationProperties; |
15 | 13 | import org.springframework.context.annotation.Bean; |
16 | | -import org.springframework.context.annotation.Conditional; |
17 | 14 | import org.springframework.context.annotation.Configuration; |
| 15 | +import org.springframework.context.annotation.Primary; |
| 16 | +import org.springframework.core.convert.converter.Converter; |
| 17 | +import org.springframework.http.HttpHeaders; |
18 | 18 | import org.springframework.security.oauth2.client.ClientCredentialsOAuth2AuthorizedClientProvider; |
19 | 19 | import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService; |
20 | | -import org.springframework.security.oauth2.client.endpoint.AbstractOAuth2AuthorizationGrantRequest; |
21 | | -import org.springframework.security.oauth2.client.endpoint.DefaultClientCredentialsTokenResponseClient; |
22 | 20 | import org.springframework.security.oauth2.client.endpoint.OAuth2AccessTokenResponseClient; |
23 | 21 | import org.springframework.security.oauth2.client.endpoint.OAuth2ClientCredentialsGrantRequest; |
24 | | -import org.springframework.security.oauth2.client.endpoint.OAuth2ClientCredentialsGrantRequestEntityConverter; |
| 22 | +import org.springframework.security.oauth2.client.endpoint.RestClientClientCredentialsTokenResponseClient; |
25 | 23 | import org.springframework.security.oauth2.client.registration.ClientRegistration; |
26 | 24 | import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository; |
| 25 | +import org.springframework.util.MultiValueMap; |
27 | 26 |
|
28 | 27 | @Slf4j |
29 | 28 | @Configuration |
30 | 29 | @EnableConfigurationProperties({ |
31 | | - OAuth2ClientHelseIDProperties.class, |
| 30 | + OAuth2ClientHelseIDProperties.class, |
32 | 31 | }) |
33 | | -@Conditional(ClientsConfiguredCondition.class) |
| 32 | +@ConditionalOnOAuth2ClientRegistrationProperties |
34 | 33 | public class HelseIDClientCredentialConfiguration { |
35 | 34 |
|
36 | 35 | private static final String HELSEID_CREDENTIALS = "helseid-credentials"; |
37 | 36 |
|
38 | 37 | @Bean |
39 | | - public OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> |
40 | | - authorizationCredentialsGrantResponseClient( |
| 38 | + public DPoPProofBuilder dPoPProofBuilder( |
41 | 39 | OAuth2ClientDetailProperties oauth2ClientKeypairProperties) { |
42 | | - DefaultClientCredentialsTokenResponseClient tokenResponseClient = |
43 | | - new DefaultClientCredentialsTokenResponseClient(); |
| 40 | + return new DPoPProofBuilder(oauth2ClientKeypairProperties.getRegistration()); |
| 41 | + } |
44 | 42 |
|
45 | | - OAuth2ClientCredentialsGrantRequestEntityConverter requestEntityConverter = |
46 | | - new OAuth2ClientCredentialsGrantRequestEntityConverter(); |
| 43 | + @Bean |
| 44 | + @Primary |
| 45 | + public OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> |
| 46 | + authorizationCredentialsGrantResponseClient( |
| 47 | + OAuth2ClientDetailProperties oauth2ClientKeypairProperties) { |
| 48 | + |
| 49 | + RestClientClientCredentialsTokenResponseClient tokenResponseClient = |
| 50 | + new RestClientClientCredentialsTokenResponseClient(); |
47 | 51 |
|
48 | | - requestEntityConverter.addParametersConverter( |
| 52 | + tokenResponseClient.addParametersConverter( |
49 | 53 | new AuthorizationDetailsJwtClientParametersConverter<>( |
50 | 54 | oauth2ClientKeypairProperties.getRegistration())); |
51 | 55 |
|
52 | | - tokenResponseClient.setRequestEntityConverter(requestEntityConverter); |
| 56 | + return tokenResponseClient; |
| 57 | + } |
| 58 | + |
| 59 | + @Bean |
| 60 | + public OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> |
| 61 | + authorizationCredentialsGrantResponseDpopClient( |
| 62 | + DPoPProofBuilder dPoPProofBuilder, |
| 63 | + OAuth2ClientDetailProperties oauth2ClientKeypairProperties) { |
| 64 | + |
| 65 | + Converter<OAuth2ClientCredentialsGrantRequest, MultiValueMap<String, String>> |
| 66 | + jwtClientParametersConverter = |
| 67 | + new AuthorizationDetailsJwtClientParametersConverter<>( |
| 68 | + oauth2ClientKeypairProperties.getRegistration()); |
| 69 | + |
| 70 | + Converter<OAuth2ClientCredentialsGrantRequest, HttpHeaders> dpopClientParametersConverter = |
| 71 | + new DPoPHttpHeadersConverter<>(jwtClientParametersConverter, dPoPProofBuilder); |
| 72 | + |
| 73 | + RestClientClientCredentialsTokenResponseClient tokenResponseClient = |
| 74 | + new RestClientClientCredentialsTokenResponseClient(); |
| 75 | + tokenResponseClient.addParametersConverter(jwtClientParametersConverter); |
| 76 | + |
| 77 | + tokenResponseClient.addHeadersConverter(dpopClientParametersConverter); |
| 78 | + |
53 | 79 | return tokenResponseClient; |
54 | 80 | } |
55 | 81 |
|
56 | 82 | @Bean |
57 | 83 | public HelseIDClientCredentialTokenService helseIDClientCredentialTokenService( |
58 | 84 | ClientRegistrationRepository clientRegistrationRepository, |
59 | 85 | OAuth2AuthorizedClientService oAuth2AuthorizedClientService, |
60 | | - OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> |
61 | | - credentialsGrantResponseClient) { |
| 86 | + @Qualifier("authorizationCredentialsGrantResponseClient") |
| 87 | + OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> |
| 88 | + authorizationCredentialsGrantResponseClient) { |
62 | 89 |
|
63 | 90 | ClientRegistration clientRegistration = |
64 | 91 | clientRegistrationRepository.findByRegistrationId(HELSEID_CREDENTIALS); |
65 | 92 |
|
66 | | - ClientCredentialsOAuth2AuthorizedClientProvider clientCredentialsAuthorizedClientProvider = new ClientCredentialsOAuth2AuthorizedClientProvider(); |
| 93 | + ClientCredentialsOAuth2AuthorizedClientProvider clientCredentialsAuthorizedClientProvider = |
| 94 | + new ClientCredentialsOAuth2AuthorizedClientProvider(); |
67 | 95 | clientCredentialsAuthorizedClientProvider.setAccessTokenResponseClient( |
68 | | - credentialsGrantResponseClient); |
| 96 | + authorizationCredentialsGrantResponseClient); |
69 | 97 |
|
70 | 98 | return new HelseIDClientCredentialTokenService( |
71 | 99 | clientRegistration, |
72 | 100 | oAuth2AuthorizedClientService, |
73 | 101 | clientCredentialsAuthorizedClientProvider); |
74 | 102 | } |
75 | 103 |
|
76 | | - @Bean |
77 | | - public DPoPProofBuilder dPoPProofBuilder( |
78 | | - OAuth2ClientDetailProperties oauth2ClientKeypairProperties) { |
79 | | - return new DPoPProofBuilder(oauth2ClientKeypairProperties.getRegistration()); |
80 | | - } |
81 | | - |
82 | | - @Bean |
83 | | - public DPoPAccessTokenResponseClient<DPoPClientCredentialsGrantRequest> |
84 | | - authorizationCredentialsGrantResponseDpopClient(DPoPProofBuilder dPoPProofBuilder, |
85 | | - OAuth2ClientDetailProperties oauth2ClientKeypairProperties) { |
86 | | - |
87 | | - AuthorizationDetailsJwtClientParametersConverter<AbstractOAuth2AuthorizationGrantRequest> |
88 | | - parametersConverter = |
89 | | - new AuthorizationDetailsJwtClientParametersConverter<>( |
90 | | - oauth2ClientKeypairProperties.getRegistration()); |
91 | | - |
92 | | - DPoPOClientCredentialsGrantRequestEntityConverter requestEntityConverter = |
93 | | - new DPoPOClientCredentialsGrantRequestEntityConverter( |
94 | | - parametersConverter, dPoPProofBuilder); |
95 | | - |
96 | | - return new DefaultDPoPAccessTokenResponseClient(requestEntityConverter); |
97 | | - } |
98 | | - |
99 | 104 | @Bean |
100 | 105 | public HelseIDDPoPClientCredentialTokenService helseIdApiDPOPClientCredentialTokenService( |
101 | 106 | ClientRegistrationRepository clientRegistrationRepository, |
102 | 107 | OAuth2AuthorizedClientService oAuth2AuthorizedClientService, |
103 | 108 | DPoPProofBuilder dPoPProofBuilder, |
104 | | - DPoPAccessTokenResponseClient<DPoPClientCredentialsGrantRequest> credentialsGrantClient) { |
| 109 | + @Qualifier("authorizationCredentialsGrantResponseDpopClient") |
| 110 | + OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> |
| 111 | + authorizationCredentialsGrantResponseDpopClient) { |
105 | 112 | ClientRegistration clientRegistration = |
106 | 113 | clientRegistrationRepository.findByRegistrationId(HELSEID_CREDENTIALS); |
| 114 | + |
| 115 | + ClientCredentialsOAuth2AuthorizedClientProvider clientCredentialsAuthorizedClientProvider = |
| 116 | + new ClientCredentialsOAuth2AuthorizedClientProvider(); |
| 117 | + clientCredentialsAuthorizedClientProvider.setAccessTokenResponseClient( |
| 118 | + authorizationCredentialsGrantResponseDpopClient); |
| 119 | + |
107 | 120 | return new HelseIDDPoPClientCredentialTokenService( |
108 | 121 | clientRegistration, |
109 | 122 | dPoPProofBuilder, |
110 | 123 | oAuth2AuthorizedClientService, |
111 | | - credentialsGrantClient, |
| 124 | + clientCredentialsAuthorizedClientProvider, |
112 | 125 | Duration.ofMinutes(2)); |
113 | 126 | } |
114 | 127 | } |
0 commit comments