-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add openapi security scheme configuration, cleanup
- Loading branch information
Showing
5 changed files
with
112 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...arter-auth/src/main/java/uk/gov/laa/ccms/springboot/auth/config/OpenApiConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package uk.gov.laa.ccms.springboot.auth.config; | ||
|
||
import io.swagger.v3.oas.models.Components; | ||
import io.swagger.v3.oas.models.OpenAPI; | ||
import io.swagger.v3.oas.models.security.SecurityRequirement; | ||
import io.swagger.v3.oas.models.security.SecurityScheme; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Slf4j | ||
@Configuration | ||
@ConditionalOnProperty(value = "laa.ccms.springboot.starter.open-api.security-scheme.enabled", matchIfMissing = true) | ||
public class OpenApiConfiguration { | ||
|
||
@Value("${laa.ccms.springboot.starter.auth.authentication-header}") | ||
String authenticationHeader; | ||
|
||
@Bean | ||
public OpenAPI openAPI() { | ||
String securitySchemeName = "ApiKeyAuth"; | ||
OpenAPI openApiSpec = new OpenAPI() | ||
.components( | ||
new Components() | ||
.addSecuritySchemes(securitySchemeName, | ||
new SecurityScheme() | ||
.type(SecurityScheme.Type.APIKEY) | ||
.in(SecurityScheme.In.HEADER) | ||
.name(authenticationHeader))) | ||
.addSecurityItem( | ||
new SecurityRequirement() | ||
.addList(securitySchemeName)); | ||
log.info("OpenAPI Security Scheme '{}' added for all endpoints.", securitySchemeName); | ||
return openApiSpec; | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
...r-auth/src/test/java/uk/gov/laa/ccms/springboot/auth/config/OpenApiConfigurationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package uk.gov.laa.ccms.springboot.auth.config; | ||
|
||
import io.swagger.v3.oas.models.OpenAPI; | ||
import io.swagger.v3.oas.models.security.SecurityScheme; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.test.context.assertj.AssertableApplicationContext; | ||
import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
|
||
import java.util.Map; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class OpenApiConfigurationTest { | ||
|
||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() | ||
.withBean(OpenApiConfiguration.class) | ||
.withPropertyValues("laa.ccms.springboot.starter.auth.authentication-header=Authorization"); | ||
|
||
private final String OPEN_API_CONFIGURATION_BEAN = "openApiConfiguration"; | ||
private final String OPEN_API_BEAN = "openAPI"; | ||
private final String SECURITY_SCHEME_NAME = "ApiKeyAuth"; | ||
|
||
@Test | ||
void testOpenApiBeanIsCreatedWhenApplicationPropertyOmitted() { | ||
contextRunner.run((context) -> { | ||
assertThat(context).hasBean(OPEN_API_CONFIGURATION_BEAN); | ||
assertSecuritySchemeApplied(context); | ||
}); | ||
} | ||
|
||
@Test | ||
void testOpenApiBeanIsCreatedWhenApplicationPropertyEnabled() { | ||
contextRunner.withPropertyValues("laa.ccms.springboot.starter.open-api.security-scheme.enabled=true").run((context) -> { | ||
assertThat(context).hasBean(OPEN_API_CONFIGURATION_BEAN); | ||
assertSecuritySchemeApplied(context); | ||
}); | ||
} | ||
|
||
@Test | ||
void testNoOpenApiBeanIsCreatedWhenApplicationPropertyDisabled() { | ||
contextRunner.withPropertyValues("laa.ccms.springboot.starter.open-api.security-scheme.enabled=false").run((context) -> { | ||
assertThat(context).doesNotHaveBean(OPEN_API_CONFIGURATION_BEAN); | ||
assertThat(context).doesNotHaveBean(OPEN_API_BEAN); | ||
}); | ||
} | ||
|
||
private void assertSecuritySchemeApplied(AssertableApplicationContext context) { | ||
OpenAPI openApiSpec = context.getBean(OPEN_API_BEAN, OpenAPI.class); | ||
assertThat(openApiSpec.getComponents().getSecuritySchemes()).isEqualTo(Map.of(SECURITY_SCHEME_NAME, getSecurityScheme())); | ||
} | ||
|
||
private SecurityScheme getSecurityScheme() { | ||
return new SecurityScheme() | ||
.type(SecurityScheme.Type.APIKEY) | ||
.in(SecurityScheme.In.HEADER) | ||
.name("Authorization"); | ||
} | ||
|
||
} |