aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/controllers/api/v1/pods.js6
-rw-r--r--server/controllers/api/v1/users.js9
-rw-r--r--server/controllers/api/v1/videos.js6
-rw-r--r--server/initializers/checker.js8
-rw-r--r--server/initializers/database.js3
-rw-r--r--server/initializers/installer.js27
-rw-r--r--server/lib/oauth-model.js70
-rw-r--r--server/middlewares/index.js4
-rw-r--r--server/middlewares/oauth.js (renamed from server/middlewares/oauth2.js)6
-rw-r--r--server/models/oauth-client.js33
-rw-r--r--server/models/oauth-token.js34
-rw-r--r--server/models/user.js28
-rw-r--r--server/models/users.js123
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')
7const logger = require('../../../helpers/logger') 7const logger = require('../../../helpers/logger')
8const friends = require('../../../lib/friends') 8const friends = require('../../../lib/friends')
9const middlewares = require('../../../middlewares') 9const middlewares = require('../../../middlewares')
10const oAuth2 = middlewares.oauth2 10const oAuth = middlewares.oauth
11const reqValidator = middlewares.reqValidators.pods 11const reqValidator = middlewares.reqValidators.pods
12const signatureValidator = middlewares.reqValidators.remote.signature 12const signatureValidator = middlewares.reqValidators.remote.signature
13 13
@@ -17,8 +17,8 @@ const Video = mongoose.model('Video')
17 17
18router.get('/', listPodsUrl) 18router.get('/', listPodsUrl)
19router.post('/', reqValidator.podsAdd, addPods) 19router.post('/', reqValidator.podsAdd, addPods)
20router.get('/makefriends', oAuth2.authenticate, reqValidator.makeFriends, makeFriends) 20router.get('/makefriends', oAuth.authenticate, reqValidator.makeFriends, makeFriends)
21router.get('/quitfriends', oAuth2.authenticate, quitFriends) 21router.get('/quitfriends', oAuth.authenticate, quitFriends)
22// Post because this is a secured request 22// Post because this is a secured request
23router.post('/remove', signatureValidator, removePods) 23router.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
3const config = require('config') 3const config = require('config')
4const mongoose = require('mongoose')
4const express = require('express') 5const express = require('express')
5const oAuth2 = require('../../../middlewares').oauth2 6const oAuth = require('../../../middlewares').oauth
6 7
7const Users = require('../../../models/users') 8const Client = mongoose.model('OAuthClient')
8 9
9const router = express.Router() 10const router = express.Router()
10 11
11router.get('/client', getAngularClient) 12router.get('/client', getAngularClient)
12router.post('/token', oAuth2.token, success) 13router.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')
9const logger = require('../../../helpers/logger') 9const logger = require('../../../helpers/logger')
10const friends = require('../../../lib/friends') 10const friends = require('../../../lib/friends')
11const middlewares = require('../../../middlewares') 11const middlewares = require('../../../middlewares')
12const oAuth2 = middlewares.oauth2 12const oAuth = middlewares.oauth
13const pagination = middlewares.pagination 13const pagination = middlewares.pagination
14const reqValidator = middlewares.reqValidators 14const reqValidator = middlewares.reqValidators
15const reqValidatorPagination = reqValidator.pagination 15const reqValidatorPagination = reqValidator.pagination
@@ -51,7 +51,7 @@ router.get('/',
51 listVideos 51 listVideos
52) 52)
53router.post('/', 53router.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)
63router.delete('/:id', 63router.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
3const config = require('config') 3const config = require('config')
4const mongoose = require('mongoose')
4 5
5const Users = require('../models/users') 6const Client = mongoose.model('OAuthClient')
7const User = mongoose.model('User')
6 8
7const checker = { 9const checker = {
8 checkConfig: checkConfig, 10 checkConfig: checkConfig,
@@ -29,7 +31,7 @@ function checkConfig () {
29} 31}
30 32
31function clientsExist (callback) { 33function 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
39function usersExist (callback) { 41function 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')
6const logger = require('../helpers/logger') 6const logger = require('../helpers/logger')
7 7
8// Bootstrap models 8// Bootstrap models
9require('../models/user')
10require('../models/oauth-client')
11require('../models/oauth-token')
9require('../models/pods') 12require('../models/pods')
10require('../models/video') 13require('../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 @@
3const async = require('async') 3const async = require('async')
4const config = require('config') 4const config = require('config')
5const mkdirp = require('mkdirp') 5const mkdirp = require('mkdirp')
6const mongoose = require('mongoose')
6const passwordGenerator = require('password-generator') 7const passwordGenerator = require('password-generator')
7const path = require('path') 8const path = require('path')
8 9
9const checker = require('./checker') 10const checker = require('./checker')
10const logger = require('../helpers/logger') 11const logger = require('../helpers/logger')
11const peertubeCrypto = require('../helpers/peertubeCrypto') 12const peertubeCrypto = require('../helpers/peertubeCrypto')
12const Users = require('../models/users') 13
14const Client = mongoose.model('OAuthClient')
15const User = mongoose.model('User')
13 16
14const installer = { 17const 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 @@
1const mongoose = require('mongoose')
2
3const logger = require('../helpers/logger')
4
5const OAuthClient = mongoose.model('OAuthClient')
6const OAuthToken = mongoose.model('OAuthToken')
7const User = mongoose.model('User')
8
9// See https://github.com/oauthjs/node-oauth2-server/wiki/Model-specification for the model specifications
10const OAuthModel = {
11 getAccessToken: getAccessToken,
12 getClient: getClient,
13 getRefreshToken: getRefreshToken,
14 getUser: getUser,
15 saveToken: saveToken
16}
17
18// ---------------------------------------------------------------------------
19
20function getAccessToken (bearerToken) {
21 logger.debug('Getting access token (bearerToken: ' + bearerToken + ').')
22
23 return OAuthToken.loadByTokenAndPopulateUser(bearerToken)
24}
25
26function 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
34function getRefreshToken (refreshToken) {
35 logger.debug('Getting RefreshToken (refreshToken: ' + refreshToken + ').')
36
37 return OAuthToken.loadByRefreshToken(refreshToken)
38}
39
40function getUser (username, password) {
41 logger.debug('Getting User (username: ' + username + ', password: ' + password + ').')
42
43 return User.loadByUsernameAndPassword(username, password)
44}
45
46function 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
70module.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
3const oauth2 = require('./oauth2') 3const oauth = require('./oauth')
4const pagination = require('./pagination') 4const pagination = require('./pagination')
5const reqValidatorsMiddleware = require('./reqValidators') 5const reqValidatorsMiddleware = require('./reqValidators')
6const search = require('./search') 6const search = require('./search')
@@ -8,7 +8,7 @@ const sort = require('./sort')
8const secureMiddleware = require('./secure') 8const secureMiddleware = require('./secure')
9 9
10const middlewares = { 10const 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')
5const logger = require('../helpers/logger') 5const logger = require('../helpers/logger')
6 6
7const oAuthServer = new OAuthServer({ 7const oAuthServer = new OAuthServer({
8 model: require('../models/users') 8 model: require('../lib/oauth-model')
9}) 9})
10 10
11const oAuth2 = { 11const 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
35module.exports = oAuth2 35module.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 @@
1const mongoose = require('mongoose')
2
3// ---------------------------------------------------------------------------
4
5const OAuthClientSchema = mongoose.Schema({
6 clientSecret: String,
7 grants: Array,
8 redirectUris: Array
9})
10
11OAuthClientSchema.path('clientSecret').required(true)
12
13OAuthClientSchema.statics = {
14 list: list,
15 loadByIdAndSecret: loadByIdAndSecret,
16 loadFirstClient: loadFirstClient
17}
18
19mongoose.model('OAuthClient', OAuthClientSchema)
20
21// ---------------------------------------------------------------------------
22
23function list (callback) {
24 return this.find(callback)
25}
26
27function loadFirstClient (callback) {
28 return this.findOne({}, callback)
29}
30
31function 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 @@
1const mongoose = require('mongoose')
2
3// ---------------------------------------------------------------------------
4
5const 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
14OAuthTokenSchema.path('accessToken').required(true)
15OAuthTokenSchema.path('client').required(true)
16OAuthTokenSchema.path('user').required(true)
17
18OAuthTokenSchema.statics = {
19 loadByRefreshToken: loadByRefreshToken,
20 loadByTokenAndPopulateUser: loadByTokenAndPopulateUser
21}
22
23mongoose.model('OAuthToken', OAuthTokenSchema)
24
25// ---------------------------------------------------------------------------
26
27function loadByRefreshToken (refreshToken, callback) {
28 return this.findOne({ refreshToken: refreshToken }, callback)
29}
30
31function 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 @@
1const mongoose = require('mongoose')
2
3// ---------------------------------------------------------------------------
4
5const UserSchema = mongoose.Schema({
6 password: String,
7 username: String
8})
9
10UserSchema.path('password').required(true)
11UserSchema.path('username').required(true)
12
13UserSchema.statics = {
14 list: list,
15 loadByUsernameAndPassword: loadByUsernameAndPassword
16}
17
18mongoose.model('User', UserSchema)
19
20// ---------------------------------------------------------------------------
21
22function list (callback) {
23 return this.find(callback)
24}
25
26function 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 @@
1const mongoose = require('mongoose')
2
3const logger = require('../helpers/logger')
4
5// ---------------------------------------------------------------------------
6
7const 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})
15const OAuthTokensDB = mongoose.model('oAuthTokens', oAuthTokensSchema)
16
17const oAuthClientsSchema = mongoose.Schema({
18 clientSecret: String,
19 grants: Array,
20 redirectUris: Array
21})
22const OAuthClientsDB = mongoose.model('oAuthClients', oAuthClientsSchema)
23
24const usersSchema = mongoose.Schema({
25 password: String,
26 username: String
27})
28const UsersDB = mongoose.model('users', usersSchema)
29
30// ---------------------------------------------------------------------------
31
32const 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
45function 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
56function createUser (username, password, callback) {
57 logger.debug('Creating user.')
58
59 return UsersDB.create({ username: username, password: password }, callback)
60}
61
62function getAccessToken (bearerToken, callback) {
63 logger.debug('Getting access token (bearerToken: ' + bearerToken + ').')
64
65 return OAuthTokensDB.findOne({ accessToken: bearerToken }).populate('user')
66}
67
68function getFirstClient (callback) {
69 return OAuthClientsDB.findOne({}, callback)
70}
71
72function 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
80function getClients (callback) {
81 return OAuthClientsDB.find(callback)
82}
83
84function getRefreshToken (refreshToken) {
85 logger.debug('Getting RefreshToken (refreshToken: ' + refreshToken + ').')
86
87 return OAuthTokensDB.findOne({ refreshToken: refreshToken })
88}
89
90function getUser (username, password) {
91 logger.debug('Getting User (username: ' + username + ', password: ' + password + ').')
92 return UsersDB.findOne({ username: username, password: password })
93}
94
95function getUsers (callback) {
96 return UsersDB.find(callback)
97}
98
99function 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
123module.exports = Users