Skip to content

Commit

Permalink
Domains
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Sofaer committed Jul 20, 2012
1 parent e1a6efa commit a230b21
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cloudflare.json
Expand Up @@ -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":{
Expand Down
3 changes: 2 additions & 1 deletion db.js
Expand Up @@ -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());");
})
}

Expand Down
58 changes: 50 additions & 8 deletions web.js
Expand Up @@ -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");
Expand All @@ -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);
});

0 comments on commit a230b21

Please sign in to comment.