fxa-auth-server/bin/key_server.js

165 строки
4.4 KiB
JavaScript
Исходник Обычный вид История

2013-07-26 05:46:16 +04:00
/* 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/. */
// Only `require()` the newrelic module if explicity enabled.
// If required, modules will be instrumented.
require('../lib/newrelic')()
var config = require('../config').getProperties()
var jwtool = require('fxa-jwtool')
var log = require('../lib/log')(config.log.level)
var getGeoData = require('../lib/geodb')(log)
function main() {
// Force the geo to load and run at startup, not waiting for it to run on
// some route later.
var knownIp = '63.245.221.32' // Mozilla MTV
getGeoData(knownIp)
.then(function(result) {
log.info({ op: 'geodb.check', result: result })
})
// RegExp instances serialise to empty objects, display regex strings instead.
const stringifiedConfig =
JSON.stringify(config, (k, v) =>
v && v.constructor === RegExp ? v.toString() : v
)
process.stdout.write('{"event":"config","data":' + stringifiedConfig + '}\n')
if (config.env !== 'prod') {
log.info(stringifiedConfig, 'starting config')
2013-11-26 05:27:08 +04:00
}
2015-05-10 22:11:59 +03:00
var error = require('../lib/error')
var Token = require('../lib/tokens')(log, config)
2015-05-10 22:11:59 +03:00
var Password = require('../lib/crypto/password')(log, config)
var UnblockCode = require('../lib/crypto/base32')(config.signinUnblock.codeLength)
2013-10-27 06:49:33 +04:00
2015-05-10 22:11:59 +03:00
var signer = require('../lib/signer')(config.secretKeyFile, config.domain)
2015-09-22 03:14:23 +03:00
var serverPublicKeys = {
primary: jwtool.JWK.fromFile(
config.publicKeyFile,
{
algorithm: 'RS',
use: 'sig',
kty: 'RSA'
}
),
secondary: config.oldPublicKeyFile ?
jwtool.JWK.fromFile(
config.oldPublicKeyFile,
{
algorithm: 'RS',
use: 'sig',
kty: 'RSA'
}
)
: null
}
2013-10-30 00:46:09 +04:00
2015-05-10 22:11:59 +03:00
var Customs = require('../lib/customs')(log, error)
2015-05-10 22:11:59 +03:00
var Server = require('../lib/server')
2013-10-30 00:46:09 +04:00
var server = null
var senders = null
var statsInterval = null
var database = null
var customs = null
function logStatInfo() {
log.stat(server.stat())
log.stat(Password.stat())
}
require('../lib/senders')(config, log)
.then(
function(result) {
senders = result
2013-10-30 00:46:09 +04:00
2015-05-10 22:11:59 +03:00
var DB = require('../lib/db')(
config,
log,
error,
Token.SessionToken,
Token.KeyFetchToken,
Token.AccountResetToken,
Token.PasswordForgotToken,
Token.PasswordChangeToken,
UnblockCode
)
2013-10-30 00:46:09 +04:00
DB.connect(config[config.db.backend])
.then(
function (db) {
database = db
customs = new Customs(config.customsUrl)
2015-05-10 22:11:59 +03:00
var routes = require('../lib/routes')(
log,
error,
2015-09-22 03:14:23 +03:00
serverPublicKeys,
signer,
db,
senders.email,
senders.sms,
Password,
config,
customs
)
2014-03-10 04:33:52 +04:00
server = Server.create(log, error, config, routes, db)
2013-12-06 02:52:55 +04:00
server.start(
function (err) {
if (err) {
log.error({ op: 'server.start.1', msg: 'failed startup with error',
err: { message: err.message } })
process.exit(1)
} else {
log.info({ op: 'server.start.1', msg: 'running on ' + server.info.uri })
}
}
)
statsInterval = setInterval(logStatInfo, 15000)
},
function (err) {
log.error({ op: 'DB.connect', err: { message: err.message } })
process.exit(1)
}
)
2013-10-30 00:46:09 +04:00
}
)
process.on(
'uncaughtException',
function (err) {
log.fatal(err)
process.exit(8)
}
)
process.on('SIGINT', shutdown)
log.on('error', shutdown)
function shutdown() {
log.info({ op: 'shutdown' })
clearInterval(statsInterval)
server.stop(
function () {
customs.close()
try {
senders.email.stop()
} catch (e) {
// XXX: simplesmtp module may quit early and set socket to `false`, stopping it may fail
log.warn({ op: 'shutdown', message: 'Mailer client already disconnected' })
}
database.close()
2015-07-30 01:05:29 +03:00
process.exit() //XXX: because of openid dep ಠ_ಠ
}
)
}
}
main()