Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.opendevstack.apiservice.core.security.util;

import java.util.Base64;

public class Base64Operations {

private Base64Operations() {}

/**
* Encodes a string value using Base64 encoding.
*
* @param value the string value to encode
* @return the Base64 encoded string, or null if input is null
*/
public static String encode(String value) {
if (value == null) {
return null;
}
return Base64.getEncoder().encodeToString(value.getBytes());
}

/**
* Decodes a Base64 encoded string value.
*
* @param encodedValue the Base64 encoded string to decode
* @return the decoded string, or null if input is null
* @throws IllegalArgumentException if the input is not valid Base64
*/
public static String decode(String encodedValue) {
if (encodedValue == null) {
return null;
}
try {
byte[] decodedBytes = Base64.getDecoder().decode(encodedValue);
return new String(decodedBytes);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Invalid Base64 encoded value: " + encodedValue, e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.opendevstack.apiservice.core.security.util;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class Base64OperationsTest {

@Test
void tectEncodeWhenNullInputReturnNull() {
String testString = null;

String encodedString = Base64Operations.encode(testString);

assertEquals(null, encodedString);
}

@Test
void tectEncodeWhenCorrectInputReturnEncodedResult() {
String testString = "test;string";

String encodedString = Base64Operations.encode(testString);

assertEquals("dGVzdDtzdHJpbmc=", encodedString);
}

@Test
void testDecodeWhenNullInputReturnNull() {
String testString = null;

String dencodedString = Base64Operations.decode(testString);

assertEquals(null, dencodedString);
}

@Test
void testDecodeWhenCorrectInputReturnDecodedResult() {
String testString = "dGVzdDtzdHJpbmc=";

String dencodedString = Base64Operations.decode(testString);

assertEquals("test;string", dencodedString);
}

@Test
void testDecodeWhenBadInputThrowException() {
String testString = "NOT AN ENCODED STRING";

assertThrows(IllegalArgumentException.class, () -> Base64Operations.decode(testString));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.openapitools.jackson.nullable.JsonNullableModule;
import org.opendevstack.apiservice.core.security.util.Base64Operations;
import org.opendevstack.apiservice.externalservice.marketplace.config.MarketplaceInstanceConfig;
import org.opendevstack.apiservice.externalservice.marketplace.openapi.ApiClient;
import org.opendevstack.apiservice.externalservice.marketplace.openapi.auth.HttpBearerAuth;
Expand Down Expand Up @@ -39,7 +40,7 @@ public MarketplaceApiClient(String instanceName, MarketplaceInstanceConfig confi

if (config.getUsername() != null && config.getPassword() != null) {
this.apiClient.setUsername(config.getUsername());
this.apiClient.setPassword(config.getPassword());
this.apiClient.setPassword(Base64Operations.decode(config.getPassword()));
log.info("MarketplaceApiClient for instance '{}' uses basic authentication", instanceName);
}

Expand Down
Loading