2017-05-22 20:58:25 +02:00
|
|
|
import * as Sequelize from 'sequelize'
|
|
|
|
|
2017-06-16 09:45:46 +02:00
|
|
|
import { logger } from '../../helpers'
|
2016-07-20 16:23:58 +02:00
|
|
|
|
2017-06-16 09:45:46 +02:00
|
|
|
import { addMethodsToModel } from '../utils'
|
2017-11-23 17:36:15 +01:00
|
|
|
import { OAuthTokenAttributes, OAuthTokenInfo, OAuthTokenInstance, OAuthTokenMethods } from './oauth-token-interface'
|
2017-05-22 20:58:25 +02:00
|
|
|
|
|
|
|
let OAuthToken: Sequelize.Model<OAuthTokenInstance, OAuthTokenAttributes>
|
|
|
|
let getByRefreshTokenAndPopulateClient: OAuthTokenMethods.GetByRefreshTokenAndPopulateClient
|
|
|
|
let getByTokenAndPopulateUser: OAuthTokenMethods.GetByTokenAndPopulateUser
|
|
|
|
let getByRefreshTokenAndPopulateUser: OAuthTokenMethods.GetByRefreshTokenAndPopulateUser
|
2016-07-01 16:03:53 +02:00
|
|
|
|
2017-06-11 17:35:32 +02:00
|
|
|
export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
|
|
|
|
OAuthToken = sequelize.define<OAuthTokenInstance, OAuthTokenAttributes>('OAuthToken',
|
2016-12-11 21:50:51 +01:00
|
|
|
{
|
|
|
|
accessToken: {
|
2016-12-28 15:49:23 +01:00
|
|
|
type: DataTypes.STRING,
|
|
|
|
allowNull: false
|
2016-12-11 21:50:51 +01:00
|
|
|
},
|
|
|
|
accessTokenExpiresAt: {
|
2016-12-28 15:49:23 +01:00
|
|
|
type: DataTypes.DATE,
|
|
|
|
allowNull: false
|
2016-12-11 21:50:51 +01:00
|
|
|
},
|
|
|
|
refreshToken: {
|
2016-12-28 15:49:23 +01:00
|
|
|
type: DataTypes.STRING,
|
|
|
|
allowNull: false
|
2016-12-11 21:50:51 +01:00
|
|
|
},
|
|
|
|
refreshTokenExpiresAt: {
|
2016-12-28 15:49:23 +01:00
|
|
|
type: DataTypes.DATE,
|
|
|
|
allowNull: false
|
2016-12-11 21:50:51 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2016-12-29 09:33:28 +01:00
|
|
|
indexes: [
|
|
|
|
{
|
|
|
|
fields: [ 'refreshToken' ],
|
|
|
|
unique: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'accessToken' ],
|
|
|
|
unique: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'userId' ]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'oAuthClientId' ]
|
|
|
|
}
|
2017-05-22 20:58:25 +02:00
|
|
|
]
|
2016-12-11 21:50:51 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-05-22 20:58:25 +02:00
|
|
|
const classMethods = [
|
|
|
|
associate,
|
|
|
|
|
|
|
|
getByRefreshTokenAndPopulateClient,
|
|
|
|
getByTokenAndPopulateUser,
|
2017-11-23 17:36:15 +01:00
|
|
|
getByRefreshTokenAndPopulateUser
|
2017-05-22 20:58:25 +02:00
|
|
|
]
|
|
|
|
addMethodsToModel(OAuthToken, classMethods)
|
|
|
|
|
2016-12-11 21:50:51 +01:00
|
|
|
return OAuthToken
|
2016-07-01 16:03:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2016-12-11 21:50:51 +01:00
|
|
|
function associate (models) {
|
2017-05-22 20:58:25 +02:00
|
|
|
OAuthToken.belongsTo(models.User, {
|
2016-12-11 21:50:51 +01:00
|
|
|
foreignKey: {
|
|
|
|
name: 'userId',
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
2016-12-29 09:33:28 +01:00
|
|
|
|
2017-05-22 20:58:25 +02:00
|
|
|
OAuthToken.belongsTo(models.OAuthClient, {
|
2016-12-29 09:33:28 +01:00
|
|
|
foreignKey: {
|
|
|
|
name: 'oAuthClientId',
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
2016-12-11 21:50:51 +01:00
|
|
|
}
|
|
|
|
|
2017-06-10 22:15:25 +02:00
|
|
|
getByRefreshTokenAndPopulateClient = function (refreshToken: string) {
|
2016-12-11 21:50:51 +01:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
refreshToken: refreshToken
|
|
|
|
},
|
2017-05-22 20:58:25 +02:00
|
|
|
include: [ OAuthToken['sequelize'].models.OAuthClient ]
|
2016-12-11 21:50:51 +01:00
|
|
|
}
|
|
|
|
|
2017-07-11 17:04:57 +02:00
|
|
|
return OAuthToken.findOne(query)
|
|
|
|
.then(token => {
|
|
|
|
if (!token) return null
|
|
|
|
|
|
|
|
const tokenInfos: OAuthTokenInfo = {
|
|
|
|
refreshToken: token.refreshToken,
|
|
|
|
refreshTokenExpiresAt: token.refreshTokenExpiresAt,
|
|
|
|
client: {
|
2017-07-12 11:56:02 +02:00
|
|
|
id: token.oAuthClientId
|
2017-07-11 17:04:57 +02:00
|
|
|
},
|
|
|
|
user: {
|
2017-07-12 11:56:02 +02:00
|
|
|
id: token.userId
|
2017-07-11 17:04:57 +02:00
|
|
|
}
|
2016-11-04 11:34:23 +01:00
|
|
|
}
|
2016-07-20 16:23:58 +02:00
|
|
|
|
2017-07-11 17:04:57 +02:00
|
|
|
return tokenInfos
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
logger.info('getRefreshToken error.', err)
|
|
|
|
throw err
|
|
|
|
})
|
2016-07-01 16:03:53 +02:00
|
|
|
}
|
|
|
|
|
2017-06-10 22:15:25 +02:00
|
|
|
getByTokenAndPopulateUser = function (bearerToken: string) {
|
2016-12-11 21:50:51 +01:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
accessToken: bearerToken
|
|
|
|
},
|
2017-10-24 19:41:09 +02:00
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: OAuthToken['sequelize'].models.User,
|
|
|
|
include: [
|
|
|
|
{
|
2017-11-10 14:48:08 +01:00
|
|
|
model: OAuthToken['sequelize'].models.Account,
|
2017-10-24 19:41:09 +02:00
|
|
|
required: true
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
2016-12-11 21:50:51 +01:00
|
|
|
}
|
|
|
|
|
2017-07-11 17:04:57 +02:00
|
|
|
return OAuthToken.findOne(query).then(token => {
|
2017-05-22 20:58:25 +02:00
|
|
|
if (token) token['user'] = token.User
|
2016-12-11 21:50:51 +01:00
|
|
|
|
|
|
|
return token
|
|
|
|
})
|
2016-07-01 16:03:53 +02:00
|
|
|
}
|
2016-07-20 16:23:58 +02:00
|
|
|
|
2017-06-10 22:15:25 +02:00
|
|
|
getByRefreshTokenAndPopulateUser = function (refreshToken: string) {
|
2016-12-11 21:50:51 +01:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
refreshToken: refreshToken
|
|
|
|
},
|
2017-10-24 19:41:09 +02:00
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: OAuthToken['sequelize'].models.User,
|
|
|
|
include: [
|
|
|
|
{
|
2017-11-10 14:48:08 +01:00
|
|
|
model: OAuthToken['sequelize'].models.Account,
|
2017-10-24 19:41:09 +02:00
|
|
|
required: true
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
2016-12-11 21:50:51 +01:00
|
|
|
}
|
|
|
|
|
2017-07-11 17:04:57 +02:00
|
|
|
return OAuthToken.findOne(query).then(token => {
|
2017-05-22 20:58:25 +02:00
|
|
|
token['user'] = token.User
|
2016-12-11 21:50:51 +01:00
|
|
|
|
|
|
|
return token
|
|
|
|
})
|
2016-07-20 16:23:58 +02:00
|
|
|
}
|