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