forked from springdoc/springdoc-openapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpringDocSecurityOAuth2Customizer.java
360 lines (329 loc) · 16 KB
/
SpringDocSecurityOAuth2Customizer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package org.springdoc.security;
import java.lang.reflect.Field;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.headers.Header;
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.Content;
import io.swagger.v3.oas.models.media.MapSchema;
import io.swagger.v3.oas.models.media.MediaType;
import io.swagger.v3.oas.models.media.ObjectSchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.parameters.RequestBody;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.oas.models.responses.ApiResponses;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springdoc.core.SpringDocAnnotationsUtils;
import org.springdoc.core.customizers.GlobalOpenApiCustomizer;
import org.springdoc.security.oauth2.SpringDocOAuth2AuthorizationServerMetadata;
import org.springdoc.security.oauth2.SpringDocOAuth2Token;
import org.springdoc.security.oauth2.SpringDocOAuth2TokenIntrospection;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.security.oauth2.core.OAuth2Error;
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
import org.springframework.security.oauth2.server.authorization.web.NimbusJwkSetEndpointFilter;
import org.springframework.security.oauth2.server.authorization.web.OAuth2AuthorizationEndpointFilter;
import org.springframework.security.oauth2.server.authorization.web.OAuth2AuthorizationServerMetadataEndpointFilter;
import org.springframework.security.oauth2.server.authorization.web.OAuth2TokenEndpointFilter;
import org.springframework.security.oauth2.server.authorization.web.OAuth2TokenIntrospectionEndpointFilter;
import org.springframework.security.oauth2.server.authorization.web.OAuth2TokenRevocationEndpointFilter;
import org.springframework.security.web.FilterChainProxy;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.OrRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.http.MediaType.TEXT_HTML_VALUE;
/**
* The type Spring doc security o auth 2 customizer.
*
* @author bnasslahsen
*/
public class SpringDocSecurityOAuth2Customizer implements GlobalOpenApiCustomizer, ApplicationContextAware {
/**
* The constant LOGGER.
*/
private static final Logger LOGGER = LoggerFactory.getLogger(SpringDocSecurityOAuth2Customizer.class);
/**
* The constant OAUTH2_ENDPOINT_TAG.
*/
private static final String OAUTH2_ENDPOINT_TAG = "authorization-server-endpoints";
/**
* The Context.
*/
private ApplicationContext applicationContext;
@Override
public void customise(OpenAPI openAPI) {
FilterChainProxy filterChainProxy = applicationContext.getBean(AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME, FilterChainProxy.class);
for (SecurityFilterChain filterChain : filterChainProxy.getFilterChains()) {
getNimbusJwkSetEndpoint(openAPI, filterChain);
getOAuth2AuthorizationServerMetadataEndpoint(openAPI, filterChain);
getOAuth2TokenEndpoint(openAPI, filterChain);
getOAuth2AuthorizationEndpoint(openAPI, filterChain);
getOAuth2TokenIntrospectionEndpointFilter(openAPI, filterChain);
getOAuth2TokenRevocationEndpointFilter(openAPI, filterChain);
}
}
/**
* Gets o auth 2 token revocation endpoint filter.
*
* @param openAPI the open api
* @param securityFilterChain the security filter chain
*/
private void getOAuth2TokenRevocationEndpointFilter(OpenAPI openAPI, SecurityFilterChain securityFilterChain) {
Object oAuth2EndpointFilter =
new SpringDocSecurityOAuth2EndpointUtils(OAuth2TokenRevocationEndpointFilter.class).findEndpoint(securityFilterChain);
if (oAuth2EndpointFilter != null) {
ApiResponses apiResponses = new ApiResponses();
apiResponses.addApiResponse(String.valueOf(HttpStatus.OK.value()), new ApiResponse().description(HttpStatus.OK.getReasonPhrase()));
buildApiResponsesOnInternalServerError(apiResponses);
buildApiResponsesOnBadRequest(apiResponses, openAPI);
Operation operation = buildOperation(apiResponses);
Schema<?> schema = new ObjectSchema()
.addProperty("token", new StringSchema())
.addProperty(OAuth2ParameterNames.TOKEN_TYPE_HINT, new StringSchema());
String mediaType = org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED_VALUE;
RequestBody requestBody = new RequestBody().content(new Content().addMediaType(mediaType, new MediaType().schema(schema)));
operation.setRequestBody(requestBody);
buildPath(oAuth2EndpointFilter, "tokenRevocationEndpointMatcher", openAPI, operation, HttpMethod.POST);
}
}
/**
* Gets o auth 2 token introspection endpoint filter.
*
* @param openAPI the open api
* @param securityFilterChain the security filter chain
*/
private void getOAuth2TokenIntrospectionEndpointFilter(OpenAPI openAPI, SecurityFilterChain securityFilterChain) {
Object oAuth2EndpointFilter =
new SpringDocSecurityOAuth2EndpointUtils(OAuth2TokenIntrospectionEndpointFilter.class).findEndpoint(securityFilterChain);
if (oAuth2EndpointFilter != null) {
ApiResponses apiResponses = new ApiResponses();
buildApiResponsesOnSuccess(apiResponses, SpringDocAnnotationsUtils.resolveSchemaFromType(SpringDocOAuth2TokenIntrospection.class, openAPI.getComponents(), null));
buildApiResponsesOnInternalServerError(apiResponses);
buildApiResponsesOnBadRequest(apiResponses, openAPI);
Operation operation = buildOperation(apiResponses);
Schema<?> requestSchema = new ObjectSchema()
.addProperty("token", new StringSchema())
.addProperty(OAuth2ParameterNames.TOKEN_TYPE_HINT, new StringSchema())
.addProperty("additionalParameters", new ObjectSchema().additionalProperties(new StringSchema()));
String mediaType = org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED_VALUE;
RequestBody requestBody = new RequestBody().content(new Content().addMediaType(mediaType, new MediaType().schema(requestSchema)));
operation.setRequestBody(requestBody);
buildPath(oAuth2EndpointFilter, "tokenIntrospectionEndpointMatcher", openAPI, operation, HttpMethod.POST);
}
}
/**
* Gets o auth 2 authorization server metadata endpoint.
*
* @param openAPI the open api
* @param securityFilterChain the security filter chain
*/
private void getOAuth2AuthorizationServerMetadataEndpoint(OpenAPI openAPI, SecurityFilterChain securityFilterChain) {
Object oAuth2EndpointFilter =
new SpringDocSecurityOAuth2EndpointUtils(OAuth2AuthorizationServerMetadataEndpointFilter.class).findEndpoint(securityFilterChain);
if (oAuth2EndpointFilter != null) {
ApiResponses apiResponses = new ApiResponses();
buildApiResponsesOnSuccess(apiResponses, SpringDocAnnotationsUtils.resolveSchemaFromType(SpringDocOAuth2AuthorizationServerMetadata.class, openAPI.getComponents(), null));
buildApiResponsesOnInternalServerError(apiResponses);
Operation operation = buildOperation(apiResponses);
buildPath(oAuth2EndpointFilter, "requestMatcher", openAPI, operation, HttpMethod.GET);
}
}
/**
* Gets nimbus jwk set endpoint.
*
* @param openAPI the open api
* @param securityFilterChain the security filter chain
*/
private void getNimbusJwkSetEndpoint(OpenAPI openAPI, SecurityFilterChain securityFilterChain) {
Object oAuth2EndpointFilter =
new SpringDocSecurityOAuth2EndpointUtils(NimbusJwkSetEndpointFilter.class).findEndpoint(securityFilterChain);
if (oAuth2EndpointFilter != null) {
ApiResponses apiResponses = new ApiResponses();
Schema<?> schema = new MapSchema();
schema.addProperty("keys", new ArraySchema().items(new ObjectSchema().additionalProperties(true)));
ApiResponse response = new ApiResponse().description(HttpStatus.OK.getReasonPhrase()).content(new Content().addMediaType(
APPLICATION_JSON_VALUE,
new MediaType().schema(schema)));
apiResponses.addApiResponse(String.valueOf(HttpStatus.OK.value()), response);
buildApiResponsesOnInternalServerError(apiResponses);
buildApiResponsesOnBadRequest(apiResponses, openAPI);
Operation operation = buildOperation(apiResponses);
operation.responses(apiResponses);
buildPath(oAuth2EndpointFilter, "requestMatcher", openAPI, operation, HttpMethod.GET);
}
}
/**
* Gets o auth 2 token endpoint.
*
* @param openAPI the open api
* @param securityFilterChain the security filter chain
*/
private void getOAuth2TokenEndpoint(OpenAPI openAPI, SecurityFilterChain securityFilterChain) {
Object oAuth2EndpointFilter =
new SpringDocSecurityOAuth2EndpointUtils(OAuth2TokenEndpointFilter.class).findEndpoint(securityFilterChain);
if (oAuth2EndpointFilter != null) {
ApiResponses apiResponses = new ApiResponses();
buildApiResponsesOnSuccess(apiResponses, SpringDocAnnotationsUtils.resolveSchemaFromType(SpringDocOAuth2Token.class, openAPI.getComponents(), null));
buildApiResponsesOnInternalServerError(apiResponses);
buildApiResponsesOnBadRequest(apiResponses, openAPI);
buildOAuth2Error(openAPI, apiResponses, HttpStatus.UNAUTHORIZED);
Operation operation = buildOperation(apiResponses);
Schema<?> schema = new ObjectSchema().additionalProperties(new StringSchema());
operation.addParametersItem(new Parameter().name("parameters").in(ParameterIn.QUERY.toString()).schema(schema));
buildPath(oAuth2EndpointFilter, "tokenEndpointMatcher", openAPI, operation, HttpMethod.POST);
}
}
/**
* Gets o auth 2 authorization endpoint.
*
* @param openAPI the open api
* @param securityFilterChain the security filter chain
*/
private void getOAuth2AuthorizationEndpoint(OpenAPI openAPI, SecurityFilterChain securityFilterChain) {
Object oAuth2EndpointFilter =
new SpringDocSecurityOAuth2EndpointUtils(OAuth2AuthorizationEndpointFilter.class).findEndpoint(securityFilterChain);
if (oAuth2EndpointFilter != null) {
ApiResponses apiResponses = new ApiResponses();
ApiResponse response = new ApiResponse().description(HttpStatus.OK.getReasonPhrase()).content(new Content().addMediaType(
TEXT_HTML_VALUE,
new MediaType()));
apiResponses.addApiResponse(String.valueOf(HttpStatus.OK.value()), response);
buildApiResponsesOnInternalServerError(apiResponses);
buildApiResponsesOnBadRequest(apiResponses, openAPI);
apiResponses.addApiResponse(String.valueOf(HttpStatus.MOVED_TEMPORARILY.value()),
new ApiResponse().description(HttpStatus.MOVED_TEMPORARILY.getReasonPhrase())
.addHeaderObject("Location", new Header().schema(new StringSchema())));
Operation operation = buildOperation(apiResponses);
Schema<?> schema = new ObjectSchema().additionalProperties(new StringSchema());
operation.addParametersItem(new Parameter().name("parameters").in(ParameterIn.QUERY.toString()).schema(schema));
buildPath(oAuth2EndpointFilter, "authorizationEndpointMatcher", openAPI, operation, HttpMethod.POST);
}
}
/**
* Build operation operation.
*
* @param apiResponses the api responses
* @return the operation
*/
private Operation buildOperation(ApiResponses apiResponses) {
Operation operation = new Operation();
operation.addTagsItem(OAUTH2_ENDPOINT_TAG);
operation.responses(apiResponses);
return operation;
}
/**
* Build api responses api responses on success.
*
* @param apiResponses the api responses
* @param schema the schema
* @return the api responses
*/
private ApiResponses buildApiResponsesOnSuccess(ApiResponses apiResponses, Schema schema) {
ApiResponse response = new ApiResponse().description(HttpStatus.OK.getReasonPhrase()).content(new Content().addMediaType(
APPLICATION_JSON_VALUE,
new MediaType().schema(schema)));
apiResponses.addApiResponse(String.valueOf(HttpStatus.OK.value()), response);
return apiResponses;
}
/**
* Build api responses api responses on internal server error.
*
* @param apiResponses the api responses
* @return the api responses
*/
private ApiResponses buildApiResponsesOnInternalServerError(ApiResponses apiResponses) {
apiResponses.addApiResponse(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()), new ApiResponse().description(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()));
return apiResponses;
}
/**
* Build api responses on bad request.
*
* @param apiResponses the api responses
* @param openAPI the open api
* @return the api responses
*/
private ApiResponses buildApiResponsesOnBadRequest(ApiResponses apiResponses, OpenAPI openAPI) {
buildOAuth2Error(openAPI, apiResponses, HttpStatus.BAD_REQUEST);
return apiResponses;
}
/**
* Build o auth 2 error.
*
* @param openAPI the open api
* @param apiResponses the api responses
* @param httpStatus the http status
*/
private static void buildOAuth2Error(OpenAPI openAPI, ApiResponses apiResponses, HttpStatus httpStatus) {
Schema oAuth2ErrorSchema = SpringDocAnnotationsUtils.resolveSchemaFromType(OAuth2Error.class, openAPI.getComponents(), null);
apiResponses.addApiResponse(String.valueOf(httpStatus.value()), new ApiResponse().description(httpStatus.getReasonPhrase()).content(new Content().addMediaType(
APPLICATION_JSON_VALUE,
new MediaType().schema(oAuth2ErrorSchema))));
}
/**
* Build path.
*
* @param oAuth2EndpointFilter the o auth 2 endpoint filter
* @param authorizationEndpointMatcher the authorization endpoint matcher
* @param openAPI the open api
* @param operation the operation
* @param requestMethod the request method
*/
private void buildPath(Object oAuth2EndpointFilter, String authorizationEndpointMatcher, OpenAPI openAPI, Operation operation, HttpMethod requestMethod) {
try {
Field tokenEndpointMatcherField = FieldUtils.getDeclaredField(oAuth2EndpointFilter.getClass(), authorizationEndpointMatcher, true);
RequestMatcher endpointMatcher = (RequestMatcher) tokenEndpointMatcherField.get(oAuth2EndpointFilter);
String path = null;
if (endpointMatcher instanceof AntPathRequestMatcher)
path = ((AntPathRequestMatcher) endpointMatcher).getPattern();
else if (endpointMatcher instanceof OrRequestMatcher) {
OrRequestMatcher endpointMatchers = (OrRequestMatcher) endpointMatcher;
Field requestMatchersField = FieldUtils.getDeclaredField(OrRequestMatcher.class, "requestMatchers", true);
Iterable<RequestMatcher> requestMatchers = (Iterable<RequestMatcher>) requestMatchersField.get(endpointMatchers);
for (RequestMatcher requestMatcher : requestMatchers) {
if (requestMatcher instanceof OrRequestMatcher) {
OrRequestMatcher orRequestMatcher = (OrRequestMatcher) requestMatcher;
requestMatchersField = FieldUtils.getDeclaredField(OrRequestMatcher.class, "requestMatchers", true);
requestMatchers = (Iterable<RequestMatcher>) requestMatchersField.get(orRequestMatcher);
for (RequestMatcher matcher : requestMatchers) {
if (matcher instanceof AntPathRequestMatcher)
path = ((AntPathRequestMatcher) matcher).getPattern();
}
}
}
}
PathItem pathItem = new PathItem();
switch (requestMethod) {
case POST:
pathItem.post(operation);
break;
case GET:
pathItem.get(operation);
break;
default:
// Do nothing here
break;
}
openAPI.getPaths().addPathItem(path, pathItem);
}
catch (IllegalAccessException | ClassCastException ignored) {
LOGGER.trace(ignored.getMessage());
}
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}