Skip to content

Commit

Permalink
added token(s) retrieval function + tests + makefile for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lyaunzbe committed Jan 5, 2013
1 parent 2c066f1 commit 33e9f80
Show file tree
Hide file tree
Showing 354 changed files with 61,945 additions and 51 deletions.
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
REPORTER = spec

test:
@NODE_ENV=test mocha \
--reporter $(REPORTER) \
--timeout 8000

test-w:
@NODE_ENV=test mocha \
--reporter $(REPORTER) \
--timeout 8000 \
--watch



.PHONY: test test-w
4 changes: 4 additions & 0 deletions config/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
"client_secret" : "ZYUifIK5crKoqNIDiOkAAZaz",
"client_id" : "532620919617.apps.googleusercontent.com"
};
128 changes: 81 additions & 47 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,86 @@ var request = require('request'),
exec = require('child_process').exec,
querystring = require('querystring');

var events = require('events');


module.exports = function(opts) {
if(!opts.redirect_uri){
opts.redirect_uri = 'http://localhost:3000/callback';
}

var gAuth = {};

var endpoint = 'https://accounts.google.com/o/oauth2/auth';

function getAuthCode(callback){
var qs = {
response_type: 'code',
client_id: opts.client_id,
redirect_uri: opts.redirect_uri,
scope: 'https://www.googleapis.com/auth/userinfo.profile'
};

var url = '\''+endpoint + '?' + querystring.stringify(qs) +'\'';

exec('open '+url, function(err){
if(err !== null){
console.log(err);
}
});

var server = require('http').createServer(function(req, res) {
if(req.url.match(/callback/)) return parseCode(req, res)
}).listen(3000);

function parseCode(req,res){
//for gdrive-cli:
//instead of just closing the connection, redirect to an informational page
server.close();

//split url by ? so we just have the querystring left
//extract out the auth code
callback(querystring.parse(req.url.split('?')[1])['code']);
}
}

gAuth.getAuthCode = getAuthCode;

gAuth.

return gAuth;
};
if(!opts.redirect_uri){
opts.redirect_uri = 'http://localhost:3000/callback';
}

var gAuth = {};

var endpoint = 'https://accounts.google.com/o/oauth2/auth';

/**
* Constructs a google OAuth2 request url using the provided opts.
* Spawns an http server to handle the redirect. Once user authenticates
* and the server parses the auth code, the server process is closed
* (Assuming the user has closed the window. For future, add redirect after
* authentication to a page with instructions to close tab/window).
*/
function getAuthCode(callback){
var qs = {
response_type: 'code',
client_id: opts.client_id,
redirect_uri: opts.redirect_uri,
scope: 'https://www.googleapis.com/auth/userinfo.profile'
};

var uri = '\''+endpoint + '?' + querystring.stringify(qs) +'\'';

exec('open '+uri, function(err){
if(err !== null){
callback(err);
}
var server = require('http').createServer(function(req, res) {
if(req.url.match(/callback/)) return parseCode(req, res)
}).listen(3000);

function parseCode(req,res){
//for gdrive-cli:
//instead of just closing the connection, redirect to an informational page
server.close();

//split url by ? so we just have the querystring left
//extract out the auth code
callback(null, querystring.parse(req.url.split('?')[1])['code']);
}

});
}

/**
* Given the acquired authorization code and the provided opts,
* construct a POST request to acquire the access token and refresh
* token.
*
* @param {String} code The acquired authorization code
*/
function getTokens(code, callback){
var uri = 'https://accounts.google.com/o/oauth2/token';
var form = {
code : code,
client_id : opts.client_id,
client_secret: opts.client_secret,
redirect_uri: opts.redirect_uri,
grant_type: 'authorization_code'
};

request.post({url:uri, form: form}, function(err,req, body){
if(err !== null){
callback(err);
}else{
callback(null, JSON.parse(body));
}
});
}

gAuth.getAuthCode = getAuthCode;

gAuth.getTokens = getTokens;

//gAuth.refreshToken = refreshToken;

return gAuth;

};
1 change: 1 addition & 0 deletions node_modules/.bin/_mocha

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/mocha

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions node_modules/chai/.npmignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 33e9f80

Please sign in to comment.