diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-07-01 16:03:53 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-07-01 16:03:53 +0200 |
commit | 69b0a27cbbd69ca019eb7db5f917b1dd06dc82cd (patch) | |
tree | 8659f819818a3da6419959b6b7838a7f0557c948 /server/models | |
parent | a3ee6fa22dee4b68fcde9cd23708b471db446e11 (diff) | |
download | PeerTube-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.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 |
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 @@ | |||
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 | ||