2017-12-14 03:07:57 -06:00
|
|
|
import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
|
2017-12-28 04:16:08 -06:00
|
|
|
import { logger } from '../../helpers/logger'
|
2017-12-12 10:53:50 -06:00
|
|
|
import { UserModel } from '../account/user'
|
|
|
|
import { OAuthClientModel } from './oauth-client'
|
2018-08-08 07:58:21 -05:00
|
|
|
import { Transaction } from 'sequelize'
|
2018-09-20 03:13:13 -05:00
|
|
|
import { AccountModel } from '../account/account'
|
|
|
|
import { ActorModel } from '../activitypub/actor'
|
2017-12-12 10:53:50 -06:00
|
|
|
|
|
|
|
export type OAuthTokenInfo = {
|
|
|
|
refreshToken: string
|
|
|
|
refreshTokenExpiresAt: Date,
|
|
|
|
client: {
|
|
|
|
id: number
|
|
|
|
},
|
|
|
|
user: {
|
|
|
|
id: number
|
|
|
|
}
|
|
|
|
}
|
2016-07-20 09:23:58 -05:00
|
|
|
|
2017-12-14 03:07:57 -06:00
|
|
|
enum ScopeNames {
|
2018-09-20 03:13:13 -05:00
|
|
|
WITH_USER = 'WITH_USER'
|
2017-12-14 03:07:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
@Scopes({
|
2018-09-20 03:13:13 -05:00
|
|
|
[ScopeNames.WITH_USER]: {
|
2017-12-14 03:07:57 -06:00
|
|
|
include: [
|
|
|
|
{
|
2018-09-20 03:13:13 -05:00
|
|
|
model: () => UserModel.unscoped(),
|
|
|
|
required: true,
|
2017-12-14 03:07:57 -06:00
|
|
|
include: [
|
|
|
|
{
|
2018-09-20 03:13:13 -05:00
|
|
|
attributes: [ 'id' ],
|
|
|
|
model: () => AccountModel.unscoped(),
|
|
|
|
required: true,
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
attributes: [ 'id' ],
|
|
|
|
model: () => ActorModel.unscoped(),
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
]
|
2017-12-14 03:07:57 -06: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
|
|
|
]
|
|
|
|
})
|
|
|
|
export class OAuthTokenModel extends Model<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
|
2016-07-01 09:03:53 -05:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
@AllowNull(false)
|
|
|
|
@Column
|
|
|
|
refreshTokenExpiresAt: Date
|
2016-07-01 09:03:53 -05:00
|
|
|
|
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
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
static getByRefreshTokenAndPopulateClient (refreshToken: string) {
|
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
refreshToken: refreshToken
|
|
|
|
},
|
|
|
|
include: [ OAuthClientModel ]
|
|
|
|
}
|
|
|
|
|
|
|
|
return OAuthTokenModel.findOne(query)
|
|
|
|
.then(token => {
|
|
|
|
if (!token) return null
|
|
|
|
|
|
|
|
return {
|
|
|
|
refreshToken: token.refreshToken,
|
|
|
|
refreshTokenExpiresAt: token.refreshTokenExpiresAt,
|
|
|
|
client: {
|
|
|
|
id: token.oAuthClientId
|
|
|
|
},
|
|
|
|
user: {
|
|
|
|
id: token.userId
|
|
|
|
}
|
|
|
|
} as OAuthTokenInfo
|
|
|
|
})
|
|
|
|
.catch(err => {
|
2018-08-08 07:58:21 -05:00
|
|
|
logger.error('getRefreshToken error.', { err })
|
2017-12-12 10:53:50 -06:00
|
|
|
throw err
|
|
|
|
})
|
2016-12-11 14:50:51 -06:00
|
|
|
}
|
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
static getByTokenAndPopulateUser (bearerToken: string) {
|
|
|
|
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
|
|
|
|
2018-09-20 03:13:13 -05:00
|
|
|
return OAuthTokenModel.scope(ScopeNames.WITH_USER).findOne(query).then(token => {
|
2017-12-12 10:53:50 -06:00
|
|
|
if (token) token['user'] = token.User
|
2016-07-01 09:03:53 -05:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
return token
|
|
|
|
})
|
2016-12-11 14:50:51 -06:00
|
|
|
}
|
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
static getByRefreshTokenAndPopulateUser (refreshToken: string) {
|
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
refreshToken: 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
|
|
|
|
2018-09-20 03:13:13 -05:00
|
|
|
return OAuthTokenModel.scope(ScopeNames.WITH_USER)
|
2017-12-14 03:07:57 -06:00
|
|
|
.findOne(query)
|
|
|
|
.then(token => {
|
2018-07-25 15:01:25 -05:00
|
|
|
if (token) {
|
|
|
|
token['user'] = token.User
|
|
|
|
return token
|
|
|
|
} else {
|
|
|
|
return new OAuthTokenModel()
|
|
|
|
}
|
2017-12-14 03:07:57 -06:00
|
|
|
})
|
2016-12-11 14:50:51 -06:00
|
|
|
}
|
2018-01-23 02:15:36 -06:00
|
|
|
|
2018-08-08 07:58:21 -05:00
|
|
|
static deleteUserToken (userId: number, t?: Transaction) {
|
2018-01-23 02:15:36 -06:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
userId
|
2018-08-08 07:58:21 -05:00
|
|
|
},
|
|
|
|
transaction: t
|
2018-01-23 02:15:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return OAuthTokenModel.destroy(query)
|
|
|
|
}
|
2016-07-20 09:23:58 -05:00
|
|
|
}
|