aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/users.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/models/users.js')
-rw-r--r--server/models/users.js123
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 @@
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