Search in sources :

Example 1 with DefaultJdbcComponent

use of io.apiman.gateway.engine.impl.DefaultJdbcComponent in project apiman by apiman.

the class BasicAuthJDBCTest method testApplyJdbcWithRoles.

public void testApplyJdbcWithRoles(String json) throws Exception {
    // A live LDAP server is required to run this test!
    BasicAuthenticationPolicy policy = new BasicAuthenticationPolicy();
    BasicAuthenticationConfig config = policy.parseConfiguration(json);
    ApiRequest request = new ApiRequest();
    request.setType("GET");
    request.setApiKey("12345");
    request.setRemoteAddr("1.2.3.4");
    request.setDestination("/");
    IPolicyContext context = Mockito.mock(IPolicyContext.class);
    Mockito.when(context.getComponent(IJdbcComponent.class)).thenReturn(new DefaultJdbcComponent());
    IPolicyChain<ApiRequest> chain = Mockito.mock(IPolicyChain.class);
    // Success
    request.getHeaders().put("Authorization", createBasicAuthorization(JDBC_USER, JDBC_PASSWORD));
    chain = Mockito.mock(IPolicyChain.class);
    policy.apply(request, context, config, chain);
    Mockito.verify(chain).doApply(request);
    Set<String> expectedRoles = new HashSet<>();
    expectedRoles.add("admin");
    expectedRoles.add("user");
    Mockito.verify(context).setAttribute(AuthorizationPolicy.AUTHENTICATED_USER_ROLES, expectedRoles);
}
Also used : IPolicyChain(io.apiman.gateway.engine.policy.IPolicyChain) IPolicyContext(io.apiman.gateway.engine.policy.IPolicyContext) BasicAuthenticationConfig(io.apiman.gateway.engine.policies.config.BasicAuthenticationConfig) DefaultJdbcComponent(io.apiman.gateway.engine.impl.DefaultJdbcComponent) ApiRequest(io.apiman.gateway.engine.beans.ApiRequest) HashSet(java.util.HashSet)

Example 2 with DefaultJdbcComponent

use of io.apiman.gateway.engine.impl.DefaultJdbcComponent in project apiman by apiman.

the class BasicAuthJDBCTest method testApplyJdbcNoRoles.

/**
 * @param json
 * @throws Exception
 */
public void testApplyJdbcNoRoles(String json) throws Exception {
    // A live LDAP server is required to run this test!
    BasicAuthenticationPolicy policy = new BasicAuthenticationPolicy();
    BasicAuthenticationConfig config = policy.parseConfiguration(json);
    ApiRequest request = new ApiRequest();
    request.setType("GET");
    request.setApiKey("12345");
    request.setRemoteAddr("1.2.3.4");
    request.setDestination("/");
    IPolicyContext context = Mockito.mock(IPolicyContext.class);
    final PolicyFailure failure = new PolicyFailure();
    Mockito.when(context.getComponent(IPolicyFailureFactoryComponent.class)).thenReturn(new IPolicyFailureFactoryComponent() {

        @Override
        public PolicyFailure createFailure(PolicyFailureType type, int failureCode, String message) {
            return failure;
        }
    });
    Mockito.when(context.getComponent(IJdbcComponent.class)).thenReturn(new DefaultJdbcComponent());
    IPolicyChain<ApiRequest> chain = Mockito.mock(IPolicyChain.class);
    // Failure
    policy.apply(request, context, config, chain);
    Mockito.verify(chain).doFailure(failure);
    // Failure
    request.getHeaders().put("Authorization", createBasicAuthorization(JDBC_USER, "invalid_password"));
    chain = Mockito.mock(IPolicyChain.class);
    policy.apply(request, context, config, chain);
    Mockito.verify(chain).doFailure(failure);
    // Success
    request.getHeaders().put("Authorization", createBasicAuthorization(JDBC_USER, JDBC_PASSWORD));
    chain = Mockito.mock(IPolicyChain.class);
    policy.apply(request, context, config, chain);
    Mockito.verify(chain).doApply(request);
}
Also used : DefaultJdbcComponent(io.apiman.gateway.engine.impl.DefaultJdbcComponent) ApiRequest(io.apiman.gateway.engine.beans.ApiRequest) IPolicyFailureFactoryComponent(io.apiman.gateway.engine.components.IPolicyFailureFactoryComponent) IPolicyChain(io.apiman.gateway.engine.policy.IPolicyChain) IPolicyContext(io.apiman.gateway.engine.policy.IPolicyContext) PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure) BasicAuthenticationConfig(io.apiman.gateway.engine.policies.config.BasicAuthenticationConfig) PolicyFailureType(io.apiman.gateway.engine.beans.PolicyFailureType)

Aggregations

ApiRequest (io.apiman.gateway.engine.beans.ApiRequest)2 DefaultJdbcComponent (io.apiman.gateway.engine.impl.DefaultJdbcComponent)2 BasicAuthenticationConfig (io.apiman.gateway.engine.policies.config.BasicAuthenticationConfig)2 IPolicyChain (io.apiman.gateway.engine.policy.IPolicyChain)2 IPolicyContext (io.apiman.gateway.engine.policy.IPolicyContext)2 PolicyFailure (io.apiman.gateway.engine.beans.PolicyFailure)1 PolicyFailureType (io.apiman.gateway.engine.beans.PolicyFailureType)1 IPolicyFailureFactoryComponent (io.apiman.gateway.engine.components.IPolicyFailureFactoryComponent)1 HashSet (java.util.HashSet)1