2016-03-21 06:56:33 -04:00
|
|
|
const mongoose = require('mongoose')
|
|
|
|
|
|
|
|
const logger = require('../helpers/logger')
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const oAuthTokensSchema = mongoose.Schema({
|
|
|
|
accessToken: String,
|
|
|
|
accessTokenExpiresOn: Date,
|
|
|
|
client: { type: mongoose.Schema.Types.ObjectId, ref: 'oAuthClients' },
|
|
|
|
refreshToken: String,
|
|
|
|
refreshTokenExpiresOn: Date,
|
|
|
|
user: { type: mongoose.Schema.Types.ObjectId, ref: 'users' }
|
|
|
|
})
|
|
|
|
const OAuthTokensDB = mongoose.model('oAuthTokens', oAuthTokensSchema)
|
|
|
|
|
|
|
|
const oAuthClientsSchema = mongoose.Schema({
|
|
|
|
clientSecret: String,
|
|
|
|
grants: Array,
|
|
|
|
redirectUris: Array
|
|
|
|
})
|
|
|
|
const OAuthClientsDB = mongoose.model('oAuthClients', oAuthClientsSchema)
|
|
|
|
|
|
|
|
const usersSchema = mongoose.Schema({
|
|
|
|
password: String,
|
|
|
|
username: String
|
|
|
|
})
|
|
|
|
const UsersDB = mongoose.model('users', usersSchema)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const Users = {
|
|
|
|
createClient: createClient,
|
|
|
|
createUser: createUser,
|
|
|
|
getAccessToken: getAccessToken,
|
|
|
|
getClient: getClient,
|
2016-03-21 16:11:26 -04:00
|
|
|
getClients: getClients,
|
2016-04-27 16:11:48 -04:00
|
|
|
getFirstClient: getFirstClient,
|
2016-03-21 06:56:33 -04:00
|
|
|
getRefreshToken: getRefreshToken,
|
|
|
|
getUser: getUser,
|
2016-03-21 16:11:26 -04:00
|
|
|
getUsers: getUsers,
|
2016-03-21 06:56:33 -04:00
|
|
|
saveToken: saveToken
|
|
|
|
}
|
|
|
|
|
|
|
|
function createClient (secret, grants, callback) {
|
|
|
|
logger.debug('Creating client.')
|
|
|
|
|
2016-05-11 15:19:34 -04:00
|
|
|
const mongoId = new mongoose.mongo.ObjectID()
|
|
|
|
return OAuthClientsDB.create({ _id: mongoId, clientSecret: secret, grants: grants }, function (err) {
|
2016-03-21 06:56:33 -04:00
|
|
|
if (err) return callback(err)
|
|
|
|
|
2016-05-11 15:19:34 -04:00
|
|
|
return callback(null, mongoId)
|
2016-03-21 06:56:33 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function createUser (username, password, callback) {
|
|
|
|
logger.debug('Creating user.')
|
|
|
|
|
|
|
|
return UsersDB.create({ username: username, password: password }, callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
function getAccessToken (bearerToken, callback) {
|
|
|
|
logger.debug('Getting access token (bearerToken: ' + bearerToken + ').')
|
|
|
|
|
|
|
|
return OAuthTokensDB.findOne({ accessToken: bearerToken }).populate('user')
|
|
|
|
}
|
|
|
|
|
2016-04-27 16:11:48 -04:00
|
|
|
function getFirstClient (callback) {
|
|
|
|
return OAuthClientsDB.findOne({}, callback)
|
|
|
|
}
|
|
|
|
|
2016-03-21 06:56:33 -04:00
|
|
|
function getClient (clientId, clientSecret) {
|
|
|
|
logger.debug('Getting Client (clientId: ' + clientId + ', clientSecret: ' + clientSecret + ').')
|
|
|
|
|
|
|
|
// TODO req validator
|
2016-05-11 15:19:34 -04:00
|
|
|
const mongoId = new mongoose.mongo.ObjectID(clientId)
|
|
|
|
return OAuthClientsDB.findOne({ _id: mongoId, clientSecret: clientSecret })
|
2016-03-21 06:56:33 -04:00
|
|
|
}
|
|
|
|
|
2016-03-21 16:11:26 -04:00
|
|
|
function getClients (callback) {
|
|
|
|
return OAuthClientsDB.find(callback)
|
|
|
|
}
|
|
|
|
|
2016-03-21 06:56:33 -04:00
|
|
|
function getRefreshToken (refreshToken) {
|
|
|
|
logger.debug('Getting RefreshToken (refreshToken: ' + refreshToken + ').')
|
|
|
|
|
|
|
|
return OAuthTokensDB.findOne({ refreshToken: refreshToken })
|
|
|
|
}
|
|
|
|
|
|
|
|
function getUser (username, password) {
|
|
|
|
logger.debug('Getting User (username: ' + username + ', password: ' + password + ').')
|
|
|
|
return UsersDB.findOne({ username: username, password: password })
|
|
|
|
}
|
|
|
|
|
2016-03-21 16:11:26 -04:00
|
|
|
function getUsers (callback) {
|
|
|
|
return UsersDB.find(callback)
|
|
|
|
}
|
|
|
|
|
2016-03-21 06:56:33 -04:00
|
|
|
function saveToken (token, client, user) {
|
|
|
|
logger.debug('Saving token for client ' + client.id + ' and user ' + user.id + '.')
|
|
|
|
|
2016-05-11 15:19:34 -04:00
|
|
|
const tokenToCreate = {
|
2016-03-21 06:56:33 -04:00
|
|
|
accessToken: token.accessToken,
|
|
|
|
accessTokenExpiresOn: token.accessTokenExpiresOn,
|
|
|
|
client: client.id,
|
|
|
|
refreshToken: token.refreshToken,
|
|
|
|
refreshTokenExpiresOn: token.refreshTokenExpiresOn,
|
|
|
|
user: user.id
|
|
|
|
}
|
|
|
|
|
2016-05-11 15:19:34 -04:00
|
|
|
return OAuthTokensDB.create(tokenToCreate, function (err, tokenCreated) {
|
2016-04-14 16:06:11 -04:00
|
|
|
if (err) throw err // node-oauth2-server library uses Promise.try
|
2016-03-21 06:56:33 -04:00
|
|
|
|
2016-05-11 15:19:34 -04:00
|
|
|
tokenCreated.client = client
|
|
|
|
tokenCreated.user = user
|
2016-03-21 06:56:33 -04:00
|
|
|
|
2016-05-11 15:19:34 -04:00
|
|
|
return tokenCreated
|
2016-03-21 06:56:33 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
module.exports = Users
|