Search in sources :

Example 1 with InjectorBuilder

use of ch.jalu.injector.InjectorBuilder in project AuthMeReloaded by AuthMe.

the class HashAlgorithmIntegrationTest method setUpConfigAndInjector.

@BeforeClass
public static void setUpConfigAndInjector() {
    Settings settings = mock(Settings.class);
    given(settings.getProperty(HooksSettings.BCRYPT_LOG2_ROUND)).willReturn(8);
    given(settings.getProperty(SecuritySettings.DOUBLE_MD5_SALT_LENGTH)).willReturn(16);
    given(settings.getProperty(SecuritySettings.PBKDF2_NUMBER_OF_ROUNDS)).willReturn(10_000);
    injector = new InjectorBuilder().addDefaultHandlers("fr.xephi.authme").create();
    injector.register(Settings.class, settings);
}
Also used : InjectorBuilder(ch.jalu.injector.InjectorBuilder) SecuritySettings(fr.xephi.authme.settings.properties.SecuritySettings) Settings(fr.xephi.authme.settings.Settings) HooksSettings(fr.xephi.authme.settings.properties.HooksSettings) BeforeClass(org.junit.BeforeClass)

Example 2 with InjectorBuilder

use of ch.jalu.injector.InjectorBuilder in project AuthMeReloaded by AuthMe.

the class EncryptionMethodInfoGatherer method createInitializer.

@SuppressWarnings("unchecked")
private static Injector createInitializer() {
    Settings settings = mock(Settings.class);
    // Return the default value for any property
    when(settings.getProperty(any(Property.class))).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            Property<?> property = (Property<?>) invocation.getArguments()[0];
            return property.getDefaultValue();
        }
    });
    // By passing some bogus "package" to the constructor, the injector will throw if it needs to
    // instantiate any dependency other than what we provide.
    Injector injector = new InjectorBuilder().addDefaultHandlers("fr.xephi.authme.security.crypts").create();
    injector.register(Settings.class, settings);
    return injector;
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) Injector(ch.jalu.injector.Injector) InjectorBuilder(ch.jalu.injector.InjectorBuilder) Property(ch.jalu.configme.properties.Property) Settings(fr.xephi.authme.settings.Settings)

Example 3 with InjectorBuilder

use of ch.jalu.injector.InjectorBuilder in project AuthMeReloaded by AuthMe.

the class AuthMeInitializationTest method shouldInitializeAllServices.

@Test
public void shouldInitializeAllServices() {
    // given
    Settings settings = new Settings(dataFolder, mock(PropertyResource.class), null, buildConfigurationData());
    Injector injector = new InjectorBuilder().addHandlers(new FactoryDependencyHandler(), new SingletonStoreDependencyHandler()).addDefaultHandlers("fr.xephi.authme").create();
    injector.provide(DataFolder.class, dataFolder);
    injector.register(Server.class, server);
    injector.register(PluginManager.class, pluginManager);
    injector.register(AuthMe.class, authMe);
    injector.register(Settings.class, settings);
    injector.register(DataSource.class, mock(DataSource.class));
    injector.register(BukkitService.class, mock(BukkitService.class));
    // when
    authMe.instantiateServices(injector);
    authMe.registerEventListeners(injector);
    // then
    // Take a few samples and ensure that they are not null
    assertThat(injector.getIfAvailable(BlockListener.class), not(nullValue()));
    assertThat(injector.getIfAvailable(CommandHandler.class), not(nullValue()));
    assertThat(injector.getIfAvailable(Management.class), not(nullValue()));
    assertThat(injector.getIfAvailable(NewAPI.class), not(nullValue()));
    assertThat(injector.getIfAvailable(PasswordSecurity.class), not(nullValue()));
    assertThat(injector.getIfAvailable(PermissionsManager.class), not(nullValue()));
    assertThat(injector.getIfAvailable(ProcessSyncPlayerLogin.class), not(nullValue()));
    assertThat(injector.getIfAvailable(PurgeService.class), not(nullValue()));
}
Also used : BukkitService(fr.xephi.authme.service.BukkitService) CommandHandler(fr.xephi.authme.command.CommandHandler) Management(fr.xephi.authme.process.Management) PasswordSecurity(fr.xephi.authme.security.PasswordSecurity) DataSource(fr.xephi.authme.datasource.DataSource) PurgeService(fr.xephi.authme.task.purge.PurgeService) SingletonStoreDependencyHandler(fr.xephi.authme.initialization.factory.SingletonStoreDependencyHandler) Injector(ch.jalu.injector.Injector) PropertyResource(ch.jalu.configme.resource.PropertyResource) FactoryDependencyHandler(fr.xephi.authme.initialization.factory.FactoryDependencyHandler) BlockListener(fr.xephi.authme.listener.BlockListener) NewAPI(fr.xephi.authme.api.NewAPI) PermissionsManager(fr.xephi.authme.permission.PermissionsManager) InjectorBuilder(ch.jalu.injector.InjectorBuilder) ProcessSyncPlayerLogin(fr.xephi.authme.process.login.ProcessSyncPlayerLogin) Settings(fr.xephi.authme.settings.Settings) Test(org.junit.Test)

