//app.use(express.static('public'));
//app.use(bodyParser.urlencoded({ extended: false }));
-//app.use(bodyParser.json());
+app.use(bodyParser.json());
var jsonParser = bodyParser.json();
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);
});
});
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);
}
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); });
}
});
});
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); });
+ }
+ });
+});
+