]>
Commit | Line | Data |
---|---|---|
cef534ed | 1 | import * as Bluebird from 'bluebird' |
141b177d | 2 | import { AccessDeniedError } from 'oauth2-server' |
da854ddd | 3 | import { logger } from '../helpers/logger' |
3fd3ab2d C |
4 | import { UserModel } from '../models/account/user' |
5 | import { OAuthClientModel } from '../models/oauth/oauth-client' | |
6 | import { OAuthTokenModel } from '../models/oauth/oauth-token' | |
d74d29ad | 7 | import { CONFIG, CACHE } from '../initializers/constants' |
f201a749 | 8 | import { Transaction } from 'sequelize' |
69b0a27c | 9 | |
69818c93 | 10 | type TokenInfo = { accessToken: string, refreshToken: string, accessTokenExpiresAt: Date, refreshTokenExpiresAt: Date } |
d74d29ad C |
11 | let accessTokenCache: { [ accessToken: string ]: OAuthTokenModel } = {} |
12 | let userHavingToken: { [ userId: number ]: string } = {} | |
69818c93 | 13 | |
69b0a27c C |
14 | // --------------------------------------------------------------------------- |
15 | ||
f201a749 C |
16 | function deleteUserToken (userId: number, t?: Transaction) { |
17 | clearCacheByUserId(userId) | |
18 | ||
19 | return OAuthTokenModel.deleteUserToken(userId, t) | |
20 | } | |
21 | ||
22 | function clearCacheByUserId (userId: number) { | |
23 | const token = userHavingToken[userId] | |
24 | if (token !== undefined) { | |
25 | accessTokenCache[ token ] = undefined | |
26 | userHavingToken[ userId ] = undefined | |
27 | } | |
28 | } | |
29 | ||
30 | function clearCacheByToken (token: string) { | |
31 | const tokenModel = accessTokenCache[ token ] | |
32 | if (tokenModel !== undefined) { | |
33 | userHavingToken[tokenModel.userId] = undefined | |
34 | accessTokenCache[ token ] = undefined | |
35 | } | |
36 | } | |
37 | ||
69818c93 | 38 | function getAccessToken (bearerToken: string) { |
69b0a27c C |
39 | logger.debug('Getting access token (bearerToken: ' + bearerToken + ').') |
40 | ||
cef534ed | 41 | if (accessTokenCache[bearerToken] !== undefined) return Bluebird.resolve(accessTokenCache[bearerToken]) |
f201a749 | 42 | |
3fd3ab2d | 43 | return OAuthTokenModel.getByTokenAndPopulateUser(bearerToken) |
f201a749 C |
44 | .then(tokenModel => { |
45 | if (tokenModel) { | |
d74d29ad C |
46 | // Reinit our cache |
47 | if (Object.keys(accessTokenCache).length > CACHE.USER_TOKENS.MAX_SIZE) { | |
48 | accessTokenCache = {} | |
49 | userHavingToken = {} | |
50 | } | |
51 | ||
f201a749 C |
52 | accessTokenCache[ bearerToken ] = tokenModel |
53 | userHavingToken[ tokenModel.userId ] = tokenModel.accessToken | |
54 | } | |
55 | ||
56 | return tokenModel | |
57 | }) | |
69b0a27c C |
58 | } |
59 | ||
69818c93 | 60 | function getClient (clientId: string, clientSecret: string) { |
69b0a27c C |
61 | logger.debug('Getting Client (clientId: ' + clientId + ', clientSecret: ' + clientSecret + ').') |
62 | ||
3fd3ab2d | 63 | return OAuthClientModel.getByIdAndSecret(clientId, clientSecret) |
69b0a27c C |
64 | } |
65 | ||
69818c93 | 66 | function getRefreshToken (refreshToken: string) { |
69b0a27c C |
67 | logger.debug('Getting RefreshToken (refreshToken: ' + refreshToken + ').') |
68 | ||
3fd3ab2d | 69 | return OAuthTokenModel.getByRefreshTokenAndPopulateClient(refreshToken) |
69b0a27c C |
70 | } |
71 | ||
ba12e8b3 C |
72 | async function getUser (usernameOrEmail: string, password: string) { |
73 | logger.debug('Getting User (username/email: ' + usernameOrEmail + ', password: ******).') | |
69b0a27c | 74 | |
ba12e8b3 | 75 | const user = await UserModel.loadByUsernameOrEmail(usernameOrEmail) |
f5028693 | 76 | if (!user) return null |
26d7d31b | 77 | |
f5028693 C |
78 | const passwordMatch = await user.isPasswordMatch(password) |
79 | if (passwordMatch === false) return null | |
26d7d31b | 80 | |
e6921918 C |
81 | if (user.blocked) throw new AccessDeniedError('User is blocked.') |
82 | ||
d9eaee39 JM |
83 | if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION && user.emailVerified === false) { |
84 | throw new AccessDeniedError('User email is not verified.') | |
85 | } | |
86 | ||
f5028693 | 87 | return user |
2f372a86 C |
88 | } |
89 | ||
f5028693 | 90 | async function revokeToken (tokenInfo: TokenInfo) { |
3fd3ab2d | 91 | const token = await OAuthTokenModel.getByRefreshTokenAndPopulateUser(tokenInfo.refreshToken) |
bfcef50d | 92 | if (token) { |
f201a749 C |
93 | clearCacheByToken(token.accessToken) |
94 | ||
bfcef50d C |
95 | token.destroy() |
96 | .catch(err => logger.error('Cannot destroy token when revoking token.', { err })) | |
97 | } | |
f5028693 C |
98 | |
99 | /* | |
100 | * Thanks to https://github.com/manjeshpv/node-oauth2-server-implementation/blob/master/components/oauth/mongo-models.js | |
101 | * "As per the discussion we need set older date | |
102 | * revokeToken will expected return a boolean in future version | |
103 | * https://github.com/oauthjs/node-oauth2-server/pull/274 | |
104 | * https://github.com/oauthjs/node-oauth2-server/issues/290" | |
105 | */ | |
106 | const expiredToken = token | |
107 | expiredToken.refreshTokenExpiresAt = new Date('2015-05-28T06:59:53.000Z') | |
108 | ||
109 | return expiredToken | |
69b0a27c C |
110 | } |
111 | ||
3fd3ab2d | 112 | async function saveToken (token: TokenInfo, client: OAuthClientModel, user: UserModel) { |
32bb4156 | 113 | logger.debug('Saving token ' + token.accessToken + ' for client ' + client.id + ' and user ' + user.id + '.') |
69b0a27c | 114 | |
feb4bdfd | 115 | const tokenToCreate = { |
69b0a27c | 116 | accessToken: token.accessToken, |
2f372a86 | 117 | accessTokenExpiresAt: token.accessTokenExpiresAt, |
69b0a27c | 118 | refreshToken: token.refreshToken, |
2f372a86 | 119 | refreshTokenExpiresAt: token.refreshTokenExpiresAt, |
feb4bdfd C |
120 | oAuthClientId: client.id, |
121 | userId: user.id | |
122 | } | |
69b0a27c | 123 | |
3fd3ab2d | 124 | const tokenCreated = await OAuthTokenModel.create(tokenToCreate) |
e6921918 | 125 | return Object.assign(tokenCreated, { client, user }) |
69b0a27c C |
126 | } |
127 | ||
128 | // --------------------------------------------------------------------------- | |
129 | ||
65fcc311 C |
130 | // See https://github.com/oauthjs/node-oauth2-server/wiki/Model-specification for the model specifications |
131 | export { | |
f201a749 C |
132 | deleteUserToken, |
133 | clearCacheByUserId, | |
134 | clearCacheByToken, | |
65fcc311 C |
135 | getAccessToken, |
136 | getClient, | |
137 | getRefreshToken, | |
138 | getUser, | |
139 | revokeToken, | |
140 | saveToken | |
141 | } |