reorganize files to hopefully be more sane
This commit is contained in:
Родитель
cdb5c2fa76
Коммит
7164bf1c21
|
@ -1,19 +1,63 @@
|
|||
#!/usr/bin/env node
|
||||
var server = require('../')
|
||||
var fs = require('fs')
|
||||
var config = require('../config').root()
|
||||
|
||||
server.start(
|
||||
function () {
|
||||
server.app.log.info('running on ' + server.info.uri)
|
||||
}
|
||||
)
|
||||
function main() {
|
||||
var log = require('../log')(config)
|
||||
|
||||
process.on(
|
||||
'SIGINT',
|
||||
function () {
|
||||
server.stop(
|
||||
function () {
|
||||
process.exit()
|
||||
}
|
||||
)
|
||||
}
|
||||
)
|
||||
// stats
|
||||
var statsBackend = config.stats.backend
|
||||
var Stats = require('../stats')
|
||||
// TODO: think about the stats structure
|
||||
var Backend = Stats.getBackend(statsBackend, log)
|
||||
var stats = new Stats(new Backend(config[statsBackend])) // TODO logBackend constructor
|
||||
|
||||
// memory monitor
|
||||
var MemoryMonitor = require('../memory_monitor')
|
||||
var memoryMonitor = new MemoryMonitor()
|
||||
memoryMonitor.on('mem', stats.mem.bind(stats))
|
||||
memoryMonitor.start()
|
||||
|
||||
// stored objects
|
||||
var models = require('../models')(config)
|
||||
|
||||
// server public key
|
||||
var serverPublicKey = fs.readFileSync(config.publicKeyFile)
|
||||
|
||||
//signer compute-cluster
|
||||
var CC = require('compute-cluster')
|
||||
var signer = new CC({ module: __dirname + '/signer.js' })
|
||||
|
||||
var routes = require('../routes')(
|
||||
log,
|
||||
serverPublicKey,
|
||||
signer,
|
||||
models
|
||||
)
|
||||
var Server = require('../server')
|
||||
var server = Server.create(log, config, routes, models.tokens)
|
||||
|
||||
server.start(
|
||||
function () {
|
||||
server.app.log.info('running on ' + server.info.uri)
|
||||
}
|
||||
)
|
||||
|
||||
process.on(
|
||||
'SIGINT',
|
||||
function () {
|
||||
server.stop(
|
||||
function () {
|
||||
process.exit()
|
||||
}
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
if (!fs.existsSync(config.publicKeyFile)) {
|
||||
require('../scripts/gen_keys')(main)
|
||||
}
|
||||
else {
|
||||
main()
|
||||
}
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
var crypto = require('crypto')
|
||||
var bigint = require('bigint')
|
||||
var P = require('p-promise')
|
||||
var hkdf = require('../hkdf')
|
||||
|
||||
module.exports = require('./bundle')(crypto, bigint, P, hkdf)
|
|
@ -6,12 +6,14 @@ var bigint = require('bigint')
|
|||
var hawk = require('hawk')
|
||||
var P = require('p-promise')
|
||||
|
||||
var hkdf = require('../hkdf')
|
||||
|
||||
var Bundle = require('../bundle')(crypto, bigint, P, hkdf)
|
||||
var tokens = require('../tokens')(Bundle, {})
|
||||
var AuthBundle = require('../auth_bundle')(inherits, Bundle, null, tokens)
|
||||
|
||||
var Bundle = require('../bundle')
|
||||
var Token = require('../models/token')(inherits, Bundle)
|
||||
var tokens = {
|
||||
AccountResetToken: require('../models/account_reset_token')(inherits, Token, crypto),
|
||||
KeyFetchToken: require('../models/key_fetch_token')(inherits, Token),
|
||||
SessionToken: require('../models/session_token')(inherits, Token)
|
||||
}
|
||||
var AuthBundle = require('../models/auth_bundle')(inherits, Bundle, null, tokens)
|
||||
|
||||
var srp = require('../srp')
|
||||
|
||||
|
|
|
@ -1,4 +0,0 @@
|
|||
var P = require('p-promise')
|
||||
var kvstore = require('kvstore')
|
||||
|
||||
module.exports = require('./kv')(P, kvstore)
|
78
index.js
78
index.js
|
@ -1,78 +0,0 @@
|
|||
var crypto = require('crypto')
|
||||
var fs = require('fs')
|
||||
|
||||
var config = require('./config').root()
|
||||
|
||||
if (!fs.existsSync(config.publicKeyFile)) {
|
||||
var started = false
|
||||
require('./scripts/gen_keys')(
|
||||
function () {
|
||||
load()
|
||||
process.exit(1) // TODO not ideal
|
||||
}
|
||||
)
|
||||
module.exports = {
|
||||
start: function () {}
|
||||
}
|
||||
}
|
||||
else {
|
||||
module.exports = load()
|
||||
}
|
||||
function load() {
|
||||
var log = require('./log')(config)
|
||||
|
||||
// stats
|
||||
var statsBackend = config.stats.backend
|
||||
var Stats = require('./stats')
|
||||
// TODO: think about the stats structure
|
||||
var Backend = Stats.getBackend(statsBackend, log)
|
||||
var stats = new Stats(new Backend(config[statsBackend])) // TODO logBackend constructor
|
||||
|
||||
// memory monitor
|
||||
var MemoryMonitor = require('./memory_monitor')
|
||||
var memoryMonitor = new MemoryMonitor()
|
||||
memoryMonitor.on('mem', stats.mem.bind(stats))
|
||||
memoryMonitor.start()
|
||||
|
||||
// storage
|
||||
var dbs = require('./db')(config)
|
||||
|
||||
var srp = require('./srp')
|
||||
var bigint = require('bigint')
|
||||
var P = require('p-promise')
|
||||
var uuid = require('uuid')
|
||||
var hkdf = require('./hkdf')
|
||||
|
||||
var Bundle = require('./bundle')(crypto, bigint, P, hkdf)
|
||||
var tokens = require('./tokens')(Bundle, dbs)
|
||||
|
||||
var RecoveryMethod = require('./recovery_method')(crypto, P, dbs.store)
|
||||
var Account = require('./account')(P, tokens.SessionToken, RecoveryMethod, dbs.store, config.domain)
|
||||
var SrpSession = require('./srp_session')(P, uuid, srp, bigint, dbs.cache, Account)
|
||||
|
||||
|
||||
var inherits = require('util').inherits
|
||||
var AuthBundle = require('./auth_bundle')(inherits, Bundle, Account, tokens)
|
||||
|
||||
// server public key
|
||||
var serverPublicKey = fs.readFileSync(config.publicKeyFile)
|
||||
|
||||
//signer compute-cluster
|
||||
var CC = require('compute-cluster')
|
||||
var signer = new CC({ module: __dirname + '/bin/signer.js' })
|
||||
|
||||
var routes = require('./routes')(
|
||||
log,
|
||||
dbs,
|
||||
serverPublicKey,
|
||||
signer,
|
||||
Account,
|
||||
AuthBundle,
|
||||
SrpSession,
|
||||
RecoveryMethod,
|
||||
tokens
|
||||
)
|
||||
var Server = require('./server')
|
||||
var server = Server.create(log, config, routes, tokens)
|
||||
return server
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
var bunyan = require('bunyan')
|
||||
|
||||
module.exports = function (config) {
|
||||
var logStreams = [
|
||||
{
|
||||
type: 'rotating-file',
|
||||
level: config.log.level,
|
||||
path: config.log.path,
|
||||
period: config.log.period,
|
||||
count: config.log.count
|
||||
},
|
||||
{
|
||||
type: 'raw',
|
||||
level: 'trace',
|
||||
stream: new bunyan.RingBuffer({ limit: 100 })
|
||||
}
|
||||
]
|
||||
|
||||
if (config.env !== 'production') {
|
||||
logStreams.push({ stream: process.stderr, level: 'trace' });
|
||||
}
|
||||
|
||||
var log = bunyan.createLogger(
|
||||
{
|
||||
name: 'picl-idp',
|
||||
streams: logStreams
|
||||
}
|
||||
)
|
||||
|
||||
log.info(config, "starting config")
|
||||
|
||||
return log
|
||||
}
|
|
@ -1,3 +0,0 @@
|
|||
var bunyan = require('bunyan')
|
||||
|
||||
module.exports = require('./log')(bunyan)
|
37
log/log.js
37
log/log.js
|
@ -1,37 +0,0 @@
|
|||
|
||||
module.exports = function (bunyan) {
|
||||
|
||||
function createLog(config) {
|
||||
var logStreams = [
|
||||
{
|
||||
type: 'rotating-file',
|
||||
level: config.log.level,
|
||||
path: config.log.path,
|
||||
period: config.log.period,
|
||||
count: config.log.count
|
||||
},
|
||||
{
|
||||
type: 'raw',
|
||||
level: 'trace',
|
||||
stream: new bunyan.RingBuffer({ limit: 100 })
|
||||
}
|
||||
]
|
||||
|
||||
if (config.env !== 'production') {
|
||||
logStreams.push({ stream: process.stderr, level: 'trace' });
|
||||
}
|
||||
|
||||
const log = bunyan.createLogger(
|
||||
{
|
||||
name: 'picl-idp',
|
||||
streams: logStreams
|
||||
}
|
||||
)
|
||||
|
||||
log.info(config, "starting config")
|
||||
|
||||
return log
|
||||
}
|
||||
|
||||
return createLog
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
var crypto = require('crypto')
|
||||
var inherits = require('util').inherits
|
||||
|
||||
var bigint = require('bigint')
|
||||
var kvstore = require('kvstore')
|
||||
var P = require('p-promise')
|
||||
var uuid = require('uuid')
|
||||
|
||||
var Bundle = require('../bundle')
|
||||
var srp = require('../srp')
|
||||
|
||||
module.exports = function (config) {
|
||||
var dbs = require('./kv')(P, kvstore, config)
|
||||
var Token = require('./token')(inherits, Bundle)
|
||||
|
||||
var KeyFetchToken = require('./key_fetch_token')(
|
||||
inherits,
|
||||
Token,
|
||||
dbs.store
|
||||
)
|
||||
var AccountResetToken = require('./account_reset_token')(
|
||||
inherits,
|
||||
Token,
|
||||
crypto,
|
||||
dbs.store
|
||||
)
|
||||
var SessionToken = require('./session_token')(
|
||||
inherits,
|
||||
Token,
|
||||
dbs.store
|
||||
)
|
||||
var tokens = {
|
||||
AccountResetToken: AccountResetToken,
|
||||
KeyFetchToken: KeyFetchToken,
|
||||
SessionToken: SessionToken
|
||||
}
|
||||
|
||||
var RecoveryMethod = require('./recovery_method')(
|
||||
crypto,
|
||||
P,
|
||||
dbs.store
|
||||
)
|
||||
var Account = require('./account')(
|
||||
P,
|
||||
SessionToken,
|
||||
RecoveryMethod,
|
||||
dbs.store,
|
||||
config.domain
|
||||
)
|
||||
var SrpSession = require('./srp_session')(
|
||||
P,
|
||||
uuid,
|
||||
srp,
|
||||
bigint,
|
||||
dbs.cache,
|
||||
Account
|
||||
)
|
||||
var AuthBundle = require('./auth_bundle')(
|
||||
inherits,
|
||||
Bundle,
|
||||
Account,
|
||||
tokens
|
||||
)
|
||||
|
||||
return {
|
||||
dbs: dbs,
|
||||
Account: Account,
|
||||
AuthBundle: AuthBundle,
|
||||
RecoveryMethod: RecoveryMethod,
|
||||
SrpSession: SrpSession,
|
||||
tokens: tokens
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
module.exports = function (P, kvstore) {
|
||||
module.exports = function (P, kvstore, config) {
|
||||
|
||||
var KV = null
|
||||
var KV = kvstore(config)
|
||||
|
||||
function KVPromise(config) {
|
||||
this.kv = KV.connect(config)
|
||||
|
@ -46,13 +46,8 @@ module.exports = function (P, kvstore) {
|
|||
return d.promise
|
||||
}
|
||||
|
||||
function create(config) {
|
||||
KV = kvstore(config)
|
||||
return {
|
||||
cache: new KVPromise({ backend: config.kvstore.cache }),
|
||||
store: new KVPromise({ backend: config.kvstore.backend })
|
||||
}
|
||||
return {
|
||||
cache: new KVPromise({ backend: config.kvstore.cache }),
|
||||
store: new KVPromise({ backend: config.kvstore.backend })
|
||||
}
|
||||
|
||||
return create
|
||||
}
|
|
@ -12,22 +12,18 @@ var isA = Hapi.types
|
|||
|
||||
module.exports = function (
|
||||
log,
|
||||
dbs,
|
||||
serverPublicKey,
|
||||
signer,
|
||||
Account,
|
||||
AuthBundle,
|
||||
SrpSession,
|
||||
RecoveryMethod,
|
||||
tokens) {
|
||||
var srp = require('./srp')(SrpSession, AuthBundle)
|
||||
models
|
||||
) {
|
||||
var srp = require('./srp')(models.SrpSession, models.AuthBundle)
|
||||
|
||||
var defaults = require('./defaults')(P, dbs)
|
||||
var defaults = require('./defaults')(P, models.dbs)
|
||||
var idp = require('./idp')(crypto, error, isA, serverPublicKey)
|
||||
var account = require('./account')(crypto, uuid, isA, error, Account, RecoveryMethod)
|
||||
var password = require('./password')(isA, error, srp, Account)
|
||||
var session = require('./session')(srp, isA, error, Account)
|
||||
var sign = require('./sign')(isA, error, signer, Account)
|
||||
var account = require('./account')(crypto, uuid, isA, error, models.Account, models.RecoveryMethod)
|
||||
var password = require('./password')(isA, error, srp, models.Account)
|
||||
var session = require('./session')(srp, isA, error, models.Account)
|
||||
var sign = require('./sign')(isA, error, signer, models.Account)
|
||||
|
||||
var routes = defaults.concat(
|
||||
idp,
|
||||
|
|
|
@ -1,17 +1,16 @@
|
|||
var test = require('tap').test
|
||||
var kvstore = require('kvstore')
|
||||
var P = require('p-promise')
|
||||
|
||||
function FakeToken() {}
|
||||
|
||||
FakeToken.get = function () { return P(new FakeToken())}
|
||||
|
||||
FakeToken.prototype.del = function () { return P(null) }
|
||||
|
||||
function FakeRecoveryMethod() {}
|
||||
FakeRecoveryMethod.create = function () { return P(new FakeRecoveryMethod()) }
|
||||
FakeRecoveryMethod.get = function () { return P(new FakeRecoveryMethod()) }
|
||||
|
||||
var db = require('../db')({
|
||||
var db = require('../models/kv')(P, kvstore, {
|
||||
kvstore: {
|
||||
available_backends: ['memory'],
|
||||
backend: 'memory',
|
||||
|
@ -19,7 +18,7 @@ var db = require('../db')({
|
|||
}
|
||||
})
|
||||
var DOMAIN = 'example.com'
|
||||
var Account = require('../account')(P, FakeToken, FakeRecoveryMethod, db.store, DOMAIN)
|
||||
var Account = require('../models/account')(P, FakeToken, FakeRecoveryMethod, db.store, DOMAIN)
|
||||
var a = {
|
||||
uid: 'xxx',
|
||||
email: 'somebody@example.com',
|
||||
|
|
|
@ -2,10 +2,11 @@ var test = require('tap').test
|
|||
var inherits = require('util').inherits
|
||||
var crypto = require('crypto')
|
||||
var bigint = require('bigint')
|
||||
var kvstore = require('kvstore')
|
||||
var P = require('p-promise')
|
||||
var hkdf = require('../hkdf')
|
||||
|
||||
var db = require('../db')({
|
||||
var db = require('../models/kv')(P, kvstore, {
|
||||
kvstore: {
|
||||
available_backends: ['memory'],
|
||||
backend: 'memory',
|
||||
|
@ -255,24 +256,24 @@ var accountReset = {
|
|||
'76590dba8d924713' + '78eb6e2197fed99f'
|
||||
}
|
||||
|
||||
var KBundle = require('../bundle')(
|
||||
var KBundle = require('../bundle/bundle')(
|
||||
fakeCrypto(accountKeys.keyFetchToken),
|
||||
bigint, P, hkdf)
|
||||
var KToken = require('../tokens/token')(inherits, KBundle)
|
||||
var KToken = require('../models/token')(inherits, KBundle)
|
||||
|
||||
var SBundle = require('../bundle')(
|
||||
var SBundle = require('../bundle/bundle')(
|
||||
fakeCrypto(useSession.sessionToken),
|
||||
bigint, P, hkdf)
|
||||
var SToken = require('../tokens/token')(inherits, SBundle)
|
||||
var SToken = require('../models/token')(inherits, SBundle)
|
||||
|
||||
var RBundle = require('../bundle')(
|
||||
var RBundle = require('../bundle/bundle')(
|
||||
fakeCrypto(accountReset.accountResetToken),
|
||||
bigint, P, hkdf)
|
||||
var RToken = require('../tokens/token')(inherits, RBundle)
|
||||
var RToken = require('../models/token')(inherits, RBundle)
|
||||
|
||||
var KeyFetchToken = require('../tokens/key_fetch_token')(inherits, KToken, db.store)
|
||||
var AccountResetToken = require('../tokens/account_reset_token')(inherits, RToken, crypto, db.store)
|
||||
var SessionToken = require('../tokens/session_token')(inherits, SToken, db.store)
|
||||
var KeyFetchToken = require('../models/key_fetch_token')(inherits, KToken, db.store)
|
||||
var AccountResetToken = require('../models/account_reset_token')(inherits, RToken, crypto, db.store)
|
||||
var SessionToken = require('../models/session_token')(inherits, SToken, db.store)
|
||||
var tokens = {
|
||||
KeyFetchToken: KeyFetchToken,
|
||||
AccountResetToken: AccountResetToken,
|
||||
|
@ -294,8 +295,7 @@ FakeAccount.prototype.setResetToken = function (t) {
|
|||
return P(null)
|
||||
}
|
||||
|
||||
var Bundle = require('../bundle')(crypto, bigint, P, hkdf)
|
||||
var AuthBundle = require('../auth_bundle')(inherits, Bundle, FakeAccount, tokens)
|
||||
var AuthBundle = require('../models/auth_bundle')(inherits, require('../bundle'), FakeAccount, tokens)
|
||||
|
||||
test(
|
||||
'create / get',
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
var test = require('tap').test
|
||||
var crypto = require('crypto')
|
||||
var bigint = require('bigint')
|
||||
var kvstore = require('kvstore')
|
||||
var P = require('p-promise')
|
||||
var uuid = require('uuid')
|
||||
var bigint = require('bigint')
|
||||
var srp = require('../srp')
|
||||
|
||||
var db = require('../db')({
|
||||
var db = require('../models/kv')(P, kvstore, {
|
||||
kvstore: {
|
||||
available_backends: ['memory'],
|
||||
backend: 'memory',
|
||||
|
@ -17,8 +17,8 @@ function FakeRecoveryMethod() {}
|
|||
FakeRecoveryMethod.create = function () { return P(new FakeRecoveryMethod()) }
|
||||
FakeRecoveryMethod.get = function () { return P(new FakeRecoveryMethod()) }
|
||||
|
||||
var Account = require('../account')(null, null, FakeRecoveryMethod, db.store, 'example.com')
|
||||
var SrpSession = require('../srp_session')(P, uuid, srp, bigint, db.cache, Account)
|
||||
var Account = require('../models/account')(P, null, FakeRecoveryMethod, db.store, 'example.com')
|
||||
var SrpSession = require('../models/srp_session')(P, uuid, srp, bigint, db.cache, Account)
|
||||
|
||||
var alice = {
|
||||
uid: 'xxx',
|
||||
|
|
|
@ -1,16 +0,0 @@
|
|||
var inherits = require('util').inherits
|
||||
var crypto = require('crypto')
|
||||
|
||||
module.exports = function (Bundle, dbs) {
|
||||
var Token = require('./token')(inherits, Bundle)
|
||||
|
||||
var KeyFetchToken = require('./key_fetch_token')(inherits, Token, dbs.store)
|
||||
var AccountResetToken = require('./account_reset_token')(inherits, Token, crypto, dbs.store)
|
||||
var SessionToken = require('./session_token')(inherits, Token, dbs.store)
|
||||
|
||||
return {
|
||||
KeyFetchToken: KeyFetchToken,
|
||||
AccountResetToken: AccountResetToken,
|
||||
SessionToken: SessionToken
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче