Skip to content

Commit

Permalink
Fixes fossasia#342: adding support for sending notification if access…
Browse files Browse the repository at this point in the history
… token revoked
  • Loading branch information
poonai committed Aug 9, 2017
1 parent 291af60 commit ac20c60
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 5 deletions.
11 changes: 9 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ var passport = require("passport");
var dotenv = require("dotenv");
var session = require("express-session");
var mongoose = require("mongoose");

var task = require("./backend/task");
var cron = require('node-cron');
dotenv.config({path: './.env'});
require('./util/passport')(passport);

Expand Down Expand Up @@ -62,7 +63,6 @@ app.use("/deploy", require("./routes/deploy"));
app.use("/dashboard", require("./routes/dashboard"));
app.use("/ci", require("./routes/ci"));
app.use("/repository", require("./routes/repository"));

/**
* Server-side Event Handling
*/
Expand Down Expand Up @@ -92,6 +92,13 @@ io.on('connection', function(socket){
})
});


// Tasks runs on every 24 hrs

cron.schedule('* 23 * * *', function(){
task.checkExpiredToken();
});

// catch 404 and forward to error handler
app.use(function(req, res, next) {
res.status(404);
Expand Down
22 changes: 22 additions & 0 deletions backend/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,25 @@ exports.deleteHook = function (name, hook, accessToken, callback) {
}
});
};

/**
* Get user details by access token
* @param accessToken: Access Token of the user
* @param callback: callback
*/

exports.retriveUser = function (accessToken, callback) {
request({
url: 'https://api.github.com/users/',
headers: {
'User-Agent': 'Yaydoc',
'Authorization': 'token ' + crypter.decrypt(accessToken)
}
}, function (error, response, body) {
if (response.status !== 200) {
callback({statusCode: response.status}, null)
} else {
callback(error, JSON.parse(body))
}
})
};
21 changes: 21 additions & 0 deletions backend/mailer.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,24 @@ exports.sendMailOnBuild = function (buildStatus, email, repository) {
}
});
};

exports.sendMailOnTokenFailure = function (email) {
var client = nodemailer.createTransport(sgTransport(options));

var textContent = 'Access token for Yaydoc is expired. Sign in once again to continue the service';
var htmlContent = 'Access token for Yaydoc is expired. Sign in once again to continue the service';

client.sendMail({
from: '[email protected]',
to: email,
subject: 'Token expired - Yaydoc',
text: textContent,
html: htmlContent
}, function (error, info) {
if (error) {
console.log(error);
} else {
console.log('Message sent: ' + info.response);
}
});
};
27 changes: 27 additions & 0 deletions backend/queue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const github = require("./github.js");
const mailer = require("./mailer");
const async = require("async");
User = require("../model/user");
var tokenRevokedQueue = async.queue(function (user, done) {
github.retriveUser(user.token, function (error, userData) {
if (error) {
if (user.expired === false) {
mailer.sendMailOnTokenFailure(user.email);
User.updateUserById(user.id, {
expired: true
}, function(error, data) {
if (error) {
console.log(error);
}
});
}
done();
} else {
done();
}
})
}, 2);

exports.addTokenRevokedJob = function(user) {
tokenRevokedQueue.push(user);
};
34 changes: 34 additions & 0 deletions backend/task.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const github = require("./github")
const queue = require("./queue")

User = require("../model/user");

exports.checkExpiredToken = function () {
User.count(function (error, count) {
if (error) {
console.log(error);
} else {
var page = 0;
if (count < 10) {
page = 1;
} else {
page = count / 10;
if (page * 10 < count) {
page = (count + 10) /10;
}
}
for (var i = 0; i <= page; i++) {
User.paginateUsers(i, 10,
function (error, users) {
if (error) {
console.log(error);
} else {
users.forEach(function(user) {
queue.addTokenRevokedJob(user);
})
}
})
}
}
})
}
36 changes: 35 additions & 1 deletion model/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const userSchema = mongoose.Schema({
token: String,
email: String,
name: String,
username: String
username: String,
expired: Boolean
});

const User = module.exports = mongoose.model('User', userSchema);
Expand All @@ -27,3 +28,36 @@ module.exports.getUserById = function(id, callback) {
module.exports.getUserByUsername = function(username, callback) {
User.findOne({username: username}, callback);
};

/**
* Count the number of repository
*/

module.exports.countUsers = function (callback) {
User.count({}, callback);
};

/**
* paginates repositories
* @param page: n'th page
* @param limit: limit for number of repository to return
*/

module.exports.paginateUsers = function (page, limit, callback) {
var skip = 0;
if (page > 1) {
skip = page * limit;
}
User.find({}).skip(skip).limit(limit).exec(callback);
};

/**
* Update the user by Github's Users id
* @param id: Github's user id
* @param update: user update
*/
module.exports.updateUserById = function(id, update, callback) {
User.update({id: id}, update, function(error, data) {
callback(error, data);
});
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"mocha": "^3.4.2",
"mongoose": "^4.11.0",
"morgan": "~1.8.1",
"node-cron": "^1.2.0",
"nodemailer": "^4.0.1",
"nodemailer-sendgrid-transport": "^0.2.0",
"passport": "^0.3.2",
Expand Down
11 changes: 9 additions & 2 deletions util/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ module.exports = function (passport) {
return done(error);
}
if (user) {

User.updateUserById(profile.id, {
expired: false,
token: crypter.encrypt(accessToken)
}, function(error, data) {
if (error) {
console.log(error);
}
});
return done(null, user);
} else {
let newUser = new User();
Expand All @@ -35,6 +42,7 @@ module.exports = function (passport) {
newUser.name = profile.displayName;
newUser.email = profile.emails[0].value;
newUser.username = profile.username;
newUser.expired = false;

newUser.save(function (error) {
if (error) {
Expand All @@ -56,5 +64,4 @@ module.exports = function (passport) {
cb(null, profile);
}
));

};

0 comments on commit ac20c60

Please sign in to comment.