|
1 | 1 | package io.kafbat.ui.service; |
2 | 2 |
|
| 3 | +import static io.kafbat.ui.api.model.AuthType.DISABLED; |
| 4 | +import static io.kafbat.ui.api.model.AuthType.OAUTH2; |
3 | 5 | import static io.kafbat.ui.model.ApplicationInfoDTO.EnabledFeaturesEnum; |
4 | 6 | import static io.kafbat.ui.util.GithubReleaseInfo.GITHUB_RELEASE_INFO_TIMEOUT; |
5 | 7 |
|
6 | 8 | import com.google.common.annotations.VisibleForTesting; |
| 9 | +import com.google.common.collect.Streams; |
| 10 | +import io.kafbat.ui.model.AppAuthenticationSettingsDTO; |
7 | 11 | import io.kafbat.ui.model.ApplicationInfoBuildDTO; |
8 | 12 | import io.kafbat.ui.model.ApplicationInfoDTO; |
9 | 13 | import io.kafbat.ui.model.ApplicationInfoLatestReleaseDTO; |
| 14 | +import io.kafbat.ui.model.AuthTypeDTO; |
| 15 | +import io.kafbat.ui.model.OAuthProviderDTO; |
10 | 16 | import io.kafbat.ui.util.DynamicConfigOperations; |
11 | 17 | import io.kafbat.ui.util.GithubReleaseInfo; |
12 | 18 | import java.time.format.DateTimeFormatter; |
13 | 19 | import java.util.ArrayList; |
| 20 | +import java.util.Collections; |
14 | 21 | import java.util.List; |
15 | 22 | import java.util.Optional; |
16 | 23 | import java.util.Properties; |
17 | 24 | import org.springframework.beans.factory.annotation.Autowired; |
18 | 25 | import org.springframework.beans.factory.annotation.Value; |
19 | 26 | import org.springframework.boot.info.BuildProperties; |
20 | 27 | import org.springframework.boot.info.GitProperties; |
| 28 | +import org.springframework.context.ApplicationContext; |
| 29 | +import org.springframework.core.ResolvableType; |
21 | 30 | import org.springframework.scheduling.annotation.Scheduled; |
| 31 | +import org.springframework.security.oauth2.client.registration.ClientRegistration; |
| 32 | +import org.springframework.security.oauth2.core.AuthorizationGrantType; |
22 | 33 | import org.springframework.stereotype.Service; |
23 | 34 |
|
24 | 35 | @Service |
25 | 36 | public class ApplicationInfoService { |
26 | 37 | private final GithubReleaseInfo githubReleaseInfo; |
| 38 | + private final ApplicationContext applicationContext; |
27 | 39 | private final DynamicConfigOperations dynamicConfigOperations; |
28 | 40 | private final BuildProperties buildProperties; |
29 | 41 | private final GitProperties gitProperties; |
30 | 42 |
|
31 | 43 | public ApplicationInfoService(DynamicConfigOperations dynamicConfigOperations, |
| 44 | + ApplicationContext applicationContext, |
32 | 45 | @Autowired(required = false) BuildProperties buildProperties, |
33 | 46 | @Autowired(required = false) GitProperties gitProperties, |
34 | 47 | @Value("${" + GITHUB_RELEASE_INFO_TIMEOUT + ":10}") int githubApiMaxWaitTime) { |
| 48 | + this.applicationContext = applicationContext; |
35 | 49 | this.dynamicConfigOperations = dynamicConfigOperations; |
36 | 50 | this.buildProperties = Optional.ofNullable(buildProperties).orElse(new BuildProperties(new Properties())); |
37 | 51 | this.gitProperties = Optional.ofNullable(gitProperties).orElse(new GitProperties(new Properties())); |
@@ -70,6 +84,38 @@ private List<EnabledFeaturesEnum> getEnabledFeatures() { |
70 | 84 | return enabledFeatures; |
71 | 85 | } |
72 | 86 |
|
| 87 | + public AppAuthenticationSettingsDTO getAuthenticationProperties() { |
| 88 | + return new AppAuthenticationSettingsDTO() |
| 89 | + .authType(AuthTypeDTO.fromValue(getAuthType())) |
| 90 | + .oAuthProviders(getOAuthProviders()); |
| 91 | + } |
| 92 | + |
| 93 | + private String getAuthType() { |
| 94 | + return Optional.ofNullable(applicationContext.getEnvironment().getProperty("auth.type")) |
| 95 | + .orElse(DISABLED.getValue()); |
| 96 | + } |
| 97 | + |
| 98 | + @SuppressWarnings("unchecked") |
| 99 | + private List<OAuthProviderDTO> getOAuthProviders() { |
| 100 | + if (!getAuthType().equalsIgnoreCase(OAUTH2.getValue())) { |
| 101 | + return Collections.emptyList(); |
| 102 | + } |
| 103 | + var type = ResolvableType.forClassWithGenerics(Iterable.class, ClientRegistration.class); |
| 104 | + String[] names = this.applicationContext.getBeanNamesForType(type); |
| 105 | + var bean = (Iterable<ClientRegistration>) (names.length == 1 ? this.applicationContext.getBean(names[0]) : null); |
| 106 | + |
| 107 | + if (bean == null) { |
| 108 | + return Collections.emptyList(); |
| 109 | + } |
| 110 | + |
| 111 | + return Streams.stream(bean.iterator()) |
| 112 | + .filter(r -> AuthorizationGrantType.AUTHORIZATION_CODE.equals(r.getAuthorizationGrantType())) |
| 113 | + .map(r -> new OAuthProviderDTO() |
| 114 | + .clientName(r.getClientName()) |
| 115 | + .authorizationUri("/oauth2/authorization/" + r.getRegistrationId())) |
| 116 | + .toList(); |
| 117 | + } |
| 118 | + |
73 | 119 | // updating on startup and every hour |
74 | 120 | @Scheduled(fixedRateString = "${github-release-info-update-rate:3600000}") |
75 | 121 | public void updateGithubReleaseInfo() { |
|
0 commit comments