Merge pull request #305 from Parabot/feature/hooks-authentication

[FEATURE] Added hooks authentication
This commit is contained in:
Jeroen Ketelaar
2019-05-08 14:32:47 -05:00
committed by GitHub
4 changed files with 39 additions and 9 deletions
@@ -1,24 +1,29 @@
package org.parabot.core.asm.hooks; package org.parabot.core.asm.hooks;
import org.parabot.core.forum.AccountManager;
import org.parabot.core.parsers.hooks.HookParser; import org.parabot.core.parsers.hooks.HookParser;
import org.parabot.core.parsers.hooks.JSONHookParser; import org.parabot.core.parsers.hooks.JSONHookParser;
import org.parabot.core.parsers.hooks.XMLHookParser; import org.parabot.core.parsers.hooks.XMLHookParser;
import org.parabot.environment.api.utils.WebUtil; import org.parabot.environment.api.utils.WebUtil;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
public class HookFile { public class HookFile {
public static final int TYPE_XML = 0; public static final int TYPE_XML = 0;
public static final int TYPE_JSON = 1; public static final int TYPE_JSON = 1;
private URL url; private URL url;
private int type; private int type;
private boolean isLocal;
public HookFile(File file, int type) throws MalformedURLException { public HookFile(File file, int type) throws MalformedURLException {
this(file.toURI().toURL(), type); this(file.toURI().toURL(), type);
this.isLocal = true;
} }
public HookFile(URL url, int type) { public HookFile(URL url, int type) {
@@ -26,17 +31,22 @@ public class HookFile {
this.url = url; this.url = url;
} }
private void setType(int type) {
if (type < 0 || type > 1) {
throw new IllegalArgumentException("This type does not exist");
}
this.type = type;
}
public InputStream getInputStream() { public InputStream getInputStream() {
return WebUtil.getInputStream(url); return WebUtil.getInputStream(url);
} }
public InputStream getInputStream(AccountManager manager) {
if (isLocal) {
return this.getInputStream();
} else {
try {
return WebUtil.getConnection(url, "apikey=" + manager.getAccount().getApi()).getInputStream();
} catch (IOException e) {
return null;
}
}
}
public HookParser getParser() { public HookParser getParser() {
switch (type) { switch (type) {
case TYPE_XML: case TYPE_XML:
@@ -47,4 +57,11 @@ public class HookFile {
return null; return null;
} }
private void setType(int type) {
if (type < 0 || type > 1) {
throw new IllegalArgumentException("This type does not exist");
}
this.type = type;
}
} }
@@ -4,6 +4,7 @@ import org.json.simple.JSONObject;
import org.parabot.core.Configuration; import org.parabot.core.Configuration;
import org.parabot.core.Context; import org.parabot.core.Context;
import org.parabot.core.Core; import org.parabot.core.Core;
import org.parabot.core.parsers.hooks.HookParser;
import org.parabot.core.parsers.scripts.BDNScripts; import org.parabot.core.parsers.scripts.BDNScripts;
import org.parabot.core.parsers.servers.PublicServers; import org.parabot.core.parsers.servers.PublicServers;
import org.parabot.core.ui.components.VerboseLoader; import org.parabot.core.ui.components.VerboseLoader;
@@ -48,6 +49,7 @@ public final class AccountManager {
accessors.add(PublicServers.MANAGER_FETCHER); accessors.add(PublicServers.MANAGER_FETCHER);
accessors.add(PublicServerExecuter.MANAGER_FETCHER); accessors.add(PublicServerExecuter.MANAGER_FETCHER);
accessors.add(PBPreferences.MANAGER_FETCHER); accessors.add(PBPreferences.MANAGER_FETCHER);
accessors.add(HookParser.MANAGER_FETCHER);
for (final AccountManagerAccess accessor : accessors) { for (final AccountManagerAccess accessor : accessors) {
accessor.setManager(instance); accessor.setManager(instance);
@@ -3,6 +3,9 @@ package org.parabot.core.parsers.hooks;
import org.parabot.core.asm.hooks.HookFile; import org.parabot.core.asm.hooks.HookFile;
import org.parabot.core.asm.interfaces.Injectable; import org.parabot.core.asm.interfaces.Injectable;
import org.parabot.core.asm.wrappers.*; import org.parabot.core.asm.wrappers.*;
import org.parabot.core.forum.AccountManager;
import org.parabot.core.forum.AccountManagerAccess;
import org.parabot.environment.api.utils.PBPreferences;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@@ -17,6 +20,14 @@ import java.util.Map;
*/ */
public abstract class HookParser { public abstract class HookParser {
protected static AccountManager manager;
public static final AccountManagerAccess MANAGER_FETCHER = new AccountManagerAccess() {
@Override
public final void setManager(AccountManager manager) {
HookParser.manager = manager;
}
};
public HookParser(HookFile hookFile) { public HookParser(HookFile hookFile) {
} }
@@ -35,7 +35,7 @@ public class XMLHookParser extends HookParser {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory DocumentBuilderFactory dbFactory = DocumentBuilderFactory
.newInstance(); .newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
doc = dBuilder.parse(hookFile.getInputStream()); doc = dBuilder.parse(hookFile.getInputStream(manager));
doc.getDocumentElement().normalize(); doc.getDocumentElement().normalize();
if (!doc.getDocumentElement().getNodeName().equals("injector")) { if (!doc.getDocumentElement().getNodeName().equals("injector")) {
throw new RuntimeException("Incorrect hook file."); throw new RuntimeException("Incorrect hook file.");