Skip to content

Commit

Permalink
add tests to get biscuit from http header or query param
Browse files Browse the repository at this point in the history
  • Loading branch information
KannarFr committed Nov 15, 2023
1 parent ef88ea9 commit 4b5f29c
Showing 1 changed file with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
import com.clevercloud.biscuit.token.builder.Block;
import org.apache.pulsar.broker.ServiceConfiguration;
import org.apache.pulsar.broker.authentication.AuthenticationDataSource;
import org.apache.pulsar.broker.authentication.AuthenticationState;
import org.hamcrest.core.StringStartsWith;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.naming.AuthenticationException;
import javax.servlet.http.HttpServletRequest;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Properties;
Expand All @@ -22,6 +24,8 @@
import static com.clevercloud.biscuit.token.builder.Utils.s;
import static org.junit.Assert.assertThrows;
import static org.hamcrest.MatcherAssert.*;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;

public class AuthenticationProviderBiscuitTest {
private static final Logger LOGGER = LoggerFactory.getLogger(AuthenticationProviderBiscuitTest.class);
Expand Down Expand Up @@ -106,4 +110,66 @@ public String getCommandData() {

provider.close();
}

@Test
public void testTokenFromHttpParams() throws Exception {
KeyPair root = new KeyPair("D283C7E436D89C544CC2B20C1028A7ADDC18FCED6386A6130465C17B996CD893");

AuthenticationProviderBiscuit provider = new AuthenticationProviderBiscuit();

Properties properties = new Properties();
properties.setProperty(AuthenticationProviderBiscuit.CONF_BISCUIT_PUBLIC_ROOT_KEY, hex(root.public_key().key.getAbyte()));

ServiceConfiguration conf = new ServiceConfiguration();
conf.setProperties(properties);
provider.initialize(conf);

SymbolTable symbols = Biscuit.default_symbol_table();

Block authority_builder = new Block(0, symbols);
authority_builder.add_fact(fact("right", Arrays.asList(s("topic"), s("public"), s("default"), s("test"), s("produce"))));

byte[] seed = {0, 0, 0, 0};
SecureRandom rng = new SecureRandom(seed);
Biscuit b = Biscuit.make(rng, root, Biscuit.default_symbol_table(), authority_builder.build());

HttpServletRequest servletRequest = mock(HttpServletRequest.class);
doReturn(b.serialize_b64url()).when(servletRequest).getParameter("token");
doReturn(null).when(servletRequest).getHeader("Authorization");
doReturn("127.0.0.1").when(servletRequest).getRemoteAddr();
doReturn(0).when(servletRequest).getRemotePort();

AuthenticationState authState = provider.newHttpAuthState(servletRequest);
provider.authenticate(authState.getAuthDataSource());
}

@Test
public void testTokenFromHttpHeaders() throws Exception {
KeyPair root = new KeyPair("D283C7E436D89C544CC2B20C1028A7ADDC18FCED6386A6130465C17B996CD893");

AuthenticationProviderBiscuit provider = new AuthenticationProviderBiscuit();

Properties properties = new Properties();
properties.setProperty(AuthenticationProviderBiscuit.CONF_BISCUIT_PUBLIC_ROOT_KEY, hex(root.public_key().key.getAbyte()));

ServiceConfiguration conf = new ServiceConfiguration();
conf.setProperties(properties);
provider.initialize(conf);

SymbolTable symbols = Biscuit.default_symbol_table();

Block authority_builder = new Block(0, symbols);
authority_builder.add_fact(fact("right", Arrays.asList(s("topic"), s("public"), s("default"), s("test"), s("produce"))));

byte[] seed = {0, 0, 0, 0};
SecureRandom rng = new SecureRandom(seed);
Biscuit b = Biscuit.make(rng, root, Biscuit.default_symbol_table(), authority_builder.build());
HttpServletRequest servletRequest = mock(HttpServletRequest.class);
doReturn("Bearer " + b.serialize_b64url()).when(servletRequest).getHeader("Authorization");
doReturn("127.0.0.1").when(servletRequest).getRemoteAddr();
doReturn(0).when(servletRequest).getRemotePort();

AuthenticationState authState = provider.newHttpAuthState(servletRequest);
provider.authenticate(authState.getAuthDataSource());
}
}

0 comments on commit 4b5f29c

Please sign in to comment.