-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
333a9e0
commit 8dfb04c
Showing
9 changed files
with
213 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
allprojects { | ||
group 'com.azuriom' | ||
version '1.0.2' | ||
version '1.1.0' | ||
} | ||
|
||
subprojects { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
bukkit/src/main/java/com/azuriom/azlink/bukkit/integrations/AuthMeIntegration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
package com.azuriom.azlink.bukkit.integrations; | ||
|
||
import com.azuriom.azlink.bukkit.AzLinkBukkitPlugin; | ||
import fr.xephi.authme.api.v3.AuthMeApi; | ||
import fr.xephi.authme.api.v3.AuthMePlayer; | ||
import fr.xephi.authme.datasource.DataSource; | ||
import fr.xephi.authme.events.EmailChangedEvent; | ||
import fr.xephi.authme.events.PasswordEncryptionEvent; | ||
import fr.xephi.authme.events.RegisterEvent; | ||
import fr.xephi.authme.security.crypts.EncryptionMethod; | ||
import fr.xephi.authme.security.crypts.HashedPassword; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.Event; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.EventPriority; | ||
import org.bukkit.event.Listener; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.Field; | ||
import java.net.InetAddress; | ||
import java.net.InetSocketAddress; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class AuthMeIntegration implements Listener { | ||
|
||
private final Map<String, String> passwords = new HashMap<>(); | ||
|
||
private final AzLinkBukkitPlugin plugin; | ||
private DataSource dataSource; | ||
|
||
public AuthMeIntegration(AzLinkBukkitPlugin plugin) { | ||
this.plugin = plugin; | ||
|
||
this.plugin.getLoggerAdapter().info("AuthMe integration enabled."); | ||
|
||
this.plugin.getServer().getScheduler().runTask(this.plugin, () -> { | ||
try { | ||
Field field = AuthMeApi.class.getDeclaredField("dataSource"); | ||
field.setAccessible(true); | ||
|
||
this.dataSource = (DataSource) field.get(AuthMeApi.getInstance()); | ||
} catch (ReflectiveOperationException e) { | ||
throw new UnsupportedOperationException("Unable to register AuthMe integration", e); | ||
} | ||
}); | ||
} | ||
|
||
@EventHandler(priority = EventPriority.HIGHEST) | ||
public void onPasswordEncryption(PasswordEncryptionEvent event) { | ||
event.setMethod(new ForwardingEncryptionMethod(event.getMethod())); | ||
} | ||
|
||
@EventHandler | ||
public void onEmailChanged(EmailChangedEvent event) { | ||
Player player = event.getPlayer(); | ||
|
||
runAsync(event, () -> { | ||
try { | ||
this.plugin.getPlugin().getHttpClient().updateEmail(player.getUniqueId(), event.getNewEmail()); | ||
} catch (IOException e) { | ||
this.plugin.getLoggerAdapter().error("Unable to update email for " + player.getName(), e); | ||
} | ||
}); | ||
} | ||
|
||
@EventHandler | ||
public void onRegister(RegisterEvent event) { | ||
Player player = event.getPlayer(); | ||
HashedPassword hashedPassword = this.dataSource.getPassword(event.getPlayer().getName()); | ||
String password = this.passwords.remove(hashedPassword.getHash()); | ||
String email = AuthMeApi.getInstance().getPlayerInfo(player.getName()) | ||
.flatMap(AuthMePlayer::getEmail) | ||
.orElse(null); | ||
InetSocketAddress address = player.getAddress(); | ||
InetAddress ip = address != null ? address.getAddress() : null; | ||
|
||
if (password == null) { | ||
this.plugin.getLoggerAdapter().warn("Unable to get password of " + player.getName()); | ||
return; | ||
} | ||
|
||
runAsync(event, () -> { | ||
try { | ||
this.plugin.getPlugin() | ||
.getHttpClient() | ||
.registerUser(player.getName(), email, player.getUniqueId(), password, ip); | ||
} catch (IOException e) { | ||
this.plugin.getLoggerAdapter().error("Unable to register " + player.getName(), e); | ||
} | ||
}); | ||
} | ||
|
||
private void runAsync(Event event, Runnable runnable) { | ||
if (event.isAsynchronous()) { | ||
runnable.run(); | ||
return; | ||
} | ||
|
||
this.plugin.getSchedulerAdapter().executeAsync(runnable); | ||
} | ||
|
||
public class ForwardingEncryptionMethod implements EncryptionMethod { | ||
|
||
private final EncryptionMethod method; | ||
|
||
public ForwardingEncryptionMethod(EncryptionMethod method) { | ||
this.method = method; | ||
} | ||
|
||
@Override | ||
public HashedPassword computeHash(String password, String name) { | ||
HashedPassword hash = this.method.computeHash(password, name); | ||
AuthMeIntegration.this.passwords.put(hash.getHash(), password); | ||
return hash; | ||
} | ||
|
||
@Override | ||
public String computeHash(String password, String salt, String name) { | ||
String hash = this.method.computeHash(password, salt, name); | ||
AuthMeIntegration.this.passwords.put(hash, password); | ||
return hash; | ||
} | ||
|
||
@Override | ||
public boolean comparePassword(String password, HashedPassword hashedPassword, String name) { | ||
return this.method.comparePassword(password, hashedPassword, name); | ||
} | ||
|
||
@Override | ||
public String generateSalt() { | ||
return this.method.generateSalt(); | ||
} | ||
|
||
@Override | ||
public boolean hasSeparateSalt() { | ||
return this.method.hasSeparateSalt(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
return this.method.equals(o); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return this.method.hashCode(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters