This repository has been archived by the owner on Dec 1, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
config.js
251 lines (233 loc) · 6.53 KB
/
config.js
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const crypto = require('crypto');
const fs = require('fs');
const url = require('url');
const convict = require('convict');
function strictUrl(raw) {
var p = url.parse(raw);
if (!/^https?:$/.test(p.protocol)) {
throw new Error('must have http or https scheme');
}
if (!p.hostname) {
throw new Error('must supply hostname or ip address');
}
if (p.path !== '/') { // url.parse normalizes path to '/' when omitted
throw new Error('must not provide a path or query string');
}
if (p.hash) {
throw new Error('must not provide an anchor');
}
}
const conf = convict({
server: {
publicUrl: {
doc: 'The public-facing bridge URL in scheme://host[:port] format.',
env: 'PUBLIC_URL',
format: strictUrl,
default: 'http://127.0.0.1:3000'
},
personaUrl : {
doc: 'Base URL for including Persona\'s JS files in scheme://host[:port]',
env: 'PERSONA_URL',
format: strictUrl,
default: 'http://127.0.0.1:10002'
},
openidRealm: {
doc: 'The openid realm.',
env: 'OPENID_REALM',
format: String,
default: undefined // Derives from publicUrl by default.
},
port: {
doc: 'The port to listen on.',
env: 'PORT',
format: 'port',
default: 3000
},
host: {
doc: 'The hostname or IP to bind to.',
env: 'HOST',
format: String,
default: '127.0.0.1'
},
},
google: {
clientId : {
doc: 'Google APIs Client ID',
env: 'GOOGLE_CLIENT_ID',
format: String,
default: 'REPLACE_WITH_REAL_CLIENT_ID'
},
clientSecret : {
doc: 'Google APIs Client Secret',
env: 'GOOGLE_CLIENT_SECRET',
format: String,
default: 'REPLACE_WITH_REAL_CLIENT_SECRET'
}
},
cert: {
domain: {
doc: 'The domain used in the signed certificate.',
env: 'CERT_DOMAIN',
arg: 'issuer',
format: String,
default: undefined // Derives from publicUrl by default.
},
pubKeyPath: {
doc: 'Path to the public key to render the .well-known',
env: 'PUB_KEY_PATH',
format: String,
default: 'var/key.publickey'
},
privKeyPath: {
doc: 'Path to the private key to sign certs.',
env: 'PRIV_KEY_PATH',
format: String,
default: 'var/key.secretkey'
},
maxDuration: {
doc: 'Maximum certificate duration in milliseconds',
env: 'CERT_MAX_DURATION',
format: 'int',
default: 1000 * 60 * 60 * 24 // 24 hours max
},
minDuration: {
doc: 'Minimum / default certificate duration in milliseconds.',
env: 'CERT_MIN_DURATION',
format: 'int',
default: 1000 * 60 * 5 // 5 minutes default
}
},
logPath: {
doc: 'Path to log file.',
env: 'LOG_PATH',
format: String,
default: '-' // Log to stdout / stderr.
},
session: {
secret: {
doc: 'The secret used for signing cookies',
env: 'SESSION_SECRET',
format: String,
default: String(crypto.randomBytes(256)) // Ephemeral. OVERRIDE THIS.
},
duration: {
doc: 'The duration of session cookies, in milliseconds.',
env: 'SESSION_DURATION',
format: 'nat',
default: 15 * 60 * 1000
}
},
locale: {
list: {
doc: 'An array of locales to enabled.',
env: 'LOCALE_LIST',
format: Array,
default: ['en-US', 'it-CH']
},
base: {
doc: 'The default locale.',
env: 'LOCALE_DEFAULT',
format: String,
default: 'en-US'
},
debug: {
doc: 'The debug locale.',
env: 'LOCALE_DEBUG',
format: String,
default: 'it-CH'
},
dir: {
doc: 'The path to the directory containing translations.',
env: 'LOCALE_DIR',
format: String,
default: 'static/i18n'
}
},
proxy: {
host: {
doc: 'Host for HTTP proxy of outgoing requests.',
env: 'PROXY_HOST',
format: String,
default: ''
},
port: {
doc: 'Port for HTTP proxy of outgoing requests.',
env: 'PROXY_PORT',
format: Number,
default: 0
}
},
statsd: {
enabled: {
doc: 'Whether to broadcast statsd data or not.',
env: 'STATSD_ENABLED',
format: Boolean,
default: false
},
host: {
doc: 'Host of statsd daemon.',
env: 'STATSD_HOST',
format: String,
default: 'localhost'
},
port: {
doc: 'Port of statsd daemon.',
env: 'STATSD_PORT',
format: 'port',
default: 8125
}
}
});
// Load configuration files specified in the environment
if (process.env.CONFIG_FILES) {
conf.loadFile(process.env.CONFIG_FILES.split(','));
}
// Strip trailing slashes and whitespace from personaUrl and publicUrl
conf.set('server.personaUrl',
conf.get('server.personaUrl').trim().replace(/[\s\/]*$/, ''));
conf.set('server.publicUrl',
conf.get('server.publicUrl').trim().replace(/[\s\/]*$/, ''));
// Derive openid realm from publicUrl
// https://foo.bar.example.com -> https://*.example.com
// http://127.0.0.1:3000 -> http://127.0.0.1:3000
if (conf.get('server.openidRealm') === undefined) {
// If only v8 supported lexical scoping...
(function () {
var publicUrl = url.parse(conf.get('server.publicUrl'));
var basename = publicUrl.hostname.split('.').slice(-2).join('.');
// Don't munge the realm in local development environment
if ('port' in publicUrl) {
conf.set('server.openidRealm', conf.get('server.publicUrl'));
} else {
conf.set('server.openidRealm', publicUrl.protocol + '//*.' + basename);
}
})();
}
// Derive default issuer from publicUrl
if (conf.get('cert.domain') === undefined) {
conf.set('cert.domain', url.parse(conf.get('server.publicUrl')).hostname);
}
// Augment default locales from local filesystem
if (JSON.stringify(conf.get('locale.list')) ===
JSON.stringify(conf.default('locale.list'))) {
(function() {
var dir = conf.get('locale.dir');
var defaults = conf.default('locale.list');
if (dir && fs.existsSync(dir)) {
conf.set('locale.list', defaults.concat(fs.readdirSync(dir)));
}
})();
}
conf.validate();
if (conf.get('proxy.host')) {
process.env.HTTP_PROXY_HOST = conf.get('proxy.host');
process.env.HTTPS_PROXY_HOST = conf.get('proxy.host');
}
if (conf.get('proxy.port')) {
process.env.HTTP_PROXY_PORT = conf.get('proxy.port');
process.env.HTTPS_PROXY_PORT = conf.get('proxy.port');
}
module.exports = conf;