OAuth broker API added, OAuth client Rest API call added 85/139885/1
authorSang-Hun Chung <sanghun.chung@samsung.com>
Fri, 21 Jul 2017 02:34:08 +0000 (11:34 +0900)
committerSang-Hun Chung <sanghun.chung@samsung.com>
Fri, 21 Jul 2017 02:34:08 +0000 (11:34 +0900)
Change-Id: I6401067fd98ff696ce072ad21680a569a9cfd9db

test/oauth_broker/broker/oauth_broker.js [new file with mode: 0755]
test/oauth_broker/broker/package.json [new file with mode: 0755]
test/oauth_broker/client/oauth_redirect_uri_handler.js [new file with mode: 0755]
test/oauth_broker/client/package.json [new file with mode: 0755]
test/oauth_broker/client/req_auth.html [new file with mode: 0755]
test/oauth_broker/req_auth.html [deleted file]

diff --git a/test/oauth_broker/broker/oauth_broker.js b/test/oauth_broker/broker/oauth_broker.js
new file mode 100755 (executable)
index 0000000..aeb5d59
--- /dev/null
@@ -0,0 +1,44 @@
+const PORT = 8080
+
+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 sqlite3 = require('sqlite3').verbose();
+//var db = new sqlite3.Database('firmware.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)
+})
+
+app.post('/auth_response', function(req, res) {
+       var code = req.query.code;
+       var id = req.query.id;
+
+       console.log("auth code: [", code, "]");
+       console.log("id: [", id, "]");
+
+       /* DB write */
+
+       res.sendStatus(200);
+})
+
+app.get('/auth_code', function(req, res) {
+       var id = req.query.id;
+       console.log("id: [", id, "]");
+
+       /* DB read */
+
+       res.sendStatus(200);
+})
+
+
diff --git a/test/oauth_broker/broker/package.json b/test/oauth_broker/broker/package.json
new file mode 100755 (executable)
index 0000000..396b784
--- /dev/null
@@ -0,0 +1,11 @@
+{
+    "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"
+    }
+}
diff --git a/test/oauth_broker/client/oauth_redirect_uri_handler.js b/test/oauth_broker/client/oauth_redirect_uri_handler.js
new file mode 100755 (executable)
index 0000000..ada5fef
--- /dev/null
@@ -0,0 +1,65 @@
+const OAUTH_BROKER_ADDR = "http://127.0.0.1:8080";
+const PORT = 8000;
+const REQ_ID = "0101231234";
+
+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 request = require('request');
+
+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 redirect_uri handler listening at http://127.0.0.1:%s", port)
+})
+
+app.get('/oauth_callback', function(req, res) {
+       console.log("auth code: [", req.query.code, "]");
+       var http = require('http');
+
+       var jsonBody={};
+       jsonBody.id = REQ_ID;
+       jsonBody.code = req.query.code;
+
+       console.log("jsonBody:", jsonBody);
+
+       var postHeaders = {
+           'Content-Type' : 'application/json',
+               'Content-Length' : Buffer.byteLength(JSON.stringify(jsonBody), 'utf8')
+       };
+
+       var query = "id=" + REQ_ID + "&" + "code="+req.query.code;
+       var options = {
+               host: '127.0.0.1',
+               port: '8080',
+               path: '/auth_response?' + query,
+               method: 'POST' ,
+               headers : postHeaders
+       };
+
+       console.info(options);
+
+       var http_req = http.request(options, function(res) {
+               console.log('STATUS:' + res.statusCode);
+               //console.log('HEADERS:' + JSON.stringify(res.headers));
+               res.setEncoding('utf8');
+               res.on('data', function (chunk) {
+               console.log('BODY: ' + chunk);
+               });
+       });
+       http_req.write(JSON.stringify(jsonBody)); //'{"id":"12334", "code":req.query.code}');
+       http_req.end();
+       http_req.on('error', function(e) {
+               console.error(e);
+       });
+
+       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);
+})
diff --git a/test/oauth_broker/client/package.json b/test/oauth_broker/client/package.json
new file mode 100755 (executable)
index 0000000..88ecdc1
--- /dev/null
@@ -0,0 +1,10 @@
+{
+    "name": "oauth-broker-client",
+    "version": "0.0.1",
+    "main": "oauth_redirect_uri_handler.js",
+    "dependencies": {
+        "express": "4.14.x",
+        "body-parser": "1.16.x",
+               "request" : "2.81.x"
+    }
+}
diff --git a/test/oauth_broker/client/req_auth.html b/test/oauth_broker/client/req_auth.html
new file mode 100755 (executable)
index 0000000..055177c
--- /dev/null
@@ -0,0 +1,18 @@
+<!DOCTYPE html>
+<html>
+<body>
+<p>Click the button to send req_auth to GITHUB.</p>
+
+<button onclick="reqAuthToGithub()">GITHUB</button>
+<!-- this page acts as client app in mobile phone, and will be replaced  -->
+<script>
+function reqAuthToGithub() {
+    window.open("https://github.com/login/oauth/authorize?client_id=e6bd5594e38b7f268af1&redirect_uri=http://127.0.0.1:8000/oauth_callback");
+
+}
+</script>
+
+</body>
+</html>
+
+
diff --git a/test/oauth_broker/req_auth.html b/test/oauth_broker/req_auth.html
deleted file mode 100755 (executable)
index 055177c..0000000
+++ /dev/null
@@ -1,18 +0,0 @@
-<!DOCTYPE html>
-<html>
-<body>
-<p>Click the button to send req_auth to GITHUB.</p>
-
-<button onclick="reqAuthToGithub()">GITHUB</button>
-<!-- this page acts as client app in mobile phone, and will be replaced  -->
-<script>
-function reqAuthToGithub() {
-    window.open("https://github.com/login/oauth/authorize?client_id=e6bd5594e38b7f268af1&redirect_uri=http://127.0.0.1:8000/oauth_callback");
-
-}
-</script>
-
-</body>
-</html>
-
-