From: Sang-Hun Chung Date: Thu, 3 Aug 2017 11:04:58 +0000 (+0900) Subject: Oauth broker API - auth_request added and sqlite cb func renamed X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ab132cdbc1cfd0e2aa7a3e5dc843198ca36a151c;p=platform%2Fcore%2Fservice%2Fcloud%2Fua-client.git Oauth broker API - auth_request added and sqlite cb func renamed Change-Id: I1f01f7161ad7e89ad7142f70f400cfb0668d7376 --- diff --git a/test/oauth_broker/broker/oauth_broker.js b/test/oauth_broker/broker/oauth_broker.js index 9e86696..ebdc0e4 100755 --- a/test/oauth_broker/broker/oauth_broker.js +++ b/test/oauth_broker/broker/oauth_broker.js @@ -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); }); + } + }); +}); +