Skip to content

Commit

Permalink
https forcing, simplified authenticator
Browse files Browse the repository at this point in the history
  • Loading branch information
Boris committed Apr 23, 2018
1 parent 74ae589 commit f2dd850
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 106 deletions.
2 changes: 1 addition & 1 deletion config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"application_name": "Flagture",
"consumer_key": 328825967593367,
"consumer_secret": "56b902ade32adbff835492d83eed3b3c",
"callback_url": "/auth/facebook/callback" ,
"callback_url": "http://localhost:8080/auth/facebook/callback" ,
"jwt_secret": "tasmanianDevil",

"http_status_codes":{
Expand Down
28 changes: 28 additions & 0 deletions config/key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCfKBFpqNOf04vl
MwgsJa1y1sXn0T++xhaUDFtgxi6jadrftrPEa7Wmbp9EjMLo/oS0RCBDUjCTTAIB
9IDQ7NhjCy5Z+B/ITxHV35o3bK+tDN0/+S84SLBBLo2h2+KnKEo1XW/s4xEAb45d
6997+5tq8q+f5bV8J240iXYqLuzFKFkgs8WjM2u9k5sBqQ7LFyfBr+kX09tTiPbD
0Am0ez5siLd1gopPChGfG4ehO59y7et5zUHnQd4p0GfpJBxbWmB6ZkrfZkxuYEQ3
Yx4NrIRd+bwMDHs8+pEXYC1pDLWYTWZZbHCEPgrEXKpM6KO1cOLzMqb4dPKKg4+f
tQFUtq/7AgMBAAECggEAHbDZ3r4yDIToei+U/LdiriPG88IjhDFsEN5lRZ5zIOUk
gcuRqbj2QEAu0hj4W4Lvurli02xHx5LMZRT52v/0NjXlxb72HuYxS5bVLDXIaB4Y
QFD6MCPPTbmjjmh41fo8LcxqjuSlTVGCW58fJ//+NGtlvAycWLb6Zi6mrqPmY7kh
5B14/Sg21f7+gJVXAIyZUpzxenxzHtpxdyHRRSkOfk2cwCm47s/7BKUBaTZlluvW
2bzxmF4edRB9legMdBR1//ujjoli+dKKINk4HFc/tgvQ9wb4Yxa0jTiuGPT/cJGr
FBh091xY6IpYDoUSXyI/3ioptug2qH9oX83+cuPDUQKBgQDNoXUAMyj/WX6Ti+ZB
Bh94zvK2zrKWVD8p65t9R8bukSRkkm6RHlZnooICWBZG5BM3Whip+zLNuIqHkqDw
YNbNc2XHXRhB+vgfuvmlB0agfAIp/O2BjPKbV8a2OH8TRJskqn1JqPKmcs7yO+sA
wCuo4F3dOvsLrXBhh5tgo9+TVwKBgQDGJFdqC/gUvGgFVURXTIjrOiKqMbjAF1Xa
tw2NPeDAcDlVvUtQKNn8Y7PnEPMeEZv5pm5gyIKsCUUmmdFkbWhvjCO+Im0qlngD
6vENs3N/2dpG1B3F7NteEWc57gcykE/584o+20RZyeJ3ro3NwMjJVFPj5Db9a8Bz
69PP062l/QKBgQCd5q6mkaKYuotFF7efFOmT5i5hDEX8xAKg2ShUQorS5SdyBzPa
QdaNzC9TR3sLwtWyun+X/6oaaOwPgRwOmf44U7XDKoyXFfX0v6o1/GlSkQdEYNGs
mqQtf3C6upVyIUuVE0oUByUVhbG+Nx6VO/wKuQe6+zCN3ggfDHfW9N+ZDwKBgC5z
bprJ24rg8HHvn2nn42Fh66aCqZE+5z2zFFY8X2g95vMWwYU1ToVAyngtHRG7hxlr
zwyZdSjns3JtNQ2aKJWeAPMeFwGY7OHePky7+SPL6sQYDjLMaFERO2X1S3KrKlSq
p4o81TVHgvQT0+Kcg3XPcytF4S5gCGokqy9ZrKfxAoGBAJv9xwy9KxenGfC5zHKW
4gg/j/QOzgjNI99tgV/ngMHHlG8NYiqpUCMPsWsggfKSWKaKEBUcERkvIfaWCk4B
oRYkhutCM7tPDVKfjGh9bnEivpVHLb7mJ5Yw2X+9BEOcbnz/v9XoqWoP3AGR60y6
YGxSNfTYhFSx8vwUeQjt3Fjr
-----END PRIVATE KEY-----
19 changes: 19 additions & 0 deletions config/server.crt
Original file line number Diff line number Diff line change
@@ -0,0 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==
-----END CERTIFICATE-----
15 changes: 0 additions & 15 deletions config/ssl-cert.pem

This file was deleted.

15 changes: 0 additions & 15 deletions config/ssl-key.pem

This file was deleted.

2 changes: 1 addition & 1 deletion models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var bcrypt = require('bcryptjs');
// Define our user schema
var UserSchema = new mongoose.Schema({
local:{
private password: String,
password: String,
},
facebook: {
facebookId: Number,
Expand Down
1 change: 0 additions & 1 deletion modules/authenticator.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ module.exports = {
break;

case 'local':

try {
var decoded_payload = jwt.decode(access_token, config.jwt_secret);
var user = decoded_payload.user;
Expand Down
9 changes: 5 additions & 4 deletions modules/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ module.exports = function (passport) {
passport.use(new FacebookStrategy({
clientID: config.consumer_key,
clientSecret: config.consumer_secret,
callbackURL: config.callback_url,
profileFields: ['email', 'first_name', 'picture']
callbackURL: config.callback_url
},
function (accessToken, refreshToken, profile, callback) {
process.nextTick(function () {
console.log('Strategy entered');
User.findOne({ 'facebook.facebookId': profile.id },
function (err, user) {
if (err)
return callback(err, null, { status: status_codes.server_error, message: 'Internal Error' });
if (user) {
return callback(null, user, { status: status_codes.success, message: 'User found' });
console.log(1, accessToken);
return callback(null, user, { status: status_codes.success, message: 'User found', token: accessToken });
}
else {
// Set the user properties that came from the POST data
Expand All @@ -33,7 +34,7 @@ module.exports = function (passport) {
if (err)
return callback(err, newUser, { status: status_codes.server_error, message: 'Couldnt insert user' });
else
return callback(null, newUser, { status: status_codes.created, message: 'New user inserted' });
return callback(null, newUser, { status: status_codes.created, message: 'New user inserted', token: accessToken });
});
}
}
Expand Down
30 changes: 13 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
"bcryptjs": "^2.4.3",
"body-parser": "^1.18.2",
"cookie-parser": "^1.4.0",
"cors": "^2.8.4",
"csurf": "^1.9.0",
"ejs": "^2.3.4",
"express": "^4.13.3",
"express-session": "^1.15.6",
"fs": "0.0.1-security",
"fs": "0.0.2",
"http": "0.0.0",
"https": "^1.0.0",
"jsonwebtoken": "^8.1.0",
"jwt-simple": "^0.5.1",
"mongoose": "^4.10.8",
Expand Down
9 changes: 4 additions & 5 deletions routing/api_routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,30 @@ module.exports = function (apiRouter, authenticator) {

// IDEA: I get the Id from the debug_token function, so may I use it from there instead of the param
// --> Every request is handles as if the user is in the db

authenticator.isValidRequest(req, function (valid, msg) {
if (valid) {
return res.status(200).json({ data: msg });
return res.status(200).json({ data: valid });
}
else {
return res.status(401).json({ error: msg });
}
});
});

apiRouter.get('/user', function (req, res) {
authenticator.isValidRequest(req, function (valid, msg) {
if (valid) {
User.find(function (err, data) {
if (err) {
return res.status(500).json(error);
}
res.status(200).json(data);
return res.status(200).json(data);
});
}
else {
return res.status(401).json({ error: msg });
}
});
});

}
11 changes: 8 additions & 3 deletions routing/auth_routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ module.exports = function (authRouter, passport) {
}
passport.authenticate('local-login', function (err, user, info) {
if (user) {
var payload = { id: req.user._id };
var payload = { user: user, strategy: 'local' };
var token = jwt.encode(payload, config.jwt_secret);
return res.status(info.status).json({ user: user, info: info.message, token: token });
}
else
else{
return res.status(info.status).json({ info: info.message });
}

})(req, res);
});
Expand All @@ -34,6 +35,9 @@ module.exports = function (authRouter, passport) {
return res.status(status_codes.bad_request).json({ info: 'Signup request must contain username and password in body' });
}
passport.authenticate('local-signup', function (err, user, info) {
if(err){
return res.status(info.status).json({ info: info.message });
}
if (user) {
//How does the user object look like? Bette not include passwords here
var payload = { user: user, strategy: 'local' };
Expand All @@ -52,8 +56,9 @@ module.exports = function (authRouter, passport) {
//If the passed credentials are invalid, the callback is never accessed!
authRouter.get('/facebook/callback', function (req, res) {
passport.authenticate('facebook', function (err, user, info) {
console.log(err,user,info);
if (user) {
return res.status(info.status).json({ user: user, info: info.message });
return res.status(info.status).json({ user: user, info: info.message, token: info.token });
}
else
return res.status(info.status).json({ info: info.message });
Expand Down
Loading

0 comments on commit f2dd850

Please sign in to comment.