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

145 lines
2.8 KiB
JavaScript
Raw Normal View History

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