Skip to content

Commit

Permalink
Impl authentication page backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Haarolean committed Oct 25, 2024
1 parent 25dea60 commit 1e668f8
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ protected AbstractAuthSecurityConfig() {
"/login",
"/logout",
"/oauth2/**",
"/static/**"
"/static/**",
"/api/config/authentication"
};

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.kafbat.ui.api.ApplicationConfigApi;
import io.kafbat.ui.config.ClustersProperties;
import io.kafbat.ui.model.ActionDTO;
import io.kafbat.ui.model.AppAuthenticationSettingsDTO;
import io.kafbat.ui.model.ApplicationConfigDTO;
import io.kafbat.ui.model.ApplicationConfigPropertiesDTO;
import io.kafbat.ui.model.ApplicationConfigValidationDTO;
Expand Down Expand Up @@ -66,6 +67,13 @@ public Mono<ResponseEntity<ApplicationInfoDTO>> getApplicationInfo(ServerWebExch
return Mono.just(applicationInfoService.getApplicationInfo()).map(ResponseEntity::ok);
}

@Override
public Mono<ResponseEntity<AppAuthenticationSettingsDTO>> getAuthenticationSettings(
ServerWebExchange exchange) {
return Mono.just(applicationInfoService.getAuthenticationProperties())
.map(ResponseEntity::ok);
}

@Override
public Mono<ResponseEntity<ApplicationConfigDTO>> getCurrentConfig(ServerWebExchange exchange) {
var context = AccessContext.builder()
Expand Down Expand Up @@ -109,7 +117,7 @@ public Mono<ResponseEntity<UploadedFileInfoDTO>> uploadConfigRelatedFile(Flux<Pa
.then(fileFlux.single())
.flatMap(file ->
dynamicConfigOperations.uploadConfigRelatedFile((FilePart) file)
.map(path -> new UploadedFileInfoDTO().location(path.toString()))
.map(path -> new UploadedFileInfoDTO(path.toString()))
.map(ResponseEntity::ok))
.doOnEach(sig -> audit(context, sig));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,49 @@
package io.kafbat.ui.service;

import static io.kafbat.ui.api.model.AuthType.DISABLED;
import static io.kafbat.ui.api.model.AuthType.OAUTH2;
import static io.kafbat.ui.model.ApplicationInfoDTO.EnabledFeaturesEnum;

import com.google.common.collect.Streams;
import io.kafbat.ui.model.AppAuthenticationSettingsDTO;
import io.kafbat.ui.model.ApplicationInfoBuildDTO;
import io.kafbat.ui.model.ApplicationInfoDTO;
import io.kafbat.ui.model.ApplicationInfoLatestReleaseDTO;
import io.kafbat.ui.model.AuthTypeDTO;
import io.kafbat.ui.model.OAuthProviderDTO;
import io.kafbat.ui.util.DynamicConfigOperations;
import io.kafbat.ui.util.GithubReleaseInfo;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.info.BuildProperties;
import org.springframework.boot.info.GitProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.core.ResolvableType;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.core.AuthorizationGrantType;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

@Service
public class ApplicationInfoService {

private final GithubReleaseInfo githubReleaseInfo = new GithubReleaseInfo();

private final ApplicationContext applicationContext;
private final DynamicConfigOperations dynamicConfigOperations;
private final BuildProperties buildProperties;
private final GitProperties gitProperties;

public ApplicationInfoService(DynamicConfigOperations dynamicConfigOperations,
ApplicationContext applicationContext,
@Autowired(required = false) BuildProperties buildProperties,
@Autowired(required = false) GitProperties gitProperties) {
this.applicationContext = applicationContext;
this.dynamicConfigOperations = dynamicConfigOperations;
this.buildProperties = Optional.ofNullable(buildProperties).orElse(new BuildProperties(new Properties()));
this.gitProperties = Optional.ofNullable(gitProperties).orElse(new GitProperties(new Properties()));
Expand Down Expand Up @@ -68,6 +81,38 @@ private List<EnabledFeaturesEnum> getEnabledFeatures() {
return enabledFeatures;
}

public AppAuthenticationSettingsDTO getAuthenticationProperties() {
return new AppAuthenticationSettingsDTO()
.authType(AuthTypeDTO.fromValue(getAuthType()))
.oAuthProviders(getOAuthProviders());
}

private String getAuthType() {
return Optional.ofNullable(applicationContext.getEnvironment().getProperty("auth.type"))
.orElse(DISABLED.getValue());
}

@SuppressWarnings("unchecked")
private List<OAuthProviderDTO> getOAuthProviders() {
if (!getAuthType().equalsIgnoreCase(OAUTH2.getValue())) {
return Collections.emptyList();
}
var type = ResolvableType.forClassWithGenerics(Iterable.class, ClientRegistration.class);
String[] names = this.applicationContext.getBeanNamesForType(type);
var bean = (Iterable<ClientRegistration>) (names.length == 1 ? this.applicationContext.getBean(names[0]) : null);

if (bean == null) {
return Collections.emptyList();
}

return Streams.stream(bean.iterator())
.filter(r -> AuthorizationGrantType.AUTHORIZATION_CODE.equals(r.getAuthorizationGrantType()))
.map(r -> new OAuthProviderDTO()
.clientName(r.getClientName())
.authorizationUri("/oauth2/authorization/" + r.getRegistrationId()))
.toList();
}

// updating on startup and every hour
@Scheduled(fixedRateString = "${github-release-info-update-rate:3600000}")
public void updateGithubReleaseInfo() {
Expand Down
42 changes: 41 additions & 1 deletion contract/src/main/resources/swagger/kafbat-ui-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2124,7 +2124,7 @@ paths:
get:
tags:
- Authorization
summary: Get user authentication related info
summary: Get user authorization related info
operationId: getUserAuthInfo
responses:
200:
Expand Down Expand Up @@ -2218,6 +2218,20 @@ paths:
schema:
$ref: '#/components/schemas/UploadedFileInfo'

/api/config/authentication:
get:
tags:
- ApplicationConfig
summary: Get authentication methods enabled for the app and other related settings
operationId: getAuthenticationSettings
responses:
200:
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/AppAuthenticationSettings'

components:
schemas:
TopicSerdeSuggestion:
Expand Down Expand Up @@ -2328,6 +2342,32 @@ components:
htmlUrl:
type: string

AppAuthenticationSettings:
type: object
properties:
authType:
$ref: '#/components/schemas/AuthType'
oAuthProviders:
type: array
items:
$ref: '#/components/schemas/OAuthProvider'

OAuthProvider:
type: object
properties:
clientName:
type: string
authorizationUri:
type: string

AuthType:
type: string
enum:
- DISABLED
- OAUTH2
- LOGIN_FORM
- LDAP

Cluster:
type: object
properties:
Expand Down

0 comments on commit 1e668f8

Please sign in to comment.