diff options
author | Chocobozzz <me@florianbigard.com> | 2017-12-12 17:53:50 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2017-12-13 16:50:33 +0100 |
commit | 3fd3ab2d34d512b160a5e6084d7609be7b4f4452 (patch) | |
tree | e5ca358287fca6ecacce83defcf23af1e8e9f419 /server/lib/oauth-model.ts | |
parent | c893d4514e6ecbf282c7985fe5f82b8acd8a1137 (diff) | |
download | PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.tar.gz PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.tar.zst PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.zip |
Move models to typescript-sequelize
Diffstat (limited to 'server/lib/oauth-model.ts')
-rw-r--r-- | server/lib/oauth-model.ts | 19 |
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 @@ | |||
1 | import { OAuthClientInstance, UserInstance } from '../models' | ||
2 | import { database as db } from '../initializers/database' | ||
3 | import { logger } from '../helpers' | 1 | import { logger } from '../helpers' |
2 | import { UserModel } from '../models/account/user' | ||
3 | import { OAuthClientModel } from '../models/oauth/oauth-client' | ||
4 | import { OAuthTokenModel } from '../models/oauth/oauth-token' | ||
4 | 5 | ||
5 | type TokenInfo = { accessToken: string, refreshToken: string, accessTokenExpiresAt: Date, refreshTokenExpiresAt: Date } | 6 | type TokenInfo = { accessToken: string, refreshToken: string, accessTokenExpiresAt: Date, refreshTokenExpiresAt: Date } |
6 | 7 | ||
@@ -9,25 +10,25 @@ type TokenInfo = { accessToken: string, refreshToken: string, accessTokenExpires | |||
9 | function getAccessToken (bearerToken: string) { | 10 | function 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 | ||
15 | function getClient (clientId: string, clientSecret: string) { | 16 | function 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 | ||
21 | function getRefreshToken (refreshToken: string) { | 22 | function 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 | ||
27 | async function getUser (username: string, password: string) { | 28 | async 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 | ||
39 | async function revokeToken (tokenInfo: TokenInfo) { | 40 | async 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 | ||
56 | async function saveToken (token: TokenInfo, client: OAuthClientInstance, user: UserInstance) { | 57 | async 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 |