-const PORT = 8080
-
+const PORT = 8080;
const HTTP_OK = 200;
const HTTP_BAD_REQUEST = 400;
const HTTP_INTERNAL_ERROR = 500;
var app = express();
var bodyParser = require('body-parser');
-//var sqlite3 = require('sqlite3').verbose();
-//var db = new sqlite3.Database('firmware.db');
+var sqlite3 = require('sqlite3').verbose();
+var db = new sqlite3.Database('auth_info.db');
+
+db.serialize(function() {
+ db.run("CREATE TABLE IF NOT EXISTS auth_tbl (id TEXT primary key, code TEXT)");
+ db.each("SELECT id, code FROM auth_tbl", function(err, row) {
+ console.log(row);
+ });
+});
+
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: false }));
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 insertAuthCodeCb(err, res) {
+ if (err){
+ console.error("error", err);
+ res.status(HTTP_INTERNAL_ERROR).json(err);
+ }
+ else {
+ console.log("insert completed");
+ res.sendStatus(200);
+ }
+}
app.post('/auth_response', function(req, res) {
var code = req.query.code;
console.log("id: [", id, "]");
/* DB write */
+ db.run("INSERT INTO auth_tbl (id, code) VALUES (?,?)", id, code,
+ function(err) { insertAuthCodeCb(err, res); });
+});
- res.sendStatus(200);
-})
+function getAuthCodeCb(err, row, res) {
+ 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 */
-
- res.sendStatus(200);
-})
-
+ db.get("SELECT * FROM auth_tbl WHERE id=?", id, function(err,row){ getAuthCodeCb(err, row, res);});
+});
+++ /dev/null
-// configurable variable: HOST_ADDR, PORT, IMAGE_DIR
-const HOST_ADDR = "10.113.63.216"
-const PORT = 8000
-
-const HTTP_OK = 200;
-const HTTP_BAD_REQUEST = 400;
-const HTTP_INTERNAL_ERROR = 500;
-
-var express = require('express');
-var app = express();
-var bodyParser = require('body-parser');
-
-var host_ip = require("ip");
-
-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 host = host_ip.address();
- var port = server.address().port;
-
- console.log("OAuth redirect_uri handler listening at http://%s:%s", host, port)
-})
-
-app.get('/oauth_callback', function(req, res) {
- console.log("auth code: [", req.query.code, "]");
-// res.sendFile( __dirname + "/" + "test_login.html" );
- var html = "<!DOCTYPE html>\n<html>\n <head>\n </head>\n <body>\n <h1>CODE [" + req.query.code +"]</h1>\n </body>\n</html>";
- res.send(html);
-})
\ No newline at end of file