aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models
diff options
context:
space:
mode:
Diffstat (limited to 'server/models')
-rw-r--r--server/models/users.js108
1 files changed, 108 insertions, 0 deletions
diff --git a/server/models/users.js b/server/models/users.js
new file mode 100644
index 000000000..355d991bd
--- /dev/null
+++ b/server/models/users.js
@@ -0,0 +1,108 @@
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 getRefreshToken: getRefreshToken,
38 getUser: getUser,
39 saveToken: saveToken
40}
41
42function createClient (secret, grants, callback) {
43 logger.debug('Creating client.')
44
45 const mongo_id = new mongoose.mongo.ObjectID()
46 return OAuthClientsDB.create({ _id: mongo_id, clientSecret: secret, grants: grants }, function (err) {
47 if (err) return callback(err)
48
49 return callback(null, mongo_id)
50 })
51}
52
53function createUser (username, password, callback) {
54 logger.debug('Creating user.')
55
56 return UsersDB.create({ username: username, password: password }, callback)
57}
58
59function getAccessToken (bearerToken, callback) {
60 logger.debug('Getting access token (bearerToken: ' + bearerToken + ').')
61
62 return OAuthTokensDB.findOne({ accessToken: bearerToken }).populate('user')
63}
64
65function getClient (clientId, clientSecret) {
66 logger.debug('Getting Client (clientId: ' + clientId + ', clientSecret: ' + clientSecret + ').')
67
68 // TODO req validator
69 const mongo_id = new mongoose.mongo.ObjectID(clientId)
70 return OAuthClientsDB.findOne({ _id: mongo_id, clientSecret: clientSecret })
71}
72
73function getRefreshToken (refreshToken) {
74 logger.debug('Getting RefreshToken (refreshToken: ' + refreshToken + ').')
75
76 return OAuthTokensDB.findOne({ refreshToken: refreshToken })
77}
78
79function getUser (username, password) {
80 logger.debug('Getting User (username: ' + username + ', password: ' + password + ').')
81 return UsersDB.findOne({ username: username, password: password })
82}
83
84function saveToken (token, client, user) {
85 logger.debug('Saving token for client ' + client.id + ' and user ' + user.id + '.')
86
87 const token_to_create = {
88 accessToken: token.accessToken,
89 accessTokenExpiresOn: token.accessTokenExpiresOn,
90 client: client.id,
91 refreshToken: token.refreshToken,
92 refreshTokenExpiresOn: token.refreshTokenExpiresOn,
93 user: user.id
94 }
95
96 return OAuthTokensDB.create(token_to_create, function (err, token_created) {
97 if (err) throw err // node-oauth2-server library use Promise.try
98
99 token_created.client = client
100 token_created.user = user
101
102 return token_created
103 })
104}
105
106// ---------------------------------------------------------------------------
107
108module.exports = Users