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