Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clear #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore0
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
node_modules
77 changes: 77 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session')


var passport = require('passport');
var Strategy = require('passport-local')
.Strategy;

var index = require('./routes/index');
var users = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public

app.use(passport.initialize());
app.use(passport.session());

app.use(session({
cookieName: 'session',
secret: 'eg[isfd-8yF9-7w2315df{}+Ijsli;;to8',
duration: 30 * 60 * 1000,
activeDuration: 5 * 60 * 1000,
httpOnly: true,
secure: true,
ephemeral: true,
resave: true,
saveUninitialized: true
}))
// penambahan untuk session
app.use(function (req, res, next) {
res.set('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0');
next();
});

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));


app.use('/', index);
app.use('/users', users);



// catch 404 and forward to error handler
app.use(function (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});

// error handler
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};

// render the error page
res.status(err.status || 500);
res.render('error');
});

module.exports = app;
90 changes: 90 additions & 0 deletions bin/www
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

var app = require('../app');
var debug = require('debug')('api-oauth-thirdparty:server');
var http = require('http');

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
* Create HTTP server.
*/

var server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
32 changes: 32 additions & 0 deletions cara-pakai-passport-facebook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// FACEBOOK
var passport = require('passport');
var FacebookStrategy = require('passport-facebook')
.Strategy;

passport.use(new FacebookStrategy({
clientID: "1420197958030285",
clientSecret: "2722281df2b0f30eb6267b2d71e1e701",
// harus sama dengan URL di facebook developer
callbackURL: "http://localhost:3000/auth/facebook/callback"
},
function (accessToken, refreshToken, profile, cb) {
return cb(null, profile);
}
));

router.get('/auth/facebook',
passport.authenticate('facebook', {
failureRedirect: '/error'
}),
function (req, res) {
// Successful authentication, redirect home.
// memang kosong, & harus ada agar bisa redirect ke halaman selanjutnya
});

router.get('/auth/facebook/callback', passport.authenticate('facebook', {
failureRedirect: '/error'
}),
function (req, res) {
// Successful authentication, redirect home.
res.send(res.req.user);
});
23 changes: 23 additions & 0 deletions helper/hash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module.exports = function (req, res, next) {
let crypto = require('crypto');
let mongoose = require('mongoose');
// mongoose.connect('mongodb://localhost/AOuth');

let salt = "";
let hashPassword = "";
// generate random namber untuk diisi ke variabel salt
let possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (var i = 0; i < 5; i++) {
salt += possible.charAt(Math.floor(Math.random() * possible.length));
}
// hash hasil generate salt dengan password yang di inputkan
// lalu simpan hasil nya ke variabel hash
hashPassword = crypto.createHmac('sha256', salt)
.update(req.body.hashPassword)
.digest('hex');

req.hash = {}
req.hash.salt = salt
req.hash.hashPassword = hashPassword
next()
}
11 changes: 11 additions & 0 deletions models/userSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
let mongoose = require("mongoose");

let user = mongoose.Schema({
userName: String,
hashPassword: String,
salt: String,
dataId: Number
})

let User = mongoose.model('user', user)
module.exports = User;
22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "api-oauth-thirdparty",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www",
"dev": "nodemon ./bin/www"
},
"dependencies": {
"body-parser": "~1.15.2",
"cookie-parser": "~1.4.3",
"debug": "~2.2.0",
"ejs": "~2.5.2",
"express": "~4.14.0",
"mongodb": "^2.2.24",
"morgan": "~1.7.0",
"nodemon": "^1.11.0",
"passport": "^0.3.2",
"passport-local": "^1.0.0",
"serve-favicon": "~2.3.0"
}
}
Empty file added passport/passport.js
Empty file.
8 changes: 8 additions & 0 deletions public/stylesheets/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
body {
padding: 50px;
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
}

a {
color: #00B7FF;
}
15 changes: 15 additions & 0 deletions query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/AOuth');

let User = require("./models/userSchema");

passport.use(new LocalStrategy(
function(username, password, done) {
User.findOne({ username: username }, function (err, user) {
if (err) { return done(err); }
if (!user) { return done(null, false); }
if (!user.verifyPassword(password)) { return done(null, false); }
return done(null, user);
});
}
));
Loading