2019-06-02 05:03:08 +03:00
|
|
|
package pro.gravit.launchserver.auth;
|
2019-03-22 09:14:29 +03:00
|
|
|
|
2021-10-12 12:55:32 +03:00
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
import org.apache.logging.log4j.Logger;
|
2019-06-03 11:50:28 +03:00
|
|
|
import pro.gravit.launchserver.LaunchServer;
|
2021-05-16 16:07:44 +03:00
|
|
|
import pro.gravit.launchserver.auth.core.AuthCoreProvider;
|
2021-05-28 15:13:16 +03:00
|
|
|
import pro.gravit.launchserver.auth.core.AuthSocialProvider;
|
2021-10-12 12:55:32 +03:00
|
|
|
import pro.gravit.launchserver.auth.core.MySQLCoreProvider;
|
2019-06-02 05:03:08 +03:00
|
|
|
import pro.gravit.launchserver.auth.texture.TextureProvider;
|
2019-03-22 09:14:29 +03:00
|
|
|
|
2019-10-19 19:46:04 +03:00
|
|
|
import java.io.IOException;
|
2021-05-28 15:13:16 +03:00
|
|
|
import java.util.HashSet;
|
2020-01-19 09:50:48 +03:00
|
|
|
import java.util.Map;
|
2021-05-28 15:13:16 +03:00
|
|
|
import java.util.Set;
|
2019-10-19 19:46:04 +03:00
|
|
|
|
2021-06-15 15:24:28 +03:00
|
|
|
public final class AuthProviderPair {
|
2021-10-12 12:55:32 +03:00
|
|
|
private transient final Logger logger = LogManager.getLogger();
|
2021-06-15 15:24:28 +03:00
|
|
|
public boolean isDefault = true;
|
2021-06-16 12:35:55 +03:00
|
|
|
public AuthCoreProvider core;
|
|
|
|
public AuthSocialProvider social;
|
2020-01-19 09:50:48 +03:00
|
|
|
public TextureProvider textureProvider;
|
|
|
|
public Map<String, String> links;
|
2020-02-11 08:08:35 +03:00
|
|
|
public transient String name;
|
2021-05-28 15:13:16 +03:00
|
|
|
public transient Set<String> features;
|
2019-04-13 20:55:01 +03:00
|
|
|
public String displayName;
|
2021-10-12 12:55:32 +03:00
|
|
|
private transient boolean warnOAuthShow = false;
|
2019-03-22 09:14:29 +03:00
|
|
|
|
2021-06-16 12:35:55 +03:00
|
|
|
public AuthProviderPair(AuthCoreProvider core, TextureProvider textureProvider) {
|
|
|
|
this.core = core;
|
|
|
|
this.textureProvider = textureProvider;
|
|
|
|
}
|
|
|
|
|
|
|
|
public AuthProviderPair(AuthCoreProvider core, AuthSocialProvider social) {
|
|
|
|
this.core = core;
|
|
|
|
this.social = social;
|
|
|
|
}
|
|
|
|
|
|
|
|
public AuthProviderPair(AuthCoreProvider core, AuthSocialProvider social, TextureProvider textureProvider) {
|
|
|
|
this.core = core;
|
|
|
|
this.social = social;
|
|
|
|
this.textureProvider = textureProvider;
|
|
|
|
}
|
|
|
|
|
2021-07-21 19:43:47 +03:00
|
|
|
public static Set<String> getFeatures(Class<?> clazz) {
|
|
|
|
Set<String> list = new HashSet<>();
|
|
|
|
getFeatures(clazz, list);
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2021-10-12 12:55:32 +03:00
|
|
|
public void internalShowOAuthWarnMessage() {
|
|
|
|
if(!warnOAuthShow) {
|
|
|
|
if(!(core instanceof MySQLCoreProvider)) { // MySQL upgraded later
|
|
|
|
logger.warn("AuthCoreProvider {} ({}) not supported OAuth. Legacy session system may be removed in next release", name, core.getClass().getName());
|
|
|
|
}
|
|
|
|
warnOAuthShow = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-21 19:43:47 +03:00
|
|
|
public static void getFeatures(Class<?> clazz, Set<String> list) {
|
|
|
|
Features features = clazz.getAnnotation(Features.class);
|
|
|
|
if (features != null) {
|
|
|
|
for (Feature feature : features.value()) {
|
|
|
|
list.add(feature.value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Class<?> superClass = clazz.getSuperclass();
|
|
|
|
if (superClass != null && superClass != Object.class) {
|
|
|
|
getFeatures(superClass, list);
|
|
|
|
}
|
|
|
|
Class<?>[] interfaces = clazz.getInterfaces();
|
|
|
|
for (Class<?> i : interfaces) {
|
|
|
|
getFeatures(i, list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-15 15:24:28 +03:00
|
|
|
public final <T> T isSupport(Class<T> clazz) {
|
|
|
|
if (core == null) return null;
|
2021-06-16 12:35:55 +03:00
|
|
|
T result = null;
|
|
|
|
if (social != null) result = social.isSupport(clazz);
|
|
|
|
if (result == null) result = core.isSupport(clazz);
|
|
|
|
return result;
|
2021-06-15 15:24:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public final void init(LaunchServer srv, String name) {
|
2020-04-05 10:27:04 +03:00
|
|
|
this.name = name;
|
|
|
|
if (links != null) link(srv);
|
2021-09-22 08:45:48 +03:00
|
|
|
core.init(srv);
|
|
|
|
features = new HashSet<>();
|
|
|
|
getFeatures(core.getClass(), features);
|
|
|
|
if (social != null) {
|
|
|
|
social.init(srv, core);
|
|
|
|
getFeatures(social.getClass(), features);
|
2021-05-16 16:07:44 +03:00
|
|
|
}
|
2019-03-22 09:14:29 +03:00
|
|
|
}
|
2020-04-05 10:27:04 +03:00
|
|
|
|
2021-06-15 15:24:28 +03:00
|
|
|
public final void link(LaunchServer srv) {
|
2020-04-05 10:27:04 +03:00
|
|
|
links.forEach((k, v) -> {
|
2020-01-19 09:50:48 +03:00
|
|
|
AuthProviderPair pair = srv.config.getAuthProviderPair(v);
|
2020-04-05 10:27:04 +03:00
|
|
|
if (pair == null) {
|
2020-01-19 09:50:48 +03:00
|
|
|
throw new NullPointerException(String.format("Auth %s link failed. Pair %s not found", name, v));
|
|
|
|
}
|
2021-09-22 08:45:48 +03:00
|
|
|
if ("core".equals(k)) {
|
2021-05-28 15:13:16 +03:00
|
|
|
if (pair.core == null)
|
|
|
|
throw new NullPointerException(String.format("Auth %s link failed. %s.core is null", name, v));
|
|
|
|
core = pair.core;
|
2020-01-19 09:50:48 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-03-22 09:14:29 +03:00
|
|
|
|
2021-06-15 15:24:28 +03:00
|
|
|
public final void close() throws IOException {
|
2021-05-28 15:13:16 +03:00
|
|
|
if (social != null) {
|
|
|
|
social.close();
|
|
|
|
}
|
2021-09-22 08:45:48 +03:00
|
|
|
core.close();
|
2021-05-28 15:13:16 +03:00
|
|
|
if (textureProvider != null) {
|
|
|
|
textureProvider.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-15 15:24:28 +03:00
|
|
|
public final boolean isUseSocial() {
|
2021-05-28 15:13:16 +03:00
|
|
|
return core != null && social != null;
|
|
|
|
}
|
2019-03-22 09:14:29 +03:00
|
|
|
}
|