1
0
Fork 0
peertube/server/models/oauth/oauth-token.ts
2017-11-27 19:40:53 +01:00

164 lines
3.6 KiB
TypeScript

import * as Sequelize from 'sequelize'
import { logger } from '../../helpers'
import { addMethodsToModel } from '../utils'
import { OAuthTokenAttributes, OAuthTokenInfo, OAuthTokenInstance, OAuthTokenMethods } from './oauth-token-interface'
let OAuthToken: Sequelize.Model<OAuthTokenInstance, OAuthTokenAttributes>
let getByRefreshTokenAndPopulateClient: OAuthTokenMethods.GetByRefreshTokenAndPopulateClient
let getByTokenAndPopulateUser: OAuthTokenMethods.GetByTokenAndPopulateUser
let getByRefreshTokenAndPopulateUser: OAuthTokenMethods.GetByRefreshTokenAndPopulateUser
export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
OAuthToken = sequelize.define<OAuthTokenInstance, OAuthTokenAttributes>('OAuthToken',
{
accessToken: {
type: DataTypes.STRING,
allowNull: false
},
accessTokenExpiresAt: {
type: DataTypes.DATE,
allowNull: false
},
refreshToken: {
type: DataTypes.STRING,
allowNull: false
},
refreshTokenExpiresAt: {
type: DataTypes.DATE,
allowNull: false
}
},
{
indexes: [
{
fields: [ 'refreshToken' ],
unique: true
},
{
fields: [ 'accessToken' ],
unique: true
},
{
fields: [ 'userId' ]
},
{
fields: [ 'oAuthClientId' ]
}
]
}
)
const classMethods = [
associate,
getByRefreshTokenAndPopulateClient,
getByTokenAndPopulateUser,
getByRefreshTokenAndPopulateUser
]
addMethodsToModel(OAuthToken, classMethods)
return OAuthToken
}
// ---------------------------------------------------------------------------
function associate (models) {
OAuthToken.belongsTo(models.User, {
foreignKey: {
name: 'userId',
allowNull: false
},
onDelete: 'cascade'
})
OAuthToken.belongsTo(models.OAuthClient, {
foreignKey: {
name: 'oAuthClientId',
allowNull: false
},
onDelete: 'cascade'
})
}
getByRefreshTokenAndPopulateClient = function (refreshToken: string) {
const query = {
where: {
refreshToken: refreshToken
},
include: [ OAuthToken['sequelize'].models.OAuthClient ]
}
return OAuthToken.findOne(query)
.then(token => {
if (!token) return null
const tokenInfos: OAuthTokenInfo = {
refreshToken: token.refreshToken,
refreshTokenExpiresAt: token.refreshTokenExpiresAt,
client: {
id: token.oAuthClientId
},
user: {
id: token.userId
}
}
return tokenInfos
})
.catch(err => {
logger.info('getRefreshToken error.', err)
throw err
})
}
getByTokenAndPopulateUser = function (bearerToken: string) {
const query = {
where: {
accessToken: bearerToken
},
include: [
{
model: OAuthToken['sequelize'].models.User,
include: [
{
model: OAuthToken['sequelize'].models.Account,
required: true
}
]
}
]
}
return OAuthToken.findOne(query).then(token => {
if (token) token['user'] = token.User
return token
})
}
getByRefreshTokenAndPopulateUser = function (refreshToken: string) {
const query = {
where: {
refreshToken: refreshToken
},
include: [
{
model: OAuthToken['sequelize'].models.User,
include: [
{
model: OAuthToken['sequelize'].models.Account,
required: true
}
]
}
]
}
return OAuthToken.findOne(query).then(token => {
token['user'] = token.User
return token
})
}