1
0
Fork 0
peertube/server/models/oauth/oauth-token.ts

173 lines
3.2 KiB
TypeScript
Raw Normal View History

2017-12-14 04:07:57 -05:00
import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
2017-12-28 05:16:08 -05:00
import { logger } from '../../helpers/logger'
2017-12-12 11:53:50 -05:00
import { AccountModel } from '../account/account'
import { UserModel } from '../account/user'
import { OAuthClientModel } from './oauth-client'
export type OAuthTokenInfo = {
refreshToken: string
refreshTokenExpiresAt: Date,
client: {
id: number
},
user: {
id: number
}
}
2016-07-20 10:23:58 -04:00
2017-12-14 04:07:57 -05:00
enum ScopeNames {
WITH_ACCOUNT = 'WITH_ACCOUNT'
}
@Scopes({
[ScopeNames.WITH_ACCOUNT]: {
include: [
{
model: () => UserModel,
include: [
{
model: () => AccountModel,
required: true
}
]
}
]
}
})
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
]
})
export class OAuthTokenModel extends Model<OAuthTokenModel> {
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
2017-12-12 11:53:50 -05:00
@AllowNull(false)
@Column
refreshTokenExpiresAt: Date
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
2017-12-12 11:53:50 -05: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-03-26 09:54:13 -04:00
logger.info('getRefreshToken error.', { err })
2017-12-12 11:53:50 -05:00
throw err
})
2016-12-11 15:50:51 -05:00
}
2017-12-12 11:53:50 -05:00
static getByTokenAndPopulateUser (bearerToken: string) {
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
2017-12-14 04:07:57 -05:00
return OAuthTokenModel.scope(ScopeNames.WITH_ACCOUNT).findOne(query).then(token => {
2017-12-12 11:53:50 -05:00
if (token) token['user'] = token.User
2017-12-12 11:53:50 -05:00
return token
})
2016-12-11 15:50:51 -05:00
}
2017-12-12 11:53:50 -05:00
static getByRefreshTokenAndPopulateUser (refreshToken: string) {
const query = {
where: {
refreshToken: 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
2017-12-14 04:07:57 -05:00
return OAuthTokenModel.scope(ScopeNames.WITH_ACCOUNT)
.findOne(query)
.then(token => {
token['user'] = token.User
2016-07-20 10:23:58 -04:00
2017-12-14 04:07:57 -05:00
return token
})
2016-12-11 15:50:51 -05:00
}
static deleteUserToken (userId: number) {
const query = {
where: {
userId
}
}
return OAuthTokenModel.destroy(query)
}
2016-07-20 10:23:58 -04:00
}