Search in sources :

Example 1 with PasswordEncryptionEvent

use of fr.xephi.authme.events.PasswordEncryptionEvent 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 2 with PasswordEncryptionEvent

use of fr.xephi.authme.events.PasswordEncryptionEvent in project AuthMeReloaded by AuthMe.

the class PasswordSecurity method initializeEncryptionMethodWithEvent.

/**
 * Get the encryption method from the given {@link HashAlgorithm} value and emit a
 * {@link PasswordEncryptionEvent}. The encryption method from the event is then returned,
 * which may have been changed by an external listener.
 *
 * @param algorithm  The algorithm to retrieve the encryption method for
 *
 * @return The encryption method
 */
private EncryptionMethod initializeEncryptionMethodWithEvent(HashAlgorithm algorithm) {
    EncryptionMethod method = initializeEncryptionMethod(algorithm);
    PasswordEncryptionEvent event = new PasswordEncryptionEvent(method);
    pluginManager.callEvent(event);
    return event.getMethod();
}
Also used : PasswordEncryptionEvent(fr.xephi.authme.events.PasswordEncryptionEvent) EncryptionMethod(fr.xephi.authme.security.crypts.EncryptionMethod)

Aggregations

PasswordEncryptionEvent (fr.xephi.authme.events.PasswordEncryptionEvent)2 Injector (ch.jalu.injector.Injector)1 InjectorBuilder (ch.jalu.injector.InjectorBuilder)1 BeforeInjecting (ch.jalu.injector.testing.BeforeInjecting)1 EncryptionMethod (fr.xephi.authme.security.crypts.EncryptionMethod)1 Event (org.bukkit.event.Event)1 BeforeClass (org.junit.BeforeClass)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1