Skip to content

Commit

Permalink
Merge pull request #20005 from phillip-kruger/swagger-link-keycloak-d…
Browse files Browse the repository at this point in the history
…evui

Added link to Swagger UI from OIDC Dev UI Screen
  • Loading branch information
gastaldi authored Sep 8, 2021
2 parents bd293c9 + b001d1a commit e11f58b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 4 deletions.
2 changes: 1 addition & 1 deletion bom/application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<smallrye-config.version>2.4.4</smallrye-config.version>
<smallrye-health.version>3.1.1</smallrye-health.version>
<smallrye-metrics.version>3.0.1</smallrye-metrics.version>
<smallrye-open-api.version>2.1.12</smallrye-open-api.version>
<smallrye-open-api.version>2.1.13</smallrye-open-api.version>
<smallrye-graphql.version>1.3.2</smallrye-graphql.version>
<smallrye-opentracing.version>2.0.1</smallrye-opentracing.version>
<smallrye-fault-tolerance.version>5.2.1</smallrye-fault-tolerance.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.Map;
import java.util.Optional;

import io.quarkus.deployment.Capabilities;
import io.quarkus.deployment.Capability;
import io.quarkus.deployment.IsDevelopment;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
Expand All @@ -20,7 +22,8 @@ public class KeycloakDevConsoleProcessor {
@BuildStep(onlyIf = IsDevelopment.class)
@Consume(RuntimeConfigSetupCompleteBuildItem.class)
public void setConfigProperties(BuildProducer<DevConsoleTemplateInfoBuildItem> console,
Optional<KeycloakDevServicesConfigBuildItem> configProps) {
Optional<KeycloakDevServicesConfigBuildItem> configProps,
Capabilities capabilities) {
if (configProps.isPresent() && configProps.get().getProperties().containsKey("keycloak.url")) {
String keycloakUrl = (String) configProps.get().getProperties().get("keycloak.url");
String realmUrl = keycloakUrl + "/realms/" + configProps.get().getProperties().get("keycloak.realm");
Expand All @@ -41,6 +44,9 @@ public void setConfigProperties(BuildProducer<DevConsoleTemplateInfoBuildItem> c
new DevConsoleTemplateInfoBuildItem("authorizationUrl", realmUrl + "/protocol/openid-connect/auth"));
console.produce(new DevConsoleTemplateInfoBuildItem("logoutUrl", realmUrl + "/protocol/openid-connect/logout"));
console.produce(new DevConsoleTemplateInfoBuildItem("oidcGrantType", config.devservices.grant.type.getGrantType()));
console.produce(new DevConsoleTemplateInfoBuildItem("swaggerIsAvailable",
capabilities.isPresent(Capability.SMALLRYE_OPENAPI)));

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,32 @@
copyToClipboard(idToken,"dummyIdTokenClipBoard");
}

function navigateToSwaggerUi(){
var url = "{config:http-path('quarkus.swagger-ui.path')}";

var authorizedValue = {
"SecurityScheme":{
"schema":{
"flow":"implicit",
"authorizationUrl":"{info:authorizationUrl}",
"tokenUrl":"{info:tokenUrl}",
"type":"oauth2",
"description":"Authentication"
},
"clientId":"{info:clientId}",
"name":"SecurityScheme",
"token":{
"access_token":accessToken,
"token_type":"Bearer",
"expires_in":"900"
}
}
};

localStorage.setItem('authorized', JSON.stringify(authorizedValue));
window.open(url, '_blank').focus();
}

function copyToClipboard(token, type){
var dummy = document.createElement("input");
document.body.appendChild(dummy);
Expand All @@ -146,6 +172,8 @@
}

function logout() {
localStorage.removeItem('authorized');

window.location.assign('{info:logoutUrl??}'
+ "?post_logout_redirect_uri=" + "http%3A%2F%2Flocalhost%3A" + port + "%2Fq%2Fdev%2Fio.quarkus.quarkus-oidc%2Fprovider");
}
Expand Down Expand Up @@ -374,7 +402,16 @@ <h5 class="card-title text-light">Decoded</h5>
<br>
<div class="card implicitLoggedIn">
<div class="card-header">
Test your service
<div class="float-left">
Test your service
</div>
<div class="float-right">
{#if info:swaggerIsAvailable}
<a onclick="navigateToSwaggerUi();" class="btn btn-link" title="Test in Swagger UI">
<i class="fas fa-external-link-alt"></i> Swagger UI
</a>
{/if}
</div>
</div>
<div class="card-body border-0">
<div class="row">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -15,6 +16,7 @@
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.DevServicesLauncherConfigResultBuildItem;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.GeneratedResourceBuildItem;
import io.quarkus.deployment.builditem.HotDeploymentWatchedFileBuildItem;
Expand Down Expand Up @@ -51,6 +53,9 @@ public class SwaggerUiProcessor {
private static final String BRANDING_FAVICON_GENERAL = BRANDING_DIR + "favicon.ico";
private static final String BRANDING_FAVICON_MODULE = BRANDING_DIR + "smallrye-open-api-ui.ico";

// To autoset some security config from OIDC
private static final String OIDC_CLIENT_ID = "quarkus.oidc.client-id";

@BuildStep
void feature(BuildProducer<FeatureBuildItem> feature,
LaunchModeBuildItem launchMode,
Expand Down Expand Up @@ -81,7 +86,8 @@ public void getSwaggerUiFinalDestination(
LaunchModeBuildItem launchMode,
SwaggerUiConfig swaggerUiConfig,
SmallRyeOpenApiConfig openapi,
LiveReloadBuildItem liveReloadBuildItem) throws Exception {
LiveReloadBuildItem liveReloadBuildItem,
Optional<DevServicesLauncherConfigResultBuildItem> devServicesLauncherConfig) throws Exception {

if (shouldInclude(launchMode, swaggerUiConfig)) {
if ("/".equals(swaggerUiConfig.path)) {
Expand All @@ -96,13 +102,31 @@ public void getSwaggerUiFinalDestination(

}

if (devServicesLauncherConfig.isPresent()) {
DevServicesLauncherConfigResultBuildItem devServicesLauncherConfigResult = devServicesLauncherConfig.get();
Map<String, String> devServiceConfig = devServicesLauncherConfigResult.getConfig();
if (devServiceConfig != null && !devServiceConfig.isEmpty()) {
// Map client Id from OIDC Dev Services
if (devServiceConfig.containsKey(OIDC_CLIENT_ID) && !swaggerUiConfig.oauthClientId.isPresent()) {
String clientId = devServiceConfig.get(OIDC_CLIENT_ID);
swaggerUiConfig.oauthClientId = Optional.of(clientId);
}
}
}

String openApiPath = nonApplicationRootPathBuildItem.resolvePath(openapi.path);
String swaggerUiPath = nonApplicationRootPathBuildItem.resolvePath(swaggerUiConfig.path);

AppArtifact artifact = WebJarUtil.getAppArtifact(curateOutcomeBuildItem, SWAGGER_UI_WEBJAR_GROUP_ID,
SWAGGER_UI_WEBJAR_ARTIFACT_ID);

if (launchMode.getLaunchMode().isDevOrTest()) {

// In dev mode, default to persist Authorization true
if (!swaggerUiConfig.persistAuthorization.isPresent()) {
swaggerUiConfig.persistAuthorization = Optional.of(true);
}

Path tempPath = WebJarUtil.copyResourcesForDevOrTest(liveReloadBuildItem, curateOutcomeBuildItem, launchMode,
artifact,
SWAGGER_UI_WEBJAR_PREFIX);
Expand Down

0 comments on commit e11f58b

Please sign in to comment.