Skip to content

Commit 721a19d

Browse files
committed
Enable HTTPS
1 parent fd7eaad commit 721a19d

11 files changed

+165
-77
lines changed

auth.test.mjs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import tap from 'tap';
2-
import got from 'got';
32
import * as db from './db.mjs';
43
import * as util from './util.mjs';
54
import * as test_util from './test_util.mjs';
5+
import { request as got } from './test_util.mjs';
66

77
let tokens;
88

@@ -39,22 +39,22 @@ tap.teardown(async () => {
3939
});
4040

4141
tap.test('get token', async t => {
42-
let res = await got('http://localhost:3000/get_token', {
42+
let res = await got('/get_token', {
4343
responseType: 'json',
4444
searchParams: { id: tokens[0].id },
4545
headers: { 'Authorization': 'Bearer ' + jwt.public }
4646
});
4747
t.equal(res.statusCode, 200, 'valid auth');
4848

49-
res = await got('http://localhost:3000/get_token', {
49+
res = await got('/get_token', {
5050
responseType: 'json',
5151
searchParams: { id: tokens[0].id },
5252
headers: { 'Authorization': 'Bearer ' + jwt.admin }
5353
});
5454
t.equal(res.statusCode, 200, 'valid auth (other valid subject)');
5555

5656
try {
57-
let res = await got('http://localhost:3000/get_token', {
57+
let res = await got('/get_token', {
5858
responseType: 'json',
5959
searchParams: { id: tokens[0].id }
6060
});
@@ -67,7 +67,7 @@ tap.test('get token', async t => {
6767
}
6868

6969
try {
70-
let res = await got('http://localhost:3000/get_token', {
70+
let res = await got('/get_token', {
7171
responseType: 'json',
7272
searchParams: { id: tokens[0].id },
7373
headers: { 'Authorization': 'Bearer ' + jwt.exhibition }
@@ -81,7 +81,7 @@ tap.test('get token', async t => {
8181
}
8282

8383
try {
84-
let res = await got('http://localhost:3000/get_token', {
84+
let res = await got('/get_token', {
8585
responseType: 'json',
8686
searchParams: { id: tokens[0].id },
8787
headers: { 'Authorization': 'Bearer ' + jwt.public_invalid }
@@ -95,7 +95,7 @@ tap.test('get token', async t => {
9595
}
9696

9797
try {
98-
let res = await got('http://localhost:3000/get_token', {
98+
let res = await got('/get_token', {
9999
responseType: 'json',
100100
searchParams: { id: tokens[0].id },
101101
headers: { 'Authorization': 'Bearer ' + jwt.garbage }
@@ -109,7 +109,7 @@ tap.test('get token', async t => {
109109
}
110110

111111
try {
112-
let res = await got('http://localhost:3000/get_token', {
112+
let res = await got('/get_token', {
113113
responseType: 'json',
114114
searchParams: { id: tokens[0].id },
115115
headers: { 'Authorization': 'Bearer ' + jwt.public_expired }

cert/localhost.crt

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIC8DCCAdigAwIBAgIUTuVLsL+W/LpSp+3ZnsAPQ4xrAjIwDQYJKoZIhvcNAQEL
3+
BQAwFDESMBAGA1UEAwwJbG9jYWxob3N0MB4XDTIxMDQyMTA5MTIzNloXDTIxMDUy
4+
MTA5MTIzNlowFDESMBAGA1UEAwwJbG9jYWxob3N0MIIBIjANBgkqhkiG9w0BAQEF
5+
AAOCAQ8AMIIBCgKCAQEApDIG7S6He/ZjLOi8zdq6SyhsyH83v9nSsAi3YJ8LQkLt
6+
gbnX4VD820TwHHI+3B0+r+a236Bc5M8R55mN7/lGFsgLpzZ2uF9N3H97YHMXhWaQ
7+
zoLqPTOMm3mbIN8Vo1PxLuGKZBCIXVdjDoN0UeTzruKHKDrsIdgg8XW6p3v2yLRi
8+
IUood8V+0k4lt797kBODpm2gbT2JDM2Xm6UbOsH8VHSaVn4wio/5txr+07kpVvdF
9+
MzczfD7SwfXZM2FzO5Pe713vup5Md8azkwhDVHWh6osUJdQ3Im7STyNc4yPla4kf
10+
kml2sdhyiZWr/JDKZ1yNERMpWflNjqzLRHQ3NG/kawIDAQABozowODAUBgNVHREE
11+
DTALgglsb2NhbGhvc3QwCwYDVR0PBAQDAgeAMBMGA1UdJQQMMAoGCCsGAQUFBwMB
12+
MA0GCSqGSIb3DQEBCwUAA4IBAQB2zR5mVMCut63OkhR0VDBZAjdlWsJuAS7075by
13+
ifxw4Kw+D+9ccnARqgd2cPRUXzH2mi6XxbvRQpHTSUKz7PD6N9kVSJsocoy0BOsJ
14+
WEFYzxGiRQRDWZWVbQ82Rc/WTDg2xacUOEcMsfyMvxdNQ7BmqU51mkAqYIcv6ksL
15+
7YyXXR8s3b4xwY/KIM7cVkTb6q3TY53g/QauzZYTbdvwCWSUOOGt9hVtnsc4gD49
16+
WT0Sn9QsMmzN/rQ6ouyDY5aJrugfc7SA6FUD7X2ZOL91J85W1y2Hn9XS3tFvzI86
17+
X1EUuu3ZjSSuuPuiOEH7oIAekq4MLNN2pyML1ZxTzWqjbmO7
18+
-----END CERTIFICATE-----

cert/localhost.key

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
-----BEGIN PRIVATE KEY-----
2+
MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCkMgbtLod79mMs
3+
6LzN2rpLKGzIfze/2dKwCLdgnwtCQu2BudfhUPzbRPAccj7cHT6v5rbfoFzkzxHn
4+
mY3v+UYWyAunNna4X03cf3tgcxeFZpDOguo9M4ybeZsg3xWjU/Eu4YpkEIhdV2MO
5+
g3RR5POu4ocoOuwh2CDxdbqne/bItGIhSih3xX7STiW3v3uQE4OmbaBtPYkMzZeb
6+
pRs6wfxUdJpWfjCKj/m3Gv7TuSlW90UzNzN8PtLB9dkzYXM7k97vXe+6nkx3xrOT
7+
CENUdaHqixQl1DcibtJPI1zjI+VriR+SaXax2HKJlav8kMpnXI0REylZ+U2OrMtE
8+
dDc0b+RrAgMBAAECggEBAKDTX3Vx3cLN0tZqIISBeXekYOkBstl3waYq43MAVUbR
9+
x1EJdyp66TDclVNHk8qF2qDwmq7vpA15xU/UV/J1dvFZgBCIiECPGPSg6XfPQ3qm
10+
u6+LC2Rm2ZJvykKoP8l6rwH8aFEa4HaM4bmViJmuaAEkYL5G+IMt7xaS+gCSq2Zp
11+
tv4VDSEjeUv/nXyW0ZO7TX/zJ/Lxw0JZnFWDhwooGx2NHjBc+Qwolxk98cw7WcBC
12+
zvEoatbIgvmW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file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import tap from 'tap';
2-
import got from 'got';
32
import * as db from './db.mjs';
43
import * as util from './util.mjs';
54
import * as test_util from './test_util.mjs';
5+
import { request as got } from './test_util.mjs';
66

77
let tokens;
88

@@ -28,7 +28,7 @@ tap.test('get token (errors)', async t => {
2828
const url_save = db.DB.url;
2929
db.DB.url = 'http://localhost:9999';
3030
try {
31-
await got('http://localhost:3000/get_token', {
31+
await got('/get_token', {
3232
responseType: 'json',
3333
searchParams: { id: tokens[0].id },
3434
retry: 0
@@ -44,7 +44,7 @@ tap.test('get token (errors)', async t => {
4444

4545
// no id
4646
try {
47-
await got('http://localhost:3000/get_token', {
47+
await got('/get_token', {
4848
responseType: 'json',
4949
});
5050
t.fail('should throw');
@@ -57,7 +57,7 @@ tap.test('get token (errors)', async t => {
5757

5858
// empty id
5959
try {
60-
await got('http://localhost:3000/get_token', {
60+
await got('/get_token', {
6161
responseType: 'json',
6262
searchParams: { id: '' }
6363
});
@@ -71,7 +71,7 @@ tap.test('get token (errors)', async t => {
7171

7272
// invalid id
7373
try {
74-
await got('http://localhost:3000/get_token', {
74+
await got('/get_token', {
7575
responseType: 'json',
7676
retry: 0,
7777
searchParams: { id: 'abcdef' }
@@ -87,7 +87,7 @@ tap.test('get token (errors)', async t => {
8787

8888
tap.test('get tokens by offset (errors)', async t => {
8989
try {
90-
await got('http://localhost:3000/get_tokens', {
90+
await got('/get_tokens', {
9191
responseType: 'json',
9292
searchParams: {}
9393
});
@@ -100,7 +100,7 @@ tap.test('get tokens by offset (errors)', async t => {
100100
}
101101

102102
try {
103-
await got('http://localhost:3000/get_tokens', {
103+
await got('/get_tokens', {
104104
responseType: 'json',
105105
searchParams: { offset:0, count:0 }
106106
});
@@ -113,7 +113,7 @@ tap.test('get tokens by offset (errors)', async t => {
113113
}
114114

115115
try {
116-
await got('http://localhost:3000/get_tokens', {
116+
await got('/get_tokens', {
117117
responseType: 'json',
118118
searchParams: { offset:0, count:999999 }
119119
});
@@ -125,7 +125,7 @@ tap.test('get tokens by offset (errors)', async t => {
125125
}, 'count too big');
126126
}
127127

128-
let res = await got('http://localhost:3000/get_tokens', {
128+
let res = await got('/get_tokens', {
129129
responseType: 'json',
130130
searchParams: { offset:10, count:1 }
131131
});
@@ -134,7 +134,7 @@ tap.test('get tokens by offset (errors)', async t => {
134134
body: { offset:10, rows: [], prev: tokens[9].id, next: null }
135135
}, 'going one over');
136136

137-
res = await got('http://localhost:3000/get_tokens', {
137+
res = await got('/get_tokens', {
138138
responseType: 'json',
139139
searchParams: { offset:99, count:1 }
140140
});
@@ -143,7 +143,7 @@ tap.test('get tokens by offset (errors)', async t => {
143143
body: { offset:99, rows: [], prev: null, next: null }
144144
}, 'going over more');
145145

146-
res = await got('http://localhost:3000/get_tokens', {
146+
res = await got('/get_tokens', {
147147
responseType: 'json',
148148
searchParams: { offset:-11, count:1 }
149149
});
@@ -152,7 +152,7 @@ tap.test('get tokens by offset (errors)', async t => {
152152
body: { offset:-11, rows: [], prev: null, next: tokens[0].id }
153153
}, 'going one below');
154154

155-
res = await got('http://localhost:3000/get_tokens', {
155+
res = await got('/get_tokens', {
156156
responseType: 'json',
157157
searchParams: { offset:-99, count:1 }
158158
});

main.config.json

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
{
22
"host": "localhost",
33
"port": 3000,
4+
"https": {
5+
"enabled": true,
6+
"key": "cert/localhost.key",
7+
"cert": "cert/localhost.crt"
8+
},
49
"page_limit": 1000,
510
"enable_auth": true,
611
"jwt_secret": "y2ZHC@KS/KW6Nw;whGVKl-Nc2y/;HpOc",

main.mjs

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env node
2-
2+
import http from 'http';
3+
import https from 'https';
34
import {readFileSync} from 'fs';
45
import express from 'express';
56
import jwt from 'express-jwt';
@@ -262,9 +263,21 @@ if (!filter_status) {
262263
await db.create_filters();
263264
console.log('updated filters');
264265
}
266+
265267
// start server
266-
const server = app.listen(CONFIG.port, () => {
267-
console.log('Server running on port ' + CONFIG.port);
268+
let server;
269+
if (CONFIG.https.enabled) {
270+
server = https.createServer({
271+
key: readFileSync(CONFIG.https.key),
272+
cert: readFileSync(CONFIG.https.cert),
273+
}, app);
274+
} else {
275+
server = http.createServer(app);
276+
}
277+
server.listen(CONFIG.port, () => {
278+
const secure = server instanceof https.Server;
279+
console.log(`${secure ? 'HTTPS ' : ''}Server running on port ${CONFIG.port}`);
280+
if (!secure) console.log('Warning: Server is not secure (HTTPS disbaled)');
268281
});
269282

270283
// Instance of http.Server. See: https://expressjs.com/en/4x/api.html#app.listen

0 commit comments

Comments
 (0)