From 69b0a27cbbd69ca019eb7db5f917b1dd06dc82cd Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 1 Jul 2016 16:03:53 +0200 Subject: OAuth/User models refractoring -> use mongoose api --- server/models/oauth-client.js | 33 ++++++++++++ server/models/oauth-token.js | 34 ++++++++++++ server/models/user.js | 28 ++++++++++ server/models/users.js | 123 ------------------------------------------ 4 files changed, 95 insertions(+), 123 deletions(-) create mode 100644 server/models/oauth-client.js create mode 100644 server/models/oauth-token.js create mode 100644 server/models/user.js delete mode 100644 server/models/users.js (limited to 'server/models') diff --git a/server/models/oauth-client.js b/server/models/oauth-client.js new file mode 100644 index 000000000..048e5af48 --- /dev/null +++ b/server/models/oauth-client.js @@ -0,0 +1,33 @@ +const mongoose = require('mongoose') + +// --------------------------------------------------------------------------- + +const OAuthClientSchema = mongoose.Schema({ + clientSecret: String, + grants: Array, + redirectUris: Array +}) + +OAuthClientSchema.path('clientSecret').required(true) + +OAuthClientSchema.statics = { + list: list, + loadByIdAndSecret: loadByIdAndSecret, + loadFirstClient: loadFirstClient +} + +mongoose.model('OAuthClient', OAuthClientSchema) + +// --------------------------------------------------------------------------- + +function list (callback) { + return this.find(callback) +} + +function loadFirstClient (callback) { + return this.findOne({}, callback) +} + +function loadByIdAndSecret (id, clientSecret) { + return this.findOne({ _id: id, clientSecret: clientSecret }) +} diff --git a/server/models/oauth-token.js b/server/models/oauth-token.js new file mode 100644 index 000000000..5da5da417 --- /dev/null +++ b/server/models/oauth-token.js @@ -0,0 +1,34 @@ +const mongoose = require('mongoose') + +// --------------------------------------------------------------------------- + +const OAuthTokenSchema = mongoose.Schema({ + accessToken: String, + accessTokenExpiresOn: Date, + client: { type: mongoose.Schema.Types.ObjectId, ref: 'OAuthClient' }, + refreshToken: String, + refreshTokenExpiresOn: Date, + user: { type: mongoose.Schema.Types.ObjectId, ref: 'User' } +}) + +OAuthTokenSchema.path('accessToken').required(true) +OAuthTokenSchema.path('client').required(true) +OAuthTokenSchema.path('user').required(true) + +OAuthTokenSchema.statics = { + loadByRefreshToken: loadByRefreshToken, + loadByTokenAndPopulateUser: loadByTokenAndPopulateUser +} + +mongoose.model('OAuthToken', OAuthTokenSchema) + +// --------------------------------------------------------------------------- + +function loadByRefreshToken (refreshToken, callback) { + return this.findOne({ refreshToken: refreshToken }, callback) +} + +function loadByTokenAndPopulateUser (bearerToken, callback) { + // FIXME: allow to use callback + return this.findOne({ accessToken: bearerToken }).populate('user') +} diff --git a/server/models/user.js b/server/models/user.js new file mode 100644 index 000000000..130b49b55 --- /dev/null +++ b/server/models/user.js @@ -0,0 +1,28 @@ +const mongoose = require('mongoose') + +// --------------------------------------------------------------------------- + +const UserSchema = mongoose.Schema({ + password: String, + username: String +}) + +UserSchema.path('password').required(true) +UserSchema.path('username').required(true) + +UserSchema.statics = { + list: list, + loadByUsernameAndPassword: loadByUsernameAndPassword +} + +mongoose.model('User', UserSchema) + +// --------------------------------------------------------------------------- + +function list (callback) { + return this.find(callback) +} + +function loadByUsernameAndPassword (username, password, callback) { + return this.findOne({ username: username, password: password }, callback) +} diff --git a/server/models/users.js b/server/models/users.js deleted file mode 100644 index a1bdece23..000000000 --- a/server/models/users.js +++ /dev/null @@ -1,123 +0,0 @@ -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, - getClients: getClients, - getFirstClient: getFirstClient, - getRefreshToken: getRefreshToken, - getUser: getUser, - getUsers: getUsers, - saveToken: saveToken -} - -function createClient (secret, grants, callback) { - logger.debug('Creating client.') - - const mongoId = new mongoose.mongo.ObjectID() - return OAuthClientsDB.create({ _id: mongoId, clientSecret: secret, grants: grants }, function (err) { - if (err) return callback(err) - - return callback(null, mongoId) - }) -} - -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') -} - -function getFirstClient (callback) { - return OAuthClientsDB.findOne({}, callback) -} - -function getClient (clientId, clientSecret) { - logger.debug('Getting Client (clientId: ' + clientId + ', clientSecret: ' + clientSecret + ').') - - // TODO req validator - const mongoId = new mongoose.mongo.ObjectID(clientId) - return OAuthClientsDB.findOne({ _id: mongoId, clientSecret: clientSecret }) -} - -function getClients (callback) { - return OAuthClientsDB.find(callback) -} - -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 }) -} - -function getUsers (callback) { - return UsersDB.find(callback) -} - -function saveToken (token, client, user) { - logger.debug('Saving token for client ' + client.id + ' and user ' + user.id + '.') - - const tokenToCreate = { - accessToken: token.accessToken, - accessTokenExpiresOn: token.accessTokenExpiresOn, - client: client.id, - refreshToken: token.refreshToken, - refreshTokenExpiresOn: token.refreshTokenExpiresOn, - user: user.id - } - - return OAuthTokensDB.create(tokenToCreate, function (err, tokenCreated) { - if (err) throw err // node-oauth2-server library uses Promise.try - - tokenCreated.client = client - tokenCreated.user = user - - return tokenCreated - }) -} - -// --------------------------------------------------------------------------- - -module.exports = Users -- cgit v1.2.3