aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/oauth/oauth-token.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-07-31 14:34:36 +0200
committerChocobozzz <me@florianbigard.com>2023-08-11 15:02:33 +0200
commit3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch)
treee4510b39bdac9c318fdb4b47018d08f15368b8f0 /server/models/oauth/oauth-token.ts
parent04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff)
downloadPeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge conflicts, but it's a major step forward: * Server can be faster at startup because imports() are async and we can easily lazy import big modules * Angular doesn't seem to support ES import (with .js extension), so we had to correctly organize peertube into a monorepo: * Use yarn workspace feature * Use typescript reference projects for dependencies * Shared projects have been moved into "packages", each one is now a node module (with a dedicated package.json/tsconfig.json) * server/tools have been moved into apps/ and is now a dedicated app bundled and published on NPM so users don't have to build peertube cli tools manually * server/tests have been moved into packages/ so we don't compile them every time we want to run the server * Use isolatedModule option: * Had to move from const enum to const (https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums) * Had to explictely specify "type" imports when used in decorators * Prefer tsx (that uses esbuild under the hood) instead of ts-node to load typescript files (tests with mocha or scripts): * To reduce test complexity as esbuild doesn't support decorator metadata, we only test server files that do not import server models * We still build tests files into js files for a faster CI * Remove unmaintained peertube CLI import script * Removed some barrels to speed up execution (less imports)
Diffstat (limited to 'server/models/oauth/oauth-token.ts')
-rw-r--r--server/models/oauth/oauth-token.ts220
1 files changed, 0 insertions, 220 deletions
diff --git a/server/models/oauth/oauth-token.ts b/server/models/oauth/oauth-token.ts
deleted file mode 100644
index f72423190..000000000
--- a/server/models/oauth/oauth-token.ts
+++ /dev/null
@@ -1,220 +0,0 @@
1import { Transaction } from 'sequelize'
2import {
3 AfterDestroy,
4 AfterUpdate,
5 AllowNull,
6 BelongsTo,
7 Column,
8 CreatedAt,
9 ForeignKey,
10 Model,
11 Scopes,
12 Table,
13 UpdatedAt
14} from 'sequelize-typescript'
15import { TokensCache } from '@server/lib/auth/tokens-cache'
16import { MUserAccountId } from '@server/types/models'
17import { MOAuthTokenUser } from '@server/types/models/oauth/oauth-token'
18import { AttributesOnly } from '@shared/typescript-utils'
19import { logger } from '../../helpers/logger'
20import { AccountModel } from '../account/account'
21import { ActorModel } from '../actor/actor'
22import { UserModel } from '../user/user'
23import { OAuthClientModel } from './oauth-client'
24
25export type OAuthTokenInfo = {
26 refreshToken: string
27 refreshTokenExpiresAt: Date
28 client: {
29 id: number
30 }
31 user: MUserAccountId
32 token: MOAuthTokenUser
33}
34
35enum ScopeNames {
36 WITH_USER = 'WITH_USER'
37}
38
39@Scopes(() => ({
40 [ScopeNames.WITH_USER]: {
41 include: [
42 {
43 model: UserModel.unscoped(),
44 required: true,
45 include: [
46 {
47 attributes: [ 'id' ],
48 model: AccountModel.unscoped(),
49 required: true,
50 include: [
51 {
52 attributes: [ 'id', 'url' ],
53 model: ActorModel.unscoped(),
54 required: true
55 }
56 ]
57 }
58 ]
59 }
60 ]
61 }
62}))
63@Table({
64 tableName: 'oAuthToken',
65 indexes: [
66 {
67 fields: [ 'refreshToken' ],
68 unique: true
69 },
70 {
71 fields: [ 'accessToken' ],
72 unique: true
73 },
74 {
75 fields: [ 'userId' ]
76 },
77 {
78 fields: [ 'oAuthClientId' ]
79 }
80 ]
81})
82export class OAuthTokenModel extends Model<Partial<AttributesOnly<OAuthTokenModel>>> {
83
84 @AllowNull(false)
85 @Column
86 accessToken: string
87
88 @AllowNull(false)
89 @Column
90 accessTokenExpiresAt: Date
91
92 @AllowNull(false)
93 @Column
94 refreshToken: string
95
96 @AllowNull(false)
97 @Column
98 refreshTokenExpiresAt: Date
99
100 @Column
101 authName: string
102
103 @CreatedAt
104 createdAt: Date
105
106 @UpdatedAt
107 updatedAt: Date
108
109 @ForeignKey(() => UserModel)
110 @Column
111 userId: number
112
113 @BelongsTo(() => UserModel, {
114 foreignKey: {
115 allowNull: false
116 },
117 onDelete: 'cascade'
118 })
119 User: UserModel
120
121 @ForeignKey(() => OAuthClientModel)
122 @Column
123 oAuthClientId: number
124
125 @BelongsTo(() => OAuthClientModel, {
126 foreignKey: {
127 allowNull: false
128 },
129 onDelete: 'cascade'
130 })
131 OAuthClients: OAuthClientModel[]
132
133 @AfterUpdate
134 @AfterDestroy
135 static removeTokenCache (token: OAuthTokenModel) {
136 return TokensCache.Instance.clearCacheByToken(token.accessToken)
137 }
138
139 static loadByRefreshToken (refreshToken: string) {
140 const query = {
141 where: { refreshToken }
142 }
143
144 return OAuthTokenModel.findOne(query)
145 }
146
147 static getByRefreshTokenAndPopulateClient (refreshToken: string) {
148 const query = {
149 where: {
150 refreshToken
151 },
152 include: [ OAuthClientModel ]
153 }
154
155 return OAuthTokenModel.scope(ScopeNames.WITH_USER)
156 .findOne(query)
157 .then(token => {
158 if (!token) return null
159
160 return {
161 refreshToken: token.refreshToken,
162 refreshTokenExpiresAt: token.refreshTokenExpiresAt,
163 client: {
164 id: token.oAuthClientId
165 },
166 user: token.User,
167 token
168 } as OAuthTokenInfo
169 })
170 .catch(err => {
171 logger.error('getRefreshToken error.', { err })
172 throw err
173 })
174 }
175
176 static getByTokenAndPopulateUser (bearerToken: string): Promise<MOAuthTokenUser> {
177 const query = {
178 where: {
179 accessToken: bearerToken
180 }
181 }
182
183 return OAuthTokenModel.scope(ScopeNames.WITH_USER)
184 .findOne(query)
185 .then(token => {
186 if (!token) return null
187
188 return Object.assign(token, { user: token.User })
189 })
190 }
191
192 static getByRefreshTokenAndPopulateUser (refreshToken: string): Promise<MOAuthTokenUser> {
193 const query = {
194 where: {
195 refreshToken
196 }
197 }
198
199 return OAuthTokenModel.scope(ScopeNames.WITH_USER)
200 .findOne(query)
201 .then(token => {
202 if (!token) return undefined
203
204 return Object.assign(token, { user: token.User })
205 })
206 }
207
208 static deleteUserToken (userId: number, t?: Transaction) {
209 TokensCache.Instance.deleteUserToken(userId)
210
211 const query = {
212 where: {
213 userId
214 },
215 transaction: t
216 }
217
218 return OAuthTokenModel.destroy(query)
219 }
220}