aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/oauth-model.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/oauth-model.ts')
-rw-r--r--server/lib/oauth-model.ts19
1 files changed, 10 insertions, 9 deletions
diff --git a/server/lib/oauth-model.ts b/server/lib/oauth-model.ts
index d91b00c55..dce71e83b 100644
--- a/server/lib/oauth-model.ts
+++ b/server/lib/oauth-model.ts
@@ -1,6 +1,7 @@
1import { OAuthClientInstance, UserInstance } from '../models'
2import { database as db } from '../initializers/database'
3import { logger } from '../helpers' 1import { logger } from '../helpers'
2import { UserModel } from '../models/account/user'
3import { OAuthClientModel } from '../models/oauth/oauth-client'
4import { OAuthTokenModel } from '../models/oauth/oauth-token'
4 5
5type TokenInfo = { accessToken: string, refreshToken: string, accessTokenExpiresAt: Date, refreshTokenExpiresAt: Date } 6type TokenInfo = { accessToken: string, refreshToken: string, accessTokenExpiresAt: Date, refreshTokenExpiresAt: Date }
6 7
@@ -9,25 +10,25 @@ type TokenInfo = { accessToken: string, refreshToken: string, accessTokenExpires
9function getAccessToken (bearerToken: string) { 10function getAccessToken (bearerToken: string) {
10 logger.debug('Getting access token (bearerToken: ' + bearerToken + ').') 11 logger.debug('Getting access token (bearerToken: ' + bearerToken + ').')
11 12
12 return db.OAuthToken.getByTokenAndPopulateUser(bearerToken) 13 return OAuthTokenModel.getByTokenAndPopulateUser(bearerToken)
13} 14}
14 15
15function getClient (clientId: string, clientSecret: string) { 16function getClient (clientId: string, clientSecret: string) {
16 logger.debug('Getting Client (clientId: ' + clientId + ', clientSecret: ' + clientSecret + ').') 17 logger.debug('Getting Client (clientId: ' + clientId + ', clientSecret: ' + clientSecret + ').')
17 18
18 return db.OAuthClient.getByIdAndSecret(clientId, clientSecret) 19 return OAuthClientModel.getByIdAndSecret(clientId, clientSecret)
19} 20}
20 21
21function getRefreshToken (refreshToken: string) { 22function getRefreshToken (refreshToken: string) {
22 logger.debug('Getting RefreshToken (refreshToken: ' + refreshToken + ').') 23 logger.debug('Getting RefreshToken (refreshToken: ' + refreshToken + ').')
23 24
24 return db.OAuthToken.getByRefreshTokenAndPopulateClient(refreshToken) 25 return OAuthTokenModel.getByRefreshTokenAndPopulateClient(refreshToken)
25} 26}
26 27
27async function getUser (username: string, password: string) { 28async function getUser (username: string, password: string) {
28 logger.debug('Getting User (username: ' + username + ', password: ******).') 29 logger.debug('Getting User (username: ' + username + ', password: ******).')
29 30
30 const user = await db.User.getByUsername(username) 31 const user = await UserModel.getByUsername(username)
31 if (!user) return null 32 if (!user) return null
32 33
33 const passwordMatch = await user.isPasswordMatch(password) 34 const passwordMatch = await user.isPasswordMatch(password)
@@ -37,7 +38,7 @@ async function getUser (username: string, password: string) {
37} 38}
38 39
39async function revokeToken (tokenInfo: TokenInfo) { 40async function revokeToken (tokenInfo: TokenInfo) {
40 const token = await db.OAuthToken.getByRefreshTokenAndPopulateUser(tokenInfo.refreshToken) 41 const token = await OAuthTokenModel.getByRefreshTokenAndPopulateUser(tokenInfo.refreshToken)
41 if (token) token.destroy() 42 if (token) token.destroy()
42 43
43 /* 44 /*
@@ -53,7 +54,7 @@ async function revokeToken (tokenInfo: TokenInfo) {
53 return expiredToken 54 return expiredToken
54} 55}
55 56
56async function saveToken (token: TokenInfo, client: OAuthClientInstance, user: UserInstance) { 57async function saveToken (token: TokenInfo, client: OAuthClientModel, user: UserModel) {
57 logger.debug('Saving token ' + token.accessToken + ' for client ' + client.id + ' and user ' + user.id + '.') 58 logger.debug('Saving token ' + token.accessToken + ' for client ' + client.id + ' and user ' + user.id + '.')
58 59
59 const tokenToCreate = { 60 const tokenToCreate = {
@@ -65,7 +66,7 @@ async function saveToken (token: TokenInfo, client: OAuthClientInstance, user: U
65 userId: user.id 66 userId: user.id
66 } 67 }
67 68
68 const tokenCreated = await db.OAuthToken.create(tokenToCreate) 69 const tokenCreated = await OAuthTokenModel.create(tokenToCreate)
69 const tokenToReturn = Object.assign(tokenCreated, { client, user }) 70 const tokenToReturn = Object.assign(tokenCreated, { client, user })
70 71
71 return tokenToReturn 72 return tokenToReturn