mirror of
https://gitee.com/BDWare/cp.git
synced 2025-04-27 14:42:15 +00:00
add Access
This commit is contained in:
parent
07174bb3b4
commit
05e301d9e9
@ -6,7 +6,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
group = "org.bdware.sc"
|
group = "org.bdware.sc"
|
||||||
version = "1.7.1"
|
version = "1.7.2"
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
mavenLocal()
|
mavenLocal()
|
||||||
@ -61,8 +61,8 @@ jar {
|
|||||||
// uncomment this when publish,
|
// uncomment this when publish,
|
||||||
//while develop at local use "false"
|
//while develop at local use "false"
|
||||||
configurations.runtimeClasspath.filter {
|
configurations.runtimeClasspath.filter {
|
||||||
it.getAbsolutePath().contains("/lib/")
|
// it.getAbsolutePath().contains("/lib/")
|
||||||
// false
|
false
|
||||||
}.collect {
|
}.collect {
|
||||||
it.isDirectory() ? it : zipTree(it)
|
it.isDirectory() ? it : zipTree(it)
|
||||||
}
|
}
|
||||||
|
@ -63,7 +63,8 @@ public class DebugMain {
|
|||||||
= new ResultCallback() {
|
= new ResultCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void onResult(String str) {
|
public void onResult(String str) {
|
||||||
LOGGER.debug(str);
|
|
||||||
|
LOGGER.info("[PrintCB] " + str);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
ContractProcess.instance.handler.setDBInfo(wrap("", config.dbPath), printCallback);
|
ContractProcess.instance.handler.setDBInfo(wrap("", config.dbPath), printCallback);
|
||||||
@ -80,7 +81,7 @@ public class DebugMain {
|
|||||||
Map<String, Object> resp = HttpUtil.httpGet(url);
|
Map<String, Object> resp = HttpUtil.httpGet(url);
|
||||||
|
|
||||||
String data = (String) resp.get("response");
|
String data = (String) resp.get("response");
|
||||||
// LOGGER.info(JsonUtil.toPrettyJson(JsonUtil.parseStringAsJsonObject(data)));
|
LOGGER.info(JsonUtil.toPrettyJson(JsonUtil.parseStringAsJsonObject(data)));
|
||||||
LOGGER.info("start done!");
|
LOGGER.info("start done!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -553,14 +553,16 @@ public class ContractProcess {
|
|||||||
if (fun.isConfidential()) {
|
if (fun.isConfidential()) {
|
||||||
fun.appendBeforeInvokeHandler(new ConfidentialHandler(fun));
|
fun.appendBeforeInvokeHandler(new ConfidentialHandler(fun));
|
||||||
}
|
}
|
||||||
|
|
||||||
ArgSchemaHandler argSchemaHandler = createHandlerIfExist(fun, fun.annotations, ArgSchemaHandler.class);
|
ArgSchemaHandler argSchemaHandler = createHandlerIfExist(fun, fun.annotations, ArgSchemaHandler.class);
|
||||||
if (argSchemaHandler != null) {
|
if (argSchemaHandler != null) {
|
||||||
fun.appendBeforeInvokeHandler(argSchemaHandler);
|
fun.appendBeforeInvokeHandler(argSchemaHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fun.isExport()) {
|
if (fun.isExport()) {
|
||||||
//if(fun.annotations...)
|
//if(fun.annotations...)
|
||||||
|
AccessHandler accessHandler = createHandlerIfExist(fun,fun.annotations,AccessHandler.class);
|
||||||
|
if (accessHandler != null) {
|
||||||
|
fun.appendBeforeInvokeHandler(accessHandler);
|
||||||
|
}
|
||||||
fun.appendAfterInvokeHandler(new ObjToJsonHandler());
|
fun.appendAfterInvokeHandler(new ObjToJsonHandler());
|
||||||
// fun.appendBeforeInvokeHandler(new ReadMeHandler());
|
// fun.appendBeforeInvokeHandler(new ReadMeHandler());
|
||||||
// Mask是用于返回真正结果之后,做一些偏移,以保护数据隐私。
|
// Mask是用于返回真正结果之后,做一些偏移,以保护数据隐私。
|
||||||
|
@ -3,13 +3,14 @@ package org.bdware.sc.compiler.ap;
|
|||||||
import org.bdware.sc.compiler.AnnotationProcessor;
|
import org.bdware.sc.compiler.AnnotationProcessor;
|
||||||
import org.bdware.sc.node.AnnotationNode;
|
import org.bdware.sc.node.AnnotationNode;
|
||||||
import org.bdware.sc.node.ContractNode;
|
import org.bdware.sc.node.ContractNode;
|
||||||
|
import org.bdware.sc.node.FunctionNode;
|
||||||
|
|
||||||
public class Access extends AnnotationProcessor {
|
public class Access extends AnnotationProcessor {
|
||||||
@Override
|
@Override
|
||||||
public void processContract(AnnotationNode anno, ContractNode contractNode) {
|
public void processContract(AnnotationNode anno, ContractNode contractNode) {
|
||||||
contractNode.sigRequired = false;
|
for (FunctionNode fn : contractNode.getFunctions()) {
|
||||||
if (anno != null) {
|
fn.addAnnotation(anno);
|
||||||
contractNode.sigRequired = "\"verified\"".equals(anno.getArgs().get(0));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -607,15 +607,14 @@ public class DesktopEngine extends JSEngine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Object executeWithoutLock(FunctionNode fun, ContractRequest input) throws ScriptException, NoSuchMethodException {
|
private Object executeWithoutLock(FunctionNode fun, ContractRequest input) throws ScriptException, NoSuchMethodException {
|
||||||
Object ret = null;
|
|
||||||
// long start = System.currentTimeMillis();
|
// long start = System.currentTimeMillis();
|
||||||
|
ArgPacks argPacks = new ArgPacks(input, input.getArg(), null);
|
||||||
for (AnnotationHook handler : fun.beforeExecutionAnnotations()) {
|
for (AnnotationHook handler : fun.beforeExecutionAnnotations()) {
|
||||||
ret = handler.handle(this, new ArgPacks(input, input.getArg(), ret));
|
argPacks = handler.handle(this, argPacks);
|
||||||
}
|
}
|
||||||
// actually invoke!
|
// actually invoke!
|
||||||
if (ret == null) {
|
if (argPacks.ret == null) {
|
||||||
ret =
|
argPacks.ret =
|
||||||
engine.invokeFunction(
|
engine.invokeFunction(
|
||||||
input.getAction(),
|
input.getAction(),
|
||||||
(fun.isHandler()
|
(fun.isHandler()
|
||||||
@ -624,13 +623,13 @@ public class DesktopEngine extends JSEngine {
|
|||||||
input.getRequester(),
|
input.getRequester(),
|
||||||
input.getRequesterDOI());
|
input.getRequesterDOI());
|
||||||
}
|
}
|
||||||
|
|
||||||
for (AnnotationHook handler : fun.afterExecutionAnnotations()) {
|
for (AnnotationHook handler : fun.afterExecutionAnnotations()) {
|
||||||
//Mask在after裏面
|
//Mask在after裏面
|
||||||
//System.out.println("afterHook"+contract.Mask);
|
//System.out.println("afterHook"+contract.Mask);
|
||||||
|
argPacks = handler.handle(this, argPacks);
|
||||||
ret = handler.handle(this, new ArgPacks(input, input.getArg(), ret));
|
|
||||||
}
|
}
|
||||||
return ret;
|
return argPacks.ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String extractException(String msg, List<String> stack) {
|
private String extractException(String msg, List<String> stack) {
|
||||||
|
78
src/main/java/org/bdware/sc/engine/hook/AccessHandler.java
Normal file
78
src/main/java/org/bdware/sc/engine/hook/AccessHandler.java
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
package org.bdware.sc.engine.hook;
|
||||||
|
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import com.google.gson.JsonParser;
|
||||||
|
import org.bdware.sc.JSEngine;
|
||||||
|
import org.bdware.sc.bean.ContractRequest;
|
||||||
|
import org.bdware.sc.boundry.ScriptReturnException;
|
||||||
|
import org.bdware.sc.engine.DesktopEngine;
|
||||||
|
import org.bdware.sc.engine.JSONTool;
|
||||||
|
import org.bdware.sc.node.AnnotationHook;
|
||||||
|
import org.bdware.sc.node.AnnotationNode;
|
||||||
|
import org.bdware.sc.node.ArgPacks;
|
||||||
|
import org.bdware.sc.node.FunctionNode;
|
||||||
|
import wrp.jdk.nashorn.internal.objects.Global;
|
||||||
|
import wrp.jdk.nashorn.internal.runtime.Context;
|
||||||
|
import wrp.jdk.nashorn.internal.runtime.PropertyMap;
|
||||||
|
import wrp.jdk.nashorn.internal.scripts.JO;
|
||||||
|
|
||||||
|
@YJSAnnotation(name = "Access")
|
||||||
|
public class AccessHandler implements AnnotationHook {
|
||||||
|
public String acFunction;
|
||||||
|
public boolean requireSign;
|
||||||
|
|
||||||
|
public AccessHandler(AnnotationNode annoNode) {
|
||||||
|
requireSign = false;
|
||||||
|
String content = annoNode.getArgs().get(0);
|
||||||
|
JsonElement je = JsonParser.parseString(content);
|
||||||
|
if (je.isJsonPrimitive() && je.getAsJsonPrimitive().getAsString().equals("verified")) {
|
||||||
|
requireSign = true;
|
||||||
|
}
|
||||||
|
if (je.isJsonObject()) {
|
||||||
|
acFunction = je.getAsJsonObject().get("ACFunction").getAsString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static AccessHandler fromAnnotationNode(FunctionNode funNode, AnnotationNode annoNode) {
|
||||||
|
return new AccessHandler(annoNode);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ArgPacks handle(JSEngine desktopEngine, ArgPacks argPacks) {
|
||||||
|
if (requireSign) {
|
||||||
|
if (!argPacks.request.verifySignature()) {
|
||||||
|
throw new ScriptReturnException(JsonParser.parseString("{\"code\":400,\"msg\":\"permission denied\"}"));
|
||||||
|
}
|
||||||
|
return argPacks;
|
||||||
|
}
|
||||||
|
if (acFunction == null) return argPacks;
|
||||||
|
DesktopEngine de = (DesktopEngine) desktopEngine;
|
||||||
|
Global oldGlobal = Context.getGlobal();
|
||||||
|
Global newGlobal = de.getDesktopGlobal();
|
||||||
|
boolean globalChanged = (oldGlobal != newGlobal);
|
||||||
|
try {
|
||||||
|
if (globalChanged) {
|
||||||
|
Context.setGlobal(newGlobal);
|
||||||
|
}
|
||||||
|
ContractRequest input = argPacks.request;
|
||||||
|
JO jo = new JO(PropertyMap.newMap());
|
||||||
|
jo.put("requester", input.getRequester(), false);
|
||||||
|
jo.put("action", input.getAction(), false);
|
||||||
|
jo.put("arg", JSONTool.convertJsonElementToMirror(input.getArg()), false);
|
||||||
|
de.engine.invokeFunction(acFunction, jo);
|
||||||
|
return argPacks;
|
||||||
|
} catch (ScriptReturnException e) {
|
||||||
|
throw e;
|
||||||
|
} catch (Exception e) {
|
||||||
|
JsonObject jo = new JsonObject();
|
||||||
|
jo.addProperty("code", "401");
|
||||||
|
jo.addProperty("msg", "access check meets exception! " + e);
|
||||||
|
throw new ScriptReturnException(jo);
|
||||||
|
} finally {
|
||||||
|
if (globalChanged) Context.setGlobal(oldGlobal);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -29,7 +29,6 @@ public class ArgSchemaHandler implements AnnotationHook {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static ArgSchemaHandler fromAnnotationNode(FunctionNode funNode, AnnotationNode annoNode) {
|
public static ArgSchemaHandler fromAnnotationNode(FunctionNode funNode, AnnotationNode annoNode) {
|
||||||
//a= annoNode;
|
|
||||||
return new ArgSchemaHandler(annoNode);
|
return new ArgSchemaHandler(annoNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user