2014-11-13 12:13:37 -05:00
|
|
|
var bodyParser = require('body-parser')
|
|
|
|
var Cookies = require('cookies')
|
|
|
|
var express = require('express')
|
|
|
|
var fs = require('fs')
|
|
|
|
var Handlebars = require('handlebars')
|
|
|
|
var Error = require('http-errors')
|
2014-12-10 01:14:31 -05:00
|
|
|
var renderReadme = require('render-readme')
|
2014-11-13 12:13:37 -05:00
|
|
|
var Search = require('./search')
|
|
|
|
var Middleware = require('./middleware')
|
|
|
|
var match = Middleware.match
|
|
|
|
var validate_name = Middleware.validate_name
|
2014-11-16 12:44:46 -05:00
|
|
|
var validate_pkg = Middleware.validate_package
|
2014-11-04 09:47:03 -05:00
|
|
|
|
2015-02-24 14:28:16 -05:00
|
|
|
module.exports = function(config, auth, storage, packages) {
|
2014-11-13 12:13:37 -05:00
|
|
|
var app = express.Router()
|
2015-02-24 14:28:16 -05:00
|
|
|
var can = Middleware.allow(config, packages)
|
2014-11-13 10:52:13 -05:00
|
|
|
|
2014-11-13 12:13:37 -05:00
|
|
|
// validate all of these params as a package name
|
|
|
|
// this might be too harsh, so ask if it causes trouble
|
2014-11-16 12:44:46 -05:00
|
|
|
app.param('package', validate_pkg)
|
2014-11-13 12:13:37 -05:00
|
|
|
app.param('filename', validate_name)
|
|
|
|
app.param('version', validate_name)
|
|
|
|
app.param('anything', match(/.*/))
|
|
|
|
|
2014-11-12 06:14:37 -05:00
|
|
|
app.use(Cookies.express())
|
2014-11-13 11:15:50 -05:00
|
|
|
app.use(bodyParser.urlencoded({ extended: false }))
|
2014-11-12 06:14:37 -05:00
|
|
|
app.use(auth.cookie_middleware())
|
|
|
|
app.use(function(req, res, next) {
|
|
|
|
// disable loading in frames (clickjacking, etc.)
|
|
|
|
res.header('X-Frame-Options', 'deny')
|
|
|
|
next()
|
|
|
|
})
|
|
|
|
|
|
|
|
Search.configureStorage(storage)
|
|
|
|
|
|
|
|
Handlebars.registerPartial('entry', fs.readFileSync(require.resolve('./GUI/entry.hbs'), 'utf8'))
|
|
|
|
var template = Handlebars.compile(fs.readFileSync(require.resolve('./GUI/index.hbs'), 'utf8'))
|
|
|
|
|
|
|
|
app.get('/', function(req, res, next) {
|
|
|
|
var base = config.url_prefix || req.protocol + '://' + req.get('host')
|
|
|
|
res.setHeader('Content-Type', 'text/html')
|
|
|
|
|
|
|
|
storage.get_local(function(err, packages) {
|
|
|
|
if (err) throw err // that function shouldn't produce any
|
2014-11-13 13:32:31 -05:00
|
|
|
next(template({
|
2014-11-16 08:38:01 -05:00
|
|
|
name: config.web && config.web.title ? config.web.title : 'Sinopia',
|
2014-11-13 10:52:13 -05:00
|
|
|
packages: packages.filter(allow),
|
2014-11-12 06:14:37 -05:00
|
|
|
baseUrl: base,
|
|
|
|
username: req.remote_user.name,
|
|
|
|
}))
|
|
|
|
})
|
2014-11-13 10:52:13 -05:00
|
|
|
|
|
|
|
function allow(package) {
|
|
|
|
return config.allow_access(package.name, req.remote_user)
|
|
|
|
}
|
2014-11-12 06:14:37 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
// Static
|
|
|
|
app.get('/-/static/:filename', function(req, res, next) {
|
|
|
|
var file = __dirname + '/static/' + req.params.filename
|
2014-11-13 11:15:50 -05:00
|
|
|
res.sendFile(file, function(err) {
|
2014-11-13 13:32:31 -05:00
|
|
|
if (!err) return
|
2014-11-12 11:25:33 -05:00
|
|
|
if (err.status === 404) {
|
|
|
|
next()
|
2014-11-12 06:14:37 -05:00
|
|
|
} else {
|
2014-11-12 11:25:33 -05:00
|
|
|
next(err)
|
2014-11-12 06:14:37 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
app.get('/-/logo', function(req, res, next) {
|
2014-11-13 13:32:31 -05:00
|
|
|
res.sendFile(config.web.logo ? config.web.logo : __dirname + '/static/logo.png')
|
2014-11-12 06:14:37 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
app.get('/-/logo-sm', function(req, res, next) {
|
2014-11-13 13:32:31 -05:00
|
|
|
res.sendFile(config.web.logosm ? config.web.logosm : __dirname + '/static/logo-sm.png')
|
2014-11-12 06:14:37 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
app.post('/-/login', function(req, res, next) {
|
2014-11-16 07:37:50 -05:00
|
|
|
auth.authenticate(req.body.user, req.body.pass, function(err, user) {
|
|
|
|
if (!err) {
|
|
|
|
req.remote_user = user
|
2014-11-24 14:46:37 -05:00
|
|
|
//res.cookies.set('token', auth.issue_token(req.remote_user))
|
|
|
|
|
|
|
|
var str = req.body.user + ':' + req.body.pass
|
|
|
|
res.cookies.set('token', auth.aes_encrypt(str).toString('base64'))
|
2014-11-16 07:37:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var base = config.url_prefix || req.protocol + '://' + req.get('host')
|
|
|
|
res.redirect(base)
|
|
|
|
})
|
2014-11-12 06:14:37 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
app.post('/-/logout', function(req, res, next) {
|
|
|
|
var base = config.url_prefix || req.protocol + '://' + req.get('host')
|
|
|
|
res.cookies.set('token', '')
|
|
|
|
res.redirect(base)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Search
|
|
|
|
app.get('/-/search/:anything', function(req, res, next) {
|
2014-11-13 10:52:13 -05:00
|
|
|
var results = Search.query(req.params.anything)
|
|
|
|
var packages = []
|
2014-11-12 06:14:37 -05:00
|
|
|
|
|
|
|
var getData = function(i) {
|
|
|
|
storage.get_package(results[i].ref, function(err, entry) {
|
|
|
|
if (!err && entry) {
|
|
|
|
packages.push(entry.versions[entry['dist-tags'].latest])
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= results.length - 1) {
|
2014-11-13 13:32:31 -05:00
|
|
|
next(packages)
|
2014-11-12 06:14:37 -05:00
|
|
|
} else {
|
|
|
|
getData(i + 1)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (results.length) {
|
|
|
|
getData(0)
|
|
|
|
} else {
|
2014-11-13 13:32:31 -05:00
|
|
|
next([])
|
2014-11-12 06:14:37 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2014-11-13 10:52:13 -05:00
|
|
|
app.get('/-/readme/:package/:version?', can('access'), function(req, res, next) {
|
2014-11-12 06:14:37 -05:00
|
|
|
storage.get_package(req.params.package, {req: req}, function(err, info) {
|
|
|
|
if (err) return next(err)
|
2014-12-10 01:14:31 -05:00
|
|
|
next( renderReadme(info.readme || 'ERROR: No README data found!') )
|
2014-11-12 06:14:37 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
return app
|
2014-09-06 14:53:28 -05:00
|
|
|
}
|
2014-11-12 06:14:37 -05:00
|
|
|
|