aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/oauth-model.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-07-01 16:03:53 +0200
committerChocobozzz <florian.bigard@gmail.com>2016-07-01 16:03:53 +0200
commit69b0a27cbbd69ca019eb7db5f917b1dd06dc82cd (patch)
tree8659f819818a3da6419959b6b7838a7f0557c948 /server/lib/oauth-model.js
parenta3ee6fa22dee4b68fcde9cd23708b471db446e11 (diff)
downloadPeerTube-69b0a27cbbd69ca019eb7db5f917b1dd06dc82cd.tar.gz
PeerTube-69b0a27cbbd69ca019eb7db5f917b1dd06dc82cd.tar.zst
PeerTube-69b0a27cbbd69ca019eb7db5f917b1dd06dc82cd.zip
OAuth/User models refractoring -> use mongoose api
Diffstat (limited to 'server/lib/oauth-model.js')
-rw-r--r--server/lib/oauth-model.js70
1 files changed, 70 insertions, 0 deletions
diff --git a/server/lib/oauth-model.js b/server/lib/oauth-model.js
new file mode 100644
index 000000000..f4fd9805a
--- /dev/null
+++ b/server/lib/oauth-model.js
@@ -0,0 +1,70 @@
1const mongoose = require('mongoose')
2
3const logger = require('../helpers/logger')
4
5const OAuthClient = mongoose.model('OAuthClient')
6const OAuthToken = mongoose.model('OAuthToken')
7const User = mongoose.model('User')
8
9// See https://github.com/oauthjs/node-oauth2-server/wiki/Model-specification for the model specifications
10const OAuthModel = {
11 getAccessToken: getAccessToken,
12 getClient: getClient,
13 getRefreshToken: getRefreshToken,
14 getUser: getUser,
15 saveToken: saveToken
16}
17
18// ---------------------------------------------------------------------------
19
20function getAccessToken (bearerToken) {
21 logger.debug('Getting access token (bearerToken: ' + bearerToken + ').')
22
23 return OAuthToken.loadByTokenAndPopulateUser(bearerToken)
24}
25
26function getClient (clientId, clientSecret) {
27 logger.debug('Getting Client (clientId: ' + clientId + ', clientSecret: ' + clientSecret + ').')
28
29 // TODO req validator
30 const mongoId = new mongoose.mongo.ObjectID(clientId)
31 return OAuthClient.loadByIdAndSecret(mongoId, clientSecret)
32}
33
34function getRefreshToken (refreshToken) {
35 logger.debug('Getting RefreshToken (refreshToken: ' + refreshToken + ').')
36
37 return OAuthToken.loadByRefreshToken(refreshToken)
38}
39
40function getUser (username, password) {
41 logger.debug('Getting User (username: ' + username + ', password: ' + password + ').')
42
43 return User.loadByUsernameAndPassword(username, password)
44}
45
46function saveToken (token, client, user) {
47 logger.debug('Saving token for client ' + client.id + ' and user ' + user.id + '.')
48
49 const tokenObj = new OAuthToken({
50 accessToken: token.accessToken,
51 accessTokenExpiresOn: token.accessTokenExpiresOn,
52 client: client.id,
53 refreshToken: token.refreshToken,
54 refreshTokenExpiresOn: token.refreshTokenExpiresOn,
55 user: user.id
56 })
57
58 return tokenObj.save(function (err, tokenCreated) {
59 if (err) throw err // node-oauth2-server library uses Promise.try
60
61 tokenCreated.client = client
62 tokenCreated.user = user
63
64 return tokenCreated
65 })
66}
67
68// ---------------------------------------------------------------------------
69
70module.exports = OAuthModel