Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DTSRD-3772- Refdata - Swagger Fix Authentication Params appearing twice #1713

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,45 +1,83 @@
package uk.gov.hmcts.reform.professionalapi.configuration;

import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.enums.SecuritySchemeIn;
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType;
import io.swagger.v3.oas.annotations.security.SecurityScheme;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springdoc.core.GroupedOpenApi;
import org.springdoc.core.customizers.OperationCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.HandlerMethod;



@Configuration
@SecurityScheme(name = "Authorization", type = SecuritySchemeType.HTTP, bearerFormat = "JWT", scheme = "bearer")
@SecurityScheme(name = "ServiceAuthorization", type = SecuritySchemeType.APIKEY,
in = SecuritySchemeIn.HEADER, bearerFormat = "JWT", description = "ServiceAuthorization")
@SecurityScheme(name = "UserEmail", type = SecuritySchemeType.APIKEY, in = SecuritySchemeIn.HEADER)
public class SwaggerConfiguration {

private static final String DESCRIPTION = "API will help to provide Professional Reference data to clients.";
private static final String AUTHORIZATION = "Authorization";
private static final String SERVICE_AUTHORIZATION = "ServiceAuthorization";
private static final String USER_EMAIL = "UserEmail";

@Bean
public GroupedOpenApi internalOrganisationApiV2(OperationCustomizer customGlobalHeaders) {
public OpenAPI openApi() {
return new OpenAPI()
.components(new Components()
.addSecuritySchemes(
AUTHORIZATION,
new io.swagger.v3.oas.models.security.SecurityScheme()
.name(AUTHORIZATION)
.type(io.swagger.v3.oas.models.security.SecurityScheme.Type.HTTP)
.scheme("bearer")
.bearerFormat("JWT")
.description("Valid IDAM user token, (Bearer keyword is "
+ "added automatically)")
)
.addSecuritySchemes(SERVICE_AUTHORIZATION,
new io.swagger.v3.oas.models.security.SecurityScheme()
.in(io.swagger.v3.oas.models.security.SecurityScheme.In.HEADER)
.name(SERVICE_AUTHORIZATION)
.type(SecurityScheme.Type.APIKEY)
.scheme("bearer")
.bearerFormat("JWT")
.description("Valid Service-to-Service JWT token for a "
+ "whitelisted micro-service")
)
.addSecuritySchemes(USER_EMAIL,
new io.swagger.v3.oas.models.security.SecurityScheme()
.in(io.swagger.v3.oas.models.security.SecurityScheme.In.HEADER)
.name(USER_EMAIL)
.type(SecurityScheme.Type.APIKEY)
)
)
.info(new Info().title("RD Professional Ref Api service")
.description(DESCRIPTION))
.externalDocs(new ExternalDocumentation()
.description("README")
.url("https://github.com/hmcts/rd-professional-api/blob/master/README.md"))
.addSecurityItem(new SecurityRequirement().addList(AUTHORIZATION))
.addSecurityItem(new SecurityRequirement().addList(SERVICE_AUTHORIZATION))
.addSecurityItem(new SecurityRequirement().addList(USER_EMAIL));
}

@Bean
public GroupedOpenApi internalOrganisationApiV2() {
return GroupedOpenApi.builder()
.group("V2: Internal API")
.pathsToMatch("/refdata/internal/v2/**")
.build();
}

@Bean
public GroupedOpenApi externalOrganisationApiV2(OperationCustomizer customGlobalHeaders) {
public GroupedOpenApi externalOrganisationApiV2() {
return GroupedOpenApi.builder()
.group("V2: External API")
.pathsToMatch("/refdata/external/v2/**")
.build();
}

@Bean
public GroupedOpenApi publicApi(OperationCustomizer customGlobalHeaders) {
public GroupedOpenApi publicApi() {
return GroupedOpenApi.builder()
.group("rd-professional-api")
.pathsToMatch("/**")
Expand All @@ -48,32 +86,4 @@ public GroupedOpenApi publicApi(OperationCustomizer customGlobalHeaders) {
}


@Bean
public OperationCustomizer customGlobalHeaders() {
return (Operation customOperation, HandlerMethod handlerMethod) -> {
Parameter serviceAuthorizationHeader = new Parameter()
.in(ParameterIn.HEADER.toString())
.schema(new StringSchema())
.name("ServiceAuthorization")
.description("Keyword `Bearer` followed "
+ "by a service-to-service token for a whitelisted micro-service")
.required(true);
Parameter authorizationHeader = new Parameter()
.in(ParameterIn.HEADER.toString())
.schema(new StringSchema())
.name("Authorization")
.description("Authorization token")
.required(true);
Parameter userEmail = new Parameter()
.in(ParameterIn.HEADER.toString())
.schema(new StringSchema())
.name("UserEmail")
.description("UserEmail")
.required(false);
customOperation.addParametersItem(authorizationHeader);
customOperation.addParametersItem(serviceAuthorizationHeader);
customOperation.addParametersItem(userEmail);
return customOperation;
};
}
}
3 changes: 2 additions & 1 deletion src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ security:
- "/health/liveness"
- "/actuator/**"
- "/loggers/**"
- "/swagger"
- "/swagger-ui.html"
- "/swagger-resources/**"
- "/v3/**"
- "/v3/api-docs/**"
- "/swagger-ui/**"
- "/webjars/springfox-swagger-ui/**"
- "/csrf"
Expand Down