Launcher/LaunchServer/src/main/java/pro/gravit/launchserver/auth/AuthProviderPair.java

132 lines
4.8 KiB
Java
Raw Normal View History

package pro.gravit.launchserver.auth;
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;
import pro.gravit.launchserver.auth.handler.AuthHandler;
import pro.gravit.launchserver.auth.provider.AuthProvider;
import pro.gravit.launchserver.auth.texture.TextureProvider;
2019-10-19 19:46:04 +03:00
import java.io.IOException;
2021-05-28 15:13:16 +03:00
import java.util.HashSet;
import java.util.Map;
2021-05-28 15:13:16 +03:00
import java.util.Set;
2019-10-19 19:46:04 +03:00
public class AuthProviderPair {
2020-04-05 10:27:04 +03:00
public final boolean isDefault = true;
public AuthProvider provider;
public AuthHandler handler;
public TextureProvider textureProvider;
2021-05-16 16:07:44 +03:00
public AuthCoreProvider core;
2021-05-28 15:13:16 +03:00
public AuthSocialProvider social;
public Map<String, String> links;
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;
public AuthProviderPair(AuthProvider provider, AuthHandler handler, TextureProvider textureProvider) {
this.provider = provider;
this.handler = handler;
this.textureProvider = textureProvider;
}
public void init(LaunchServer srv, String name) {
2020-04-05 10:27:04 +03:00
this.name = name;
if (links != null) link(srv);
2021-05-25 12:17:29 +03:00
if (core == null) {
2021-05-16 16:07:44 +03:00
if (provider == null) throw new NullPointerException(String.format("Auth %s provider null", name));
if (handler == null) throw new NullPointerException(String.format("Auth %s handler null", name));
2021-05-28 15:13:16 +03:00
if (social != null)
throw new IllegalArgumentException(String.format("Auth %s social can't be used in provider/handler method", name));
2021-05-16 16:07:44 +03:00
provider.init(srv);
handler.init(srv);
} else {
if (provider != null) throw new IllegalArgumentException(String.format("Auth %s provider not null", name));
if (handler != null) throw new IllegalArgumentException(String.format("Auth %s handler not null", name));
core.init(srv);
2021-05-28 15:13:16 +03:00
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
}
}
2020-04-05 10:27:04 +03:00
public void link(LaunchServer srv) {
links.forEach((k, v) -> {
AuthProviderPair pair = srv.config.getAuthProviderPair(v);
2020-04-05 10:27:04 +03:00
if (pair == null) {
throw new NullPointerException(String.format("Auth %s link failed. Pair %s not found", name, v));
}
2020-04-05 10:27:04 +03:00
if ("provider".equals(k)) {
if (pair.provider == null)
throw new NullPointerException(String.format("Auth %s link failed. %s.provider is null", name, v));
provider = pair.provider;
2020-04-05 10:27:04 +03:00
} else if ("handler".equals(k)) {
if (pair.handler == null)
throw new NullPointerException(String.format("Auth %s link failed. %s.handler is null", name, v));
handler = pair.handler;
2020-04-05 10:27:04 +03:00
} else if ("textureProvider".equals(k)) {
if (pair.textureProvider == null)
throw new NullPointerException(String.format("Auth %s link failed. %s.textureProvider is null", name, v));
textureProvider = pair.textureProvider;
2021-05-28 15:13:16 +03:00
} else if ("core".equals(k)) {
if (pair.core == null)
throw new NullPointerException(String.format("Auth %s link failed. %s.core is null", name, v));
core = pair.core;
}
});
}
public void close() throws IOException {
2021-05-28 15:13:16 +03:00
if (social != null) {
social.close();
}
2021-05-25 12:17:29 +03:00
if (core == null) {
2021-05-16 16:07:44 +03:00
provider.close();
handler.close();
} else {
core.close();
}
2021-05-28 15:13:16 +03:00
if (textureProvider != null) {
textureProvider.close();
}
}
public static Set<String> getFeatures(Class<?> clazz) {
Set<String> list = new HashSet<>();
getFeatures(clazz, list);
return list;
}
public static void getFeatures(Class<?> clazz, Set<String> list) {
Features features = clazz.getAnnotation(Features.class);
2021-05-28 17:24:06 +03:00
if (features != null) {
for (Feature feature : features.value()) {
list.add(feature.value());
}
2021-05-28 15:13:16 +03:00
}
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-05-16 16:07:44 +03:00
public boolean isUseCore() {
return core != null;
}
2021-05-28 15:13:16 +03:00
public boolean isUseSocial() {
return core != null && social != null;
}
2021-05-16 16:07:44 +03:00
public boolean isUseProviderAndHandler() {
return !isUseCore();
}
}