1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
package moe.yuuta.dn42peering.admin;
import io.vertx.core.Vertx;
import io.vertx.core.http.Cookie;
import io.vertx.core.impl.logging.Logger;
import io.vertx.core.impl.logging.LoggerFactory;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.common.template.TemplateEngine;
import io.vertx.ext.web.handler.BasicAuthHandler;
import io.vertx.ext.web.handler.BodyHandler;
import io.vertx.ext.web.handler.SessionHandler;
import io.vertx.ext.web.sstore.LocalSessionStore;
import io.vertx.ext.web.templ.freemarker.FreeMarkerTemplateEngine;
import io.vertx.ext.web.validation.RequestParameters;
import io.vertx.ext.web.validation.RequestPredicate;
import io.vertx.ext.web.validation.ValidationHandler;
import io.vertx.ext.web.validation.builder.Bodies;
import io.vertx.json.schema.SchemaParser;
import io.vertx.json.schema.SchemaRouter;
import io.vertx.json.schema.SchemaRouterOptions;
import io.vertx.json.schema.common.dsl.ObjectSchemaBuilder;
import moe.yuuta.dn42peering.admin.asn.ASNHandler;
import moe.yuuta.dn42peering.admin.nodes.NodeHandler;
import moe.yuuta.dn42peering.asn.IASNService;
import moe.yuuta.dn42peering.manage.AdminASNAuthProvider;
import moe.yuuta.dn42peering.node.INodeService;
import moe.yuuta.dn42peering.peer.IPeerService;
import moe.yuuta.dn42peering.portal.ISubRouter;
import javax.annotation.Nonnull;
import java.util.UUID;
import static io.vertx.json.schema.common.dsl.Schemas.objectSchema;
import static io.vertx.json.schema.common.dsl.Schemas.stringSchema;
public class AdminHandler implements ISubRouter {
private final Logger logger = LoggerFactory.getLogger(getClass().getSimpleName());
@Nonnull
@Override
public Router mount(@Nonnull Vertx vertx) {
final IASNService asnService = IASNService.createProxy(vertx, IASNService.ADDRESS);
final INodeService nodeService = INodeService.createProxy(vertx);
final IPeerService peerService = IPeerService.createProxy(vertx);
final TemplateEngine engine = FreeMarkerTemplateEngine.create(vertx, "ftlh");
final Router router = Router.router(vertx);
router.post().handler(BodyHandler.create().setBodyLimit(100 * 1024));
router.route().handler(SessionHandler.create(LocalSessionStore.create(vertx)));
router.route().handler(
BasicAuthHandler.create(
new AdminASNAuthProvider(vertx.getOrCreateContext()
.config()
.getString("admin", UUID.randomUUID().toString()),
asnService), "admin portal"));
router.get("/")
.produces("text/html")
.handler(ctx -> {
final String asn = ctx.user().principal().getString("username");
AdminUI.renderIndex(
engine,
asnService,
peerService,
nodeService,
asn,
ctx
);
});
router.get("/sudo")
.produces("text/html")
.handler(ctx -> {
final String asn = ctx.user().principal().getString("username");
final Cookie cookie = ctx.getCookie(SudoUtils.SUDO_COOKIE);
AdminUI.renderSudo(engine, asn, null,
cookie == null ? null : SudoUtils.getTargetASN(cookie), ctx);
});
final ObjectSchemaBuilder registerSchema = objectSchema()
.allowAdditionalProperties(false)
.property("asn", stringSchema());
final SchemaParser parser = SchemaParser.createDraft7SchemaParser(
SchemaRouter.create(vertx, new SchemaRouterOptions()));
router.post("/sudo")
.handler(BodyHandler.create().setBodyLimit(100 * 1024))
.handler(ValidationHandler
.builder(parser)
.body(Bodies.formUrlEncoded(registerSchema))
.predicate(RequestPredicate.BODY_REQUIRED)
.build())
.handler(ctx -> {
final String asn = ctx.user().principal().getString("username");
final JsonObject parameters = ctx.<RequestParameters>get(ValidationHandler.REQUEST_CONTEXT_KEY)
.body().getJsonObject();
final String targetASN = parameters.getString("asn");
if (asn == null || asn.equals("")) {
// Clear
ctx.removeCookie(SudoUtils.SUDO_COOKIE);
ctx.response()
.setStatusCode(303)
.putHeader("Location", "/admin")
.end();
} else {
ctx.addCookie(Cookie.cookie(SudoUtils.SUDO_COOKIE, targetASN).setPath("/"));
ctx.response()
.setStatusCode(303)
.putHeader("Location", "/manage")
.end();
}
});
router.mountSubRouter("/nodes", new NodeHandler().mount(vertx));
router.mountSubRouter("/asn", new ASNHandler().mount(vertx));
return router;
}
}
|