Example 4 with InjectorBuilder

use of ch.jalu.injector.InjectorBuilder in project AuthMeReloaded by AuthMe.

the class PasswordSecurityTest method setUpMocks.

@BeforeInjecting
public void setUpMocks() {
    caughtClassInEvent = null;
    // When the password encryption event is emitted, replace the encryption method with our mock.
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            Object[] arguments = invocation.getArguments();
            if (arguments[0] instanceof PasswordEncryptionEvent) {
                PasswordEncryptionEvent event = (PasswordEncryptionEvent) arguments[0];
                caughtClassInEvent = event.getMethod() == null ? null : event.getMethod().getClass();
                event.setMethod(method);
            }
            return null;
        }
    }).when(pluginManager).callEvent(any(Event.class));
    given(settings.getProperty(SecuritySettings.PASSWORD_HASH)).willReturn(HashAlgorithm.BCRYPT);
    given(settings.getProperty(SecuritySettings.LEGACY_HASHES)).willReturn(Collections.emptySet());
    given(settings.getProperty(HooksSettings.BCRYPT_LOG2_ROUND)).willReturn(8);
    Injector injector = new InjectorBuilder().addDefaultHandlers("fr.xephi.authme.security.crypts").create();
    injector.register(Settings.class, settings);
    given(hashAlgorithmFactory.newInstance(any(Class.class))).willAnswer(invocation -> {
        Object o = injector.createIfHasDependencies(invocation.getArgument(0));
        if (o == null) {
            throw new IllegalArgumentException("Cannot create object of class '" + invocation.getArgument(0) + "': missing class that needs to be provided?");
        }
        return o;
    });
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) Injector(ch.jalu.injector.Injector) PasswordEncryptionEvent(fr.xephi.authme.events.PasswordEncryptionEvent) Event(org.bukkit.event.Event) PasswordEncryptionEvent(fr.xephi.authme.events.PasswordEncryptionEvent) BeforeClass(org.junit.BeforeClass) InjectorBuilder(ch.jalu.injector.InjectorBuilder) BeforeInjecting(ch.jalu.injector.testing.BeforeInjecting)

Example 5 with InjectorBuilder

use of ch.jalu.injector.InjectorBuilder in project AuthMeReloaded by AuthMe.

the class AuthMe method initialize.

/**
     * Initialize the plugin and all the services.
     */
