Skip to content

Commit

Permalink
Remove the extra code use UserNamePasswordProvider class
Browse files Browse the repository at this point in the history
Signed-off-by: AayushSaini101 <[email protected]>
  • Loading branch information
AayushSaini101 committed Feb 12, 2025
1 parent 80b7ca6 commit bd125e4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 82 deletions.
61 changes: 10 additions & 51 deletions src/main/java/land/oras/auth/FileStoreAuthenticationProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import land.oras.credentials.FileStore;
import land.oras.credentials.FileStore.Credential;
import land.oras.exception.ConfigLoadingException;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
* FileStoreAuthenticationProvider is an implementation of the AuthProvider interface.
Expand All @@ -14,68 +13,28 @@ public class FileStoreAuthenticationProvider implements AuthProvider {

private final FileStore fileStore;
private final String serverAddress;

private String username;
private String password;
private final UsernamePasswordProvider usernamePasswordAuthProvider;

/**
* Constructor for FileStoreAuthenticationProvider.
*
* @param fileStore The FileStore instance to retrieve credentials from.
* @param serverAddress The server address for which to retrieve credentials.
*/
public FileStoreAuthenticationProvider(FileStore fileStore, String serverAddress) {
public FileStoreAuthenticationProvider(FileStore fileStore, String serverAddress) throws Exception {
this.fileStore = fileStore;
this.serverAddress = serverAddress;
}

/**
* Generates the Basic Authentication header for the provided server address.
*
* @return A Basic Authentication header string.
* @throws RuntimeException if no credentials are found for the server address.
*/
@Override
public String getAuthHeader() {
try {
// Retrieve the credential for the server address
Credential credential = fileStore.get(serverAddress);

if (credential == null) {
throw new RuntimeException("No credentials found for server address: " + serverAddress);
}

// Set the username and password fields
this.username = credential.getUsername();
this.password = credential.getPassword();

// Generate Basic Auth header (Base64 encoding of "username:password")
String authString = username + ":" + password;
String encodedAuth = Base64.getEncoder().encodeToString(authString.getBytes(StandardCharsets.UTF_8));

return "Basic " + encodedAuth;

} catch (Exception e) {
throw new RuntimeException("Failed to generate authentication header", e);
Credential credential = fileStore.get(serverAddress);
if (credential == null) {
throw new ConfigLoadingException("No credentials found for server address: " + serverAddress);
}
}
this.usernamePasswordAuthProvider = new UsernamePasswordProvider(credential.getUsername(), credential.getPassword());

/**
* Gets the username of the retrieved credential.
*
* @return The username.
*/
public String getUsername() {
return username;
}

/**
* Gets the password of the retrieved credential.
*
* @return The password.
*/
public String getPassword() {
return password;
@Override
public String getAuthHeader() {
return usernamePasswordAuthProvider.getAuthHeader();
}
}

Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package land.oras.auth;


import land.oras.credentials.FileStore;
import land.oras.credentials.FileStore.Credential;
import land.oras.exception.ConfigLoadingException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.Base64;

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

import java.nio.charset.StandardCharsets;
import java.util.Base64;

class FileStoreAuthenticationProviderTest {

private FileStore mockFileStore;
Expand All @@ -19,52 +20,65 @@ class FileStoreAuthenticationProviderTest {

@BeforeEach
void setUp() {
// Create a mock FileStore
// Mock the FileStore
mockFileStore = mock(FileStore.class);

// Initialize FileStoreAuthenticationProvider with the mock
authProvider = new FileStoreAuthenticationProvider(mockFileStore, serverAddress);
}

@Test
void testGetAuthHeader_validCredentials() throws Exception {
void testConstructor_validCredentials() throws Exception {
// Mock valid credentials for the server address
Credential credential = new Credential("testUser", "testPassword");
when(mockFileStore.get(serverAddress)).thenReturn(credential);

// Generate the authentication header
String authHeader = authProvider.getAuthHeader();

// Verify the expected Basic Auth header
String expectedAuthString = "testUser:testPassword";
String expectedEncodedAuth = "Basic " + Base64.getEncoder().encodeToString(expectedAuthString.getBytes());
assertEquals(expectedEncodedAuth, authHeader);
// Create the authentication provider
authProvider = new FileStoreAuthenticationProvider(mockFileStore, serverAddress);

// Verify that username and password fields are set correctly
assertEquals("testUser", authProvider.getUsername());
assertEquals("testPassword", authProvider.getPassword());
// Assert that the authentication provider is created successfully
assertNotNull(authProvider);
}

@Test
void testGetAuthHeader_retrievalError() throws Exception {
// Mock an exception during credential retrieval
when(mockFileStore.get(serverAddress)).thenThrow(new Exception("FileStore error"));
void testConstructor_missingCredentials() throws Exception {
// Mock no credentials for the server address
when(mockFileStore.get(serverAddress)).thenReturn(null);

// Verify that a RuntimeException is thrown
RuntimeException exception = assertThrows(RuntimeException.class, authProvider::getAuthHeader);
assertTrue(exception.getMessage().contains("Failed to generate authentication header"));
// Verify that the constructor throws ConfigLoadingException
ConfigLoadingException exception = assertThrows(ConfigLoadingException.class, () -> {
new FileStoreAuthenticationProvider(mockFileStore, serverAddress);
});

// Ensure username and password fields are not set
assertNull(authProvider.getUsername());
assertNull(authProvider.getPassword());
// Assert the exception message
assertTrue(exception.getMessage().contains("No credentials found for server address"));
}

@Test
void testUsernameAndPasswordNotSetBeforeCall() {
// Ensure username and password are null before calling getAuthHeader
assertNull(authProvider.getUsername());
assertNull(authProvider.getPassword());
void testGetAuthHeader_validCredentials() throws Exception {
// Mock valid credentials for the server address
Credential credential = new Credential("testUser", "testPassword");
when(mockFileStore.get(serverAddress)).thenReturn(credential);

// Create the authentication provider
authProvider = new FileStoreAuthenticationProvider(mockFileStore, serverAddress);

// Verify that the getAuthHeader method returns the expected Basic Auth header
String authHeader = authProvider.getAuthHeader();
String expectedAuthString = "testUser:testPassword";
String expectedEncodedAuth = "Basic " + Base64.getEncoder().encodeToString(expectedAuthString.getBytes(StandardCharsets.UTF_8));

assertEquals(expectedEncodedAuth, authHeader);
}

@Test
void testGetAuthHeader_missingCredentials() throws Exception {
// Mock no credentials for the server address
when(mockFileStore.get(serverAddress)).thenReturn(null);

// Create the authentication provider, expecting it to throw ConfigLoadingException
ConfigLoadingException exception = assertThrows(ConfigLoadingException.class, () -> {
new FileStoreAuthenticationProvider(mockFileStore, serverAddress);
});

// Verify the exception message
assertTrue(exception.getMessage().contains("No credentials found for server address"));
}
}

0 comments on commit bd125e4

Please sign in to comment.