Skip to content

Commit a318c56

Browse files
committed
feat: add oauth2 login from swagger-ui
1 parent f057bd9 commit a318c56

1 file changed

Lines changed: 35 additions & 18 deletions

File tree

hawkbit-mgmt/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtOpenApiConfiguration.java

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010
package org.eclipse.hawkbit.mgmt.rest.resource;
1111

1212
import java.util.Comparator;
13+
import java.util.HashMap;
1314
import java.util.List;
1415
import java.util.Map;
1516
import java.util.Optional;
1617

1718
import io.swagger.v3.oas.models.info.Info;
19+
import io.swagger.v3.oas.models.security.OAuthFlow;
20+
import io.swagger.v3.oas.models.security.OAuthFlows;
1821
import io.swagger.v3.oas.models.security.SecurityRequirement;
1922
import io.swagger.v3.oas.models.security.SecurityScheme;
2023
import io.swagger.v3.oas.models.servers.Server;
@@ -37,11 +40,37 @@ public class MgmtOpenApiConfiguration {
3740

3841
@Bean
3942
@ConditionalOnProperty(
40-
value = "hawkbit.server.openapi.mgmt.enabled",
41-
havingValue = "true",
42-
matchIfMissing = true)
43-
public GroupedOpenApi mgmtApi(@Value("${hawkbit.server.openapi.mgmt.tenant-endpoint.enabled:false}") final boolean tenantEndpointEnabled) {
43+
value = "hawkbit.server.openapi.mgmt.enabled", havingValue = "true", matchIfMissing = true)
44+
public GroupedOpenApi mgmtApi(
45+
@Value("${hawkbit.server.openapi.mgmt.tenant-endpoint.enabled:false}") final boolean tenantEndpointEnabled,
46+
@Value("${hawkbit.server.security.oauth2.resourceserver.enabled:false}") final boolean oauth2Enabled,
47+
@Value("${hawkbit.server.security.allow-http-basic-on-o-auth-enabled:false}") final boolean allowBasicAuthWithOauth,
48+
@Value("${springdoc.oauth-flow.authorizationUrl:}") final String authorizationUrl,
49+
@Value("${springdoc.oauth-flow.tokenUrl:}") final String tokenUrl
50+
) {
4451
// @formatter:off
52+
Map<String, SecurityScheme> securitySchemeMap = new HashMap<>();
53+
final SecurityRequirement securityRequirement = new SecurityRequirement();
54+
if (!oauth2Enabled || allowBasicAuthWithOauth) {
55+
securityRequirement.addList(BASIC_AUTH_SEC_SCHEME_NAME);
56+
securitySchemeMap.put(BASIC_AUTH_SEC_SCHEME_NAME,
57+
new SecurityScheme()
58+
.description(BASIC_AUTH_SEC_SCHEME_NAME + " Authentication")
59+
.type(SecurityScheme.Type.HTTP)
60+
.scheme("basic"));
61+
}
62+
if(oauth2Enabled){
63+
securityRequirement.addList(BEARER_AUTH_SEC_SCHEME_NAME);
64+
securitySchemeMap.put(BEARER_AUTH_SEC_SCHEME_NAME,
65+
new SecurityScheme()
66+
.description(BEARER_AUTH_SEC_SCHEME_NAME + " Authentication")
67+
.type(SecurityScheme.Type.OAUTH2)
68+
.flows(new OAuthFlows()
69+
.authorizationCode(new OAuthFlow().authorizationUrl(authorizationUrl).tokenUrl(tokenUrl))
70+
.clientCredentials(new OAuthFlow().tokenUrl(tokenUrl)))
71+
.bearerFormat("JWT")
72+
.scheme("bearer"));
73+
}
4574
return GroupedOpenApi
4675
.builder()
4776
.group("Management API")
@@ -62,23 +91,11 @@ public GroupedOpenApi mgmtApi(@Value("${hawkbit.server.openapi.mgmt.tenant-endpo
6291
.variables(new ServerVariables().addServerVariable("tenant", tenantSeverVariable())),
6392
new Server().url("/"))
6493
: List.of(new Server().url("/")))
65-
.addSecurityItem(new SecurityRequirement()
66-
.addList(BASIC_AUTH_SEC_SCHEME_NAME)
67-
.addList(BEARER_AUTH_SEC_SCHEME_NAME))
94+
.addSecurityItem(securityRequirement)
6895
.components(
6996
openApi
7097
.getComponents()
71-
.addSecuritySchemes(BASIC_AUTH_SEC_SCHEME_NAME,
72-
new SecurityScheme()
73-
.description(BASIC_AUTH_SEC_SCHEME_NAME + " Authentication")
74-
.type(SecurityScheme.Type.HTTP)
75-
.scheme("basic"))
76-
.addSecuritySchemes(BEARER_AUTH_SEC_SCHEME_NAME,
77-
new SecurityScheme()
78-
.description(BEARER_AUTH_SEC_SCHEME_NAME + " Authentication")
79-
.type(SecurityScheme.Type.HTTP)
80-
.bearerFormat("JWT")
81-
.scheme("bearer")))
98+
.securitySchemes(securitySchemeMap))
8299
.tags(sort(openApi.getTags())))
83100
.build();
84101
// @formatter:on

0 commit comments

Comments
 (0)