private void initialize() {
    // Set the Logger instance and log file path
    ConsoleLogger.setLogger(getLogger());
    ConsoleLogger.setLogFile(new File(getDataFolder(), LOG_FILENAME));
    // Check java version
    if (!SystemUtils.isJavaVersionAtLeast(1.8f)) {
        throw new IllegalStateException("You need Java 1.8 or above to run this plugin!");
    }
    OnStartupTasks.verifyIfLegacyJarIsNeeded();
    // Create plugin folder
    getDataFolder().mkdir();
    // Create injector, provide elements from the Bukkit environment and register providers
    injector = new InjectorBuilder().addHandlers(new FactoryDependencyHandler(), new SingletonStoreDependencyHandler()).addDefaultHandlers("fr.xephi.authme").create();
    injector.register(AuthMe.class, this);
    injector.register(Server.class, getServer());
    injector.register(PluginManager.class, getServer().getPluginManager());
    injector.register(BukkitScheduler.class, getServer().getScheduler());
    injector.provide(DataFolder.class, getDataFolder());
    injector.registerProvider(Settings.class, SettingsProvider.class);
    injector.registerProvider(DataSource.class, DataSourceProvider.class);
    // Get settings and set up logger
    settings = injector.getSingleton(Settings.class);
    ConsoleLogger.setLoggingOptions(settings);
    OnStartupTasks.setupConsoleFilter(settings, getLogger());
    // Set all service fields on the AuthMe class
    instantiateServices(injector);
    // Convert deprecated PLAINTEXT hash entries
    MigrationService.changePlainTextToSha256(settings, database, new Sha256());
    // If the server is empty (fresh start) just set all the players as unlogged
    if (bukkitService.getOnlinePlayers().isEmpty()) {
        database.purgeLogged();
    }
    // Register event listeners
    registerEventListeners(injector);
    // Start Email recall task if needed
    OnStartupTasks onStartupTasks = injector.newInstance(OnStartupTasks.class);
    onStartupTasks.scheduleRecallEmailTask();
}
Also used : OnStartupTasks(fr.xephi.authme.initialization.OnStartupTasks) SingletonStoreDependencyHandler(fr.xephi.authme.initialization.factory.SingletonStoreDependencyHandler) FactoryDependencyHandler(fr.xephi.authme.initialization.factory.FactoryDependencyHandler) InjectorBuilder(ch.jalu.injector.InjectorBuilder) File(java.io.File) PluginDescriptionFile(org.bukkit.plugin.PluginDescriptionFile) SecuritySettings(fr.xephi.authme.settings.properties.SecuritySettings) RestrictionSettings(fr.xephi.authme.settings.properties.RestrictionSettings) PluginSettings(fr.xephi.authme.settings.properties.PluginSettings) Settings(fr.xephi.authme.settings.Settings) EmailSettings(fr.xephi.authme.settings.properties.EmailSettings) Sha256(fr.xephi.authme.security.crypts.Sha256)

Aggregations

InjectorBuilder (ch.jalu.injector.InjectorBuilder)5 Settings (fr.xephi.authme.settings.Settings)4 Injector (ch.jalu.injector.Injector)3 FactoryDependencyHandler (fr.xephi.authme.initialization.factory.FactoryDependencyHandler)2 SingletonStoreDependencyHandler (fr.xephi.authme.initialization.factory.SingletonStoreDependencyHandler)2 SecuritySettings (fr.xephi.authme.settings.properties.SecuritySettings)2 BeforeClass (org.junit.BeforeClass)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Property (ch.jalu.configme.properties.Property)1 PropertyResource (ch.jalu.configme.resource.PropertyResource)1 BeforeInjecting (ch.jalu.injector.testing.BeforeInjecting)1 NewAPI (fr.xephi.authme.api.NewAPI)1 CommandHandler (fr.xephi.authme.command.CommandHandler)1 DataSource (fr.xephi.authme.datasource.DataSource)1 PasswordEncryptionEvent (fr.xephi.authme.events.PasswordEncryptionEvent)1 OnStartupTasks (fr.xephi.authme.initialization.OnStartupTasks)1 BlockListener (fr.xephi.authme.listener.BlockListener)1 PermissionsManager (fr.xephi.authme.permission.PermissionsManager)1 Management (fr.xephi.authme.process.Management)1 ProcessSyncPlayerLogin (fr.xephi.authme.process.login.ProcessSyncPlayerLogin)1