PeerTube/server/models/oauth/oauth-token.ts

221 lines
4.8 KiB
TypeScript
Raw Normal View History

2020-12-08 07:30:29 -06:00
import { Transaction } from 'sequelize'
2018-09-20 04:31:48 -05:00
import {
2018-11-19 10:08:18 -06:00
AfterDestroy,
2018-09-20 04:31:48 -05:00
AfterUpdate,
AllowNull,
BelongsTo,
Column,
CreatedAt,
ForeignKey,
Model,
Scopes,
Table,
UpdatedAt
} from 'sequelize-typescript'
import { TokensCache } from '@server/lib/auth/tokens-cache'
import { MUserAccountId } from '@server/types/models'
2020-12-08 07:30:29 -06:00
import { MOAuthTokenUser } from '@server/types/models/oauth/oauth-token'
import { AttributesOnly } from '@shared/typescript-utils'
2017-12-28 04:16:08 -06:00
import { logger } from '../../helpers/logger'
import { AccountModel } from '../account/account'
2021-05-11 04:15:29 -05:00
import { ActorModel } from '../actor/actor'
import { UserModel } from '../user/user'
2020-12-08 07:30:29 -06:00
import { OAuthClientModel } from './oauth-client'
2017-12-12 10:53:50 -06:00
export type OAuthTokenInfo = {
refreshToken: string
2020-01-31 09:56:52 -06:00
refreshTokenExpiresAt: Date
2017-12-12 10:53:50 -06:00
client: {
id: number
2020-01-31 09:56:52 -06:00
}
user: MUserAccountId
token: MOAuthTokenUser
2017-12-12 10:53:50 -06:00
}
2016-07-20 09:23:58 -05:00
2017-12-14 03:07:57 -06:00
enum ScopeNames {
WITH_USER = 'WITH_USER'
2017-12-14 03:07:57 -06:00
}
2019-04-23 02:50:57 -05:00
@Scopes(() => ({
[ScopeNames.WITH_USER]: {
2017-12-14 03:07:57 -06:00
include: [
{
2019-04-23 02:50:57 -05:00
model: UserModel.unscoped(),
required: true,
2017-12-14 03:07:57 -06:00
include: [
{
attributes: [ 'id' ],
2019-04-23 02:50:57 -05:00
model: AccountModel.unscoped(),
required: true,
include: [
{
2018-11-14 08:01:28 -06:00
attributes: [ 'id', 'url' ],
2019-04-23 02:50:57 -05:00
model: ActorModel.unscoped(),
required: true
}
]
2017-12-14 03:07:57 -06:00
}
]
}
2019-04-23 02:50:57 -05:00
]
2017-12-14 03:07:57 -06:00
}
2019-04-23 02:50:57 -05:00
}))
2017-12-12 10:53:50 -06:00
@Table({
tableName: 'oAuthToken',
indexes: [
2016-12-11 14:50:51 -06:00
{
2017-12-12 10:53:50 -06:00
fields: [ 'refreshToken' ],
unique: true
2016-12-11 14:50:51 -06:00
},
{
2017-12-12 10:53:50 -06:00
fields: [ 'accessToken' ],
unique: true
},
{
fields: [ 'userId' ]
},
{
fields: [ 'oAuthClientId' ]
2016-12-11 14:50:51 -06:00
}
2017-12-12 10:53:50 -06:00
]
})
2021-05-12 07:09:04 -05:00
export class OAuthTokenModel extends Model<Partial<AttributesOnly<OAuthTokenModel>>> {
2016-12-11 14:50:51 -06:00
2017-12-12 10:53:50 -06:00
@AllowNull(false)
@Column
accessToken: string
2017-05-22 13:58:25 -05:00
2017-12-12 10:53:50 -06:00
@AllowNull(false)
@Column
accessTokenExpiresAt: Date
2017-05-22 13:58:25 -05:00
2017-12-12 10:53:50 -06:00
@AllowNull(false)
@Column
refreshToken: string
2017-12-12 10:53:50 -06:00
@AllowNull(false)
@Column
refreshTokenExpiresAt: Date
@Column
authName: string
2017-12-12 10:53:50 -06:00
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@ForeignKey(() => UserModel)
@Column
userId: number
@BelongsTo(() => UserModel, {
2016-12-11 14:50:51 -06:00
foreignKey: {
allowNull: false
},
onDelete: 'cascade'
})
2017-12-12 10:53:50 -06:00
User: UserModel
2016-12-29 02:33:28 -06:00
2017-12-12 10:53:50 -06:00
@ForeignKey(() => OAuthClientModel)
@Column
oAuthClientId: number
@BelongsTo(() => OAuthClientModel, {
2016-12-29 02:33:28 -06:00
foreignKey: {
allowNull: false
},
onDelete: 'cascade'
})
2017-12-12 10:53:50 -06:00
OAuthClients: OAuthClientModel[]
2016-12-11 14:50:51 -06:00
2018-09-20 04:31:48 -05:00
@AfterUpdate
2018-11-19 10:08:18 -06:00
@AfterDestroy
2018-09-20 04:31:48 -05:00
static removeTokenCache (token: OAuthTokenModel) {
return TokensCache.Instance.clearCacheByToken(token.accessToken)
2018-09-20 04:31:48 -05:00
}
static loadByRefreshToken (refreshToken: string) {
const query = {
where: { refreshToken }
}
return OAuthTokenModel.findOne(query)
}
2017-12-12 10:53:50 -06:00
static getByRefreshTokenAndPopulateClient (refreshToken: string) {
const query = {
where: {
refreshToken
2017-12-12 10:53:50 -06:00
},
include: [ OAuthClientModel ]
}
return OAuthTokenModel.scope(ScopeNames.WITH_USER)
.findOne(query)
.then(token => {
if (!token) return null
return {
refreshToken: token.refreshToken,
refreshTokenExpiresAt: token.refreshTokenExpiresAt,
client: {
id: token.oAuthClientId
},
2020-05-07 09:36:04 -05:00
user: token.User,
token
} as OAuthTokenInfo
})
.catch(err => {
logger.error('getRefreshToken error.', { err })
throw err
})
2016-12-11 14:50:51 -06:00
}
2020-12-08 07:30:29 -06:00
static getByTokenAndPopulateUser (bearerToken: string): Promise<MOAuthTokenUser> {
2017-12-12 10:53:50 -06:00
const query = {
where: {
accessToken: bearerToken
2017-12-14 03:07:57 -06:00
}
2017-12-12 10:53:50 -06:00
}
2016-07-20 09:23:58 -05:00
2019-04-23 02:50:57 -05:00
return OAuthTokenModel.scope(ScopeNames.WITH_USER)
.findOne(query)
.then(token => {
2019-08-15 04:53:26 -05:00
if (!token) return null
2019-08-15 04:53:26 -05:00
return Object.assign(token, { user: token.User })
2019-04-23 02:50:57 -05:00
})
2016-12-11 14:50:51 -06:00
}
2020-12-08 07:30:29 -06:00
static getByRefreshTokenAndPopulateUser (refreshToken: string): Promise<MOAuthTokenUser> {
2017-12-12 10:53:50 -06:00
const query = {
where: {
refreshToken
2017-12-14 03:07:57 -06:00
}
2017-12-12 10:53:50 -06:00
}
2016-12-11 14:50:51 -06:00
return OAuthTokenModel.scope(ScopeNames.WITH_USER)
2017-12-14 03:07:57 -06:00
.findOne(query)
.then(token => {
if (!token) return undefined
2019-08-15 04:53:26 -05:00
return Object.assign(token, { user: token.User })
2017-12-14 03:07:57 -06:00
})
2016-12-11 14:50:51 -06:00
}
2018-08-08 07:58:21 -05:00
static deleteUserToken (userId: number, t?: Transaction) {
TokensCache.Instance.deleteUserToken(userId)
const query = {
where: {
userId
2018-08-08 07:58:21 -05:00
},
transaction: t
}
return OAuthTokenModel.destroy(query)
}
2016-07-20 09:23:58 -05:00
}