PeerTube/server/models/oauth-token.ts

143 lines
2.8 KiB
TypeScript
Raw Normal View History

2017-05-15 15:22:03 -05:00
import { logger } from '../helpers'
2016-07-20 09:23:58 -05:00
// ---------------------------------------------------------------------------
2016-12-11 14:50:51 -06:00
module.exports = function (sequelize, DataTypes) {
const OAuthToken = sequelize.define('OAuthToken',
{
accessToken: {
2016-12-28 08:49:23 -06:00
type: DataTypes.STRING,
allowNull: false
2016-12-11 14:50:51 -06:00
},
accessTokenExpiresAt: {
2016-12-28 08:49:23 -06:00
type: DataTypes.DATE,
allowNull: false
2016-12-11 14:50:51 -06:00
},
refreshToken: {
2016-12-28 08:49:23 -06:00
type: DataTypes.STRING,
allowNull: false
2016-12-11 14:50:51 -06:00
},
refreshTokenExpiresAt: {
2016-12-28 08:49:23 -06:00
type: DataTypes.DATE,
allowNull: false
2016-12-11 14:50:51 -06:00
}
},
{
2016-12-29 02:33:28 -06:00
indexes: [
{
fields: [ 'refreshToken' ],
unique: true
},
{
fields: [ 'accessToken' ],
unique: true
},
{
fields: [ 'userId' ]
},
{
fields: [ 'oAuthClientId' ]
}
],
2016-12-11 14:50:51 -06:00
classMethods: {
associate,
getByRefreshTokenAndPopulateClient,
getByTokenAndPopulateUser,
getByRefreshTokenAndPopulateUser,
removeByUserId
}
}
)
return OAuthToken
}
// ---------------------------------------------------------------------------
2016-12-11 14:50:51 -06:00
function associate (models) {
this.belongsTo(models.User, {
foreignKey: {
name: 'userId',
allowNull: false
},
onDelete: 'cascade'
})
2016-12-29 02:33:28 -06:00
this.belongsTo(models.OAuthClient, {
foreignKey: {
name: 'oAuthClientId',
allowNull: false
},
onDelete: 'cascade'
})
2016-12-11 14:50:51 -06:00
}
2016-07-20 09:23:58 -05:00
function getByRefreshTokenAndPopulateClient (refreshToken) {
2016-12-11 14:50:51 -06:00
const query = {
where: {
refreshToken: refreshToken
},
include: [ this.associations.OAuthClient ]
}
return this.findOne(query).then(function (token) {
2016-07-20 09:23:58 -05:00
if (!token) return token
const tokenInfos = {
refreshToken: token.refreshToken,
refreshTokenExpiresAt: token.refreshTokenExpiresAt,
client: {
2016-12-11 14:50:51 -06:00
id: token.client.id
2016-07-20 09:23:58 -05:00
},
2016-11-04 05:34:23 -05:00
user: {
id: token.user
}
2016-07-20 09:23:58 -05:00
}
return tokenInfos
}).catch(function (err) {
logger.info('getRefreshToken error.', { error: err })
})
}
2016-07-20 09:23:58 -05:00
function getByTokenAndPopulateUser (bearerToken) {
2016-12-11 14:50:51 -06:00
const query = {
where: {
accessToken: bearerToken
},
include: [ this.sequelize.models.User ]
}
return this.findOne(query).then(function (token) {
if (token) token.user = token.User
return token
})
}
2016-07-20 09:23:58 -05:00
function getByRefreshTokenAndPopulateUser (refreshToken) {
2016-12-11 14:50:51 -06:00
const query = {
where: {
refreshToken: refreshToken
},
include: [ this.sequelize.models.User ]
}
return this.findOne(query).then(function (token) {
token.user = token.User
return token
})
2016-07-20 09:23:58 -05:00
}
function removeByUserId (userId, callback) {
2016-12-11 14:50:51 -06:00
const query = {
where: {
userId: userId
}
}
return this.destroy(query).asCallback(callback)
}