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/models/oauth/oauth-token.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/models/oauth/oauth-token.ts')
-rw-r--r-- | server/models/oauth/oauth-token.ts | 259 |
1 files changed, 129 insertions, 130 deletions
diff --git a/server/models/oauth/oauth-token.ts b/server/models/oauth/oauth-token.ts index a82bff130..0d21c42fd 100644 --- a/server/models/oauth/oauth-token.ts +++ b/server/models/oauth/oauth-token.ts | |||
@@ -1,164 +1,163 @@ | |||
1 | import * as Sequelize from 'sequelize' | 1 | import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript' |
2 | |||
3 | import { logger } from '../../helpers' | 2 | import { logger } from '../../helpers' |
3 | import { AccountModel } from '../account/account' | ||
4 | import { UserModel } from '../account/user' | ||
5 | import { OAuthClientModel } from './oauth-client' | ||
6 | |||
7 | export type OAuthTokenInfo = { | ||
8 | refreshToken: string | ||
9 | refreshTokenExpiresAt: Date, | ||
10 | client: { | ||
11 | id: number | ||
12 | }, | ||
13 | user: { | ||
14 | id: number | ||
15 | } | ||
16 | } | ||
4 | 17 | ||
5 | import { addMethodsToModel } from '../utils' | 18 | @Table({ |
6 | import { OAuthTokenAttributes, OAuthTokenInfo, OAuthTokenInstance, OAuthTokenMethods } from './oauth-token-interface' | 19 | tableName: 'oAuthToken', |
7 | 20 | indexes: [ | |
8 | let OAuthToken: Sequelize.Model<OAuthTokenInstance, OAuthTokenAttributes> | ||
9 | let getByRefreshTokenAndPopulateClient: OAuthTokenMethods.GetByRefreshTokenAndPopulateClient | ||
10 | let getByTokenAndPopulateUser: OAuthTokenMethods.GetByTokenAndPopulateUser | ||
11 | let getByRefreshTokenAndPopulateUser: OAuthTokenMethods.GetByRefreshTokenAndPopulateUser | ||
12 | |||
13 | export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { | ||
14 | OAuthToken = sequelize.define<OAuthTokenInstance, OAuthTokenAttributes>('OAuthToken', | ||
15 | { | 21 | { |
16 | accessToken: { | 22 | fields: [ 'refreshToken' ], |
17 | type: DataTypes.STRING, | 23 | unique: true |
18 | allowNull: false | ||
19 | }, | ||
20 | accessTokenExpiresAt: { | ||
21 | type: DataTypes.DATE, | ||
22 | allowNull: false | ||
23 | }, | ||
24 | refreshToken: { | ||
25 | type: DataTypes.STRING, | ||
26 | allowNull: false | ||
27 | }, | ||
28 | refreshTokenExpiresAt: { | ||
29 | type: DataTypes.DATE, | ||
30 | allowNull: false | ||
31 | } | ||
32 | }, | 24 | }, |
33 | { | 25 | { |
34 | indexes: [ | 26 | fields: [ 'accessToken' ], |
35 | { | 27 | unique: true |
36 | fields: [ 'refreshToken' ], | 28 | }, |
37 | unique: true | 29 | { |
38 | }, | 30 | fields: [ 'userId' ] |
39 | { | 31 | }, |
40 | fields: [ 'accessToken' ], | 32 | { |
41 | unique: true | 33 | fields: [ 'oAuthClientId' ] |
42 | }, | ||
43 | { | ||
44 | fields: [ 'userId' ] | ||
45 | }, | ||
46 | { | ||
47 | fields: [ 'oAuthClientId' ] | ||
48 | } | ||
49 | ] | ||
50 | } | 34 | } |
51 | ) | 35 | ] |
36 | }) | ||
37 | export class OAuthTokenModel extends Model<OAuthTokenModel> { | ||
52 | 38 | ||
53 | const classMethods = [ | 39 | @AllowNull(false) |
54 | associate, | 40 | @Column |
41 | accessToken: string | ||
55 | 42 | ||
56 | getByRefreshTokenAndPopulateClient, | 43 | @AllowNull(false) |
57 | getByTokenAndPopulateUser, | 44 | @Column |
58 | getByRefreshTokenAndPopulateUser | 45 | accessTokenExpiresAt: Date |
59 | ] | ||
60 | addMethodsToModel(OAuthToken, classMethods) | ||
61 | 46 | ||
62 | return OAuthToken | 47 | @AllowNull(false) |
63 | } | 48 | @Column |
49 | refreshToken: string | ||
64 | 50 | ||
65 | // --------------------------------------------------------------------------- | 51 | @AllowNull(false) |
52 | @Column | ||
53 | refreshTokenExpiresAt: Date | ||
66 | 54 | ||
67 | function associate (models) { | 55 | @CreatedAt |
68 | OAuthToken.belongsTo(models.User, { | 56 | createdAt: Date |
57 | |||
58 | @UpdatedAt | ||
59 | updatedAt: Date | ||
60 | |||
61 | @ForeignKey(() => UserModel) | ||
62 | @Column | ||
63 | userId: number | ||
64 | |||
65 | @BelongsTo(() => UserModel, { | ||
69 | foreignKey: { | 66 | foreignKey: { |
70 | name: 'userId', | ||
71 | allowNull: false | 67 | allowNull: false |
72 | }, | 68 | }, |
73 | onDelete: 'cascade' | 69 | onDelete: 'cascade' |
74 | }) | 70 | }) |
71 | User: UserModel | ||
75 | 72 | ||
76 | OAuthToken.belongsTo(models.OAuthClient, { | 73 | @ForeignKey(() => OAuthClientModel) |
74 | @Column | ||
75 | oAuthClientId: number | ||
76 | |||
77 | @BelongsTo(() => OAuthClientModel, { | ||
77 | foreignKey: { | 78 | foreignKey: { |
78 | name: 'oAuthClientId', | ||
79 | allowNull: false | 79 | allowNull: false |
80 | }, | 80 | }, |
81 | onDelete: 'cascade' | 81 | onDelete: 'cascade' |
82 | }) | 82 | }) |
83 | } | 83 | OAuthClients: OAuthClientModel[] |
84 | 84 | ||
85 | getByRefreshTokenAndPopulateClient = function (refreshToken: string) { | 85 | static getByRefreshTokenAndPopulateClient (refreshToken: string) { |
86 | const query = { | 86 | const query = { |
87 | where: { | 87 | where: { |
88 | refreshToken: refreshToken | 88 | refreshToken: refreshToken |
89 | }, | 89 | }, |
90 | include: [ OAuthToken['sequelize'].models.OAuthClient ] | 90 | include: [ OAuthClientModel ] |
91 | } | ||
92 | |||
93 | return OAuthTokenModel.findOne(query) | ||
94 | .then(token => { | ||
95 | if (!token) return null | ||
96 | |||
97 | return { | ||
98 | refreshToken: token.refreshToken, | ||
99 | refreshTokenExpiresAt: token.refreshTokenExpiresAt, | ||
100 | client: { | ||
101 | id: token.oAuthClientId | ||
102 | }, | ||
103 | user: { | ||
104 | id: token.userId | ||
105 | } | ||
106 | } as OAuthTokenInfo | ||
107 | }) | ||
108 | .catch(err => { | ||
109 | logger.info('getRefreshToken error.', err) | ||
110 | throw err | ||
111 | }) | ||
91 | } | 112 | } |
92 | 113 | ||
93 | return OAuthToken.findOne(query) | 114 | static getByTokenAndPopulateUser (bearerToken: string) { |
94 | .then(token => { | 115 | const query = { |
95 | if (!token) return null | 116 | where: { |
96 | 117 | accessToken: bearerToken | |
97 | const tokenInfos: OAuthTokenInfo = { | 118 | }, |
98 | refreshToken: token.refreshToken, | 119 | include: [ |
99 | refreshTokenExpiresAt: token.refreshTokenExpiresAt, | 120 | { |
100 | client: { | 121 | model: UserModel, |
101 | id: token.oAuthClientId | 122 | include: [ |
102 | }, | 123 | { |
103 | user: { | 124 | model: AccountModel, |
104 | id: token.userId | 125 | required: true |
126 | } | ||
127 | ] | ||
105 | } | 128 | } |
106 | } | 129 | ] |
130 | } | ||
107 | 131 | ||
108 | return tokenInfos | 132 | return OAuthTokenModel.findOne(query).then(token => { |
109 | }) | 133 | if (token) token['user'] = token.User |
110 | .catch(err => { | ||
111 | logger.info('getRefreshToken error.', err) | ||
112 | throw err | ||
113 | }) | ||
114 | } | ||
115 | 134 | ||
116 | getByTokenAndPopulateUser = function (bearerToken: string) { | 135 | return token |
117 | const query = { | 136 | }) |
118 | where: { | ||
119 | accessToken: bearerToken | ||
120 | }, | ||
121 | include: [ | ||
122 | { | ||
123 | model: OAuthToken['sequelize'].models.User, | ||
124 | include: [ | ||
125 | { | ||
126 | model: OAuthToken['sequelize'].models.Account, | ||
127 | required: true | ||
128 | } | ||
129 | ] | ||
130 | } | ||
131 | ] | ||
132 | } | 137 | } |
133 | 138 | ||
134 | return OAuthToken.findOne(query).then(token => { | 139 | static getByRefreshTokenAndPopulateUser (refreshToken: string) { |
135 | if (token) token['user'] = token.User | 140 | const query = { |
141 | where: { | ||
142 | refreshToken: refreshToken | ||
143 | }, | ||
144 | include: [ | ||
145 | { | ||
146 | model: UserModel, | ||
147 | include: [ | ||
148 | { | ||
149 | model: AccountModel, | ||
150 | required: true | ||
151 | } | ||
152 | ] | ||
153 | } | ||
154 | ] | ||
155 | } | ||
136 | 156 | ||
137 | return token | 157 | return OAuthTokenModel.findOne(query).then(token => { |
138 | }) | 158 | token['user'] = token.User |
139 | } | ||
140 | 159 | ||
141 | getByRefreshTokenAndPopulateUser = function (refreshToken: string) { | 160 | return token |
142 | const query = { | 161 | }) |
143 | where: { | ||
144 | refreshToken: refreshToken | ||
145 | }, | ||
146 | include: [ | ||
147 | { | ||
148 | model: OAuthToken['sequelize'].models.User, | ||
149 | include: [ | ||
150 | { | ||
151 | model: OAuthToken['sequelize'].models.Account, | ||
152 | required: true | ||
153 | } | ||
154 | ] | ||
155 | } | ||
156 | ] | ||
157 | } | 162 | } |
158 | |||
159 | return OAuthToken.findOne(query).then(token => { | ||
160 | token['user'] = token.User | ||
161 | |||
162 | return token | ||
163 | }) | ||
164 | } | 163 | } |