Oauth broker API - auth_request added and sqlite cb func renamed 28/142328/1
authorSang-Hun Chung <sanghun.chung@samsung.com>
Thu, 3 Aug 2017 11:04:58 +0000 (20:04 +0900)
committerSang-Hun Chung <sanghun.chung@samsung.com>
Thu, 3 Aug 2017 11:04:58 +0000 (20:04 +0900)
Change-Id: I1f01f7161ad7e89ad7142f70f400cfb0668d7376

test/oauth_broker/broker/oauth_broker.js

index 9e866960f31507b42a9bac3f7a6c353a0bc949ae..ebdc0e4127dd9f8f3ee5dcf797dde527805d17e3 100755 (executable)
@@ -9,7 +9,7 @@ var app = express();
 
 //app.use(express.static('public'));
 //app.use(bodyParser.urlencoded({ extended: false }));
-//app.use(bodyParser.json());
+app.use(bodyParser.json());
 var jsonParser = bodyParser.json();
 
 
@@ -17,8 +17,8 @@ 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) {
+        db.run("CREATE TABLE IF NOT EXISTS auth_tbl (id TEXT primary key, email TEXT, code TEXT)");
+        db.each("SELECT * FROM auth_tbl", function(err, row) {
                  console.log(row);
         });
 });
@@ -29,14 +29,19 @@ var server = app.listen(PORT, function () {
         console.log("OAuth broker listening at http://127.0.0.1:%s", port)
 });
 
-function updateAuthCodeCb(err, res) {
+function updateCb(err, res) {
        if (err) {
                console.error("error", err);
                res.status(HTTP_INTERNAL_ERROR).json(err);
        }
+       else {
+               console.log("update completed");
+               res.sendStatus(200);
+       }
+
 }
 
-function insertAuthCodeCb(err, res) {
+function insertCb(err, res) {
        if (err){
                console.error("error", err);
                res.status(HTTP_INTERNAL_ERROR).json(err);
@@ -63,11 +68,11 @@ app.post('/auth_response', jsonParser, function(req, res) {
                }
                else if(row) {
                        db.run("UPDATE auth_tbl SET code=? WHERE id=?", code, id,
-                               function(err) { updateAuthCodeCb(err, res); });
+                               function(err) { updateCb(err, res); });
                }
                else {
                        db.run("INSERT INTO auth_tbl (id, code) VALUES (?,?)", id, code,
-                               function(err) { insertAuthCodeCb(err, res); });
+                               function(err) { insertCb(err, res); });
                }
        });
 });
@@ -97,3 +102,28 @@ app.get('/auth_code', function(req, res) {
        db.get(query, function(err,row) { getAuthCodeCb(err, row, res);});
 });
 
+// curl -v -H "Content-Type: application/json" -d '{"id":"abc", "email":"abc@samsung.com"}' -X POST http://10.113.63.216:8080/auth_request
+app.post('/auth_request', jsonParser, function(req, res) {
+       var id = req.body.id; // id is generated by device
+       var email = req.body.email;
+
+       console.log("id: [", id, "]");
+       console.log("email: [", email, "]"); // this field is used for confirmation about auth
+
+       /* 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) {
+                       db.run("UPDATE auth_tbl SET email=? WHERE id=?", email, id,
+                               function(err) { updateCb(err, res); });
+               }
+               else {
+                       db.run("INSERT INTO auth_tbl (id, email) VALUES (?,?)", id, email,
+                               function(err) { insertCb(err, res); });
+               }
+       });
+});
+