package com.bls.patronage;

import com.bls.patronage.db.dao.DeckDAO;
import com.bls.patronage.db.dao.FlashcardDAO;
import com.bls.patronage.db.exception.DataAccessExceptionMapper;
import com.bls.patronage.resources.DeckResource;
import com.bls.patronage.resources.DecksResource;
import com.bls.patronage.resources.FlashcardResource;
import com.bls.patronage.resources.FlashcardsResource;
import io.dropwizard.Application;
import io.dropwizard.configuration.EnvironmentVariableSubstitutor;
import io.dropwizard.configuration.SubstitutingSourceProvider;
import io.dropwizard.db.DataSourceFactory;
import io.dropwizard.jdbi.DBIFactory;
import io.dropwizard.migrations.MigrationsBundle;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import org.skife.jdbi.v2.DBI;

public class StudyBox extends Application<StudyBoxConfiguration> {

    private static final String APP_NAME = "backend";
    private static final String HEALTH_CHECK_DATABASE_NAME = "database";

    public static void main(String[] args) throws Exception {
        new StudyBox().run(args);
    }

    @Override
    public String getName() {
        return APP_NAME;
    }

    @Override
    public void initialize(Bootstrap<StudyBoxConfiguration> bootstrap) {
        // Enable variable substitution with environment variables
        bootstrap.setConfigurationSourceProvider(
                new SubstitutingSourceProvider(
                        bootstrap.getConfigurationSourceProvider(),
                        new EnvironmentVariableSubstitutor(false)
                )
        );

        bootstrap.addBundle(new MigrationsBundle<StudyBoxConfiguration>() {
            @Override
            public DataSourceFactory getDataSourceFactory(StudyBoxConfiguration configuration) {
                return configuration.getDatabase();
            }
        });
    }

    @Override
    public void run(StudyBoxConfiguration configuration, Environment environment) throws Exception {
        final DBI jdbi = new DBIFactory().build(environment, configuration.getDatabase(), HEALTH_CHECK_DATABASE_NAME);
        environment.jersey().register(new DeckResource(jdbi.onDemand(DeckDAO.class)));
        environment.jersey().register(new DecksResource(jdbi.onDemand(DeckDAO.class)));
        environment.jersey().register(new FlashcardResource(jdbi.onDemand(FlashcardDAO.class)));
        environment.jersey().register(new FlashcardsResource(jdbi.onDemand(FlashcardDAO.class)));
        environment.jersey().register(new DataAccessExceptionMapper());
    }
}

@@ -21,16 +21,66 @@
 onage;%0A%0A
+import com.bls.patronage.auth.BasicAuthenticator;%0A
 import c
@@ -108,24 +108,24 @@
 ao.DeckDAO;%0A
-
 import com.b
@@ -154,24 +154,65 @@
 ashcardDAO;%0A
+import com.bls.patronage.db.dao.UserDAO;%0A
 import com.b
@@ -260,24 +260,64 @@
 tionMapper;%0A
+import com.bls.patronage.db.model.User;%0A
 import com.b
@@ -508,24 +508,24 @@
 dsResource;%0A
-
 import io.dr
@@ -546,16 +546,221 @@
 cation;%0A
+import io.dropwizard.auth.AuthDynamicFeature;%0Aimport io.dropwizard.auth.AuthValueFactoryProvider;%0Aimport io.dropwizard.auth.CachingAuthenticator;%0Aimport io.dropwizard.auth.basic.BasicCredentialAuthFilter;%0A
 import i
@@ -2867,16 +2867,629 @@
 pper());
+%0A%0A        final BasicAuthenticator basicAuthenticator = new BasicAuthenticator(jdbi.onDemand(UserDAO.class));%0A        final CachingAuthenticator cachingAuthenticator = new CachingAuthenticator(environment.metrics(), basicAuthenticator,%0A                configuration.getAuthCacheBuilder());%0A        environment.jersey().register(new AuthDynamicFeature(%0A                new BasicCredentialAuthFilter.Builder%3CUser%3E()%0A                        .setAuthenticator(cachingAuthenticator)%0A                        .buildAuthFilter()));%0A        environment.jersey().register(new AuthValueFactoryProvider.Binder%3C%3E(User.class));
 %0A    %7D%0A%7D

enable Basic Authenticate