Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Premium Players Support Reworked #197

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions api/src/main/java/com/bivashy/auth/api/AuthPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,19 @@
import java.io.File;

import com.bivashy.auth.api.account.AccountFactory;
import com.bivashy.auth.api.bucket.AuthenticatingAccountBucket;
import com.bivashy.auth.api.bucket.AuthenticationStepContextFactoryBucket;
import com.bivashy.auth.api.bucket.AuthenticationStepFactoryBucket;
import com.bivashy.auth.api.bucket.AuthenticationTaskBucket;
import com.bivashy.auth.api.bucket.CryptoProviderBucket;
import com.bivashy.auth.api.bucket.LinkAuthenticationBucket;
import com.bivashy.auth.api.bucket.LinkConfirmationBucket;
import com.bivashy.auth.api.bucket.*;
import com.bivashy.auth.api.config.PluginConfig;
import com.bivashy.auth.api.database.AccountDatabase;
import com.bivashy.auth.api.hook.PluginHook;
import com.bivashy.auth.api.link.user.entry.LinkEntryUser;
import com.bivashy.auth.api.management.LibraryManagement;
import com.bivashy.auth.api.management.LoginManagement;
import com.bivashy.auth.api.premium.PremiumProvider;
import com.bivashy.auth.api.provider.LinkTypeProvider;
import com.bivashy.auth.api.server.ServerCore;
import com.bivashy.auth.api.util.Castable;
import com.bivashy.configuration.ConfigurationProcessor;
import com.google.gson.Gson;
import com.warrenstrange.googleauth.GoogleAuthenticator;

import io.github.revxrsal.eventbus.EventBus;
Expand All @@ -46,10 +42,14 @@ static AuthPlugin instance() {

AuthenticationStepContextFactoryBucket getAuthenticationContextFactoryBucket();

AuthenticationStepContextFactoryBucket getPremiumAuthenticationContextFactoryBucket();

ConfigurationProcessor getConfigurationProcessor();

LoginManagement getLoginManagement();

PremiumProvider getPremiumProvider();

AuthPlugin setLoginManagement(LoginManagement loginManagement);

LinkTypeProvider getLinkTypeProvider();
Expand All @@ -58,10 +58,14 @@ static AuthPlugin instance() {

AuthPlugin setEventBus(EventBus eventBus);

Gson getGson();

AuthenticationTaskBucket getAuthenticationTaskBucket();

AuthenticatingAccountBucket getAuthenticatingAccountBucket();

PendingLoginBucket getPendingLoginBucket();

LinkConfirmationBucket getLinkConfirmationBucket();

LinkAuthenticationBucket<LinkEntryUser> getLinkEntryBucket();
Expand Down
6 changes: 6 additions & 0 deletions api/src/main/java/com/bivashy/auth/api/account/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ default void setHashType(CryptoProvider cryptoProvider) {

String getName();

void setName(String newName);

HashedPassword getPasswordHash();

void setPasswordHash(HashedPassword passwordHash);
Expand Down Expand Up @@ -136,6 +138,10 @@ default void logout(long sessionDurability) {

boolean isSessionActive(long sessionDurability);

boolean isPremium();

void setPremium(boolean newPremium);

default KickResultType kick(String reason) {
Optional<ServerPlayer> serverPlayer = getPlayer();
if (!serverPlayer.isPresent())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,11 @@ public interface AccountFactory {
long DEFAULT_TELEGRAM_ID = -1;
int DEFAULT_VK_ID = -1;

/**
* @deprecated use {@link #createAccount(String, IdentifierType, UUID, String, CryptoProvider, String, String, boolean)} instead.
*/
@Deprecated
Account createAccount(String id, IdentifierType identifierType, UUID uuid, String name, CryptoProvider cryptoProvider, String passwordHash, String lastIp);

Account createAccount(String id, IdentifierType identifierType, UUID uuid, String name, CryptoProvider cryptoProvider, String passwordHash, String lastIp, boolean isPremium);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.bivashy.auth.api.bucket;

import com.bivashy.auth.api.account.Account;
import com.bivashy.auth.api.bucket.AuthenticatingAccountBucket.AuthenticatingAccountState;
import com.bivashy.auth.api.model.PlayerIdSupplier;

import java.util.Collection;
import java.util.Optional;
import java.util.stream.Collectors;

public interface PendingLoginBucket extends Bucket<PendingLoginBucket.PendingLoginState> {
default boolean hasFailedLogin(String ip, String username) {
return hasByValue(PendingLoginState::getPendingLoginId, ip + username);
}

void addPendingLogin(String ip, String username);

void removePendingLogin(String ip, String username);

interface PendingLoginState {
default String getPendingLoginId() {
return getAddress() + getUsername();
}

String getAddress();
String getUsername();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.bivashy.auth.api.config.link.TelegramSettings;
import com.bivashy.auth.api.config.link.VKSettings;
import com.bivashy.auth.api.config.message.server.ServerMessages;
import com.bivashy.auth.api.config.premium.PremiumSettings;
import com.bivashy.auth.api.config.server.ConfigurationServer;
import com.bivashy.auth.api.crypto.CryptoProvider;
import com.bivashy.auth.api.database.DatabaseConnectionProvider;
Expand All @@ -33,6 +34,10 @@ public interface PluginConfig {

DatabaseConnectionProvider getStorageType();

int getNameMinLength();

int getNameMaxLength();

Pattern getNamePattern();

List<ConfigurationServer> getAuthServers();
Expand All @@ -49,6 +54,8 @@ public interface PluginConfig {

String getAuthenticationStepName(int index);

String getPremiumAuthenticationStepName(int index);

boolean isPasswordConfirmationEnabled();

boolean isPasswordInChatEnabled();
Expand Down Expand Up @@ -81,6 +88,8 @@ public interface PluginConfig {

GoogleAuthenticatorSettings getGoogleAuthenticatorSettings();

PremiumSettings getPremiumSettings();

TelegramSettings getTelegramSettings();

VKSettings getVKSettings();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.bivashy.auth.api.config.premium;

import java.util.List;

public interface PremiumSettings {
boolean isEnabled();

boolean getBlockOfflinePlayersWithPremiumName();

ProfileConflictResolutionStrategy getProfileConflictResolutionStrategy();

List<String> getAuthenticationSteps();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.bivashy.auth.api.config.premium;

public enum ProfileConflictResolutionStrategy {
BLOCK, USE_OFFLINE, OVERWRITE
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bivashy.auth.api.database;

import java.util.Collection;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

import com.bivashy.auth.api.account.Account;
Expand All @@ -12,6 +13,8 @@ public interface AccountDatabase {

CompletableFuture<Account> getAccountFromName(String playerName);

CompletableFuture<Account> getAccountFromUUID(UUID uuid);

@Deprecated
CompletableFuture<Collection<Account>> getAccountsByVKID(Integer id);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.bivashy.auth.api.event.result;

import com.bivashy.auth.api.server.message.ServerComponent;

public class PreLoginResult {
private final PreLoginState state;
private ServerComponent disconnectMessage;

public PreLoginResult(PreLoginState state, ServerComponent disconnectMessage) {
this.state = state;
this.disconnectMessage = disconnectMessage;
}

public PreLoginResult(PreLoginState state) {
this.state = state;
}

public PreLoginState getState() {
return state;
}

public ServerComponent getDisconnectMessage() {
return disconnectMessage;
}

public enum PreLoginState {
FORCE_ONLINE,
FORCE_OFFLINE,
DENIED
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package com.bivashy.auth.api.management;

import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;

import com.bivashy.auth.api.account.Account;
import com.bivashy.auth.api.event.result.PreLoginResult;
import com.bivashy.auth.api.server.player.ServerPlayer;

/**
* Class that manages player login. For example send player to the server if he has session. Or block if player trying to join multiple accounts with
* same ip. Or remove
*/
public interface LoginManagement {
/**
* Handle player pre login. Check for premium UUID and
* force online/offline mode.
*/
void onPreLogin(String ip, String username, Consumer<PreLoginResult> continuation);

/**
* Handle player join. Start authentication/registration/session process.
* On BungeeCord this will use LoginEvent and player from "connection".
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.bivashy.auth.api.premium;

/**
* An exception that is thrown when fetching premium data fails.
*
* @author kyngs
*/
public class PremiumException extends Exception {

/**
* The issue that caused this exception.
*/
private final Issue issue;

/**
* Construct a new PremiumException with the given Issue and Exception.
*
* @param issue The issue related to the exception.
* @param exception The exception that caused the issue.
*/
public PremiumException(Issue issue, Exception exception) {
super(exception);
this.issue = issue;
}

/**
* Construct a new PremiumException with the given Issue and message.
*
* @param issue The issue related to the exception.
* @param message The message describing the exception.
*/
public PremiumException(Issue issue, String message) {
super(message);
this.issue = issue;
}

/**
* Gets the issue that caused this exception.
*
* @return the issue that caused this exception
*/
public Issue getIssue() {
return issue;
}

/**
* Possible issues that can cause this exception.
*/
public enum Issue {
/**
* The API throttled the request.
*/
THROTTLED,
/**
* The API returned an invalid response.
*/
SERVER_EXCEPTION,
/**
* Other issues.
*/
UNDEFINED
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.bivashy.auth.api.premium;

import java.util.UUID;

public interface PremiumProvider {

/**
* This method fetches a user by their username.
*
* @param name The username of the user.
* @return The user, or null if the user does not exist.
* @throws PremiumException If the user could not be fetched.
*/
UUID getPremiumUUIDForName(String name) throws PremiumException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,7 @@ default void sendMessage(String message) {

Optional<ProxyServer> getCurrentServer();

boolean isOnlineMode();

<T> T getRealPlayer();
}
Loading
Loading