1d9b8a0922af1b8699e7232fd078b5f244a66f8e
[websub-hub] / src / service.js
1 'use strict';
2
3 /**
4 * Here we extend the base API server to define our routes and any route-specific
5 * behavior (middlewares) before handing off to the manager.
6 */
7
8 const { Dingus } = require('@squeep/api-dingus');
9 const common = require('./common');
10 const Enum = require('./enum');
11 const Manager = require('./manager');
12 const SessionManager = require('./session-manager');
13 const Authenticator = require('./authenticator');
14 const path = require('path');
15
16 const _fileScope = common.fileScope(__filename);
17
18 class Service extends Dingus {
19 constructor(logger, db, options) {
20 super(logger, {
21 ...options.dingus,
22 ignoreTrailingSlash: false,
23 });
24
25 this.manager = new Manager(logger, db, options);
26 this.authenticator = new Authenticator(logger, db, options);
27 this.sessionManager = new SessionManager(logger, this.authenticator, options);
28 this.staticPath = path.join(__dirname, '..', 'static');
29 this.loginPath = `${options.dingus.proxyPrefix}/admin/login`;
30
31 // Primary API endpoint
32 this.on('POST', '/', this.handlerPostRoot.bind(this));
33
34 // Information page about service
35 this.on(['GET', 'HEAD'], '/', this.handlerGetRoot.bind(this));
36
37 // Give load-balancers something to check
38 this.on(['GET', 'HEAD'], '/healthcheck', this.handlerGetHealthcheck.bind(this));
39
40 // Public information about topics
41 this.on('GET', '/info', this.handlerGetInfo.bind(this));
42 this.on('GET', '/info/', this.handlerGetInfo.bind(this));
43
44 // These routes are intended for accessing static content during development.
45 // In production, a proxy server would likely handle these first.
46 this.on(['GET', 'HEAD'], '/static', this.handlerRedirect.bind(this), `${options.dingus.proxyPrefix}/static/`);
47 this.on(['GET', 'HEAD'], '/static/', this.handlerGetStaticFile.bind(this), 'index.html');
48 this.on(['GET', 'HEAD'], '/static/:file', this.handlerGetStaticFile.bind(this));
49 this.on(['GET', 'HEAD'], '/favicon.ico', this.handlerGetStaticFile.bind(this), 'favicon.ico');
50 this.on(['GET', 'HEAD'], '/robots.txt', this.handlerGetStaticFile.bind(this), 'robots.txt');
51
52 // Private informational endpoints
53 this.on(['GET', 'HEAD'], '/admin', this.handlerRedirect.bind(this), `${options.dingus.proxyPrefix}/admin/`);
54 this.on(['GET', 'HEAD'], '/admin/', this.handlerGetAdminOverview.bind(this));
55 this.on(['GET', 'HEAD'], '/admin/topic/:topicId', this.handlerGetAdminTopicDetails.bind(this));
56
57 // Private data-editing endpoints
58 this.on(['PATCH', 'DELETE'], '/admin/topic/:topicId', this.handlerUpdateTopic.bind(this));
59 this.on(['PATCH', 'DELETE'], '/admin/subscription/:subscriptionId', this.handlerUpdateSubscription.bind(this));
60
61 // Private server-action endpoints
62 this.on('POST', '/admin/process', this.handlerPostAdminProcess.bind(this));
63
64 // Admin login
65 this.on(['GET', 'HEAD'], '/admin/login', this.handlerGetAdminLogin.bind(this));
66 this.on(['POST'], '/admin/login', this.handlerPostAdminLogin.bind(this));
67 this.on(['GET'], '/admin/logout', this.handlerGetAdminLogout.bind(this));
68 this.on(['GET'], '/admin/_ia', this.handlerGetAdminIA.bind(this));
69
70 }
71
72
73 /**
74 * @param {http.ClientRequest} req
75 * @param {http.ServerResponse} res
76 * @param {Object} ctx
77 */
78 async handlerPostRoot(req, res, ctx) {
79 const _scope = _fileScope('handlerPostRoot');
80 this.logger.debug(_scope, 'called', { req: common.requestLogData(req), ctx });
81
82 this.setResponseType(this.responseTypes, req, res, ctx);
83 await this.ingestBody(req, res, ctx);
84
85 await this.manager.postRoot(req, res, ctx);
86 }
87
88
89 /**
90 * @param {http.ClientRequest} req
91 * @param {http.ServerResponse} res
92 * @param {Object} ctx
93 */
94 async handlerGetRoot(req, res, ctx) {
95 const _scope = _fileScope('handlerGetRoot');
96 const responseTypes = [
97 Enum.ContentType.TextHTML,
98 ];
99 this.logger.debug(_scope, 'called', { req: common.requestLogData(req), ctx });
100
101 Dingus.setHeadHandler(req, res, ctx);
102
103 this.setResponseType(responseTypes, req, res, ctx);
104
105 await this.manager.getRoot(req, res, ctx);
106 }
107
108
109 /**
110 * @param {http.ClientRequest} req
111 * @param {http.ServerResponse} res
112 * @param {Object} ctx
113 */
114 async handlerGetHealthcheck(req, res, ctx) {
115 const _scope = _fileScope('handlerGetHealthcheck');
116 this.logger.debug(_scope, 'called', { req: common.requestLogData(req), ctx });
117
118 Dingus.setHeadHandler(req, res, ctx);
119
120 this.setResponseType(this.responseTypes, req, res, ctx);
121
122 await this.manager.getHealthcheck(res, ctx);
123 }
124
125
126 /**
127 * @param {http.ClientRequest} req
128 * @param {http.ServerResponse} res
129 * @param {Object} ctx
130 */
131 async handlerGetInfo(req, res, ctx) {
132 const _scope = _fileScope('handlerGetInfo');
133 this.logger.debug(_scope, 'called', { req: common.requestLogData(req), ctx });
134
135 const responseTypes = [...this.responseTypes, Enum.ContentType.ImageSVG];
136
137 Dingus.setHeadHandler(req, res, ctx);
138
139 this.setResponseType(responseTypes, req, res, ctx);
140
141 await this.manager.getInfo(res, ctx);
142 }
143
144
145 /**
146 * @param {http.ClientRequest} req
147 * @param {http.ServerResponse} res
148 * @param {Object} ctx
149 */
150 async handlerGetAdminOverview(req, res, ctx) {
151 const _scope = _fileScope('handlerGetAdminOverview');
152 this.logger.debug(_scope, 'called', { req: common.requestLogData(req), ctx });
153
154 Dingus.setHeadHandler(req, res, ctx);
155
156 this.setResponseType(this.responseTypes, req, res, ctx);
157
158 await this.authenticator.required(req, res, ctx, this.loginPath);
159
160 await this.manager.getAdminOverview(res, ctx);
161 }
162
163
164 /**
165 * @param {http.ClientRequest} req
166 * @param {http.ServerResponse} res
167 * @param {Object} ctx
168 */
169 async handlerGetAdminTopicDetails(req, res, ctx) {
170 const _scope = _fileScope('handlerGetAdminTopicDetails');
171 this.logger.debug(_scope, 'called', { req: common.requestLogData(req), ctx });
172
173 Dingus.setHeadHandler(req, res, ctx);
174
175 this.setResponseType(this.responseTypes, req, res, ctx);
176
177 await this.authenticator.required(req, res, ctx, this.loginPath);
178
179 await this.manager.getTopicDetails(res, ctx);
180 }
181
182
183 /**
184 * Same as super.ingestBody, but if no body was sent, do not parse (and
185 * thus avoid possible unsupported media type error).
186 * @param {http.ClientRequest} req
187 * @param {http.ServerResponse} res
188 * @param {Object} ctx
189 */
190 async maybeIngestBody(req, res, ctx) {
191 ctx.rawBody = await this.bodyData(req);
192 const contentType = Dingus.getRequestContentType(req);
193 if (ctx.rawBody) {
194 this.parseBody(contentType, ctx);
195 }
196 }
197
198
199 /**
200 * @param {http.ClientRequest} req
201 * @param {http.ServerResponse} res
202 * @param {Object} ctx
203 */
204 async handlerUpdateTopic(req, res, ctx) {
205 const _scope = _fileScope('handlerUpdateTopic');
206 this.logger.debug(_scope, 'called', { req: common.requestLogData(req), ctx });
207
208 this.setResponseType(this.responseTypes, req, res, ctx);
209
210 await this.authenticator.requiredLocal(req, res, ctx, this.loginPath);
211
212 await this.maybeIngestBody(req, res, ctx);
213 ctx.method = req.method;
214 await this.manager.updateTopic(res, ctx);
215 }
216
217
218 /**
219 * @param {http.ClientRequest} req
220 * @param {http.ServerResponse} res
221 * @param {Object} ctx
222 */
223 async handlerUpdateSubscription(req, res, ctx) {
224 const _scope = _fileScope('handlerUpdateSubscription');
225 this.logger.debug(_scope, 'called', { req: common.requestLogData(req), ctx });
226
227 this.setResponseType(this.responseTypes, req, res, ctx);
228
229 await this.authenticator.requiredLocal(req, res, ctx, this.loginPath);
230
231 await this.maybeIngestBody(req, res, ctx);
232 ctx.method = req.method;
233 await this.manager.updateSubscription(res, ctx);
234 }
235
236
237 /**
238 * @param {http.ClientRequest} req
239 * @param {http.ServerResponse} res
240 * @param {Object} ctx
241 */
242 async handlerPostAdminProcess(req, res, ctx) {
243 const _scope = _fileScope('handlerPostAdminProcess');
244 this.logger.debug(_scope, 'called', { req: common.requestLogData(req), ctx });
245
246 this.setResponseType(this.responseTypes, req, res, ctx);
247
248 await this.authenticator.requiredLocal(req, res, ctx, this.loginPath);
249
250 await this.manager.processTasks(res, ctx);
251 }
252
253
254 /**
255 * @param {http.ClientRequest} req
256 * @param {http.ServerResponse} res
257 * @param {Object} ctx
258 */
259 async handlerGetAdminLogin(req, res, ctx) {
260 const _scope = _fileScope('handlerGetAdminLogin');
261 this.logger.debug(_scope, 'called', { req: common.requestLogData(req), ctx });
262
263 Dingus.setHeadHandler(req, res, ctx);
264
265 this.setResponseType(this.responseTypes, req, res, ctx);
266
267 await this.sessionManager.getAdminLogin(res, ctx);
268 }
269
270
271 /**
272 * @param {http.ClientRequest} req
273 * @param {http.ServerResponse} res
274 * @param {Object} ctx
275 */
276 async handlerPostAdminLogin(req, res, ctx) {
277 const _scope = _fileScope('handlerPostAdminLogin');
278 this.logger.debug(_scope, 'called', { req: common.requestLogData(req), ctx });
279
280 this.setResponseType(this.responseTypes, req, res, ctx);
281
282 await this.maybeIngestBody(req, res, ctx);
283
284 await this.sessionManager.postAdminLogin(res, ctx);
285 }
286
287
288 /**
289 * @param {http.ClientRequest} req
290 * @param {http.ServerResponse} res
291 * @param {Object} ctx
292 */
293 async handlerGetAdminLogout(req, res, ctx) {
294 const _scope = _fileScope('handlerGetAdminLogout');
295 this.logger.debug(_scope, 'called', { req: common.requestLogData(req), ctx });
296
297 this.setResponseType(this.responseTypes, req, res, ctx);
298
299 await this.sessionManager.getAdminLogout(res, ctx);
300 }
301
302
303 /**
304 * @param {http.ClientRequest} req
305 * @param {http.ServerResponse} res
306 * @param {Object} ctx
307 */
308 async handlerGetAdminIA(req, res, ctx) {
309 const _scope = _fileScope('handlerGetAdminIA');
310 this.logger.debug(_scope, 'called', { req: common.requestLogData(req), ctx });
311
312 this.setResponseType(this.responseTypes, req, res, ctx);
313
314 // Special case here, to see cookie before session established
315 ctx.cookie = req.getHeader(Enum.Header.Cookie);
316
317 await this.sessionManager.getAdminIA(res, ctx);
318 }
319
320 }
321
322 module.exports = Service;