-
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
Showing
12 changed files
with
747 additions
and
95 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module.exports = { | ||
database: 'mongodb://localhost:27017/oath-nodejs', | ||
secret: 'yoursecret' | ||
}; | ||
database: 'mongodb://localhost:27017/oauth2', | ||
secret: 'yoursecret', | ||
}; |
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,20 @@ | ||
const bcrypt = require('bcrypt'); | ||
const User = require('../model/user'); | ||
|
||
const comparePassword = (textPassword, hash) => { | ||
return new Promise((resolve, reject) => { | ||
bcrypt.compare(textPassword, hash) | ||
.then((res) => { | ||
resolve(res); | ||
}) | ||
.catch((err) => { | ||
console.log(err); | ||
reject(err); | ||
}); | ||
}); | ||
}; | ||
|
||
const utils = { | ||
comparePassword, | ||
}; | ||
module.exports = utils; |
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
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
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
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 @@ | ||
// Load required packages | ||
var oauth2orize = require('oauth2orize'); | ||
var jwt = require('jwt-simple'); | ||
var Client = require('../model/client'); | ||
var Token = require('../model/token'); | ||
var Code = require('../model/code'); | ||
var config = require('../config/database'); | ||
|
||
var server = oauth2orize.createServer(); | ||
|
||
|
||
const getRandomInt = (min, max) => { | ||
return Math.floor(Math.random() * (max - min + 1)) + min; | ||
}; | ||
|
||
const uid = (len) => { | ||
var buf = []; | ||
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | ||
var charlen = chars.length; | ||
|
||
for (let i = 0; i < len; i++) { | ||
buf.push(chars[getRandomInt(0, charlen - 1)]); | ||
} | ||
return buf.join(''); | ||
}; | ||
|
||
// Register serialialization function | ||
server.serializeClient((client, callback) => { | ||
return callback(null, client._id); | ||
}); | ||
|
||
// Register deserialization function | ||
server.deserializeClient((id, callback) => { | ||
Client.findOne({ _id: id }, (err, client) => { | ||
if (err) { return callback(err); } | ||
return callback(null, client); | ||
}); | ||
}); | ||
|
||
// Register authorization code grant type | ||
server.grant(oauth2orize.grant.code((client, redirectUri, user, ares, callback) => { | ||
// Create a new authorization code | ||
var code = new Code({ | ||
value: uid(16), | ||
clientId: client._id, | ||
redirectUri: redirectUri, | ||
userId: client.userId, | ||
}); | ||
|
||
// Save the auth code and check for errors | ||
code.save((err) => { | ||
if (err) { return callback(err); } | ||
|
||
callback(null, code.value); | ||
}); | ||
})); | ||
|
||
server.exchange(oauth2orize.exchange.code((client, code, redirectUri, callback) => { | ||
Code.findOne({ value: code }, (err, authCode) => { | ||
if (err) { return callback(err); } | ||
if (authCode === undefined) { return callback(null, false); } | ||
if (client._id.toString() !== authCode.clientId) { return callback(null, false); } | ||
if (redirectUri !== authCode.redirectUri) { return callback(null, false); } | ||
|
||
// Delete auth code now that it has been used | ||
authCode.remove((err) => { | ||
if (err) { return callback(err); } | ||
|
||
// Create a new access token | ||
const token = new Token({ | ||
value: uid(256), | ||
clientId: authCode.clientId, | ||
userId: authCode.userId, | ||
}); | ||
|
||
// Save the access token and check for errors | ||
token.save((err) => { | ||
if (err) { return callback(err); } | ||
const enctoken = jwt.encode(token, config.secret); | ||
callback(null, enctoken); | ||
}); | ||
}); | ||
}); | ||
})); | ||
|
||
// User authorization endpoint | ||
exports.authorization = [ | ||
server.authorization((clientId, redirectUri, callback) => { | ||
Client.findOne({ id: clientId }, (err, client) => { | ||
if (err) { return callback(err); } | ||
|
||
return callback(null, client, redirectUri); | ||
}); | ||
}), | ||
(req, res) => { | ||
console.log(config.userid); | ||
res.render('dialog', { transactionID: req.oauth2.transactionID, user: config.userid, client: req.oauth2.client }); | ||
}, | ||
]; | ||
|
||
exports.decision = [ | ||
server.decision(), | ||
]; | ||
|
||
// Application client token exchange endpoint | ||
exports.token = [ | ||
server.token(), | ||
server.errorHandler(), | ||
]; |
Oops, something went wrong.