This repository has been archived by the owner on May 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 264
/
browserid
executable file
·187 lines (161 loc) · 6.17 KB
/
browserid
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
#!/usr/bin/env node
/* 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/. */
require('../lib/baseExceptions.js').addExceptionHandler();
const
fs = require('fs'),
path = require('path'),
url = require('url'),
http = require('http'),
urlparse = require('urlparse'),
express = require('express');
const
i18n = require('i18n-abide'),
i18n_check = require('../lib/i18n_client_check'),
wsapi = require('../lib/wsapi.js'),
httputils = require('../lib/httputils.js'),
db = require('../lib/db.js'),
config = require('../lib/configuration.js'),
heartbeat = require('../lib/heartbeat.js'),
logger = require('../lib/logging.js').logger,
shutdown = require('../lib/shutdown'),
primary = require('../lib/primary'),
forward = require('../lib/http_forward.js'),
toobusy = require('../lib/busy_middleware.js');
var app = undefined;
app = express.createServer();
logger.info("browserid server starting up");
if (config.get('env') === 'production') {
logger.info('node.js version: ' + process.version + ' at ' + process.execPath);
logger.info('configuration: ', JSON.stringify(JSON.parse(config.toString())));
}
// NOTE: ordering of middleware registration is important in this file, it is the
// order in which middleware will be invoked as requests are processed.
// #1 - Setup health check / heartbeat middleware.
// This is in front of logging on purpose. see issue #537
heartbeat.setup(app, function(cb) {
// ping the database to verify we're really healthy.
db.ping(function(e) {
if (e) logger.error("database ping error: " + e);
cb(!e);
});
});
app.use(toobusy);
// #2 - logging! all requests other than __heartbeat__ are logged
app.use(express.logger({
format: config.get('express_log_format'),
stream: {
write: function(x) {
logger.info(typeof x === 'string' ? x.trim() : x);
}
}
}));
// #2.1 - localization
app.use(i18n.abide({
supported_languages: config.get('supported_languages'),
default_lang: config.get('default_lang'),
debug_lang: config.get('debug_lang'),
translation_directory: config.get('translation_directory'),
disable_locale_check: config.get('disable_locale_check'),
logger: logger
}));
i18n_check();
var statsd_config = config.get('statsd');
if (statsd_config && statsd_config.enabled) {
var logger_statsd = require("connect-logger-statsd");
app.use(logger_statsd({
host: statsd_config.hostname || "localhost",
port: statsd_config.port || 8125,
prefix: statsd_config.prefix || "browserid.webhead."
}));
}
// #3 - Add Strict-Transport-Security headers if we're serving over SSL
if (config.get('scheme') == 'https') {
app.use(function(req, resp, next) {
// expires in 30 days, include subdomains like www
resp.setHeader("Strict-Transport-Security", "max-age=10886400; includeSubdomains");
next();
});
}
// #4 - prevent framing of everything. content underneath that needs to be
// framed must explicitly remove the x-frame-options
app.use(function(req, resp, next) {
resp.setHeader('x-frame-options', config.get('x_frame_options'));
next();
});
// #6 - verify all JSON responses are objects - prevents regression on issue #217
app.use(function(req, resp, next) {
var realRespJSON = resp.json;
resp.json = function(obj) {
if (!obj || typeof obj !== 'object') {
logger.error("INTERNAL ERROR! *all* json responses must be objects");
return httputils.serverError(resp, "broken internal API implementation");
}
realRespJSON.call(resp, obj);
};
return next();
});
// #7 - perform response substitution to support local/dev/beta environments
// (specifically, this replaces URLs in responses, e.g. https://login.persona.org
// with https://login.anosrep.org)
config.performSubstitution(app);
// #8 - handle /wsapi requests
wsapi.setup({
forward_writes: urlparse(config.get('dbwriter_url')).validate().normalize().originOnly()
}, app);
// #9 if the BROWSERID_FAKE_VERIFICATION env var is defined, we'll include
// fake_verification.js. This is used during testing only and should
// never be included in a production deployment
if (process.env['BROWSERID_FAKE_VERIFICATION']) {
require('../lib/browserid/fake_verification.js').addVerificationWSAPI(app);
}
// open the databse
db.open(config.get('database'), function (error) {
if (error) {
logger.error("can't open database: " + error);
// let async logging flush, then exit 1
return setTimeout(function() { process.exit(1); }, 0);
}
// As we detect primaries are online in the browserid process,
// we must record this. This allows us to handle cases where
// IdP's transition online or off.
primary.events.on('idp_seen', function(domain) {
wsapi.requestToDBWriter({
path: '/wsapi/saw_idp?domain=' + encodeURIComponent(domain)
}, function(err, r) {
if (err) logger.warn("failed to note IDP seen for domain " + domain + ": " + err);
});
});
// shut down express gracefully on SIGINT
shutdown.handleTerminationSignals(app, function(readyForShutdownCB) {
require('../lib/bcrypt.js').shutdown();
db.close(readyForShutdownCB);
toobusy.shutdown();
});
var bindTo = config.get('bind_to');
app.listen(bindTo.port, bindTo.host, function() {
logger.info("running on http://" + app.address().address + ":" + app.address().port);
// #13 if the CREATE_TEST_USERS env var is defined, we'll try to create
// some test users
if (process.env['CREATE_TEST_USERS']) {
logger.warn("creating test users... this can take a while...");
require('../lib/bcrypt').encrypt(
config.get('bcrypt_work_factor'), "THE PASSWORD", function(err, hash) {
if (err) {
logger.error("error creating test users - bcrypt encrypt pass: " + err);
process.exit(1);
}
var want = parseInt(process.env['CREATE_TEST_USERS'], 10);
var have = 0;
for (var i = 1; i <= want; i++) {
db.addTestUser(i + "@loadtest.domain", hash, function(err, email) {
if (++have == want) {
logger.warn("created " + want + " test users");
}
});
}
});
}
});
});