Oauth broker API - POST auth_response and GET auth_code added 50/139950/1
authorSang-Hun Chung <sanghun.chung@samsung.com>
Fri, 21 Jul 2017 06:37:04 +0000 (15:37 +0900)
committerSang-Hun Chung <sanghun.chung@samsung.com>
Fri, 21 Jul 2017 06:37:04 +0000 (15:37 +0900)
Change-Id: I72b64211ad5867b967e4136f6f4d41ba38ab13bb

test/oauth_broker/broker/oauth_broker.js
test/oauth_broker/oauth_redirect_uri_handler.js [deleted file]

index aeb5d59ca6fbae669c8212fea6e2aef939a778b4..b8edbfe41995d446b99e16eb063ca67b23499203 100755 (executable)
@@ -1,5 +1,4 @@
-const PORT = 8080
-
+const PORT = 8080;
 const HTTP_OK = 200;
 const HTTP_BAD_REQUEST = 400;
 const HTTP_INTERNAL_ERROR = 500;
@@ -8,8 +7,16 @@ var express = require('express');
 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 }));
@@ -18,7 +25,18 @@ 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;
@@ -28,17 +46,30 @@ app.post('/auth_response', function(req, res) {
        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);});
+});
 
diff --git a/test/oauth_broker/oauth_redirect_uri_handler.js b/test/oauth_broker/oauth_redirect_uri_handler.js
deleted file mode 100755 (executable)
index eb46743..0000000
+++ /dev/null
@@ -1,31 +0,0 @@
-// 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