Skip to content
Snippets Groups Projects
Verified Commit 7da9a792 authored by Sebastian Bergandy's avatar Sebastian Bergandy :keyboard:
Browse files

OZG-7232 add basic auth if configured

Subtask: OZG-8005
parent de2c454b
Branches
Tags
1 merge request!4OZG-7232 SmartDocuments zertifikatbasierte Authentifizierung
......@@ -23,6 +23,8 @@
*/
package de.ozgcloud.document.bescheid.smartdocuments;
import java.util.Objects;
import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.CredentialsProvider;
import org.apache.hc.client5.http.impl.auth.CredentialsProviderBuilder;
......@@ -35,6 +37,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestClient;
......@@ -50,7 +53,7 @@ class SmartDocumentsConfiguration {
return RestClient.builder()
.requestFactory(new HttpComponentsClientHttpRequestFactory(buildHttpClient()))
.baseUrl(properties.getUrl())
.defaultHeaders(headers -> headers.setBasicAuth(properties.getBasicAuth().getUsername(), properties.getBasicAuth().getPassword()))
.defaultHeaders(this::addBasicAuthenticationIfConfigured)
.build();
}
......@@ -85,4 +88,10 @@ class SmartDocumentsConfiguration {
return CredentialsProviderBuilder.create().add(new AuthScope(host, port), username, password).build();
}
void addBasicAuthenticationIfConfigured(HttpHeaders headers) {
if (Objects.nonNull(properties.getBasicAuth())) {
headers.setBasicAuth(properties.getBasicAuth().getUsername(), properties.getBasicAuth().getPassword());
}
}
}
......@@ -16,6 +16,7 @@ import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.springframework.http.HttpHeaders;
import de.ozgcloud.document.bescheid.smartdocuments.SmartDocumentsProperties.ProxyConfiguration;
......@@ -115,4 +116,30 @@ class SmartDocumentsConfigurationTest {
assertThat(httpClient).isSameAs(nonProxyHttpClient);
}
}
@Nested
class AddBasicAuthenticationIfConfiguredTest {
@Test
void shouldNotAddBasicAuthentication() {
when(properties.getBasicAuth()).thenReturn(null);
var headers = new HttpHeaders();
smartDocumentsConfiguration.addBasicAuthenticationIfConfigured(headers);
assertThat(headers.get(HttpHeaders.AUTHORIZATION)).isNull();
}
@Test
void shouldAddBasicAuthentication() {
when(properties.getBasicAuth()).thenReturn(UsernamePasswordTestFactory.create());
var headers = new HttpHeaders();
smartDocumentsConfiguration.addBasicAuthenticationIfConfigured(headers);
assertThat(headers.get(HttpHeaders.AUTHORIZATION))
.isNotNull()
.containsExactly(UsernamePasswordTestFactory.getAsBasicAuthenticationHeader());
}
}
}
\ No newline at end of file
package de.ozgcloud.document.bescheid.smartdocuments;
import org.bouncycastle.util.encoders.Base64;
import de.ozgcloud.document.bescheid.smartdocuments.SmartDocumentsProperties.UsernamePassword;
public class UsernamePasswordTestFactory {
public static final String USERNAME = "max";
public static final String PASSWORD = "max123";
public static UsernamePassword create() {
var usernamePassword = new UsernamePassword();
usernamePassword.setUsername(USERNAME);
usernamePassword.setPassword(PASSWORD);
return usernamePassword;
}
public static String getAsBasicAuthenticationHeader() {
var base64EncodedAuth = new String(Base64.encode(
(UsernamePasswordTestFactory.USERNAME + ":" + UsernamePasswordTestFactory.PASSWORD).getBytes()));
return "Basic " + base64EncodedAuth;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment