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(license, online) support added #125

Closed
wants to merge 14 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
12 changes: 5 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,13 +3,7 @@
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;
Expand Down Expand Up @@ -46,6 +40,8 @@ static AuthPlugin instance() {

AuthenticationStepContextFactoryBucket getAuthenticationContextFactoryBucket();

AuthenticationStepContextFactoryBucket getPremiumAuthenticationContextFactoryBucket();

ConfigurationProcessor getConfigurationProcessor();

LoginManagement getLoginManagement();
Expand All @@ -62,6 +58,8 @@ static AuthPlugin instance() {

AuthenticatingAccountBucket getAuthenticatingAccountBucket();

PendingPremiumAccountBucket getPendingPremiumAccountBucket();

LinkConfirmationBucket getLinkConfirmationBucket();

LinkAuthenticationBucket<LinkEntryUser> getLinkEntryBucket();
Expand Down
4 changes: 4 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 @@ -96,6 +96,8 @@ default void setLastQuitTime(long time) {

void setLastIpAddress(String hostString);

void setIsPremium(boolean newIsPremium);

@Deprecated
default long getLastSessionStart() {
return getLastSessionStartTimestamp();
Expand Down Expand Up @@ -136,6 +138,8 @@ default void logout(long sessionDurability) {

boolean isSessionActive(long sessionDurability);

boolean isPremium();

default KickResultType kick(String reason) {
Optional<ServerPlayer> serverPlayer = getPlayer();
if (!serverPlayer.isPresent())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.bivashy.auth.api.bucket;

import com.bivashy.auth.api.account.Account;
import com.bivashy.auth.api.bucket.PendingPremiumAccountBucket.PendingPremiumAccountState;
import com.bivashy.auth.api.model.PlayerIdSupplier;

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

public interface PendingPremiumAccountBucket extends Bucket<PendingPremiumAccountState> {
default Collection<String> getAccountIdEntries() {
return stream().map(PendingPremiumAccountState::getAccount).map(PlayerIdSupplier::getPlayerId).collect(Collectors.toList());
}

default boolean isPendingPremium(PlayerIdSupplier playerIdSupplier) {
return hasByValue(PendingPremiumAccountState::getPlayerId, playerIdSupplier.getPlayerId());
}

default Optional<Account> getPendingPremiumAccountByName(String username) {
return findFirstByValue(PendingPremiumAccountState::getPlayerId, username).map(PendingPremiumAccountState::getAccount);
}

default Optional<Account> getPendingPremiumAccount(PlayerIdSupplier playerIdSupplier) {
return findFirstByValue(PendingPremiumAccountState::getPlayerId, playerIdSupplier.getPlayerId()).map(PendingPremiumAccountState::getAccount);
}

default Account getPendingPremiumAccountNullable(PlayerIdSupplier playerIdSupplier) {
return getPendingPremiumAccount(playerIdSupplier).orElse(null);
}

default Optional<Long> getEnterTimestamp(PlayerIdSupplier playerIdSupplier) {
return findFirstByValue(PendingPremiumAccountState::getPlayerId, playerIdSupplier.getPlayerId()).map(PendingPremiumAccountState::getEnterTimestamp);
}

default long getEnterTimestampOrZero(PlayerIdSupplier playerIdSupplier) {
return getEnterTimestamp(playerIdSupplier).orElse(0L);
}

void addPendingPremiumAccount(Account account);

void removePendingPremiumAccount(PlayerIdSupplier playerIdSupplier);


interface PendingPremiumAccountState {
default String getPlayerId() {
return getAccount().getPlayerId();
}

Account getAccount();

long getEnterTimestamp();
}
}
17 changes: 17 additions & 0 deletions api/src/main/java/com/bivashy/auth/api/config/PluginConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bivashy.auth.api.config;

import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.IntStream;
Expand Down Expand Up @@ -47,8 +48,20 @@ public interface PluginConfig {

List<String> getAuthenticationSteps();

List<String> getPremiumAuthenticationSteps();

List<String> getDayPluralsStringList();

List<String> getHourPluralsStringList();

List<String> getMinutePluralsStringList();

List<String> getSecondPluralsStringList();

String getAuthenticationStepName(int index);

String getPremiumAuthenticationStepName(int index);

boolean isPasswordConfirmationEnabled();

boolean isPasswordInChatEnabled();
Expand All @@ -69,6 +82,10 @@ public interface PluginConfig {

long getAuthTime();

boolean isLicenseSupportEnabled();

long getLicenseVerifyTimeMillis();

boolean shouldBlockChat();

ServerMessages getServerMessages();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.bivashy.auth.api.event;

import com.bivashy.auth.api.event.base.AccountEvent;
import com.bivashy.auth.api.server.proxy.ProxyServer;
import io.github.revxrsal.eventbus.gen.Index;

/**
* Called when Account(Player) joined sub-server.
*/
public interface AccountServerConnectedEvent extends AccountEvent {

@Index(1)
ProxyServer getServer();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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.server.player.ServerPlayer;
Expand All @@ -10,6 +11,13 @@
* same ip. Or remove
*/
public interface LoginManagement {
/**
* Handle player pre login. Verify/force premium account.
*
* @return should force online mode
*/
void onPreLogin(String username, Consumer<Boolean> continueConnection);

/**
* 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
Expand Up @@ -7,6 +7,7 @@
import java.util.concurrent.TimeUnit;

import com.bivashy.auth.api.AuthPlugin;
import com.bivashy.auth.api.event.AccountServerConnectedEvent;
import com.bivashy.auth.api.event.PlayerChatPasswordEvent;
import com.bivashy.auth.api.server.player.ServerPlayer;

Expand All @@ -15,10 +16,7 @@
import me.mastercapexd.auth.bungee.player.BungeeServerPlayer;
import me.mastercapexd.auth.bungee.server.BungeeServer;
import me.mastercapexd.auth.config.message.server.ServerMessageContext;
import net.md_5.bungee.api.event.ChatEvent;
import net.md_5.bungee.api.event.LoginEvent;
import net.md_5.bungee.api.event.PlayerDisconnectEvent;
import net.md_5.bungee.api.event.ServerConnectEvent;
import net.md_5.bungee.api.event.*;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.event.EventHandler;

Expand All @@ -31,6 +29,16 @@ public AuthenticationListener(AuthPlugin plugin) {
this.plugin = plugin;
}

@EventHandler
public void onPreLogin(PreLoginEvent event) {
event.registerIntent(bungeePlugin);
plugin.getLoginManagement().onPreLogin(event.getConnection().getName(), enforceOnlineMode -> {
if (enforceOnlineMode)
event.getConnection().setOnlineMode(true);
event.completeIntent(bungeePlugin);
});
}

@EventHandler
public void onLogin(LoginEvent event) {
event.registerIntent(bungeePlugin);
Expand Down Expand Up @@ -100,4 +108,14 @@ public void onBlockedServerConnect(ServerConnectEvent event) {
player.sendMessage(plugin.getConfig().getServerMessages().getMessage("disabled-server"));
event.setCancelled(true);
}

@EventHandler
public void onServerConnected(ServerConnectedEvent event) {
plugin.getCore().wrapPlayer(event.getPlayer()).ifPresent(player -> {
plugin.getAccountDatabase().getAccount(player.getPlayerId()).thenAccept(account -> {
plugin.getEventBus().publish(AccountServerConnectedEvent.class, account,
new BungeeServer(event.getServer().getInfo()));
});
});
}
}
11 changes: 11 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<repository>
<id>central</id>
<url>https://repo1.maven.org/maven2</url>
</repository>
</repositories>

<dependencies>
Expand Down Expand Up @@ -171,5 +175,12 @@
<version>${nanolimbo.version}</version>
<scope>provided</scope>
</dependency>

<!-- Joda-Time -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.12.6</version>
</dependency>
</dependencies>
</project>
32 changes: 18 additions & 14 deletions core/src/main/java/me/mastercapexd/auth/BaseAuthPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,7 @@
import com.bivashy.auth.api.asset.resource.Resource;
import com.bivashy.auth.api.asset.resource.impl.FolderResource;
import com.bivashy.auth.api.asset.resource.impl.FolderResourceReader;
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.config.duration.ConfigurationDuration;
import com.bivashy.auth.api.config.server.ConfigurationServer;
Expand All @@ -45,13 +39,7 @@
import io.github.revxrsal.eventbus.EventBus;
import io.github.revxrsal.eventbus.EventBusBuilder;
import me.mastercapexd.auth.account.factory.AuthAccountFactory;
import me.mastercapexd.auth.bucket.BaseAuthenticatingAccountBucket;
import me.mastercapexd.auth.bucket.BaseAuthenticationStepContextFactoryBucket;
import me.mastercapexd.auth.bucket.BaseAuthenticationStepFactoryBucket;
import me.mastercapexd.auth.bucket.BaseAuthenticationTaskBucket;
import me.mastercapexd.auth.bucket.BaseCryptoProviderBucket;
import me.mastercapexd.auth.bucket.BaseLinkAuthenticationBucket;
import me.mastercapexd.auth.bucket.BaseLinkConfirmationBucket;
import me.mastercapexd.auth.bucket.*;
import me.mastercapexd.auth.config.BasePluginConfig;
import me.mastercapexd.auth.config.factory.ConfigurationHolderMapResolverFactory;
import me.mastercapexd.auth.config.resolver.RawURLProviderFieldResolverFactory;
Expand All @@ -73,6 +61,7 @@
import me.mastercapexd.auth.hooks.DiscordHook;
import me.mastercapexd.auth.hooks.TelegramPluginHook;
import me.mastercapexd.auth.link.BaseLinkTypeProvider;
import me.mastercapexd.auth.listener.AccountServerChangedListener;
import me.mastercapexd.auth.listener.AuthenticationAttemptListener;
import me.mastercapexd.auth.listener.AuthenticationChatPasswordListener;
import me.mastercapexd.auth.management.BaseLibraryManagement;
Expand Down Expand Up @@ -107,11 +96,13 @@ public class BaseAuthPlugin implements AuthPlugin {
private final String version;
private final File pluginFolder;
private AuthenticationStepContextFactoryBucket authenticationStepContextFactoryBucket;
private AuthenticationStepContextFactoryBucket premiumAuthenticationStepContextFactoryBucket;
private AudienceProvider audienceProvider;
private LibraryManagement libraryManagement;
private ServerCore core;
private File dataFolder;
private AuthenticatingAccountBucket accountBucket;
private PendingPremiumAccountBucket pendingPremiumBucket;
private EventBus eventBus = EventBusBuilder.asm().executor(Executors.newFixedThreadPool(4)).build();
private GoogleAuthenticator googleAuthenticator;
private PluginConfig config;
Expand Down Expand Up @@ -140,6 +131,7 @@ public BaseAuthPlugin(AudienceProvider audienceProvider, String version, File pl

private void initializeBasic() {
this.accountBucket = new BaseAuthenticatingAccountBucket(this);
this.pendingPremiumBucket = new BasePendingPremiumAccountBucket(this);

this.registerCryptoProviders();
this.registerConfigurationProcessor();
Expand All @@ -153,6 +145,7 @@ private void initializeBasic() {
}

this.authenticationStepContextFactoryBucket = new BaseAuthenticationStepContextFactoryBucket(config.getAuthenticationSteps());
this.premiumAuthenticationStepContextFactoryBucket = new BaseAuthenticationStepContextFactoryBucket(config.getPremiumAuthenticationSteps());
this.accountFactory = new AuthAccountFactory();
this.linkTypeProvider = BaseLinkTypeProvider.allLinks();
this.accountDatabase = new AuthAccountDatabaseProxy(new DatabaseHelper(this));
Expand All @@ -164,6 +157,7 @@ private void initializeBasic() {

this.eventBus.register(new AuthenticationAttemptListener(this));
this.eventBus.register(new AuthenticationChatPasswordListener(this));
this.eventBus.register(new AccountServerChangedListener(this));
}

private void registerAuthenticationSteps() {
Expand Down Expand Up @@ -301,6 +295,11 @@ public AuthenticationStepContextFactoryBucket getAuthenticationContextFactoryBuc
return authenticationStepContextFactoryBucket;
}

@Override
public AuthenticationStepContextFactoryBucket getPremiumAuthenticationContextFactoryBucket() {
return premiumAuthenticationStepContextFactoryBucket;
}

@Override
public ConfigurationProcessor getConfigurationProcessor() {
return configurationProcessor;
Expand Down Expand Up @@ -343,6 +342,11 @@ public AuthenticatingAccountBucket getAuthenticatingAccountBucket() {
return accountBucket;
}

@Override
public PendingPremiumAccountBucket getPendingPremiumAccountBucket() {
return pendingPremiumBucket;
}

@Override
public LinkConfirmationBucket getLinkConfirmationBucket() {
return linkConfirmationBucket;
Expand Down
Loading
Loading