diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/controllers/api/v1/pods.js | 6 | ||||
-rw-r--r-- | server/controllers/api/v1/users.js | 9 | ||||
-rw-r--r-- | server/controllers/api/v1/videos.js | 6 | ||||
-rw-r--r-- | server/initializers/checker.js | 8 | ||||
-rw-r--r-- | server/initializers/database.js | 3 | ||||
-rw-r--r-- | server/initializers/installer.js | 27 | ||||
-rw-r--r-- | server/lib/oauth-model.js | 70 | ||||
-rw-r--r-- | server/middlewares/index.js | 4 | ||||
-rw-r--r-- | server/middlewares/oauth.js (renamed from server/middlewares/oauth2.js) | 6 | ||||
-rw-r--r-- | server/models/oauth-client.js | 33 | ||||
-rw-r--r-- | server/models/oauth-token.js | 34 | ||||
-rw-r--r-- | server/models/user.js | 28 | ||||
-rw-r--r-- | server/models/users.js | 123 |
13 files changed, 209 insertions, 148 deletions
diff --git a/server/controllers/api/v1/pods.js b/server/controllers/api/v1/pods.js index feb6bd958..9ce8120b2 100644 --- a/server/controllers/api/v1/pods.js +++ b/server/controllers/api/v1/pods.js | |||
@@ -7,7 +7,7 @@ const mongoose = require('mongoose') | |||
7 | const logger = require('../../../helpers/logger') | 7 | const logger = require('../../../helpers/logger') |
8 | const friends = require('../../../lib/friends') | 8 | const friends = require('../../../lib/friends') |
9 | const middlewares = require('../../../middlewares') | 9 | const middlewares = require('../../../middlewares') |
10 | const oAuth2 = middlewares.oauth2 | 10 | const oAuth = middlewares.oauth |
11 | const reqValidator = middlewares.reqValidators.pods | 11 | const reqValidator = middlewares.reqValidators.pods |
12 | const signatureValidator = middlewares.reqValidators.remote.signature | 12 | const signatureValidator = middlewares.reqValidators.remote.signature |
13 | 13 | ||
@@ -17,8 +17,8 @@ const Video = mongoose.model('Video') | |||
17 | 17 | ||
18 | router.get('/', listPodsUrl) | 18 | router.get('/', listPodsUrl) |
19 | router.post('/', reqValidator.podsAdd, addPods) | 19 | router.post('/', reqValidator.podsAdd, addPods) |
20 | router.get('/makefriends', oAuth2.authenticate, reqValidator.makeFriends, makeFriends) | 20 | router.get('/makefriends', oAuth.authenticate, reqValidator.makeFriends, makeFriends) |
21 | router.get('/quitfriends', oAuth2.authenticate, quitFriends) | 21 | router.get('/quitfriends', oAuth.authenticate, quitFriends) |
22 | // Post because this is a secured request | 22 | // Post because this is a secured request |
23 | router.post('/remove', signatureValidator, removePods) | 23 | router.post('/remove', signatureValidator, removePods) |
24 | 24 | ||
diff --git a/server/controllers/api/v1/users.js b/server/controllers/api/v1/users.js index 7ce0b9c6a..caa979567 100644 --- a/server/controllers/api/v1/users.js +++ b/server/controllers/api/v1/users.js | |||
@@ -1,15 +1,16 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const config = require('config') | 3 | const config = require('config') |
4 | const mongoose = require('mongoose') | ||
4 | const express = require('express') | 5 | const express = require('express') |
5 | const oAuth2 = require('../../../middlewares').oauth2 | 6 | const oAuth = require('../../../middlewares').oauth |
6 | 7 | ||
7 | const Users = require('../../../models/users') | 8 | const Client = mongoose.model('OAuthClient') |
8 | 9 | ||
9 | const router = express.Router() | 10 | const router = express.Router() |
10 | 11 | ||
11 | router.get('/client', getAngularClient) | 12 | router.get('/client', getAngularClient) |
12 | router.post('/token', oAuth2.token, success) | 13 | router.post('/token', oAuth.token, success) |
13 | 14 | ||
14 | // --------------------------------------------------------------------------- | 15 | // --------------------------------------------------------------------------- |
15 | 16 | ||
@@ -27,7 +28,7 @@ function getAngularClient (req, res, next) { | |||
27 | 28 | ||
28 | if (req.get('host') !== headerHostShouldBe) return res.type('json').status(403).end() | 29 | if (req.get('host') !== headerHostShouldBe) return res.type('json').status(403).end() |
29 | 30 | ||
30 | Users.getFirstClient(function (err, client) { | 31 | Client.loadFirstClient(function (err, client) { |
31 | if (err) return next(err) | 32 | if (err) return next(err) |
32 | if (!client) return next(new Error('No client available.')) | 33 | if (!client) return next(new Error('No client available.')) |
33 | 34 | ||
diff --git a/server/controllers/api/v1/videos.js b/server/controllers/api/v1/videos.js index d06ec8d08..ab115bc30 100644 --- a/server/controllers/api/v1/videos.js +++ b/server/controllers/api/v1/videos.js | |||
@@ -9,7 +9,7 @@ const multer = require('multer') | |||
9 | const logger = require('../../../helpers/logger') | 9 | const logger = require('../../../helpers/logger') |
10 | const friends = require('../../../lib/friends') | 10 | const friends = require('../../../lib/friends') |
11 | const middlewares = require('../../../middlewares') | 11 | const middlewares = require('../../../middlewares') |
12 | const oAuth2 = middlewares.oauth2 | 12 | const oAuth = middlewares.oauth |
13 | const pagination = middlewares.pagination | 13 | const pagination = middlewares.pagination |
14 | const reqValidator = middlewares.reqValidators | 14 | const reqValidator = middlewares.reqValidators |
15 | const reqValidatorPagination = reqValidator.pagination | 15 | const reqValidatorPagination = reqValidator.pagination |
@@ -51,7 +51,7 @@ router.get('/', | |||
51 | listVideos | 51 | listVideos |
52 | ) | 52 | ) |
53 | router.post('/', | 53 | router.post('/', |
54 | oAuth2.authenticate, | 54 | oAuth.authenticate, |
55 | reqFiles, | 55 | reqFiles, |
56 | reqValidatorVideos.videosAdd, | 56 | reqValidatorVideos.videosAdd, |
57 | addVideo | 57 | addVideo |
@@ -61,7 +61,7 @@ router.get('/:id', | |||
61 | getVideo | 61 | getVideo |
62 | ) | 62 | ) |
63 | router.delete('/:id', | 63 | router.delete('/:id', |
64 | oAuth2.authenticate, | 64 | oAuth.authenticate, |
65 | reqValidatorVideos.videosRemove, | 65 | reqValidatorVideos.videosRemove, |
66 | removeVideo | 66 | removeVideo |
67 | ) | 67 | ) |
diff --git a/server/initializers/checker.js b/server/initializers/checker.js index 7a2b5b132..3831efb8d 100644 --- a/server/initializers/checker.js +++ b/server/initializers/checker.js | |||
@@ -1,8 +1,10 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const config = require('config') | 3 | const config = require('config') |
4 | const mongoose = require('mongoose') | ||
4 | 5 | ||
5 | const Users = require('../models/users') | 6 | const Client = mongoose.model('OAuthClient') |
7 | const User = mongoose.model('User') | ||
6 | 8 | ||
7 | const checker = { | 9 | const checker = { |
8 | checkConfig: checkConfig, | 10 | checkConfig: checkConfig, |
@@ -29,7 +31,7 @@ function checkConfig () { | |||
29 | } | 31 | } |
30 | 32 | ||
31 | function clientsExist (callback) { | 33 | function clientsExist (callback) { |
32 | Users.getClients(function (err, clients) { | 34 | Client.list(function (err, clients) { |
33 | if (err) return callback(err) | 35 | if (err) return callback(err) |
34 | 36 | ||
35 | return callback(null, clients.length !== 0) | 37 | return callback(null, clients.length !== 0) |
@@ -37,7 +39,7 @@ function clientsExist (callback) { | |||
37 | } | 39 | } |
38 | 40 | ||
39 | function usersExist (callback) { | 41 | function usersExist (callback) { |
40 | Users.getUsers(function (err, users) { | 42 | User.list(function (err, users) { |
41 | if (err) return callback(err) | 43 | if (err) return callback(err) |
42 | 44 | ||
43 | return callback(null, users.length !== 0) | 45 | return callback(null, users.length !== 0) |
diff --git a/server/initializers/database.js b/server/initializers/database.js index e97531781..4a19bc230 100644 --- a/server/initializers/database.js +++ b/server/initializers/database.js | |||
@@ -6,6 +6,9 @@ const mongoose = require('mongoose') | |||
6 | const logger = require('../helpers/logger') | 6 | const logger = require('../helpers/logger') |
7 | 7 | ||
8 | // Bootstrap models | 8 | // Bootstrap models |
9 | require('../models/user') | ||
10 | require('../models/oauth-client') | ||
11 | require('../models/oauth-token') | ||
9 | require('../models/pods') | 12 | require('../models/pods') |
10 | require('../models/video') | 13 | require('../models/video') |
11 | // Request model needs Video model | 14 | // Request model needs Video model |
diff --git a/server/initializers/installer.js b/server/initializers/installer.js index 059fcd8d8..e0ae822cf 100644 --- a/server/initializers/installer.js +++ b/server/initializers/installer.js | |||
@@ -3,13 +3,16 @@ | |||
3 | const async = require('async') | 3 | const async = require('async') |
4 | const config = require('config') | 4 | const config = require('config') |
5 | const mkdirp = require('mkdirp') | 5 | const mkdirp = require('mkdirp') |
6 | const mongoose = require('mongoose') | ||
6 | const passwordGenerator = require('password-generator') | 7 | const passwordGenerator = require('password-generator') |
7 | const path = require('path') | 8 | const path = require('path') |
8 | 9 | ||
9 | const checker = require('./checker') | 10 | const checker = require('./checker') |
10 | const logger = require('../helpers/logger') | 11 | const logger = require('../helpers/logger') |
11 | const peertubeCrypto = require('../helpers/peertubeCrypto') | 12 | const peertubeCrypto = require('../helpers/peertubeCrypto') |
12 | const Users = require('../models/users') | 13 | |
14 | const Client = mongoose.model('OAuthClient') | ||
15 | const User = mongoose.model('User') | ||
13 | 16 | ||
14 | const installer = { | 17 | const installer = { |
15 | installApplication: installApplication | 18 | installApplication: installApplication |
@@ -60,11 +63,16 @@ function createOAuthClientIfNotExist (callback) { | |||
60 | logger.info('Creating a default OAuth Client.') | 63 | logger.info('Creating a default OAuth Client.') |
61 | 64 | ||
62 | const secret = passwordGenerator(32, false) | 65 | const secret = passwordGenerator(32, false) |
63 | Users.createClient(secret, [ 'password' ], function (err, id) { | 66 | const client = new Client({ |
67 | clientSecret: secret, | ||
68 | grants: [ 'password' ] | ||
69 | }) | ||
70 | |||
71 | client.save(function (err, createdClient) { | ||
64 | if (err) return callback(err) | 72 | if (err) return callback(err) |
65 | 73 | ||
66 | logger.info('Client id: ' + id) | 74 | logger.info('Client id: ' + createdClient._id) |
67 | logger.info('Client secret: ' + secret) | 75 | logger.info('Client secret: ' + createdClient.clientSecret) |
68 | 76 | ||
69 | return callback(null) | 77 | return callback(null) |
70 | }) | 78 | }) |
@@ -94,11 +102,16 @@ function createOAuthUserIfNotExist (callback) { | |||
94 | password = passwordGenerator(8, true) | 102 | password = passwordGenerator(8, true) |
95 | } | 103 | } |
96 | 104 | ||
97 | Users.createUser(username, password, function (err) { | 105 | const user = new User({ |
106 | username: username, | ||
107 | password: password | ||
108 | }) | ||
109 | |||
110 | user.save(function (err, createdUser) { | ||
98 | if (err) return callback(err) | 111 | if (err) return callback(err) |
99 | 112 | ||
100 | logger.info('Username: ' + username) | 113 | logger.info('Username: ' + createdUser.username) |
101 | logger.info('User password: ' + password) | 114 | logger.info('User password: ' + createdUser.password) |
102 | 115 | ||
103 | return callback(null) | 116 | return callback(null) |
104 | }) | 117 | }) |
diff --git a/server/lib/oauth-model.js b/server/lib/oauth-model.js new file mode 100644 index 000000000..f4fd9805a --- /dev/null +++ b/server/lib/oauth-model.js | |||
@@ -0,0 +1,70 @@ | |||
1 | const mongoose = require('mongoose') | ||
2 | |||
3 | const logger = require('../helpers/logger') | ||
4 | |||
5 | const OAuthClient = mongoose.model('OAuthClient') | ||
6 | const OAuthToken = mongoose.model('OAuthToken') | ||
7 | const User = mongoose.model('User') | ||
8 | |||
9 | // See https://github.com/oauthjs/node-oauth2-server/wiki/Model-specification for the model specifications | ||
10 | const OAuthModel = { | ||
11 | getAccessToken: getAccessToken, | ||
12 | getClient: getClient, | ||
13 | getRefreshToken: getRefreshToken, | ||
14 | getUser: getUser, | ||
15 | saveToken: saveToken | ||
16 | } | ||
17 | |||
18 | // --------------------------------------------------------------------------- | ||
19 | |||
20 | function getAccessToken (bearerToken) { | ||
21 | logger.debug('Getting access token (bearerToken: ' + bearerToken + ').') | ||
22 | |||
23 | return OAuthToken.loadByTokenAndPopulateUser(bearerToken) | ||
24 | } | ||
25 | |||
26 | function getClient (clientId, clientSecret) { | ||
27 | logger.debug('Getting Client (clientId: ' + clientId + ', clientSecret: ' + clientSecret + ').') | ||
28 | |||
29 | // TODO req validator | ||
30 | const mongoId = new mongoose.mongo.ObjectID(clientId) | ||
31 | return OAuthClient.loadByIdAndSecret(mongoId, clientSecret) | ||
32 | } | ||
33 | |||
34 | function getRefreshToken (refreshToken) { | ||
35 | logger.debug('Getting RefreshToken (refreshToken: ' + refreshToken + ').') | ||
36 | |||
37 | return OAuthToken.loadByRefreshToken(refreshToken) | ||
38 | } | ||
39 | |||
40 | function getUser (username, password) { | ||
41 | logger.debug('Getting User (username: ' + username + ', password: ' + password + ').') | ||
42 | |||
43 | return User.loadByUsernameAndPassword(username, password) | ||
44 | } | ||
45 | |||
46 | function saveToken (token, client, user) { | ||
47 | logger.debug('Saving token for client ' + client.id + ' and user ' + user.id + '.') | ||
48 | |||
49 | const tokenObj = new OAuthToken({ | ||
50 | accessToken: token.accessToken, | ||
51 | accessTokenExpiresOn: token.accessTokenExpiresOn, | ||
52 | client: client.id, | ||
53 | refreshToken: token.refreshToken, | ||
54 | refreshTokenExpiresOn: token.refreshTokenExpiresOn, | ||
55 | user: user.id | ||
56 | }) | ||
57 | |||
58 | return tokenObj.save(function (err, tokenCreated) { | ||
59 | if (err) throw err // node-oauth2-server library uses Promise.try | ||
60 | |||
61 | tokenCreated.client = client | ||
62 | tokenCreated.user = user | ||
63 | |||
64 | return tokenCreated | ||
65 | }) | ||
66 | } | ||
67 | |||
68 | // --------------------------------------------------------------------------- | ||
69 | |||
70 | module.exports = OAuthModel | ||
diff --git a/server/middlewares/index.js b/server/middlewares/index.js index b30a7be56..01043cd85 100644 --- a/server/middlewares/index.js +++ b/server/middlewares/index.js | |||
@@ -1,6 +1,6 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const oauth2 = require('./oauth2') | 3 | const oauth = require('./oauth') |
4 | const pagination = require('./pagination') | 4 | const pagination = require('./pagination') |
5 | const reqValidatorsMiddleware = require('./reqValidators') | 5 | const reqValidatorsMiddleware = require('./reqValidators') |
6 | const search = require('./search') | 6 | const search = require('./search') |
@@ -8,7 +8,7 @@ const sort = require('./sort') | |||
8 | const secureMiddleware = require('./secure') | 8 | const secureMiddleware = require('./secure') |
9 | 9 | ||
10 | const middlewares = { | 10 | const middlewares = { |
11 | oauth2: oauth2, | 11 | oauth: oauth, |
12 | pagination: pagination, | 12 | pagination: pagination, |
13 | reqValidators: reqValidatorsMiddleware, | 13 | reqValidators: reqValidatorsMiddleware, |
14 | search: search, | 14 | search: search, |
diff --git a/server/middlewares/oauth2.js b/server/middlewares/oauth.js index 1defdc02e..3d7429f1d 100644 --- a/server/middlewares/oauth2.js +++ b/server/middlewares/oauth.js | |||
@@ -5,10 +5,10 @@ const OAuthServer = require('express-oauth-server') | |||
5 | const logger = require('../helpers/logger') | 5 | const logger = require('../helpers/logger') |
6 | 6 | ||
7 | const oAuthServer = new OAuthServer({ | 7 | const oAuthServer = new OAuthServer({ |
8 | model: require('../models/users') | 8 | model: require('../lib/oauth-model') |
9 | }) | 9 | }) |
10 | 10 | ||
11 | const oAuth2 = { | 11 | const oAuth = { |
12 | authenticate: authenticate, | 12 | authenticate: authenticate, |
13 | token: token | 13 | token: token |
14 | } | 14 | } |
@@ -32,4 +32,4 @@ function token (req, res, next) { | |||
32 | 32 | ||
33 | // --------------------------------------------------------------------------- | 33 | // --------------------------------------------------------------------------- |
34 | 34 | ||
35 | module.exports = oAuth2 | 35 | module.exports = oAuth |
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 @@ | |||
1 | const mongoose = require('mongoose') | ||
2 | |||
3 | // --------------------------------------------------------------------------- | ||
4 | |||
5 | const OAuthClientSchema = mongoose.Schema({ | ||
6 | clientSecret: String, | ||
7 | grants: Array, | ||
8 | redirectUris: Array | ||
9 | }) | ||
10 | |||
11 | OAuthClientSchema.path('clientSecret').required(true) | ||
12 | |||
13 | OAuthClientSchema.statics = { | ||
14 | list: list, | ||
15 | loadByIdAndSecret: loadByIdAndSecret, | ||
16 | loadFirstClient: loadFirstClient | ||
17 | } | ||
18 | |||
19 | mongoose.model('OAuthClient', OAuthClientSchema) | ||
20 | |||
21 | // --------------------------------------------------------------------------- | ||
22 | |||
23 | function list (callback) { | ||
24 | return this.find(callback) | ||
25 | } | ||
26 | |||
27 | function loadFirstClient (callback) { | ||
28 | return this.findOne({}, callback) | ||
29 | } | ||
30 | |||
31 | function loadByIdAndSecret (id, clientSecret) { | ||
32 | return this.findOne({ _id: id, clientSecret: clientSecret }) | ||
33 | } | ||
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 @@ | |||
1 | const mongoose = require('mongoose') | ||
2 | |||
3 | // --------------------------------------------------------------------------- | ||
4 | |||
5 | const OAuthTokenSchema = mongoose.Schema({ | ||
6 | accessToken: String, | ||
7 | accessTokenExpiresOn: Date, | ||
8 | client: { type: mongoose.Schema.Types.ObjectId, ref: 'OAuthClient' }, | ||
9 | refreshToken: String, | ||
10 | refreshTokenExpiresOn: Date, | ||
11 | user: { type: mongoose.Schema.Types.ObjectId, ref: 'User' } | ||
12 | }) | ||
13 | |||
14 | OAuthTokenSchema.path('accessToken').required(true) | ||
15 | OAuthTokenSchema.path('client').required(true) | ||
16 | OAuthTokenSchema.path('user').required(true) | ||
17 | |||
18 | OAuthTokenSchema.statics = { | ||
19 | loadByRefreshToken: loadByRefreshToken, | ||
20 | loadByTokenAndPopulateUser: loadByTokenAndPopulateUser | ||
21 | } | ||
22 | |||
23 | mongoose.model('OAuthToken', OAuthTokenSchema) | ||
24 | |||
25 | // --------------------------------------------------------------------------- | ||
26 | |||
27 | function loadByRefreshToken (refreshToken, callback) { | ||
28 | return this.findOne({ refreshToken: refreshToken }, callback) | ||
29 | } | ||
30 | |||
31 | function loadByTokenAndPopulateUser (bearerToken, callback) { | ||
32 | // FIXME: allow to use callback | ||
33 | return this.findOne({ accessToken: bearerToken }).populate('user') | ||
34 | } | ||
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 @@ | |||
1 | const mongoose = require('mongoose') | ||
2 | |||
3 | // --------------------------------------------------------------------------- | ||
4 | |||
5 | const UserSchema = mongoose.Schema({ | ||
6 | password: String, | ||
7 | username: String | ||
8 | }) | ||
9 | |||
10 | UserSchema.path('password').required(true) | ||
11 | UserSchema.path('username').required(true) | ||
12 | |||
13 | UserSchema.statics = { | ||
14 | list: list, | ||
15 | loadByUsernameAndPassword: loadByUsernameAndPassword | ||
16 | } | ||
17 | |||
18 | mongoose.model('User', UserSchema) | ||
19 | |||
20 | // --------------------------------------------------------------------------- | ||
21 | |||
22 | function list (callback) { | ||
23 | return this.find(callback) | ||
24 | } | ||
25 | |||
26 | function loadByUsernameAndPassword (username, password, callback) { | ||
27 | return this.findOne({ username: username, password: password }, callback) | ||
28 | } | ||
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 @@ | |||
1 | const mongoose = require('mongoose') | ||
2 | |||
3 | const logger = require('../helpers/logger') | ||
4 | |||
5 | // --------------------------------------------------------------------------- | ||
6 | |||
7 | const oAuthTokensSchema = mongoose.Schema({ | ||
8 | accessToken: String, | ||
9 | accessTokenExpiresOn: Date, | ||
10 | client: { type: mongoose.Schema.Types.ObjectId, ref: 'oAuthClients' }, | ||
11 | refreshToken: String, | ||
12 | refreshTokenExpiresOn: Date, | ||
13 | user: { type: mongoose.Schema.Types.ObjectId, ref: 'users' } | ||
14 | }) | ||
15 | const OAuthTokensDB = mongoose.model('oAuthTokens', oAuthTokensSchema) | ||
16 | |||
17 | const oAuthClientsSchema = mongoose.Schema({ | ||
18 | clientSecret: String, | ||
19 | grants: Array, | ||
20 | redirectUris: Array | ||
21 | }) | ||
22 | const OAuthClientsDB = mongoose.model('oAuthClients', oAuthClientsSchema) | ||
23 | |||
24 | const usersSchema = mongoose.Schema({ | ||
25 | password: String, | ||
26 | username: String | ||
27 | }) | ||
28 | const UsersDB = mongoose.model('users', usersSchema) | ||
29 | |||
30 | // --------------------------------------------------------------------------- | ||
31 | |||
32 | const Users = { | ||
33 | createClient: createClient, | ||
34 | createUser: createUser, | ||
35 | getAccessToken: getAccessToken, | ||
36 | getClient: getClient, | ||
37 | getClients: getClients, | ||
38 | getFirstClient: getFirstClient, | ||
39 | getRefreshToken: getRefreshToken, | ||
40 | getUser: getUser, | ||
41 | getUsers: getUsers, | ||
42 | saveToken: saveToken | ||
43 | } | ||
44 | |||
45 | function createClient (secret, grants, callback) { | ||
46 | logger.debug('Creating client.') | ||
47 | |||
48 | const mongoId = new mongoose.mongo.ObjectID() | ||
49 | return OAuthClientsDB.create({ _id: mongoId, clientSecret: secret, grants: grants }, function (err) { | ||
50 | if (err) return callback(err) | ||
51 | |||
52 | return callback(null, mongoId) | ||
53 | }) | ||
54 | } | ||
55 | |||
56 | function createUser (username, password, callback) { | ||
57 | logger.debug('Creating user.') | ||
58 | |||
59 | return UsersDB.create({ username: username, password: password }, callback) | ||
60 | } | ||
61 | |||
62 | function getAccessToken (bearerToken, callback) { | ||
63 | logger.debug('Getting access token (bearerToken: ' + bearerToken + ').') | ||
64 | |||
65 | return OAuthTokensDB.findOne({ accessToken: bearerToken }).populate('user') | ||
66 | } | ||
67 | |||
68 | function getFirstClient (callback) { | ||
69 | return OAuthClientsDB.findOne({}, callback) | ||
70 | } | ||
71 | |||
72 | function getClient (clientId, clientSecret) { | ||
73 | logger.debug('Getting Client (clientId: ' + clientId + ', clientSecret: ' + clientSecret + ').') | ||
74 | |||
75 | // TODO req validator | ||
76 | const mongoId = new mongoose.mongo.ObjectID(clientId) | ||
77 | return OAuthClientsDB.findOne({ _id: mongoId, clientSecret: clientSecret }) | ||
78 | } | ||
79 | |||
80 | function getClients (callback) { | ||
81 | return OAuthClientsDB.find(callback) | ||
82 | } | ||
83 | |||
84 | function getRefreshToken (refreshToken) { | ||
85 | logger.debug('Getting RefreshToken (refreshToken: ' + refreshToken + ').') | ||
86 | |||
87 | return OAuthTokensDB.findOne({ refreshToken: refreshToken }) | ||
88 | } | ||
89 | |||
90 | function getUser (username, password) { | ||
91 | logger.debug('Getting User (username: ' + username + ', password: ' + password + ').') | ||
92 | return UsersDB.findOne({ username: username, password: password }) | ||
93 | } | ||
94 | |||
95 | function getUsers (callback) { | ||
96 | return UsersDB.find(callback) | ||
97 | } | ||
98 | |||
99 | function saveToken (token, client, user) { | ||
100 | logger.debug('Saving token for client ' + client.id + ' and user ' + user.id + '.') | ||
101 | |||
102 | const tokenToCreate = { | ||
103 | accessToken: token.accessToken, | ||
104 | accessTokenExpiresOn: token.accessTokenExpiresOn, | ||
105 | client: client.id, | ||
106 | refreshToken: token.refreshToken, | ||
107 | refreshTokenExpiresOn: token.refreshTokenExpiresOn, | ||
108 | user: user.id | ||
109 | } | ||
110 | |||
111 | return OAuthTokensDB.create(tokenToCreate, function (err, tokenCreated) { | ||
112 | if (err) throw err // node-oauth2-server library uses Promise.try | ||
113 | |||
114 | tokenCreated.client = client | ||
115 | tokenCreated.user = user | ||
116 | |||
117 | return tokenCreated | ||
118 | }) | ||
119 | } | ||
120 | |||
121 | // --------------------------------------------------------------------------- | ||
122 | |||
123 | module.exports = Users | ||