This repository has been archived by the owner on Nov 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Andrew Hobden
committed
Jun 25, 2014
0 parents
commit 45936a0
Showing
8 changed files
with
648 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
**/node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Run this build by doing something like: | ||
# docker run --name #{VIS_NAME} --link #{VISDB_NAME}:mongo -p #{VIS_PORT}:8081 -d -v /vagrant/vis:/app -e 'CONSUMER_KEY=test' -e 'CONSUMER_SECRET=test' visualizer | ||
FROM node | ||
|
||
### Configuration Parameters ### | ||
# Configure Port | ||
ENV PORT 8081 | ||
# Configure Secret | ||
ENV SECRET "Test Secret" | ||
# Configure MONGO_URI | ||
ENV MONGO_URI "mongodb://mongo/visualizer" | ||
# Request Token URL | ||
ENV REQUEST_TOKEN_URL "https://queryengine:8080/oauth/request_token" | ||
# Access Token URL | ||
ENV ACCES_TOKEN_URL "https://queryengine:8080/oauth/access_token" | ||
# User Authorization URL | ||
ENV USER_AUTHORIZATION_URL "https://queryengine:8080/oauth/authorize" | ||
|
||
### OAuth Keys | ||
# These aren't populated by default. Passing an environment variable is preferred. | ||
|
||
# Consumer Key | ||
# ENV CONSUMER_KEY "test" | ||
# Consumer Secret | ||
# ENV CONSUMER_SECRET "test" | ||
|
||
# Set directory to the volume. | ||
WORKDIR /app | ||
|
||
# Install Dependencies then start | ||
CMD npm install && npm start |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
> Not ready yet! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
'use strict'; | ||
|
||
var async = require('async'), | ||
logger = require('./lib/logger'); | ||
|
||
async.auto({ | ||
environment: environment, | ||
database: [ 'environment', database ], | ||
certificate: [ 'environment', certificate ], | ||
httpd: [ 'environment', httpd ], | ||
models: [ 'database', models ], | ||
auth: [ 'models', 'httpd', auth ], | ||
routes: [ 'auth', 'models', 'httpd', routes ] | ||
}, complete); | ||
|
||
function environment(callback) { | ||
if (!process.env.SECRET) { | ||
logger.warn('No $SECRET present. Generating a temporary random value.'); | ||
process.env.SECRET = require('crypto').randomBytes(256); | ||
} | ||
if (!process.env.PORT) { | ||
logger.warn('No $PORT present. Choosing a sane default, 8081.'); | ||
process.env.PORT = 8081; | ||
} | ||
if (!process.env.MONGO_URI) { | ||
logger.warn('No $MONGO_URI present. Defaulting to `mongodb://localhost/vis`.'); | ||
process.env.MONGO_URI = 'mongodb://localhost/vis'; | ||
} | ||
// OAuth Stuff | ||
if (!process.env.REQUEST_TOKEN_URL) { | ||
logger.warn('No $REQUEST_TOKEN_URL present. Defaulting to `https://localhost:8080/oauth/request_token`.'); | ||
process.env.REQUEST_TOKEN_URL = 'https://localhost:8080/oauth/request_token'; | ||
} | ||
if (!process.env.ACCESS_TOKEN_URL) { | ||
logger.warn('No $ACCESS_TOKEN_URL present. Defaulting to `https://localhost:8080/oauth/access_token`.'); | ||
process.env.ACCESS_TOKEN_URL = 'https://localhost:8080/oauth/access_token'; | ||
} | ||
if (!process.env.USER_AUTHORIZATION_URL) { | ||
logger.warn('No $USER_AUTHORIZATION_URL present. Defaulting to `https://localhost:8080/oauth/authorize`.'); | ||
process.env.USER_AUTHORIZATION_URL = 'https://localhost:8080/oauth/authorize'; | ||
} | ||
if (!process.env.CONSUMER_KEY) { | ||
logger.warn('No $CONSUMER_KEY present. Defaulting to `test`.'); | ||
process.env.CONSUMER_KEY = 'test'; | ||
} | ||
if (!process.env.CONSUMER_SECRET) { | ||
logger.warn('No $CONSUMER_SECRET present. Defaulting to `test`.'); | ||
process.env.CONSUMER_SECRET = 'test'; | ||
} | ||
|
||
return callback(null); | ||
} | ||
|
||
/** | ||
* Setup the SSL Certificates. | ||
* @param {Function} next - The callback. | ||
*/ | ||
function certificate(next) { | ||
var fs = require('fs'); | ||
// Get the certificates. | ||
async.auto({ | ||
key: function (next) { fs.readFile('cert/server.key', 'utf8', next); }, | ||
cert: function (next) { fs.readFile('cert/server.crt', 'utf8', next); } | ||
}, function (error, results) { | ||
if (error) { generateCertificate(error, results, next); } | ||
else { return next(error, results); } | ||
}); | ||
|
||
/** | ||
* Detects if certs are missing and generates one if needed | ||
* @param {Error|null} error - If `error` is non-null, generate a certificate, since one doesn't exist. | ||
* @param {Object|null} results - Passed to `next`. | ||
* @param {Function} next - The callback. Is passed `error` (if not a certificate error) and `results`. | ||
*/ | ||
function generateCertificate(error, results, next) { | ||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; // Tell Node it's okay. | ||
if (error && error.code === 'ENOENT') { | ||
logger.warn('No certificates present in `cert/{server.key, server.crt}`. Generating a temporary certificate.'); | ||
require('pem').createCertificate({ days: 1, selfSigned: true }, function formatKey(error, keys) { | ||
if (error) { return next(error, null); } | ||
return next(null, {key: keys.serviceKey, cert: keys.certificate }); | ||
}); | ||
} else { | ||
return next(error, results); | ||
} | ||
} | ||
} | ||
|
||
function httpd(callback, data) { | ||
var server = require('express')(), | ||
passport = require('passport'); | ||
// Set the server engine. | ||
server.set('view engine', 'hbs'); | ||
// Middleware (https://github.com/senchalabs/connect#middleware) | ||
// Ordering ~matters~. | ||
// Logger | ||
server.use(require('morgan')('dev')); | ||
// Parses Cookies | ||
server.use(require('cookie-parser')(process.env.SECRET)); | ||
// Parses bodies. | ||
server.use(require('body-parser').urlencoded({ extended: true })); | ||
server.use(require('body-parser').json()); | ||
// Session store | ||
server.use(require('express-session')({ | ||
secret: process.env.SECRET, | ||
cookie: { secure: true } | ||
})); | ||
// Passport middleware. | ||
server.use(passport.initialize()); | ||
server.use(passport.session()); | ||
// Protects against CSRF. | ||
// server.use(require('csurf')()); | ||
// Compresses responses. | ||
server.use(require('compression')()); | ||
|
||
return callback(null, server); | ||
} | ||
|
||
function database(callback, data) { | ||
var connection = require('mongoose').connect(process.env.MONGO_URI).connection; | ||
connection.on('open', function () { | ||
logger.log('Connected to database on ' + process.env.MONGO_URI); | ||
return callback(null); | ||
}); | ||
connection.on('error', function (error) { | ||
return callback(error, connection); | ||
}); | ||
} | ||
|
||
function models(callback, data) { | ||
return callback(null); | ||
} | ||
|
||
function auth(callback, data) { | ||
var passport = require('passport'), | ||
OAuth1Strategy = require('passport-oauth1'); | ||
passport.use(new OAuth1Strategy({ | ||
requestTokenURL: process.env.REQUEST_TOKEN_URL, | ||
accessTokenURL: process.env.ACCESS_TOKEN_URL, | ||
userAuthorizationURL: process.env.USER_AUTHORIZATION_URL, | ||
consumerKey: process.env.CONSUMER_KEY, | ||
consumerSecret: process.env.CONSUMER_SECRET, | ||
callbackURL: 'https://127.0.0.1:' + process.env.PORT + '/auth/callback' | ||
}, | ||
function verify(token, tokenSecret, profile, done) { | ||
// TODO: Actually verify. | ||
console.log(arguments); | ||
done(null, { key: token, secret: tokenSecret }); | ||
} | ||
)); | ||
passport.serializeUser(function(token, done) { | ||
console.log('Serialize'); | ||
return done(null, token.key); | ||
}); | ||
passport.deserializeUser(function(id, done) { | ||
// TODO | ||
console.log('Deserialize'); | ||
return done(null, id); | ||
}); | ||
return callback(null); | ||
} | ||
|
||
function routes(callback, data) { | ||
var router = new require('express').Router(), | ||
ensureLoggedIn = require('connect-ensure-login').ensureLoggedIn, | ||
ensureLoggedOut = require('connect-ensure-login').ensureLoggedOut, | ||
passport = require('passport'); | ||
|
||
router.get('/auth', | ||
passport.authenticate('oauth') | ||
); | ||
router.get('/auth/callback', | ||
passport.authenticate('oauth', { failureRedirect: '/fail' }), | ||
function (req, res) { res.redirect('/'); } | ||
); | ||
router.get('/fail', function (req, res) { | ||
res.send('You failed.'); | ||
}); | ||
// Attach the router. | ||
data.httpd.use(router); | ||
callback(null, router); | ||
} | ||
|
||
function complete(error, data) { | ||
if (error) { logger.error(error); throw error; } | ||
// No errors | ||
require('https').createServer(data.certificate, data.httpd).listen(process.env.PORT, function () { | ||
logger.success('Server listening on port ' + process.env.PORT); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
var colors = require('cli-color'); | ||
|
||
module.exports = { | ||
log: function log(text) { | ||
console.log(text); | ||
}, | ||
warn: function warn(text) { | ||
console.log(colors.yellow(text)); | ||
}, | ||
error: function error(text) { | ||
console.log(colors.red(text)); | ||
}, | ||
success: function success(text) { | ||
console.log(colors.green(text)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
var mongoose = require('mongoose'), | ||
Schema = mongoose.Schema, | ||
ObjectId = Schema.Types.ObjectId, | ||
Mixed = Schema.Types.Mixed; | ||
|
||
var schema = Schema({}); | ||
|
||
// Define the model. | ||
module.exports = mongoose.model('Template', schema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"name": "scoop-visualizer", | ||
"version": "0.0.0", | ||
"description": "The SCOOP Visualizer", | ||
"main": "init.js", | ||
"scripts": { | ||
"start": "node init.js", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "Andrew Hobden <[email protected]>", | ||
"license": "MPL", | ||
"private": true, | ||
"dependencies": { | ||
"async": "^0.9.0", | ||
"bcrypt": "^0.7.8", | ||
"body-parser": "^1.3.1", | ||
"cli-color": "^0.3.2", | ||
"compression": "^1.0.7", | ||
"connect-ensure-login": "^0.1.1", | ||
"cookie-parser": "^1.3.1", | ||
"express-session": "^1.5.0", | ||
"csurf": "^1.2.1", | ||
"express": "^4.4.3", | ||
"hbs": "^2.7.0", | ||
"mongoose": "^3.8.12", | ||
"morgan": "^1.1.1", | ||
"oauthorize": "^0.1.0", | ||
"passport": "^0.2.0", | ||
"passport-http-oauth": "^0.1.3", | ||
"passport-local": "^1.0.0", | ||
"passport-oauth1": "^1.0.1", | ||
"pem": "^1.4.1" | ||
}, | ||
"jshintConfig": { | ||
"latedef": "nofunc", | ||
"node": true | ||
} | ||
} |