-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#77 made the oas dependency optional and conditional
- Loading branch information
1 parent
b2906d0
commit 7f00f5d
Showing
15 changed files
with
179 additions
and
94 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
73 changes: 27 additions & 46 deletions
73
...rter-api/src/main/java/it/fabioformosa/quartzmanager/api/configuration/OpenApiConfig.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 |
---|---|---|
@@ -1,68 +1,49 @@ | ||
package it.fabioformosa.quartzmanager.api.configuration; | ||
|
||
import io.swagger.v3.oas.models.Components; | ||
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.info.Info; | ||
import io.swagger.v3.oas.models.info.License; | ||
import io.swagger.v3.oas.models.media.*; | ||
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 io.swagger.v3.oas.models.security.SecurityScheme; | ||
import it.fabioformosa.quartzmanager.api.common.config.OpenAPIConfigConsts; | ||
import it.fabioformosa.quartzmanager.api.common.config.QuartzManagerPaths; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springdoc.core.GroupedOpenApi; | ||
import org.springdoc.core.customizers.OpenApiCustomiser; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import java.util.Arrays; | ||
import java.util.Optional; | ||
|
||
|
||
@Slf4j | ||
@Configuration | ||
public class OpenApiConfig { | ||
|
||
@ConditionalOnProperty(name = "quartz-manager.oas.enabled") | ||
@ConditionalOnMissingBean | ||
@Bean | ||
public OpenAPI customOpenAPI(@Autowired(required = false) SecurityDiscover securityDiscover) { | ||
OpenAPI openAPI = new OpenAPI() | ||
.info(apiInfo()); | ||
|
||
if(securityDiscover != null) | ||
openAPI | ||
.components(new Components().addSecuritySchemes(OpenAPIConfigConsts.BASIC_AUTH_SEC_OAS_SCHEME, buildBasicAuthScheme())) | ||
.path(QuartzManagerPaths.QUARTZ_MANAGER_LOGIN_PATH, | ||
new PathItem().post(new Operation() | ||
.operationId("login") | ||
.tags(Arrays.asList("auth")) | ||
.requestBody(new RequestBody().content( | ||
new Content().addMediaType("application/x-www-form-urlencoded", new MediaType().schema(new Schema().type("object") | ||
.addProperties("username", new StringSchema()) | ||
.addProperties("password", new PasswordSchema()) | ||
.required(Arrays.asList("username", "password")) | ||
)))) | ||
.responses(new ApiResponses().addApiResponse("200", new ApiResponse().description("JWT Token to authenticate the next requests"))) | ||
.responses(new ApiResponses().addApiResponse("401", new ApiResponse().description("Unauthorized - Username or password are incorrect!"))) | ||
)); | ||
|
||
return openAPI; | ||
} | ||
|
||
private SecurityScheme buildBasicAuthScheme() { | ||
return new SecurityScheme() | ||
.type(SecurityScheme.Type.HTTP) | ||
.scheme("bearer") | ||
.bearerFormat("JWT"); | ||
} | ||
|
||
|
||
private Info apiInfo() { | ||
return new Info() | ||
public OpenAPI quartzManagerOpenAPI() { | ||
log.info("No OpenAPI found! Quart Manager is creating it..."); | ||
return new OpenAPI().info(new Info() | ||
.title("QUARTZ MANAGER API") | ||
.description("Quartz Manager - REST API") | ||
.version("1.0.0") | ||
.license(new License() | ||
.name("Apache License 2.0") | ||
.url("https://github.com/fabioformosa/quartz-manager/blob/master/LICENSE")); | ||
} | ||
.url("https://github.com/fabioformosa/quartz-manager/blob/master/LICENSE"))); | ||
} | ||
|
||
@ConditionalOnProperty(name = "quartz-manager.oas.enabled") | ||
@Bean | ||
public GroupedOpenApi quartzManagerStoreOpenApi(@Autowired(required = false) @Qualifier("quartzManagerOpenApiCustomiser") Optional<OpenApiCustomiser> openApiCustomiser) { | ||
String paths[] = {QuartzManagerPaths.QUARTZ_MANAGER_BASE_CONTEXT_PATH + "/**"}; | ||
GroupedOpenApi.Builder groupedOpenApiBuilder = GroupedOpenApi.builder().group("quartz-manager").pathsToMatch(paths); | ||
openApiCustomiser.ifPresent(oaCustomizer -> groupedOpenApiBuilder.addOpenApiCustomiser(oaCustomizer)); | ||
return groupedOpenApiBuilder.build(); | ||
} | ||
|
||
|
||
|
||
} |
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
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
61 changes: 61 additions & 0 deletions
61
...rc/main/java/it/fabioformosa/quartzmanager/api/security/config/SecurityOpenApiConfig.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,61 @@ | ||
package it.fabioformosa.quartzmanager.api.security.config; | ||
|
||
import io.swagger.v3.oas.models.Components; | ||
import io.swagger.v3.oas.models.Operation; | ||
import io.swagger.v3.oas.models.PathItem; | ||
import io.swagger.v3.oas.models.media.*; | ||
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 io.swagger.v3.oas.models.security.SecurityScheme; | ||
import it.fabioformosa.quartzmanager.api.common.config.OpenAPIConfigConsts; | ||
import it.fabioformosa.quartzmanager.api.common.config.QuartzManagerPaths; | ||
import it.fabioformosa.quartzmanager.api.security.properties.JwtSecurityProperties; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springdoc.core.customizers.OpenApiCustomiser; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.core.annotation.Order; | ||
|
||
import java.util.Arrays; | ||
|
||
@Slf4j | ||
@ConditionalOnProperty(name = "quartz-manager.oas.enabled") | ||
@Configuration | ||
public class SecurityOpenApiConfig { | ||
|
||
@Order(Ordered.HIGHEST_PRECEDENCE) | ||
@Bean("quartzManagerOpenApiCustomiser") | ||
public OpenApiCustomiser configureQuartzManagerOpenAPI(JwtSecurityProperties jwtSecurityProps) { | ||
return openAPI -> { | ||
if (jwtSecurityProps.getCookieStrategy().isEnabled() == false) | ||
openAPI | ||
.components(new Components().addSecuritySchemes(OpenAPIConfigConsts.QUARTZ_MANAGER_SEC_OAS_SCHEMA, buildBasicAuthScheme())); | ||
|
||
openAPI.path(QuartzManagerPaths.QUARTZ_MANAGER_LOGIN_PATH, | ||
new PathItem().post(new Operation() | ||
.operationId("login") | ||
.tags(Arrays.asList("auth")) | ||
.requestBody(new RequestBody().content( | ||
new Content().addMediaType("application/x-www-form-urlencoded", new MediaType().schema(new Schema().type("object") | ||
.addProperties("username", new StringSchema()) | ||
.addProperties("password", new PasswordSchema()) | ||
.required(Arrays.asList("username", "password")) | ||
)))) | ||
.responses(new ApiResponses().addApiResponse("200", new ApiResponse().description("JWT Token to authenticate the next requests"))) | ||
.responses(new ApiResponses().addApiResponse("401", new ApiResponse().description("Unauthorized - Username or password are incorrect!"))) | ||
)); | ||
}; | ||
} | ||
|
||
private SecurityScheme buildBasicAuthScheme() { | ||
return new SecurityScheme() | ||
.type(SecurityScheme.Type.HTTP) | ||
.scheme("bearer") | ||
.bearerFormat("JWT") | ||
.description("A JWT Token in required to access this API. You can obtain a JWT Token by providing the username and password in the login API"); | ||
} | ||
|
||
} |
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
Oops, something went wrong.