Search in sources :

Example 1 with Payload

use of net.codestory.http.payload.Payload in project datashare by ICIJ.

the class ServerMode method configure.

@Override
protected void configure() {
    super.configure();
    String authUsersProviderClassName = propertiesProvider.get("authUsersProvider").orElse("org.icij.datashare.session.UsersInRedis");
    String batchQueueType = propertiesProvider.get("batchQueueType").orElse("org.icij.datashare.extract.MemoryBlockingQueue");
    bind(TaskManager.class).toInstance(new TaskManagerRedis(propertiesProvider, getBlockingQueue(propertiesProvider, batchQueueType, "ds:batchdownload:queue")));
    Class<? extends UsersWritable> authUsersProviderClass = UsersInRedis.class;
    try {
        authUsersProviderClass = (Class<? extends UsersWritable>) Class.forName(authUsersProviderClassName);
        logger.info("setting auth users provider to {}", authUsersProviderClass);
    } catch (ClassNotFoundException e) {
        logger.warn("\"{}\" auth users provider class not found. Setting provider to {}", authUsersProviderClassName, authUsersProviderClass);
    }
    bind(UsersWritable.class).to(authUsersProviderClass);
    bind(SessionIdStore.class).to(RedisSessionIdStore.class);
    bind(ApiKeyStore.class).to(ApiKeyStoreAdapter.class);
    String authFilterClassName = propertiesProvider.get("authFilter").orElse("");
    Class<? extends Filter> authFilterClass = OAuth2CookieFilter.class;
    if (!authFilterClassName.isEmpty()) {
        try {
            authFilterClass = (Class<? extends Filter>) Class.forName(authFilterClassName);
            logger.info("setting auth filter to {}", authFilterClass);
        } catch (ClassNotFoundException e) {
            logger.warn("\"{}\" auth filter class not found. Setting filter to {}", authFilterClassName, authFilterClass);
        }
    }
    bind(Filter.class).to(authFilterClass);
    if (authFilterClass.equals(BasicAuthAdaptorFilter.class)) {
        bind(ApiKeyFilter.class).toInstance(new ApiKeyFilter(null, apiKey -> null) {

            @Override
            public Payload apply(String uri, Context context, PayloadSupplier nextFilter) throws Exception {
                return nextFilter.get();
            }
        });
    }
    bind(StatusResource.class).asEagerSingleton();
    configurePersistence();
}
Also used : SessionIdStore(net.codestory.http.security.SessionIdStore) Routes(net.codestory.http.routes.Routes) Logger(org.slf4j.Logger) Properties(java.util.Properties) Context(net.codestory.http.Context) Payload(net.codestory.http.payload.Payload) LoggerFactory(org.slf4j.LoggerFactory) org.icij.datashare.web(org.icij.datashare.web) PayloadSupplier(net.codestory.http.filters.PayloadSupplier) TaskManager(org.icij.datashare.tasks.TaskManager) Map(java.util.Map) Filter(net.codestory.http.filters.Filter) TaskManagerRedis(org.icij.datashare.tasks.TaskManagerRedis) org.icij.datashare.session(org.icij.datashare.session) Context(net.codestory.http.Context) TaskManagerRedis(org.icij.datashare.tasks.TaskManagerRedis) TaskManager(org.icij.datashare.tasks.TaskManager) SessionIdStore(net.codestory.http.security.SessionIdStore) PayloadSupplier(net.codestory.http.filters.PayloadSupplier) Filter(net.codestory.http.filters.Filter) Payload(net.codestory.http.payload.Payload)

Example 2 with Payload

use of net.codestory.http.payload.Payload in project datashare by ICIJ.

the class ProjectResource method deleteProject.

/**
 * Delete the project from database and elasticsearch indices.
 *
 * It returns 204 (no content) when something has been removed (index and/or database), or
 * 404 if nothing has been removed (i.e. index and database don't exist).
 *
 * If the project id is not the current user project (local-datashare in local mode),
 * then it will return 401 (unauthorized)
 *
 * @param id
 * @return 204 (no content) or 404
 *
 * Example :
 * $(curl -I -XDELETE -H 'Content-Type:application/json' localhost:8080/api/project/unknown-project)
 */
@Delete("/:id")
public Payload deleteProject(String id, Context context) throws Exception {
    if (!context.currentUser().isInRole("local")) {
        return new Payload(401);
    }
    boolean isDeleted = this.repository.deleteAll(id);
    boolean indexDeleted = this.indexer.deleteAll(id);
    return isDeleted || indexDeleted ? new Payload(204) : new Payload(404);
}
Also used : Payload(net.codestory.http.payload.Payload) Delete(net.codestory.http.annotations.Delete)

Example 3 with Payload

use of net.codestory.http.payload.Payload in project datashare by ICIJ.

the class YesBasicAuthFilterTest method test_reject_secure_request_without_user.

@Test
public void test_reject_secure_request_without_user() throws Exception {
    Payload payload = filter.apply("/secure/uri", context, nextFilter);
    assertThat(payload.code()).isEqualTo(401);
    assertThat(payload.headers()).includes(entry("WWW-Authenticate", "Basic realm=\"datashare\""));
}
Also used : Payload(net.codestory.http.payload.Payload) Test(org.junit.Test)

Example 4 with Payload

use of net.codestory.http.payload.Payload in project datashare by ICIJ.

the class YesBasicAuthFilterTest method test_pass_with_user_information.

@Test
public void test_pass_with_user_information() throws Exception {
    // "foo:bar" base64 encoded
    when(context.header("Authorization")).thenReturn("Basic Zm9vOmJhcg==");
    Payload payload = filter.apply("/secure/uri", context, nextFilter);
    assertThat(payload).isSameAs(next);
    verify(context).setCurrentUser(user.capture());
    assertThat(user.getValue().login()).isEqualTo("foo");
}
Also used : Payload(net.codestory.http.payload.Payload) Test(org.junit.Test)

Example 5 with Payload

use of net.codestory.http.payload.Payload in project datashare by ICIJ.

the class ApiKeyFilterTest method test_adds_user_to_context.

@Test
public void test_adds_user_to_context() throws Exception {
    when(context.header("authorization")).thenReturn("Bearer session_id");
    when(apiKeyStore.getLogin("session_id")).thenReturn("user_id");
    when(users.find("user_id")).thenReturn(new DatashareUser("user_id"));
    Payload payload = apiKeyFilter.apply("url", context, nextFilter);
    assertThat(payload).isSameAs(next);
    verify(context).setCurrentUser(user.capture());
    assertThat(user.getValue().login()).isEqualTo("user_id");
}
Also used : Payload(net.codestory.http.payload.Payload) Test(org.junit.Test)

Aggregations

Payload (net.codestory.http.payload.Payload)19 Test (org.junit.Test)11 PropertiesProvider (org.icij.datashare.PropertiesProvider)5 HashMap (java.util.HashMap)2 Context (net.codestory.http.Context)2 User (net.codestory.http.security.User)2 BatchSearch (org.icij.datashare.batch.BatchSearch)2 DatashareUser (org.icij.datashare.session.DatashareUser)2 User (org.icij.datashare.user.User)2 Inject (com.google.inject.Inject)1 Singleton (com.google.inject.Singleton)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Boolean (java.lang.Boolean)1 Integer.parseInt (java.lang.Integer.parseInt)1 String.format (java.lang.String.format)1 Path (java.nio.file.Path)1 java.util (java.util)1