2017-05-22 14:58:25 -04:00
|
|
|
import * as Sequelize from 'sequelize'
|
|
|
|
|
2017-05-15 16:22:03 -04:00
|
|
|
import { logger } from '../helpers'
|
2016-07-20 10:23:58 -04:00
|
|
|
|
2017-05-22 14:58:25 -04:00
|
|
|
import { addMethodsToModel } from './utils'
|
|
|
|
import {
|
|
|
|
OAuthTokenClass,
|
|
|
|
OAuthTokenInstance,
|
|
|
|
OAuthTokenAttributes,
|
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
OAuthTokenMethods,
|
|
|
|
OAuthTokenInfo
|
2017-05-22 14:58:25 -04:00
|
|
|
} from './oauth-token-interface'
|
|
|
|
|
|
|
|
let OAuthToken: Sequelize.Model<OAuthTokenInstance, OAuthTokenAttributes>
|
|
|
|
let getByRefreshTokenAndPopulateClient: OAuthTokenMethods.GetByRefreshTokenAndPopulateClient
|
|
|
|
let getByTokenAndPopulateUser: OAuthTokenMethods.GetByTokenAndPopulateUser
|
|
|
|
let getByRefreshTokenAndPopulateUser: OAuthTokenMethods.GetByRefreshTokenAndPopulateUser
|
|
|
|
let removeByUserId: OAuthTokenMethods.RemoveByUserId
|
2016-07-01 10:03:53 -04:00
|
|
|
|
2017-06-11 11:35:32 -04:00
|
|
|
export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
|
|
|
|
OAuthToken = sequelize.define<OAuthTokenInstance, OAuthTokenAttributes>('OAuthToken',
|
2016-12-11 15:50:51 -05:00
|
|
|
{
|
|
|
|
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' ]
|
|
|
|
}
|
2017-05-22 14:58:25 -04:00
|
|
|
]
|
2016-12-11 15:50:51 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-05-22 14:58:25 -04:00
|
|
|
const classMethods = [
|
|
|
|
associate,
|
|
|
|
|
|
|
|
getByRefreshTokenAndPopulateClient,
|
|
|
|
getByTokenAndPopulateUser,
|
|
|
|
getByRefreshTokenAndPopulateUser,
|
|
|
|
removeByUserId
|
|
|
|
]
|
|
|
|
addMethodsToModel(OAuthToken, classMethods)
|
|
|
|
|
2016-12-11 15:50:51 -05:00
|
|
|
return OAuthToken
|
2016-07-01 10:03:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2016-12-11 15:50:51 -05:00
|
|
|
function associate (models) {
|
2017-05-22 14:58:25 -04:00
|
|
|
OAuthToken.belongsTo(models.User, {
|
2016-12-11 15:50:51 -05:00
|
|
|
foreignKey: {
|
|
|
|
name: 'userId',
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
2016-12-29 03:33:28 -05:00
|
|
|
|
2017-05-22 14:58:25 -04:00
|
|
|
OAuthToken.belongsTo(models.OAuthClient, {
|
2016-12-29 03:33:28 -05:00
|
|
|
foreignKey: {
|
|
|
|
name: 'oAuthClientId',
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
2016-12-11 15:50:51 -05:00
|
|
|
}
|
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
getByRefreshTokenAndPopulateClient = function (refreshToken: string) {
|
2016-12-11 15:50:51 -05:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
refreshToken: refreshToken
|
|
|
|
},
|
2017-05-22 14:58:25 -04:00
|
|
|
include: [ OAuthToken['sequelize'].models.OAuthClient ]
|
2016-12-11 15:50:51 -05:00
|
|
|
}
|
|
|
|
|
2017-05-22 14:58:25 -04:00
|
|
|
return OAuthToken.findOne(query).then(function (token) {
|
2017-06-10 16:15:25 -04:00
|
|
|
if (!token) return null
|
2016-07-20 10:23:58 -04:00
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
const tokenInfos: OAuthTokenInfo = {
|
2016-07-20 10:23:58 -04:00
|
|
|
refreshToken: token.refreshToken,
|
|
|
|
refreshTokenExpiresAt: token.refreshTokenExpiresAt,
|
|
|
|
client: {
|
2017-05-22 14:58:25 -04:00
|
|
|
id: token['client'].id
|
2016-07-20 10:23:58 -04:00
|
|
|
},
|
2016-11-04 06:34:23 -04:00
|
|
|
user: {
|
2017-05-22 14:58:25 -04:00
|
|
|
id: token['user']
|
2016-11-04 06:34:23 -04:00
|
|
|
}
|
2016-07-20 10:23:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return tokenInfos
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.info('getRefreshToken error.', { error: err })
|
|
|
|
})
|
2016-07-01 10:03:53 -04:00
|
|
|
}
|
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
getByTokenAndPopulateUser = function (bearerToken: string) {
|
2016-12-11 15:50:51 -05:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
accessToken: bearerToken
|
|
|
|
},
|
2017-05-22 14:58:25 -04:00
|
|
|
include: [ OAuthToken['sequelize'].models.User ]
|
2016-12-11 15:50:51 -05:00
|
|
|
}
|
|
|
|
|
2017-05-22 14:58:25 -04:00
|
|
|
return OAuthToken.findOne(query).then(function (token) {
|
|
|
|
if (token) token['user'] = token.User
|
2016-12-11 15:50:51 -05:00
|
|
|
|
|
|
|
return token
|
|
|
|
})
|
2016-07-01 10:03:53 -04:00
|
|
|
}
|
2016-07-20 10:23:58 -04:00
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
getByRefreshTokenAndPopulateUser = function (refreshToken: string) {
|
2016-12-11 15:50:51 -05:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
refreshToken: refreshToken
|
|
|
|
},
|
2017-05-22 14:58:25 -04:00
|
|
|
include: [ OAuthToken['sequelize'].models.User ]
|
2016-12-11 15:50:51 -05:00
|
|
|
}
|
|
|
|
|
2017-05-22 14:58:25 -04:00
|
|
|
return OAuthToken.findOne(query).then(function (token) {
|
|
|
|
token['user'] = token.User
|
2016-12-11 15:50:51 -05:00
|
|
|
|
|
|
|
return token
|
|
|
|
})
|
2016-07-20 10:23:58 -04:00
|
|
|
}
|
2016-10-01 03:12:02 -04:00
|
|
|
|
2017-05-22 14:58:25 -04:00
|
|
|
removeByUserId = function (userId, callback) {
|
2016-12-11 15:50:51 -05:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
userId: userId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-22 14:58:25 -04:00
|
|
|
return OAuthToken.destroy(query).asCallback(callback)
|
2016-10-01 03:12:02 -04:00
|
|
|
}
|