diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-07-20 16:23:58 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-07-20 16:23:58 +0200 |
commit | 2f372a865487427ff97ad17edd0e6adfbb478c80 (patch) | |
tree | 84e8b2ba2240e83dcdeeb057579327b0a663974b /server/lib/oauth-model.js | |
parent | 66698b833f77806045b36fdeba612acda43dd7f7 (diff) | |
download | PeerTube-2f372a865487427ff97ad17edd0e6adfbb478c80.tar.gz PeerTube-2f372a865487427ff97ad17edd0e6adfbb478c80.tar.zst PeerTube-2f372a865487427ff97ad17edd0e6adfbb478c80.zip |
Server: implement refresh token
Diffstat (limited to 'server/lib/oauth-model.js')
-rw-r--r-- | server/lib/oauth-model.js | 32 |
1 files changed, 25 insertions, 7 deletions
diff --git a/server/lib/oauth-model.js b/server/lib/oauth-model.js index f4fd9805a..555a54e90 100644 --- a/server/lib/oauth-model.js +++ b/server/lib/oauth-model.js | |||
@@ -12,6 +12,7 @@ const OAuthModel = { | |||
12 | getClient: getClient, | 12 | getClient: getClient, |
13 | getRefreshToken: getRefreshToken, | 13 | getRefreshToken: getRefreshToken, |
14 | getUser: getUser, | 14 | getUser: getUser, |
15 | revokeToken: revokeToken, | ||
15 | saveToken: saveToken | 16 | saveToken: saveToken |
16 | } | 17 | } |
17 | 18 | ||
@@ -20,7 +21,7 @@ const OAuthModel = { | |||
20 | function getAccessToken (bearerToken) { | 21 | function getAccessToken (bearerToken) { |
21 | logger.debug('Getting access token (bearerToken: ' + bearerToken + ').') | 22 | logger.debug('Getting access token (bearerToken: ' + bearerToken + ').') |
22 | 23 | ||
23 | return OAuthToken.loadByTokenAndPopulateUser(bearerToken) | 24 | return OAuthToken.getByTokenAndPopulateUser(bearerToken) |
24 | } | 25 | } |
25 | 26 | ||
26 | function getClient (clientId, clientSecret) { | 27 | function getClient (clientId, clientSecret) { |
@@ -28,19 +29,36 @@ function getClient (clientId, clientSecret) { | |||
28 | 29 | ||
29 | // TODO req validator | 30 | // TODO req validator |
30 | const mongoId = new mongoose.mongo.ObjectID(clientId) | 31 | const mongoId = new mongoose.mongo.ObjectID(clientId) |
31 | return OAuthClient.loadByIdAndSecret(mongoId, clientSecret) | 32 | return OAuthClient.getByIdAndSecret(mongoId, clientSecret) |
32 | } | 33 | } |
33 | 34 | ||
34 | function getRefreshToken (refreshToken) { | 35 | function getRefreshToken (refreshToken, callback) { |
35 | logger.debug('Getting RefreshToken (refreshToken: ' + refreshToken + ').') | 36 | logger.debug('Getting RefreshToken (refreshToken: ' + refreshToken + ').') |
36 | 37 | ||
37 | return OAuthToken.loadByRefreshToken(refreshToken) | 38 | return OAuthToken.getByRefreshTokenAndPopulateClient(refreshToken) |
38 | } | 39 | } |
39 | 40 | ||
40 | function getUser (username, password) { | 41 | function getUser (username, password) { |
41 | logger.debug('Getting User (username: ' + username + ', password: ' + password + ').') | 42 | logger.debug('Getting User (username: ' + username + ', password: ' + password + ').') |
42 | 43 | ||
43 | return User.loadByUsernameAndPassword(username, password) | 44 | return User.getByUsernameAndPassword(username, password) |
45 | } | ||
46 | |||
47 | function revokeToken (token) { | ||
48 | return OAuthToken.getByRefreshToken(token.refreshToken).then(function (tokenDB) { | ||
49 | if (tokenDB) tokenDB.remove() | ||
50 | |||
51 | /* | ||
52 | * Thanks to https://github.com/manjeshpv/node-oauth2-server-implementation/blob/master/components/oauth/mongo-models.js | ||
53 | * "As per the discussion we need set older date | ||
54 | * revokeToken will expected return a boolean in future version | ||
55 | * https://github.com/oauthjs/node-oauth2-server/pull/274 | ||
56 | * https://github.com/oauthjs/node-oauth2-server/issues/290" | ||
57 | */ | ||
58 | const expiredToken = tokenDB | ||
59 | expiredToken.refreshTokenExpiresAt = new Date('2015-05-28T06:59:53.000Z') | ||
60 | return expiredToken | ||
61 | }) | ||
44 | } | 62 | } |
45 | 63 | ||
46 | function saveToken (token, client, user) { | 64 | function saveToken (token, client, user) { |
@@ -48,10 +66,10 @@ function saveToken (token, client, user) { | |||
48 | 66 | ||
49 | const tokenObj = new OAuthToken({ | 67 | const tokenObj = new OAuthToken({ |
50 | accessToken: token.accessToken, | 68 | accessToken: token.accessToken, |
51 | accessTokenExpiresOn: token.accessTokenExpiresOn, | 69 | accessTokenExpiresAt: token.accessTokenExpiresAt, |
52 | client: client.id, | 70 | client: client.id, |
53 | refreshToken: token.refreshToken, | 71 | refreshToken: token.refreshToken, |
54 | refreshTokenExpiresOn: token.refreshTokenExpiresOn, | 72 | refreshTokenExpiresAt: token.refreshTokenExpiresAt, |
55 | user: user.id | 73 | user: user.id |
56 | }) | 74 | }) |
57 | 75 | ||