package org.apereo.cas.config;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apereo.cas.configuration.CasConfigurationProperties;
import org.apereo.cas.configuration.model.core.services.ServiceRegistryProperties;
import org.apereo.cas.services.RestfulServiceRegistry;
import org.apereo.cas.services.ServiceRegistry;
import org.apereo.cas.services.ServiceRegistryExecutionPlan;
import org.apereo.cas.services.ServiceRegistryExecutionPlanConfigurer;
import org.apereo.cas.util.CollectionUtils;
import org.apereo.cas.util.EncodingUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;

/**
 * This is {@link RestServiceRegistryConfiguration}.
 *
 * @author Misagh Moayyed
 * @since 5.2.0
 */
@Configuration("restServiceRegistryConfiguration")
@EnableConfigurationProperties(CasConfigurationProperties.class)
@Slf4j
public class RestServiceRegistryConfiguration implements ServiceRegistryExecutionPlanConfigurer {
    @Autowired
    private CasConfigurationProperties casProperties;

    @Bean
    @RefreshScope
    @SneakyThrows
    public ServiceRegistry restfulServiceRegistry() {

        final ServiceRegistryProperties registry = casProperties.getServiceRegistry();
        final RestTemplate restTemplate = new RestTemplate();
        final MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();

        if (StringUtils.isNotBlank(registry.getRest().getBasicAuthUsername())
            && StringUtils.isNotBlank(registry.getRest().getBasicAuthPassword())) {
            final String auth = registry.getRest().getBasicAuthUsername() + ":" + registry.getRest().getBasicAuthPassword();
            final byte[] encodedAuth = EncodingUtils.encodeBase64ToByteArray(auth.getBytes(StandardCharsets.UTF_8));
            final String authHeader = "Basic " + new String(encodedAuth, StandardCharsets.UTF_8);
            headers.put("Authorization", CollectionUtils.wrap(authHeader));
        }
        return new RestfulServiceRegistry(restTemplate, registry.getRest().getUrl(), headers);

    }

    @Override
    public void configureServiceRegistry(final ServiceRegistryExecutionPlan plan) {
        plan.registerServiceRegistry(restfulServiceRegistry());
    }
}

@@ -236,43 +236,32 @@
 ion.
-model.core.services.ServiceRegistry
+support.BaseRestEndpoint
 Prop
@@ -530,26 +530,20 @@
 as.util.
-Collection
+Http
 Utils;%0Ai
@@ -556,50 +556,8 @@
 org.
-apereo.cas.util.EncodingUtils;%0Aimport org.
 spri
@@ -602,16 +602,95 @@
 utowired
+;%0Aimport org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
 ;%0Aimport
@@ -1108,51 +1108,8 @@
 e;%0A%0A
-import java.nio.charset.StandardCharsets;%0A%0A
 /**%0A
@@ -1547,16 +1547,82 @@
 yThrows%0A
+    @ConditionalOnProperty(name = %22cas.serviceRegistry.rest.url%22)%0A
     publ
@@ -1667,17 +1667,16 @@
 try() %7B%0A
-%0A
         
@@ -1681,31 +1681,32 @@
   final 
-ServiceRegistry
+BaseRestEndpoint
 Properti
@@ -1753,16 +1753,26 @@
 gistry()
+.getRest()
 ;%0A      
@@ -1951,34 +1951,24 @@
 nk(registry.
-getRest().
 getBasicAuth
@@ -2022,34 +2022,24 @@
 nk(registry.
-getRest().
 getBasicAuth
@@ -2069,408 +2069,120 @@
     
-final String auth = registry.getRest().getBasicAuthUsername() + %22:%22 + registry.getRest().getBasicAuthPassword();%0A            final byte%5B%5D encodedAuth = EncodingUtils.encodeBase64ToByteArray(auth.getBytes(StandardCharsets.UTF_8));%0A            final String authHeader = %22Basic %22 + new String(encodedAuth, StandardCharsets.UTF_8);%0A            headers.put(%22Authorization%22, CollectionUtils.wrap(authHeader
+headers.putAll(HttpUtils.createBasicAuthHeaders(registry.getBasicAuthUsername(), registry.getBasicAuthPassword()
 ));%0A
@@ -2260,18 +2260,8 @@
 try.
-getRest().
 getU
@@ -2276,17 +2276,16 @@
 aders);%0A
-%0A
     %7D%0A%0A 
@@ -2377,24 +2377,183 @@
 lan plan) %7B%0A
+        final BaseRestEndpointProperties registry = casProperties.getServiceRegistry().getRest();%0A        if (StringUtils.isNotBlank(registry.getUrl())) %7B%0A    
         plan
@@ -2600,20 +2600,30 @@
 egistry());%0A
+        %7D%0A
     %7D%0A%7D%0A

clean up config of restful service registry