Skip to content

Commit

Permalink
Merge pull request #31964 from MikeEdgar/smallrye-openapi-1077
Browse files Browse the repository at this point in the history
OpenAPI: add map configuration for security scheme extensions
  • Loading branch information
phillip-kruger authored Mar 20, 2023
2 parents ec32f1a + b875eb6 commit ba53ed3
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package io.quarkus.smallrye.openapi.common.deployment;

import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigRoot;
Expand Down Expand Up @@ -58,6 +61,12 @@ public final class SmallRyeOpenApiConfig {
@ConfigItem(defaultValue = "Authentication")
public String securitySchemeDescription;

/**
* Add one or more extensions to the security scheme
*/
@ConfigItem
public Map<String, String> securitySchemeExtensions = Collections.emptyMap();

/**
* This will automatically add the security requirement to all methods/classes that has a `RolesAllowed` annotation.
*/
Expand Down Expand Up @@ -216,4 +225,12 @@ public enum OperationIdStrategy {
CLASS_METHOD,
PACKAGE_CLASS_METHOD
}

public Map<String, String> getValidSecuritySchemeExtentions() {
return securitySchemeExtensions
.entrySet()
.stream()
.filter(x -> x.getKey().startsWith("x-"))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -372,18 +372,21 @@ private OASFilter getAutoSecurityFilter(List<SecurityInformationBuildItem> secur
return new AutoBearerTokenSecurityFilter(
config.securitySchemeName,
config.securitySchemeDescription,
config.getValidSecuritySchemeExtentions(),
config.jwtSecuritySchemeValue,
config.jwtBearerFormat);
case oauth2:
return new AutoBearerTokenSecurityFilter(
config.securitySchemeName,
config.securitySchemeDescription,
config.getValidSecuritySchemeExtentions(),
config.oauth2SecuritySchemeValue,
config.oauth2BearerFormat);
case basic:
return new AutoBasicSecurityFilter(
config.securitySchemeName,
config.securitySchemeDescription,
config.getValidSecuritySchemeExtentions(),
config.basicSecuritySchemeValue);
case oidc:
return securityInformationBuildItem.getOpenIDConnectInformation()
Expand All @@ -396,6 +399,7 @@ private OASFilter getAutoSecurityFilter(List<SecurityInformationBuildItem> secur
return new OpenIDConnectSecurityFilter(
config.securitySchemeName,
config.securitySchemeDescription,
config.getValidSecuritySchemeExtentions(),
openIdConnectUrl);
})
.orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public void filterOpenAPI(OpenAPI openAPI) {

SecurityScheme securityScheme = OASFactory.createSecurityScheme();
securityScheme.setDescription(config.securitySchemeDescription);
config.getValidSecuritySchemeExtentions().forEach(securityScheme::addExtension);

switch (securitySchemeOption) {
case basic:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.iterableWithSize;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;

import org.hamcrest.Matcher;
Expand All @@ -28,7 +30,9 @@ class AutoSecurityRolesAllowedTestCase {
.addAsResource(
new StringAsset("quarkus.smallrye-openapi.security-scheme=jwt\n"
+ "quarkus.smallrye-openapi.security-scheme-name=JWTCompanyAuthentication\n"
+ "quarkus.smallrye-openapi.security-scheme-description=JWT Authentication"),
+ "quarkus.smallrye-openapi.security-scheme-description=JWT Authentication\n"
+ "quarkus.smallrye-openapi.security-scheme-extensions.x-my-extension1=extension-value\n"
+ "quarkus.smallrye-openapi.security-scheme-extensions.my-extension2=extension-value"),

"application.properties"));

Expand All @@ -55,7 +59,9 @@ void testAutoSecurityRequirement() {
hasEntry("type", "http"),
hasEntry("scheme", "bearer"),
hasEntry("bearerFormat", "JWT"),
hasEntry("description", "JWT Authentication")))
hasEntry("description", "JWT Authentication"),
hasEntry("x-my-extension1", "extension-value"),
not(hasKey("my-extension2"))))
.and()
// OpenApiResourceSecuredAtMethodLevel
.body("paths.'/resource2/test-security/naked'.get.security", defaultSecurity)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.smallrye.openapi.runtime.filter;

