aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/users.js
blob: a1bdece23ad8601d2b8d16ca6e734fb94ca24b93 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const mongoose = require('mongoose')

const logger = require('../helpers/logger')

// ---------------------------------------------------------------------------

const oAuthTokensSchema = mongoose.Schema({
  accessToken: String,
  accessTokenExpiresOn: Date,
  client: { type: mongoose.Schema.Types.ObjectId, ref: 'oAuthClients' },
  refreshToken: String,
  refreshTokenExpiresOn: Date,
  user: { type: mongoose.Schema.Types.ObjectId, ref: 'users' }
})
const OAuthTokensDB = mongoose.model('oAuthTokens', oAuthTokensSchema)

const oAuthClientsSchema = mongoose.Schema({
  clientSecret: String,
  grants: Array,
  redirectUris: Array
})
const OAuthClientsDB = mongoose.model('oAuthClients', oAuthClientsSchema)

const usersSchema = mongoose.Schema({
  password: String,
  username: String
})
const UsersDB = mongoose.model('users', usersSchema)

// ---------------------------------------------------------------------------

const Users = {
  createClient: createClient,
  createUser: createUser,
  getAccessToken: getAccessToken,
  getClient: getClient,
  getClients: getClients,
  getFirstClient: getFirstClient,
  getRefreshToken: getRefreshToken,
  getUser: getUser,
  getUsers: getUsers,
  saveToken: saveToken
}

function createClient (secret, grants, callback) {
  logger.debug('Creating client.')

  const mongoId = new mongoose.mongo.ObjectID()
  return OAuthClientsDB.create({ _id: mongoId, clientSecret: secret, grants: grants }, function (err) {
    if (err) return callback(err)

    return callback(null, mongoId)
  })
}

function createUser (username, password, callback) {
  logger.debug('Creating user.')

  return UsersDB.create({ username: username, password: password }, callback)
}

function getAccessToken (bearerToken, callback) {
  logger.debug('Getting access token (bearerToken: ' + bearerToken + ').')

  return OAuthTokensDB.findOne({ accessToken: bearerToken }).populate('user')
}

function getFirstClient (callback) {
  return OAuthClientsDB.findOne({}, callback)
}

function getClient (clientId, clientSecret) {
  logger.debug('Getting Client (clientId: ' + clientId + ', clientSecret: ' + clientSecret + ').')

  // TODO req validator
  const mongoId = new mongoose.mongo.ObjectID(clientId)
  return OAuthClientsDB.findOne({ _id: mongoId, clientSecret: clientSecret })
}

function getClients (callback) {
  return OAuthClientsDB.find(callback)
}

function getRefreshToken (refreshToken) {
  logger.debug('Getting RefreshToken (refreshToken: ' + refreshToken + ').')

  return OAuthTokensDB.findOne({ refreshToken: refreshToken })
}

function getUser (username, password) {
  logger.debug('Getting User (username: ' + username + ', password: ' + password + ').')
  return UsersDB.findOne({ username: username, password: password })
}

function getUsers (callback) {
  return UsersDB.find(callback)
}

function saveToken (token, client, user) {
  logger.debug('Saving token for client ' + client.id + ' and user ' + user.id + '.')

  const tokenToCreate = {
    accessToken: token.accessToken,
    accessTokenExpiresOn: token.accessTokenExpiresOn,
    client: client.id,
    refreshToken: token.refreshToken,
    refreshTokenExpiresOn: token.refreshTokenExpiresOn,
    user: user.id
  }

  return OAuthTokensDB.create(tokenToCreate, function (err, tokenCreated) {
    if (err) throw err // node-oauth2-server library uses Promise.try

    tokenCreated.client = client
    tokenCreated.user = user

    return tokenCreated
  })
}

// ---------------------------------------------------------------------------

module.exports = Users