mirror of
https://github.com/QuiteAFancyEmerald/Holy-Unblocker.git
synced 2025-05-15 12:50:01 -04:00
Cookie Auth
This commit is contained in:
parent
8d94f42980
commit
c358c02579
2 changed files with 55 additions and 57 deletions
110
auth.js
110
auth.js
|
@ -1,64 +1,62 @@
|
|||
/**
|
||||
* Login Class
|
||||
*/
|
||||
function Login() {
|
||||
// sessionId -> user map
|
||||
this.sessionMap = {
|
||||
99999: { name: 'HolyUBUser', email: 'HolyUBPass' }
|
||||
};
|
||||
}
|
||||
/**
|
||||
* Say Hello {name} to the user
|
||||
*/
|
||||
Login.prototype.hello = function(sessionId) {
|
||||
if (this.sessionMap[sessionId] == null) {
|
||||
return this.isLoggedIn;
|
||||
#!/usr/bin/env node
|
||||
|
||||
var crypto = require('crypto');
|
||||
var os = require('os');
|
||||
var querystring = require('querystring');
|
||||
var url = require('url');
|
||||
|
||||
var Cookies = require('cookies');
|
||||
|
||||
var settings = {
|
||||
hashes: [],
|
||||
redirect: '/',
|
||||
};
|
||||
|
||||
module.exports = function(env) {
|
||||
for (var k in env) {
|
||||
settings[k] = env[k];
|
||||
}
|
||||
return module.exports;
|
||||
};
|
||||
|
||||
module.exports.auth = function(req, res, next) {
|
||||
// Allow using with express as well as socket.io
|
||||
next = next || res;
|
||||
var cookies = new Cookies(req);
|
||||
var hash = cookies.get('session') ?
|
||||
module.exports.hash(cookies.get('session')) : '';
|
||||
if (settings.hashes.indexOf(hash) >= 0) {
|
||||
next();
|
||||
} else {
|
||||
next(new Error('Bad session key.'));
|
||||
}
|
||||
};
|
||||
/**
|
||||
* Get Current Session id user name
|
||||
*/
|
||||
Login.prototype.getName = function(sessionId) {
|
||||
return this.sessionMap[sessionId].name;
|
||||
|
||||
module.exports.sign = function(req, res, next) {
|
||||
var cookies = new Cookies(req, res);
|
||||
var query = url.parse(req.url, true).query;
|
||||
cookies.set('session', query.key ? module.exports.hash(query.key) : null);
|
||||
res.writeHead(302, { location: query.path ? query.path : settings.redirect });
|
||||
res.end();
|
||||
};
|
||||
|
||||
/**
|
||||
* Get Current Session id user email
|
||||
*/
|
||||
|
||||
/**
|
||||
* Check whether the given session id is valid (is in sessionMap) or not.
|
||||
*/
|
||||
Login.prototype.isLoggedIn = function(sessionId) {
|
||||
return sessionId in this.sessionMap;
|
||||
module.exports.generate = function() {
|
||||
var key = crypto.randomBytes(24).toString('base64');
|
||||
var hash = module.exports.hash(module.exports.hash(key));
|
||||
settings.hashes.push(hash);
|
||||
return { key: key, hash: hash };
|
||||
};
|
||||
|
||||
/**
|
||||
* Create a new session id for the given user.
|
||||
*/
|
||||
Login.prototype.login = function() {
|
||||
var sessionId = new Date().getTime();
|
||||
return sessionId;
|
||||
module.exports.hash = function(key) {
|
||||
var hmac = crypto.createHmac('SHA256', key);
|
||||
hmac.update(key);
|
||||
return hmac.digest('base64');
|
||||
};
|
||||
|
||||
/**
|
||||
* Remove specific refreshed session from SessionMap
|
||||
**/
|
||||
Login.prototype.RefreshSession = function(_sessionId) {
|
||||
// Delete the session id from sessionMap
|
||||
delete this.sessionMap[_sessionId];
|
||||
return "done";
|
||||
};
|
||||
|
||||
/**
|
||||
* Logout from the server
|
||||
*/
|
||||
Login.prototype.logout = function(sessionId) {
|
||||
console.log('logout::' + sessionId);
|
||||
// Delete the session id from sessionMap
|
||||
delete this.sessionMap[sessionId];
|
||||
|
||||
};
|
||||
|
||||
// Export the Login class
|
||||
module.exports = new Login();
|
||||
if (require.main === module) {
|
||||
var pair = module.exports.generate();
|
||||
console.log('Call authlink.generate() for a keypair or add\n' +
|
||||
'authlink({hashes:[\'' + pair.hash + '\']})\n' +
|
||||
'and then authenticate on authlink.sign with the querystring\n?' +
|
||||
querystring.stringify({ key: pair.key }));
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue