Permalink
Browse files

Ok authentication is working but im not happy with variables/function…

…s scope management
  • Loading branch information...
1 parent 78bc62f commit d473d4dd83ff73f17c4876fb84ee5bef95d4c1d6 @kuryaki committed Mar 26, 2013
Showing with 105 additions and 16 deletions.
  1. +8 −4 app.js
  2. +3 −1 package.json
  3. +84 −11 routes/index.js
  4. +10 −0 test/test.js
View
12 app.js
@@ -3,10 +3,10 @@
* Module dependencies.
*/
-var express = require('express')
- , routes = require('./routes')
- , http = require('http')
- , path = require('path');
+var express = require('express');
+var routes = require('./routes');
+var http = require('http');
+var path = require('path');
var app = express();
@@ -33,6 +33,10 @@ app.post('/', routes.transport);
app.post('/email', routes.send_email);
+app.get('/login', routes.login);
+
+app.get('/oauth2callback', routes.oauthcallback);
+
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
View
@@ -9,6 +9,8 @@
"express": "3.0.0rc3",
"ejs": "*",
"less-middleware": "*",
- "nodemailer": "~0.3.31"
+ "kue": "~0.5.0",
+ "nodemailer": "~0.4.1",
+ "superagent": "~0.13.0"
}
}
View
@@ -1,23 +1,96 @@
-var nodemailer = require("nodemailer");
+var nodemailer = require('nodemailer');
+var url = require('url');
+var request = require('superagent');
+var db = require('redis').createClient();
+
+//Worried about this scope
var smtpTransport = '';
+var access_token = '';
+var refresh_token = '';
+var user = '';
-exports.index = function(req, res){
- res.render('index', { title: 'Mass Personal Mailer' });
+
+exports.login = function(req, res){
+ var google_auth = {
+ protocol: 'https',
+ host: 'accounts.google.com/o/oauth2/auth',
+ query: {
+ scope: 'https://mail.google.com https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email',
+ redirect_uri: 'http://localhost:3000/oauth2callback',
+ response_type:'code',
+ client_id: '189084383123.apps.googleusercontent.com',
+ access_type: 'offline'
+ }
+ };
+ var href = url.format(google_auth);
+ res.send("<a href="+href+">Login</a>");
};
-exports.transport = function(req, res){
+var config = {
+ client_id: '189084383123.apps.googleusercontent.com',
+ client_secret:'bq7rXcxSUqo-lZ7PntMsBqN1',
+ redirect_uri: 'http://localhost:3000/oauth2callback',
+ grant_type: 'authorization_code'
+};
- smtpTransport = nodemailer.createTransport("SMTP",{
- service: "Gmail",
- auth: {
- name: req.body.name,
- user: req.body.auth_user,
- pass: req.body.auth_password //Application specific password
- }
+exports.oauthcallback = function(req, res){
+ config.code = req.query.code;
+ getTokens(res);
+};
+
+function getTokens(res){
+ request
+ .post('https://accounts.google.com/o/oauth2/token')
+ .type('form')
+ .send(config)
+ .end(function(error, response){
+ if (!error && response.statusCode == 200) {
+ access_token = response.body.access_token;
+ refresh_token = response.body.refresh_token;
+ verifyTokens(res);
+ }else {
+ res.send(500, error);
+ }
});
+}
+function verifyTokens(res){
+ request.get('https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token='+access_token,
+ function(error, response){
+ if (!error && response.statusCode == 200) {
+ user = response.body;
+ if(refresh_token){
+ db.set(user.email, refresh_token);
+ } else {
+ refresh_token = db.get(user.email);
+ }
+ getTransport(res);
+ }else {
+ res.send(500, error);
+ }
+ }
+ );
+}
+
+function getTransport(res){
+ var XOAuth2 = {
+ user: user.email,
+ clientId: config.client_id,
+ clientSecret: config.client_secret,
+ refreshToken: refresh_token,
+ accessToken: access_token
+ };
+
+ smtpTransport = nodemailer.createTransport("SMTP",{
+ service: "Gmail",
+ auth: {XOAuth2: XOAuth2}
+ });
res.render('mail', { title: 'Mass Personal Mailer' });
+}
+
+exports.index = function(req, res){
+ res.render('index', { title: 'Mass Personal Mailer' });
};
exports.send_email = function(req, res){
View
@@ -0,0 +1,10 @@
+var assert = require("assert");
+
+describe('Array', function(){
+ describe('#indexOf()', function(){
+ it('should return -1 when the value is not present', function(){
+ assert.equal(-1, [1,2,3].indexOf(5));
+ assert.equal(-1, [1,2,3].indexOf(0));
+ });
+ });
+});

0 comments on commit d473d4d

Please sign in to comment.