From a230b21c7750cab26944e9c6e9bc887012f33be6 Mon Sep 17 00:00:00 2001 From: Michael Sofaer Date: Thu, 19 Jul 2012 19:07:21 -0700 Subject: [PATCH] Domains --- cloudflare.json | 2 +- db.js | 3 ++- web.js | 58 ++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 53 insertions(+), 10 deletions(-) diff --git a/cloudflare.json b/cloudflare.json index f6902e8..1547b19 100644 --- a/cloudflare.json +++ b/cloudflare.json @@ -4,7 +4,7 @@ "description": "A sample application to get you started with CloudFlare apps", "version": "0.0.2b", "account": { - "callback_url": "http://10.0.2.2:3000/api", + "callback_url": "http://10.0.2.2:5000/api", "user_fields": ["email"] }, "config":{ diff --git a/db.js b/db.js index 2c51196..8520976 100755 --- a/db.js +++ b/db.js @@ -3,7 +3,8 @@ var pg = require('pg'); function create_db(){ pg.connect(process.env.DATABASE_URL || "tcp://michael:1234@localhost/michael", function(err, client) { - client.query("CREATE TABLE accounts(account_id BIGINT PRIMARY KEY, status VARCHAR(8), cdate TIMESTAMPTZ default now());"); + client.query("CREATE TABLE account(account_id BIGINT PRIMARY KEY, cdate TIMESTAMPTZ default now());"); + client.query("CREATE TABLE domain(domain_id BIGINT PRIMARY KEY, account_id BIGINT REFERENCES account(account_id), cdate TIMESTAMPTZ default now());"); }) } diff --git a/web.js b/web.js index b07b98a..dc52993 100644 --- a/web.js +++ b/web.js @@ -6,10 +6,12 @@ var pg = require('pg'); var app = express.createServer(express.logger()); app.use(express.bodyParser()); +var conn_string = process.env.DATABASE_URL || "tcp://michael:1234@localhost/michael"; + function create_account(account_id, respond){ - pg.connect(process.env.DATABASE_URL || "tcp://michael:1234@localhost/michael", function(err, client) { + pg.connect(conn_string, function(err, client) { console.log(err) - var query = client.query('insert into accounts(account_id) values ('+account_id+')'); + var query = client.query('insert into account(account_id) values ('+account_id+')'); query.on('end', function() { respond("approve"); @@ -21,24 +23,64 @@ function create_account(account_id, respond){ }); } -function valid(data) { +function create_domain(body, respond){ + pg.connect(conn_string, function(err, client) { + console.log(err) + var query = client.query('insert into domain(account_id, domain_id) values ('+body.account_id+','+body.domain_id+')'); + + query.on('end', function() { + respond("approve"); + }); + + query.on('error', function(something) { + console.log(body) + console.log(something); + respond("error"); + }); + }); +} + +function valid(req) { var hmac_secret = '09aed14f2a579b0f50965418c67b600d'; var hmac = crypto.createHmac("sha256", hmac_secret); - hmac.update(data.account_id); - return data.sig == hmac.digest('hex'); + var contents = JSON.stringify(req.body); + hmac.update(contents); + return req.headers.hmac == hmac.digest('hex'); } app.post('/api/accounts', function(request, response) { - if (!valid(request.body)) { + if (!valid(request)) { response.send("Bad HMAC"); } else { var respond = function(status){ - response.send('{"account_id":"'+request.body.account_id+'","status":"'+status+'"}'); + response.send(JSON.stringify({ + "account_id":request.body.account_id, + "status":status + })); } create_account(request.body.account_id, respond); }; }); -var port = process.env.PORT || 5000; +app.post('/api/domains', function(request, response) { + if (!valid(request)) { + response.send("Bad HMAC"); + } else { + var respond = function(status){ + if (status == 'error') { + response.send("error"); + } else { + response.send(JSON.stringify({ + "account_id":request.body.account_id, + "domain_id":request.body.domain_id, + "status":status + })); + } + } + create_domain(request.body, respond); + }; +}); + +var port = 3000 || process.env.PORT || 5000; app.listen(port, function() { console.log("Listening on " + port); });