Permalink
Browse files

Adding Google OAuthV2 Support

  • Loading branch information...
1 parent 55c2d81 commit aa86fe2c59b5ca89b1c90fe9a8824b8055416632 @jrgleason committed Aug 23, 2012
Showing with 270 additions and 58 deletions.
  1. +11 −0 README
  2. +53 −2 lib/google/interface.js
  3. +183 −0 lib/google/oauth.js
  4. +11 −0 lib/google/oauthimpl.js.bkp
  5. +1 −1 lib/mongo/cart.js
  6. +2 −2 lib/mongo/file.js
  7. +5 −51 lib/mongo/user.js
  8. +4 −2 package.json
View
11 README
@@ -33,3 +33,14 @@ Mongo/RESTi(ish) Integration
bullet = require('./lib/bullettrain').useRest('mongo')
app.post('/rest/catalogentries', bullet.postCatalogEntry,routes.goHome);
+
+
+Google OAuth v2 (With ExpressJS)
+
+google = require('bullettrain').useInterface('google')
+google.setClientId(GOOGLE_CLIENT_ID)
+google.setClientSecret(GOOGLE_CLIENT_SECRET)
+
+app.get('/auth/google', google.requestCode)
+app.get('/auth/google/callback', google.callbackHandler, google.getUserInformation, routes.goHome)
+
View
@@ -32,7 +32,7 @@ exports.addProduct = function(req, res,next){
"<scp:availability>"+req.body.availability+"</scp:availability> "+
"<scp:condition>"+req.body.condition+"</scp:condition> "+
"</entry>"
- console.log(itemData)
+ //console.log(itemData)
var post_options = {
host: 'content.googleapis.com',
port: '443',
@@ -54,7 +54,7 @@ exports.addProduct = function(req, res,next){
return next();
});
});
- post_req.write(itemData)
+ post_req.write()
post_req.end()
req.on('error', function(e) {
console.error(e);
@@ -262,3 +262,54 @@ exports.deleteProduct = function(req, res, next){
return next()
}
}
+
+var googleProvider = require('./oauth').GoogleProvider,
+ googleProvider = new GoogleProvider(),
+ clientId,
+ clientSecret;
+
+exports.setClientId = googleProvider.setClientId
+exports.setClientSecret = googleProvider.setClientSecret
+
+exports.requestCode = function(req, res){
+ googleProvider.requestCode(res)
+}
+
+exports.callbackHandler = function(req,res,next){
+ console.log("Code: "+req.query["code"]);
+ if(req.query["code"] != undefined){
+ googleProvider.requestToken(req.query["code"], next, req);
+ }
+}
+
+exports.getUserInformation = function(req, res, next){
+ console.log("FROM UserInfo: "+req.session.access_token);
+ googleProvider.getUserInformation(req.session.access_token, next, req.session)
+}
+
+exports.logout = function(req, res, next){
+ var authToken = req.session.access_token
+ googleProvider.revokeToken(authToken, res, function(){
+ req.session.access_token = null;
+ req.session.userinfo = null;
+ return next()
+ })
+}
+
+exports.getFileList = function(req, res, next){
+ console.log("FROM FILELIST: "+req.session.access_token);
+ googleProvider.getFileList(next, req.session)
+}
+
+//exports.authenticate = function(req, res) {
+// oauthimpl.authenticate(req,res);
+//}
+
+//exports.reauthenticate = function(req, res, next) {
+// oauthimpl.reauthenticate(req,res, next);
+//}
+
+//exports.callback = function(req, res) {
+// oauthimpl.callback(req,res);
+//}
+
View
@@ -0,0 +1,183 @@
+//STATICS
+var GOOGLE_CLIENT_ID,
+ SSL_PORT = "443",
+ GOOGLE_CLIENT_SECRET,
+ GOOGLE_URLS = {
+ USER_INFO: "/oauth2/v1/userinfo",
+ GET_TOKEN: "/o/oauth2/token",
+ REVOKE_TOKEN: "/o/oauth2/revoke",
+ GOOGLE_DRIVE_FILES: "/drive/v2/files"
+ //GOOGLE_DRIVE: "/discovery/v1/apis/drive/v1/rest"
+ },
+ GOOGLE_DOMAINS = {
+ APIS:"www.googleapis.com",
+ ACCOUNTS:"accounts.google.com"
+ },
+ GOOGLE_REVOKE_URL = "https://"+GOOGLE_DOMAINS.APIS+GOOGLE_URLS.REVOKE_TOKEN,
+ GOOGLE_SCOPES = {
+ PROFILE : 'https://www.googleapis.com/auth/userinfo.profile',
+ DRIVE_FILE : 'https://www.googleapis.com/auth/drive.file',
+ DRIVE : 'https://www.googleapis.com/auth/drive'
+ },
+ //TODO: This should integrate with the env variable instead of localhost
+ REDIRECT_URL = 'http://localhost:3000/auth/google/getToken',
+ GOOGLE_AUTH_URL = 'https://accounts.google.com/o/oauth2/auth',
+ GOOGLE_RESPONSE_TYPE =
+ {
+ CODE:'code'
+ },
+ GOOGLE_ACCESS_TYPE =
+ {
+ ONLINE:'online',
+ OFFLINE:'offline'
+ },
+ GOOGLE_GRANT_TYPE =
+ {
+ AUTHORIZATION_CODE:'authorization_code'
+ }
+
+//IMPORTS
+var https = require('https'),
+ querystring = require('querystring')
+
+
+//CONSTRUCTORS
+GoogleProvider = function(){};
+GoogleProvider.prototype.dummyData = [];
+
+GoogleProvider = function(){
+}
+
+//SCAFFOLD
+
+GoogleProvider.prototype.getFileList = function (next, session){
+ console.log("!!"+GOOGLE_URLS.GOOGLE_DRIVE_FILES)
+ var options = {
+ host: GOOGLE_DOMAINS.APIS,
+ port: SSL_PORT,
+ path: GOOGLE_URLS.GOOGLE_DRIVE_FILES,
+ headers: {
+ Authorization: 'Bearer '+ session.access_token,
+ }
+ }
+ console.log("Options: " +JSON.stringify(options))
+ var post_req = https.request(options, function(res) {
+ res.setEncoding('utf8')
+ res.body = ''
+ res.on('data', function (chunk) {
+ res.body += chunk
+ })
+ res.on('end', function() {
+ console.log(res.body)
+ session.userinfo = JSON.parse(res.body)
+ return next();
+ })
+ })
+ post_req.write("")
+ post_req.end()
+}
+
+GoogleProvider.prototype.setClientId = function (clientId){
+ GOOGLE_CLIENT_ID = clientId
+}
+
+GoogleProvider.prototype.setClientSecret = function (clientSecret){
+ GOOGLE_CLIENT_SECRET = clientSecret
+}
+
+GoogleProvider.prototype.requestCode = function (response){
+ var requestParams = {
+ scope:GOOGLE_SCOPES.PROFILE+" "+GOOGLE_SCOPES.DRIVE,
+ response_type:GOOGLE_RESPONSE_TYPE.CODE,
+ access_type:GOOGLE_ACCESS_TYPE.ONLINE,
+ redirect_uri:REDIRECT_URL,
+ client_id:GOOGLE_CLIENT_ID
+ }
+ console.log("RequestParams: "+querystring.stringify(requestParams))
+ response.redirect(GOOGLE_AUTH_URL+'?'+querystring.stringify(requestParams))
+}
+
+GoogleProvider.prototype.revokeToken = function (authToken, response, callback){
+ var requestParams = {
+ token:authToken
+ }
+ var post_options = {
+ host: GOOGLE_DOMAINS.APIS,
+ port: SSL_PORT,
+ path: GOOGLE_URLS.REVOKE_TOKEN+'?'+querystring.stringify(requestParams),
+ };
+ var post_req = https.request(post_options, function(res) {
+ res.setEncoding('utf8');
+ res.body = '';
+ res.on('data', function (chunk) {
+ res.body += chunk;
+ })
+ res.on('end', function() {
+ console.log("Log Out is :"+res.body)
+ callback();
+ })
+ })
+ post_req.write("");
+ post_req.end()
+}
+
+GoogleProvider.prototype.getUserInformation = function(accessToken, callback, session){
+ var options = {
+ host: GOOGLE_DOMAINS.APIS,
+ port: SSL_PORT,
+ path: GOOGLE_URLS.USER_INFO,
+ headers: {
+ Authorization: 'Bearer '+accessToken,
+ }
+ }
+ var post_req = https.request(options, function(res) {
+ res.setEncoding('utf8');
+ res.body = '';
+ res.on('data', function (chunk) {
+ res.body += chunk;
+ })
+ res.on('end', function() {
+ session.userinfo = JSON.parse(res.body);
+ callback();
+ })
+ })
+ post_req.write("")
+ post_req.end()
+}
+
+GoogleProvider.prototype.requestToken = function (code,next,req){
+ var postParams = querystring.stringify({
+ code:code,
+ redirect_uri:REDIRECT_URL,
+ client_id:GOOGLE_CLIENT_ID,
+ client_secret:GOOGLE_CLIENT_SECRET,
+ grant_type:GOOGLE_GRANT_TYPE.AUTHORIZATION_CODE
+ })
+ console.log("POST_PARAMS = "+postParams)
+ var post_options = {
+ host: GOOGLE_DOMAINS.ACCOUNTS,
+ port: SSL_PORT,
+ path: GOOGLE_URLS.GET_TOKEN,
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/x-www-form-urlencoded',
+ 'Content-Length': postParams.length
+ }
+ };
+ var post_req = https.request(post_options, function(res) {
+ res.setEncoding('utf8');
+ res.body = '';
+ res.on('data', function (chunk) {
+ res.body += chunk;
+ })
+ res.on('end', function() {
+ console.log("Response is :"+res.body)
+ var obj = JSON.parse(res.body);
+ req.session.access_token = obj.access_token;
+ console.log("From Oauth: "+req.session.access_token);
+ return next();
+ })
+ })
+ post_req.write(postParams);
+ post_req.end()
+}
@@ -0,0 +1,11 @@
+var passportProvider = require('./oauth').PassportProvider,
+ passportProvider = new PassportProvider(),
+
+exports.requestToken = function(callback){
+ passportProvider.requestToken(callback)
+}
+
+exports.callback = function(req, res) {
+ passportProvider.callback(req,res)
+}
+
View
@@ -12,7 +12,7 @@ CartProvider.prototype.newCart = function(callback) {
cart.items=[]
cart.created_at = new Date()
cart.orderTotal = 0
- console.log(JSON.stringify(cart));
+ //console.log(JSON.stringify(cart));
cart_collection.insert(cart, function() {
callback(null, cart);
});
View
@@ -26,7 +26,7 @@ FileProvider = function() {
FileProvider.prototype.addFile = function(file, contentType, callback){
var test = uuid.v4();
- console.log(JSON.stringify(test))
+ //console.log(JSON.stringify(test))
var gridStore = new GridStore(this.db, uuid.v4(), 'w', {
"content_type": contentType,
"metadata":{
@@ -44,7 +44,7 @@ FileProvider.prototype.addFile = function(file, contentType, callback){
FileProvider.prototype.streamFile = function(res, _id, contenttype){
var properId = this.db.bson_serializer.ObjectID.createFromHexString(_id)
- console.log(properId);
+ //console.log(properId);
res.contentType(contenttype);
var readstream = gfs.createReadStream(properId, {
"content_type": contenttype,
View
@@ -1,42 +1,10 @@
-var Db = require('mongodb').Db;
-var Connection = require('mongodb').Connection;
-var Server = require('mongodb').Server;
-var BSON = require('mongodb').BSON;
-var ObjectID = require('mongodb').ObjectID;
+var documentProvider = require('./document').DocumentProvider
-var dbpassword = process.env.OPENSHIFT_NOSQL_DB_PASSWORD || 'password';
-var dbuser = process.env.OPENSHIFT_NOSQL_DB_USERNAME || 'admin';
-var userCounter = 1;
+UserProvider = function(){
+ this.setType('users')
+}
-UserProvider = function(){};
-UserProvider.prototype.dummyData = [];
-
-UserProvider = function(host, port) {
- this.db= new Db('myRadio', new Server(host, port, {auto_reconnect: true}, {}));
- this.db.open(function(err,db){
- db.authenticate(dbuser, dbpassword, function(err, result) {});
- });
-
-};
-
-UserProvider.prototype.getCollection= function(callback) {
- this.db.collection('users', function(error, user_collection) {
- if( error ) callback(error);
- else callback(null, user_collection);
- });
-};
-
-UserProvider.prototype.findAll = function(callback) {
- this.getCollection(function(error, user_collection) {
- if( error ) callback(error)
- else {
- user_collection.find().toArray(function(error, results) {
- if( error ) callback(error)
- else callback(null, results)
- });
- }
- });
-};
+UserProvider.prototype = new DocumentProvider()
UserProvider.prototype.findByDisplayName = function(id, callback) {
@@ -51,18 +19,6 @@ UserProvider.prototype.findByDisplayName = function(id, callback) {
});
};
-UserProvider.prototype.findByIdentifier = function(id, callback) {
- this.getCollection(function(error, user_collection) {
- if( error ) callback(error)
- else {
- user_collection.findOne({identifier: id}, function(error, result) {
- if( error ) callback(error)
- else callback(null, result)
- });
- }
- });
-};
-
UserProvider.prototype.save = function(users, callback) {
this.getCollection(function(error, user_collection) {
if( error ) callback(error)
@@ -85,5 +41,3 @@ UserProvider.prototype.save = function(users, callback) {
}
});
};
-
-exports.UserProvider = UserProvider;
Oops, something went wrong.

0 comments on commit aa86fe2

Please sign in to comment.