aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-07-01 16:03:53 +0200
committerChocobozzz <florian.bigard@gmail.com>2016-07-01 16:03:53 +0200
commit69b0a27cbbd69ca019eb7db5f917b1dd06dc82cd (patch)
tree8659f819818a3da6419959b6b7838a7f0557c948 /server/models
parenta3ee6fa22dee4b68fcde9cd23708b471db446e11 (diff)
downloadPeerTube-69b0a27cbbd69ca019eb7db5f917b1dd06dc82cd.tar.gz
PeerTube-69b0a27cbbd69ca019eb7db5f917b1dd06dc82cd.tar.zst
PeerTube-69b0a27cbbd69ca019eb7db5f917b1dd06dc82cd.zip
OAuth/User models refractoring -> use mongoose api
Diffstat (limited to 'server/models')
-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
4 files changed, 95 insertions, 123 deletions
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