-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 12503bc
Showing
20 changed files
with
897 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
OpenTok.framework/ | ||
OpenTok.framework | ||
|
||
untitled folder/ | ||
# Xcode | ||
.DS_Store | ||
*/build/* | ||
*.pbxuser | ||
!default.pbxuser | ||
*.mode1v3 | ||
!default.mode1v3 | ||
*.mode2v3 | ||
!default.mode2v3 | ||
*.perspectivev3 | ||
!default.perspectivev3 | ||
xcuserdata | ||
|
||
profile | ||
*.moved-aside | ||
DerivedData | ||
.idea/ | ||
*.hmap | ||
*.xccheckout | ||
|
||
#CocoaPods | ||
Pods | ||
|
||
Config. | ||
|
||
*.DS_Store | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
var UserId = require("../../Model/Auth/UserId"); | ||
var AuthBasic = require("../../Model/Auth/Basic"); | ||
var Utility = require("../../Utility"); | ||
|
||
exports.Register = function(req, res, next) { | ||
|
||
// Parse body | ||
var username = req.body.username; | ||
var password = req.body.password; | ||
|
||
console.log("username: " + username); | ||
console.log("password: " + password); | ||
|
||
// Undefined guards | ||
if (username == undefined || password == undefined) | ||
res.error = {code:"invalidArgument", message:"Invalid argument"}; | ||
|
||
if (res.error != null || res.error != undefined) { | ||
|
||
next(); | ||
return; | ||
} | ||
|
||
// Generate user id (A random unique number) | ||
var userId = Utility.GenerateUserId(); | ||
|
||
// Upsert user profile | ||
AuthBasic.update( | ||
{ "username": username }, | ||
{ $setOnInsert: | ||
{ "username": username, | ||
"password": password, | ||
"user_id": userId | ||
} | ||
}, | ||
{ upsert: true }, | ||
function (err, result) { | ||
|
||
// Error occur | ||
if (err) { | ||
|
||
res.error = err; | ||
next(); | ||
return; | ||
} | ||
|
||
// Duplicated user | ||
if (result.upserted == null) { | ||
|
||
console.log("res: " + JSON.stringify(result)); | ||
|
||
res.error = {code:"duplicatedUser",message:"Duplicated user"}; | ||
next(); | ||
return; | ||
} | ||
|
||
// Generate new userId | ||
UserId.update( | ||
{ "user_id": userId }, | ||
{ $set: { "user_id": userId } }, | ||
{ upsert: true }, | ||
function (err, result) { | ||
|
||
// Error occur | ||
if (err) { | ||
|
||
res.error = err; | ||
next(); | ||
return; | ||
} | ||
|
||
if (res.data == null) | ||
res.data = {}; | ||
|
||
// Attach user_id | ||
res.data.user_id = userId; | ||
|
||
next(); | ||
} | ||
); | ||
} | ||
); | ||
} | ||
|
||
exports.Login = function(req, res, next) { | ||
|
||
// Parse body | ||
var username = req.body.username; | ||
var password = req.body.password; | ||
|
||
console.log("username: " + username); | ||
console.log("password: " + password); | ||
|
||
// Undefined guards | ||
if (username == undefined || password == undefined) | ||
res.error = {code:"invalidArgument", message:"Invalid argument"}; | ||
|
||
if (res.error != null || res.error != undefined) { | ||
|
||
next(); | ||
return; | ||
} | ||
|
||
AuthBasic.findOne({ "username": username, "password": password }, function (err, user) { | ||
|
||
// Error | ||
if (err) { | ||
|
||
res.error = err; | ||
next(); | ||
} | ||
else { | ||
|
||
// User NOT exists | ||
if (user == null) { | ||
|
||
res.error = {code:"loginFailure",message:"Login failure"}; | ||
next(); | ||
return; | ||
} | ||
|
||
if (res.data == null) | ||
res.data = {}; | ||
|
||
// Pass user id | ||
res.data.user_id = user.user_id; | ||
|
||
next(); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
var UserId = require("../../Model/Auth/UserId"); | ||
var AuthToken = require("../../Model/Auth/Token"); | ||
var Utility = require("../../Utility"); | ||
|
||
exports.Update = function(req, res, next) { | ||
|
||
// Next, if error | ||
if (res.error != null) { | ||
|
||
next(); | ||
return; | ||
} | ||
|
||
// Validate value | ||
if (res.data.user_id == null) { | ||
|
||
res.error = {code:"invalidArgument", message:"Invalid argument"}; | ||
next(); | ||
return; | ||
} | ||
|
||
// Generate token | ||
var token = Utility.GenerateToken(); | ||
var timestamp = Utility.GetCurrentTimestamp(); | ||
|
||
// Update user_id | ||
AuthToken.update( | ||
{ "user_id": res.data.user_id }, | ||
{ $set: { "user_id": res.data.user_id, "token": token, "timestamp": timestamp } }, | ||
{ upsert: true }, | ||
function (err, result) { | ||
|
||
// Error occur | ||
if (err) { | ||
|
||
res.error = err; | ||
next(); | ||
return; | ||
} | ||
|
||
if (res.data == null) | ||
res.data = {}; | ||
|
||
// Attach token | ||
res.data.token = token; | ||
|
||
// Next node | ||
next(); | ||
} | ||
); | ||
} | ||
|
||
exports.Authen = function(req, res, next) { | ||
|
||
// Set var | ||
var token = req.headers["token"]; | ||
|
||
if (token == null) { | ||
|
||
res.error = {code:"invalidArgument", message:"Invalid argument"}; | ||
next(); | ||
return; | ||
} | ||
|
||
AuthToken.findOne({ "token": token }, function (err, user) { | ||
|
||
// Error | ||
if (err) { | ||
|
||
res.error = err; | ||
next(); | ||
} | ||
else { | ||
|
||
var invalidTokenObject = {code:"invalidToken",message:"Invalid token"}; | ||
|
||
// User NOT exists | ||
if (user == null) { | ||
|
||
res.error = invalidTokenObject; | ||
next(); | ||
return; | ||
} | ||
|
||
// Validate with time | ||
var currentTimestamp = Utility.GetCurrentTimestamp(); | ||
var timeDiff = currentTimestamp - user.timestamp; | ||
// Todo: Configure this duration | ||
var validTokenDuration = 86400; | ||
|
||
if (timeDiff > validTokenDuration) { | ||
|
||
res.error = invalidTokenObject; | ||
next(); | ||
return; | ||
} | ||
|
||
if (res.data == null) | ||
res.data = {}; | ||
|
||
// Pass value | ||
res.data.user_id = user.user_id; | ||
res.data.token = token; | ||
res.data.timestamp = user.timestamp; | ||
|
||
next(); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
var mongoose = require("mongoose"); | ||
|
||
var schema = mongoose.Schema({ | ||
|
||
username: String, | ||
password: String, | ||
|
||
user_id: String | ||
}); | ||
|
||
module.exports = mongoose.model("auth_basic", schema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
var mongoose = require("mongoose"); | ||
|
||
var schema = mongoose.Schema({ | ||
|
||
fb_id: String, | ||
user_id: String | ||
}); | ||
|
||
module.exports = mongoose.model("auth_facebook", schema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
var mongoose = require("mongoose"); | ||
|
||
var schema = mongoose.Schema({ | ||
|
||
token: String, | ||
timestamp: Number, | ||
user_id: String | ||
}); | ||
|
||
module.exports = mongoose.model("auth_token", schema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
var mongoose = require("mongoose"); | ||
|
||
var schema = mongoose.Schema({ | ||
|
||
user_id: String | ||
}); | ||
|
||
module.exports = mongoose.model("user_id", schema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
var mongoose = require("mongoose"); | ||
var bluebird = require('bluebird'); | ||
|
||
var isStartMongoDB = true; | ||
|
||
// --------------------------------------------------------------------------- | ||
// Public method | ||
exports.StartServer = function(app, Configurations) { | ||
|
||
if (isStartMongoDB) { | ||
StartMongoDBConnection(Configurations.dbDevName, function(err) { | ||
if (err) { | ||
console.log("MongoDB initialization failure, DO NOT start server!"); | ||
} | ||
else { | ||
StartHtttpServer(app, Configurations.apiServerDevPort); | ||
} | ||
}); | ||
} | ||
else { | ||
StartHtttpServer(app, Configurations.apiServerDevPort); | ||
} | ||
} | ||
|
||
// --------------------------------------------------------------------------- | ||
// Start HTTP server | ||
function StartHtttpServer(app, port) { | ||
|
||
var port = process.env.PORT || port; | ||
var server = app.listen(port); | ||
var io = require("socket.io").listen(server); // this tells socket.io to use our express server | ||
|
||
console.log("Listening on port " + port + "..."); | ||
} | ||
|
||
// --------------------------------------------------------------------------- | ||
// Start MongoDB connection | ||
|
||
function StartMongoDBConnection(dbDevName, errCallback) { | ||
|
||
// Compose URI string depending on environment | ||
var uriString = process.env.MONGODB_URI || | ||
"mongodb://localhost:27017/" + dbDevName; | ||
|
||
mongoose.Promise = bluebird; | ||
|
||
// Connect database | ||
mongoose.connection.openUri(uriString, function (err, res) { | ||
|
||
if (err) { | ||
console.log("ERROR connecting to: " + uriString + ". " + err); | ||
errCallback({error:{message:err}}); | ||
} | ||
else { | ||
console.log("Succeeded connected to: " +uriString); | ||
errCallback(null); | ||
} | ||
}); | ||
} | ||
|
||
|
Oops, something went wrong.