Skip to content

Instantly share code, notes, and snippets.

@adambom
Last active May 8, 2019 03:02
Show Gist options
  • Save adambom/ca63fede8181d5567e4b to your computer and use it in GitHub Desktop.
Save adambom/ca63fede8181d5567e4b to your computer and use it in GitHub Desktop.
user.js
var pb = require('../lib/pb');
var pbHelpers = require('../lib/pb-helpers');
var user = require('./user');
var auction = require('./auction');
var subject = require('./subject');
var CommandType = pb.PBCommandEnvelope.CommandType;
var ErrorType = pb.PBError.ErrorType;
var handlers = {};
var noop = function () {
return new pb.PBCommandEnvelope();
};
handlers[CommandType.AUCTION_NEW_REQUEST] = auction.create;
handlers[CommandType.AUCTION_GET_REQUEST] = auction.get;
handlers[CommandType.AUCTION_LIST_REQUEST] = auction.list;
handlers[CommandType.BID_NEW_REQUEST] = noop;
handlers[CommandType.BID_GET_REQUEST] = noop;
handlers[CommandType.BID_LIST_REQUEST] = noop;
handlers[CommandType.BID_UPDATE_REQUEST] = noop;
handlers[CommandType.SUBJECT_NEW_REQUEST] = subject.create;
handlers[CommandType.USER_SIGNUP_REQUEST] = user.signup;
handlers[CommandType.USER_LOGIN_REQUEST] = user.login;
var NO_AUTH_ACTIONS = {};
NO_AUTH_ACTIONS[CommandType.USER_LOGIN_REQUEST] = true;
NO_AUTH_ACTIONS[CommandType.USER_SIGNUP_REQUEST] = true;
exports.run = function *(session, cred, envelope) {
var valid = false;
var us;
if (cred.userID) {
if (cred.sessionID && cred.authToken) {
us = yield session.db.sessions.findOne({
userID: session.db.user.id(cred.userID),
authToken: session.db.sessions.id(cred.sessionID)
});
if (us && us.authToken === cred.authToken) {
valid = true;
}
}
} else {
valid = !!NO_AUTH_ACTIONS[envelope.type];
}
if (!valid) {
envelope.error = new pb.PBError({
code: ErrorType.INVALID_SESSION,
reason: 'Invalid session'
});
return envelope;
}
return exports.runCommand(session, cred.userID, envelope);
};
exports.runCommand = function *(session, userID, envelope) {
var handler = handlers[envelope.type];
try {
return yield handler(session, userID, envelope);
} catch (e) {
if (e instanceof pbHelpers.PBException) {
envelope.error = new pb.PBError({
code: e.code,
reason: e.reason,
reasonIdent: e.reasonIdent
});
} else {
this.throw(e);
}
return envelope;
}
}
var koa = require('koa');
var app = koa();
var logger = require('koa-logger');
var router = require('koa-router');
var koaBody = require('koa-body')();
var actions = require('./actions');
var protobuf = require('./routes/protobuf');
var db = require('./db');
app.use(logger());
app.use(function *(next) {
this.db = db;
yield next;
});
app.use(router(app));
app.post('/protobuf', koaBody, protobuf);
module.exports = app;
if (!module.parent) {
app.listen(1337);
console.log('listening on port 1337');
}
var monk = require('monk');
var wrap = require('co-monk');
var db = monk('localhost:45000/test');
db.users = wrap(db.get('users'));
db.sessions = wrap(db.get('sessions'));
db.auctions = wrap(db.get('auctions'));
db.subjects = wrap(db.get('subjects'));
module.exports = db;
var logger = require('koa-logger');
var actions = require('../actions');
var pb = require('../lib/pb');
module.exports = function *() {
var envelope;
var sessionID;
var authToken;
var userID = 0;
var decoder = this.query.base64 ?
pb.PBCommandEnvelope.decode64 :
pb.PBCommandEnvelope.decode;
var data = this.query.base64 ?
this.request.body.data :
this.request.body;
var auth = this.get('Authorization') || this.cookies.get('session_token');
if (auth) {
auth = auth.replace('token=', '').split('|');
try {
userID = auth[0];
sessionID = auth[1];
authToken = auth[2];
} catch (e) {
console.error(e);
this.throw(401);
}
}
try {
envelope = decoder(data);
} catch (e) {
this.throw(400, 'Could not parse command envelope');
}
var cred = {
userID: userID,
sessionID: sessionID,
authToken: authToken
};
try {
var response = yield actions.run(this, cred, envelope);
} catch (e) {
// Breaks the error logging
// this.throw();
}
if (this.query.debug) {
this.body = response.toRaw();
} else if (this.query.base64) {
this.body = response.encode64();
} else {
response.encode();
}
};
var uuid = require('node-uuid');
var pb = require('../lib/pb');
var PBException = require('../lib/pb-helpers').PBException;
var utils = require('../lib/utils');
module.exports.signup = function *(session, userID, envelope) {
var command = envelope.userSignupRequest;
var password = command.password;
if (!password || password.length < 6) {
throw new PBException(pb.PBError.ErrorType.INVALID_PASSWORD);
}
var email = command.email;
if (!email) {
throw new PBException(pb.PBError.ErrorType.INVALID_EMAIL);
}
var user = yield session.db.users.findOne({ email: email });
if (user) {
throw new PBException(pb.PBError.ErrorType.UNAVAILABLE_EMAIL);
}
user = yield session.db.users.insert({
email: email,
fname: command.fname,
lname: command.lname,
password: utils.sha256(password),
type: pb.PBUser.UserType.BIDDER
});
var userSession = yield session.db.sessions.insert({
userID: user._id,
authToken: uuid.v4()
});
return new pb.PBCommandEnvelope({
type: pb.PBCommandEnvelope.CommandType.USER_SIGNUP_RESPONSE,
userSignupResponse: {
user: { userID: user._id.toString() },
session: {
sessionID: userSession._id.toString(),
authToken: userSession.authToken
},
email: email
}
});
};
module.exports.login = function *(session, userID, envelope) {
var command = envelope.userLoginRequest;
var email = command.email;
if (!email) {
throw new PBException(pb.PBError.ErrorType.INVALID_EMAIL);
}
var user = yield session.db.users.findOne({ email: email });
if (!user) {
throw new PBException(pb.PBError.ErrorType.NOT_FOUND);
}
if (utils.sha256(command.password || '') !== user.password) {
throw new PBException(pb.PBError.ErrorType.UNAUTHORIZED);
}
var userSession = yield session.db.sessions.insert({
userID: user._id,
authToken: uuid.v4()
});
return new pb.PBCommandEnvelope({
type: pb.PBCommandEnvelope.CommandType.USER_LOGIN_RESPONSE,
userLoginResponse: {
user: {
email: user.email,
userID: user._id.toString(),
fname: user.fname || null,
lname: user.lname || null
},
session: {
sessionID: userSession._id.toString(),
authToken: userSession.authToken
},
email: user.email
}
});
};
@LluviaCarrera123
Copy link

Userscripts.org is DOWN for everyone.
It is not just you. The server is not responding...

@LluviaCarrera123
Copy link

AgarBots.Net

AgarBots.Net##AgarBots.Net##AgarBots.Net##AgarBots.Net##AgarBots.Net##AgarBots.Net##AgarBots.Net##AgarBots.Net##AgarBots.Net##AgarBots.Net##AgarBots.Net##AgarBots.Net##AgarBots.Net##AgarBots.Net##AgarBots.Net##AgarBots.Net##AgarBots.Net

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment