const HTTP_INTERNAL_ERROR = 500;
var express = require('express');
-var app = express();
var bodyParser = require('body-parser');
+var app = express();
+
+//app.use(express.static('public'));
+//app.use(bodyParser.urlencoded({ extended: false }));
+//app.use(bodyParser.json());
+var jsonParser = bodyParser.json();
+
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('auth_info.db');
});
});
-
-app.use(express.static('public'));
-app.use(bodyParser.urlencoded({ extended: false }));
-
// this enables desktop to accept redirect_uri from browser
var server = app.listen(PORT, function () {
var port = server.address().port;
console.log("OAuth broker listening at http://127.0.0.1:%s", port)
});
+function updateAuthCodeCb(err, res) {
+ if (err) {
+ console.error("error", err);
+ res.status(HTTP_INTERNAL_ERROR).json(err);
+ }
+}
+
function insertAuthCodeCb(err, res) {
if (err){
console.error("error", err);
}
}
-app.post('/auth_response', function(req, res) {
- var code = req.query.code;
- var id = req.query.id;
+app.post('/auth_response', jsonParser, function(req, res) {
+ var code = req.body.code;
+ var id = req.body.id;
console.log("auth code: [", code, "]");
console.log("id: [", id, "]");
- /* DB write */
- db.run("INSERT INTO auth_tbl (id, code) VALUES (?,?)", id, code,
- function(err) { insertAuthCodeCb(err, res); });
-});
-function getAuthCodeCb(err, row, res) {
- if (err) {
+ /* DB write */
+ db.get("SELECT * FROM auth_tbl WHERE id=?", id, function(err, row) {
+ if(err) {
console.error("error", err);
res.status(HTTP_INTERNAL_ERROR).json(err);
}
- else if (!row) {
- console.log('id not found');
- res.status(HTTP_BAD_REQUEST).json({errmsg:'id not found'});
+ else if(row) {
+ db.run("UPDATE auth_tbl SET code=? WHERE id=?", code, id,
+ function(err) { updateAuthCodeCb(err, res); });
}
else {
- console.log(row);
- res.json({code:row.code});
+ db.run("INSERT INTO auth_tbl (id, code) VALUES (?,?)", id, code,
+ function(err) { insertAuthCodeCb(err, res); });
}
+ });
+});
+
+function getAuthCodeCb(err, row, res) {
+ console.log(row);
+ if (err) {
+ console.error("error", err);
+ res.status(HTTP_INTERNAL_ERROR).json(err);
+ }
+ else if (!row) {
+ console.log('id not found');
+ res.status(HTTP_BAD_REQUEST).json({errmsg:'id not found'});
+ }
+ else {
+ console.log(row);
+ res.json({code:row.code});
+ }
}
app.get('/auth_code', function(req, res) {
var id = req.query.id;
- console.log("id: [", id, "]");
/* DB read */
- db.get("SELECT * FROM auth_tbl WHERE id=?", id, function(err,row){ getAuthCodeCb(err, row, res);});
+ var query = "SELECT * FROM auth_tbl WHERE id='" + id + "'";
+ console.log("query: ("+query+")");
+ db.get(query, function(err,row) { getAuthCodeCb(err, row, res);});
});