2016-07-20 17:23:58 +03:00
|
|
|
const logger = require('../helpers/logger')
|
|
|
|
|
2016-07-01 17:03:53 +03:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2016-12-11 23:50:51 +03:00
|
|
|
module.exports = function (sequelize, DataTypes) {
|
|
|
|
const OAuthToken = sequelize.define('OAuthToken',
|
|
|
|
{
|
|
|
|
accessToken: {
|
|
|
|
type: DataTypes.STRING
|
|
|
|
},
|
|
|
|
accessTokenExpiresAt: {
|
|
|
|
type: DataTypes.DATE
|
|
|
|
},
|
|
|
|
refreshToken: {
|
|
|
|
type: DataTypes.STRING
|
|
|
|
},
|
|
|
|
refreshTokenExpiresAt: {
|
|
|
|
type: DataTypes.DATE
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
classMethods: {
|
|
|
|
associate,
|
|
|
|
|
|
|
|
getByRefreshTokenAndPopulateClient,
|
|
|
|
getByTokenAndPopulateUser,
|
|
|
|
getByRefreshTokenAndPopulateUser,
|
|
|
|
removeByUserId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
return OAuthToken
|
2016-07-01 17:03:53 +03:00
|
|
|
}
|
|
|
|
|
2016-12-11 23:50:51 +03:00
|
|
|
// TODO: validation
|
|
|
|
// OAuthTokenSchema.path('accessToken').required(true)
|
|
|
|
// OAuthTokenSchema.path('client').required(true)
|
|
|
|
// OAuthTokenSchema.path('user').required(true)
|
2016-07-01 17:03:53 +03:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2016-12-11 23:50:51 +03:00
|
|
|
function associate (models) {
|
|
|
|
this.belongsTo(models.User, {
|
|
|
|
foreignKey: {
|
|
|
|
name: 'userId',
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-07-20 17:23:58 +03:00
|
|
|
function getByRefreshTokenAndPopulateClient (refreshToken) {
|
2016-12-11 23:50:51 +03:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
refreshToken: refreshToken
|
|
|
|
},
|
|
|
|
include: [ this.associations.OAuthClient ]
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.findOne(query).then(function (token) {
|
2016-07-20 17:23:58 +03:00
|
|
|
if (!token) return token
|
|
|
|
|
|
|
|
const tokenInfos = {
|
|
|
|
refreshToken: token.refreshToken,
|
|
|
|
refreshTokenExpiresAt: token.refreshTokenExpiresAt,
|
|
|
|
client: {
|
2016-12-11 23:50:51 +03:00
|
|
|
id: token.client.id
|
2016-07-20 17:23:58 +03:00
|
|
|
},
|
2016-11-04 13:34:23 +03:00
|
|
|
user: {
|
|
|
|
id: token.user
|
|
|
|
}
|
2016-07-20 17:23:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return tokenInfos
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.info('getRefreshToken error.', { error: err })
|
|
|
|
})
|
2016-07-01 17:03:53 +03:00
|
|
|
}
|
|
|
|
|
2016-07-20 17:23:58 +03:00
|
|
|
function getByTokenAndPopulateUser (bearerToken) {
|
2016-12-11 23:50:51 +03: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-01 17:03:53 +03:00
|
|
|
}
|
2016-07-20 17:23:58 +03:00
|
|
|
|
2016-11-01 20:16:47 +03:00
|
|
|
function getByRefreshTokenAndPopulateUser (refreshToken) {
|
2016-12-11 23:50:51 +03: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 17:23:58 +03:00
|
|
|
}
|
2016-10-01 10:12:02 +03:00
|
|
|
|
|
|
|
function removeByUserId (userId, callback) {
|
2016-12-11 23:50:51 +03:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
userId: userId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.destroy(query).asCallback(callback)
|
2016-10-01 10:12:02 +03:00
|
|
|
}
|