Oauth broker API - DB add and get implementation 84/141784/1
authorSang-Hun Chung <sanghun.chung@samsung.com>
Tue, 1 Aug 2017 10:52:43 +0000 (19:52 +0900)
committerSang-Hun Chung <sanghun.chung@samsung.com>
Tue, 1 Aug 2017 10:52:43 +0000 (19:52 +0900)
Change-Id: Id3035d83779759d27aadad651b72f0ff0fbdf163

test/oauth_broker/broker/oauth_broker.js
test/oauth_broker/broker/package.json
test/oauth_broker/client/oauth_redirect_uri_handler.js

index b8edbfe41995d446b99e16eb063ca67b23499203..9e866960f31507b42a9bac3f7a6c353a0bc949ae 100755 (executable)
@@ -4,8 +4,14 @@ const HTTP_BAD_REQUEST = 400;
 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');
@@ -17,16 +23,19 @@ db.serialize(function() {
         });
 });
 
-
-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);
@@ -38,38 +47,53 @@ function insertAuthCodeCb(err, res) {
        }
 }
 
-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);});
 });
 
index 396b78449506d1abb029430554163e7f251530af..a5b9f9a531121a6c463ca694fe7118d94f1fa5a3 100755 (executable)
@@ -1,11 +1,10 @@
 {
-    "name": "oauth-broker",
-    "version": "0.0.1",
-    "main": "oauth_broker.js",
-    "dependencies": {
-        "express": "4.14.x",
-        "body-parser": "1.16.x",
-        "sqlite3": "3.1.x",
-               "request" : "2.81.x"
-    }
+  "name": "oauth-broker",
+  "version": "0.0.1",
+  "main": "oauth_broker.js",
+  "dependencies": {
+    "express": "4.14.x",
+    "body-parser": "1.16.x",
+    "sqlite3": "~3.1.4"
+  }
 }
index ada5fef1fd3b187c86d56ad3ba4d81661c14ebde..62a8ce6f62e44b3df82e91af532fbe931706970d 100755 (executable)
@@ -1,4 +1,6 @@
-const OAUTH_BROKER_ADDR = "http://127.0.0.1:8080";
+const OAUTH_BROKER_ADDR = "127.0.0.1";
+const OAUTH_BOKRER_PORT = "8080";
+
 const PORT = 8000;
 const REQ_ID = "0101231234";
 
@@ -35,6 +37,8 @@ app.get('/oauth_callback', function(req, res) {
                'Content-Length' : Buffer.byteLength(JSON.stringify(jsonBody), 'utf8')
        };
 
+// uncomment when query is used instead,
+/*
        var query = "id=" + REQ_ID + "&" + "code="+req.query.code;
        var options = {
                host: '127.0.0.1',
@@ -43,6 +47,14 @@ app.get('/oauth_callback', function(req, res) {
                method: 'POST' ,
                headers : postHeaders
        };
+*/
+       var options = {
+               host: OAUTH_BROKER_ADDR,
+               port: OAUTH_BOKRER_PORT,
+               path: '/auth_response',
+               method: 'POST' ,
+               headers : postHeaders
+       };
 
        console.info(options);