Created
November 16, 2016 15:34
-
-
Save glynnbird/40df1a93806b70036ea7143507507b08 to your computer and use it in GitHub Desktop.
Cloudant Envoy Twitter Authentication - Part 3
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
var path = require('path'), | |
express = require('express'), | |
crypto = require('crypto'), | |
CryptoJS = require("crypto-js"), | |
uuid = require('uuid'), | |
cloudant = null, | |
tokensdb = null, | |
envoydb = null, | |
cfenv = require('cfenv'), | |
appEnv = cfenv.getAppEnv(), | |
appurl = (appEnv.app.application_uris)?'https://'+appEnv.app.application_uris[0]:'http://localhost:'+appEnv.port, | |
router = express.Router(); | |
var encrypt = function(str, key) { | |
return CryptoJS.AES.encrypt(str, key).toString(); | |
}; | |
var decrypt = function(str, key) { | |
var bytes = CryptoJS.AES.decrypt(str, key); | |
return bytes.toString(CryptoJS.enc.Utf8); | |
}; | |
var getOrCreateUser = function(profile, callback) { | |
var user_id = parseInt(profile.id).toString(36); | |
var name = profile.displayName; | |
envoy.auth.getUser(user_id, function (err, data) { | |
if (err) { | |
var meta = { | |
user_id: user_id, | |
name: name | |
}; | |
console.log("created new user", meta); | |
var password = uuid.v4(); | |
meta.password = encrypt(password, process.env.CLIENT_ID); | |
envoy.auth.newUser(user_id, password, meta, function (err, data) { | |
envoy.auth.getUser(user_id, function(err, data) { | |
callback(err, data); | |
}); | |
}) | |
} else { | |
console.log("User already exists", data); | |
callback(err, data); | |
} | |
}); | |
}; | |
// passport | |
var passport = require('passport'), | |
TwitterStrategy = require('passport-twitter').Strategy; | |
var opts = { | |
consumerKey: process.env.TWITTER_API_KEY, | |
consumerSecret: process.env.TWITTER_API_SECRET, | |
callbackURL: appurl + '/_twitter/callback' | |
}; | |
passport.use(new TwitterStrategy(opts , function(accessToken, refreshToken, profile, done) { | |
getOrCreateUser(profile, done); | |
})); | |
router.get('/_twitter', passport.authenticate('twitter', {session: false})); | |
router.get('/_twitter/callback', passport.authenticate('twitter', {session: false}), function(req, res) { | |
var data = req.user; | |
data._id = uuid.v4(); | |
data.ts = new Date().getTime() + 1000*60*60; | |
delete data._rev; | |
tokensdb.insert(data, function (err, data) { | |
res.redirect('/#?token=' + data.id) | |
}); | |
}); | |
// my custom API call | |
router.get('/_token/:token', function(req, res) { | |
tokensdb.get(req.params.token, function(err, data) { | |
if (err) { | |
res.send({ok: false}); | |
} else { | |
tokensdb.destroy(data._id, data._rev); | |
if (data.ts > new Date().getTime()) { | |
data.meta.password = decrypt(data.meta.password, process.env.CLIENT_ID); | |
res.send(data); | |
} else { | |
res.send({ok: false, msg: 'out of date'}); | |
} | |
} | |
}); | |
}); | |
// setup Envoy to | |
// - log incoming requests | |
// - switch off demo app | |
// - serve out our static files | |
// - add our routes | |
var opts = { | |
production: true, | |
static: path.join(__dirname, './public'), | |
router: router, | |
middleware: [ passport.initialize() ] | |
}; | |
// start up the web server | |
var envoy = require('cloudant-envoy')(opts); | |
envoy.events.on('listening', function() { | |
// setup tokens database | |
cloudant = envoy.cloudant; | |
cloudant.db.create('tokens'); | |
tokensdb = cloudant.db.use('tokens'); | |
console.log('[OK] Server is up'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment