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/users.js | |
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/users.js')
-rw-r--r-- | server/models/users.js | 123 |
1 files changed, 0 insertions, 123 deletions
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 | ||