diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-05-15 22:22:03 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-05-20 09:57:40 +0200 |
commit | 65fcc3119c334b75dd13bcfdebf186afdc580a8f (patch) | |
tree | 4f2158c61a9b7c3f47cfa233d01413b946ee53c0 /server/lib/oauth-model.js | |
parent | d5f345ed4cfac4e1fa84dcb4fce1cda4d32f9c73 (diff) | |
download | PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.gz PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.zst PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.zip |
First typescript iteration
Diffstat (limited to 'server/lib/oauth-model.js')
-rw-r--r-- | server/lib/oauth-model.js | 97 |
1 files changed, 0 insertions, 97 deletions
diff --git a/server/lib/oauth-model.js b/server/lib/oauth-model.js deleted file mode 100644 index 1c12f1b14..000000000 --- a/server/lib/oauth-model.js +++ /dev/null | |||
@@ -1,97 +0,0 @@ | |||
1 | const db = require('../initializers/database') | ||
2 | const logger = require('../helpers/logger') | ||
3 | |||
4 | // See https://github.com/oauthjs/node-oauth2-server/wiki/Model-specification for the model specifications | ||
5 | const OAuthModel = { | ||
6 | getAccessToken, | ||
7 | getClient, | ||
8 | getRefreshToken, | ||
9 | getUser, | ||
10 | revokeToken, | ||
11 | saveToken | ||
12 | } | ||
13 | |||
14 | // --------------------------------------------------------------------------- | ||
15 | |||
16 | function getAccessToken (bearerToken) { | ||
17 | logger.debug('Getting access token (bearerToken: ' + bearerToken + ').') | ||
18 | |||
19 | return db.OAuthToken.getByTokenAndPopulateUser(bearerToken) | ||
20 | } | ||
21 | |||
22 | function getClient (clientId, clientSecret) { | ||
23 | logger.debug('Getting Client (clientId: ' + clientId + ', clientSecret: ' + clientSecret + ').') | ||
24 | |||
25 | return db.OAuthClient.getByIdAndSecret(clientId, clientSecret) | ||
26 | } | ||
27 | |||
28 | function getRefreshToken (refreshToken) { | ||
29 | logger.debug('Getting RefreshToken (refreshToken: ' + refreshToken + ').') | ||
30 | |||
31 | return db.OAuthToken.getByRefreshTokenAndPopulateClient(refreshToken) | ||
32 | } | ||
33 | |||
34 | function getUser (username, password) { | ||
35 | logger.debug('Getting User (username: ' + username + ', password: ' + password + ').') | ||
36 | |||
37 | return db.User.getByUsername(username).then(function (user) { | ||
38 | if (!user) return null | ||
39 | |||
40 | // We need to return a promise | ||
41 | return new Promise(function (resolve, reject) { | ||
42 | return user.isPasswordMatch(password, function (err, isPasswordMatch) { | ||
43 | if (err) return reject(err) | ||
44 | |||
45 | if (isPasswordMatch === true) { | ||
46 | return resolve(user) | ||
47 | } | ||
48 | |||
49 | return resolve(null) | ||
50 | }) | ||
51 | }) | ||
52 | }) | ||
53 | } | ||
54 | |||
55 | function revokeToken (token) { | ||
56 | return db.OAuthToken.getByRefreshTokenAndPopulateUser(token.refreshToken).then(function (tokenDB) { | ||
57 | if (tokenDB) tokenDB.destroy() | ||
58 | |||
59 | /* | ||
60 | * Thanks to https://github.com/manjeshpv/node-oauth2-server-implementation/blob/master/components/oauth/mongo-models.js | ||
61 | * "As per the discussion we need set older date | ||
62 | * revokeToken will expected return a boolean in future version | ||
63 | * https://github.com/oauthjs/node-oauth2-server/pull/274 | ||
64 | * https://github.com/oauthjs/node-oauth2-server/issues/290" | ||
65 | */ | ||
66 | const expiredToken = tokenDB | ||
67 | expiredToken.refreshTokenExpiresAt = new Date('2015-05-28T06:59:53.000Z') | ||
68 | |||
69 | return expiredToken | ||
70 | }) | ||
71 | } | ||
72 | |||
73 | function saveToken (token, client, user) { | ||
74 | logger.debug('Saving token ' + token.accessToken + ' for client ' + client.id + ' and user ' + user.id + '.') | ||
75 | |||
76 | const tokenToCreate = { | ||
77 | accessToken: token.accessToken, | ||
78 | accessTokenExpiresAt: token.accessTokenExpiresAt, | ||
79 | refreshToken: token.refreshToken, | ||
80 | refreshTokenExpiresAt: token.refreshTokenExpiresAt, | ||
81 | oAuthClientId: client.id, | ||
82 | userId: user.id | ||
83 | } | ||
84 | |||
85 | return db.OAuthToken.create(tokenToCreate).then(function (tokenCreated) { | ||
86 | tokenCreated.client = client | ||
87 | tokenCreated.user = user | ||
88 | |||
89 | return tokenCreated | ||
90 | }).catch(function (err) { | ||
91 | throw err | ||
92 | }) | ||
93 | } | ||
94 | |||
95 | // --------------------------------------------------------------------------- | ||
96 | |||
97 | module.exports = OAuthModel | ||