import java.util.Map;

import org.eclipse.microprofile.openapi.OASFactory;
import org.eclipse.microprofile.openapi.models.security.SecurityScheme;

Expand All @@ -14,8 +16,9 @@ public AutoBasicSecurityFilter() {
}

public AutoBasicSecurityFilter(String securitySchemeName, String securitySchemeDescription,
Map<String, String> securitySchemeExtensions,
String basicSecuritySchemeValue) {
super(securitySchemeName, securitySchemeDescription);
super(securitySchemeName, securitySchemeDescription, securitySchemeExtensions);
this.basicSecuritySchemeValue = basicSecuritySchemeValue;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.smallrye.openapi.runtime.filter;

import java.util.Map;

import org.eclipse.microprofile.openapi.OASFactory;
import org.eclipse.microprofile.openapi.models.security.SecurityScheme;

Expand All @@ -15,9 +17,10 @@ public AutoBearerTokenSecurityFilter() {
}

public AutoBearerTokenSecurityFilter(String securitySchemeName, String securitySchemeDescription,
Map<String, String> securitySchemeExtensions,
String securitySchemeValue,
String bearerFormat) {
super(securitySchemeName, securitySchemeDescription);
super(securitySchemeName, securitySchemeDescription, securitySchemeExtensions);
this.securitySchemeValue = securitySchemeValue;
this.bearerFormat = bearerFormat;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ public abstract class AutoSecurityFilter implements OASFilter {

private String securitySchemeName;
private String securitySchemeDescription;
private Map<String, String> securitySchemeExtensions;

public AutoSecurityFilter() {
protected AutoSecurityFilter() {

}

public AutoSecurityFilter(String securitySchemeName, String securitySchemeDescription) {
protected AutoSecurityFilter(String securitySchemeName, String securitySchemeDescription,
Map<String, String> securitySchemeExtensions) {
this.securitySchemeName = securitySchemeName;
this.securitySchemeDescription = securitySchemeDescription;
this.securitySchemeExtensions = securitySchemeExtensions;
}

public String getSecuritySchemeName() {
Expand All @@ -45,6 +48,14 @@ public void setSecuritySchemeDescription(String securitySchemeDescription) {
this.securitySchemeDescription = securitySchemeDescription;
}

public Map<String, String> getSecuritySchemeExtensions() {
return securitySchemeExtensions;
}

public void setSecuritySchemeExtensions(Map<String, String> securitySchemeExtensions) {
this.securitySchemeExtensions = securitySchemeExtensions;
}

@Override
public void filterOpenAPI(OpenAPI openAPI) {
// Make sure components are created
Expand All @@ -62,6 +73,8 @@ public void filterOpenAPI(OpenAPI openAPI) {

SecurityScheme securityScheme = getSecurityScheme();
securityScheme.setDescription(securitySchemeDescription);
securitySchemeExtensions.forEach(securityScheme::addExtension);

securitySchemes.put(securitySchemeName, securityScheme);
openAPI.getComponents().setSecuritySchemes(securitySchemes);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.smallrye.openapi.runtime.filter;

import java.util.Map;

import org.eclipse.microprofile.openapi.OASFactory;
import org.eclipse.microprofile.openapi.models.security.SecurityScheme;

Expand All @@ -15,8 +17,9 @@ public OpenIDConnectSecurityFilter() {
}

public OpenIDConnectSecurityFilter(String securitySchemeName, String securitySchemeDescription,
Map<String, String> securitySchemeExtensions,
AutoUrl openIdConnectUrl) {
super(securitySchemeName, securitySchemeDescription);
super(securitySchemeName, securitySchemeDescription, securitySchemeExtensions);
this.openIdConnectUrl = openIdConnectUrl;
}

Expand Down

0 comments on commit ba53ed3

Please sign in to comment.