From 74889a71fe687dda74f2a687653122327807af36 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 16 Jun 2017 09:45:46 +0200 Subject: Reorganize model files --- server/models/oauth/oauth-token.ts | 160 +++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 server/models/oauth/oauth-token.ts (limited to 'server/models/oauth/oauth-token.ts') diff --git a/server/models/oauth/oauth-token.ts b/server/models/oauth/oauth-token.ts new file mode 100644 index 000000000..eab9cf858 --- /dev/null +++ b/server/models/oauth/oauth-token.ts @@ -0,0 +1,160 @@ +import * as Sequelize from 'sequelize' + +import { logger } from '../../helpers' + +import { addMethodsToModel } from '../utils' +import { + OAuthTokenClass, + OAuthTokenInstance, + OAuthTokenAttributes, + + OAuthTokenMethods, + OAuthTokenInfo +} from './oauth-token-interface' + +let OAuthToken: Sequelize.Model +let getByRefreshTokenAndPopulateClient: OAuthTokenMethods.GetByRefreshTokenAndPopulateClient +let getByTokenAndPopulateUser: OAuthTokenMethods.GetByTokenAndPopulateUser +let getByRefreshTokenAndPopulateUser: OAuthTokenMethods.GetByRefreshTokenAndPopulateUser +let removeByUserId: OAuthTokenMethods.RemoveByUserId + +export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { + OAuthToken = sequelize.define('OAuthToken', + { + accessToken: { + type: DataTypes.STRING, + allowNull: false + }, + accessTokenExpiresAt: { + type: DataTypes.DATE, + allowNull: false + }, + refreshToken: { + type: DataTypes.STRING, + allowNull: false + }, + refreshTokenExpiresAt: { + type: DataTypes.DATE, + allowNull: false + } + }, + { + indexes: [ + { + fields: [ 'refreshToken' ], + unique: true + }, + { + fields: [ 'accessToken' ], + unique: true + }, + { + fields: [ 'userId' ] + }, + { + fields: [ 'oAuthClientId' ] + } + ] + } + ) + + const classMethods = [ + associate, + + getByRefreshTokenAndPopulateClient, + getByTokenAndPopulateUser, + getByRefreshTokenAndPopulateUser, + removeByUserId + ] + addMethodsToModel(OAuthToken, classMethods) + + return OAuthToken +} + +// --------------------------------------------------------------------------- + +function associate (models) { + OAuthToken.belongsTo(models.User, { + foreignKey: { + name: 'userId', + allowNull: false + }, + onDelete: 'cascade' + }) + + OAuthToken.belongsTo(models.OAuthClient, { + foreignKey: { + name: 'oAuthClientId', + allowNull: false + }, + onDelete: 'cascade' + }) +} + +getByRefreshTokenAndPopulateClient = function (refreshToken: string) { + const query = { + where: { + refreshToken: refreshToken + }, + include: [ OAuthToken['sequelize'].models.OAuthClient ] + } + + return OAuthToken.findOne(query).then(function (token) { + if (!token) return null + + const tokenInfos: OAuthTokenInfo = { + refreshToken: token.refreshToken, + refreshTokenExpiresAt: token.refreshTokenExpiresAt, + client: { + id: token['client'].id + }, + user: { + id: token['user'] + } + } + + return tokenInfos + }).catch(function (err) { + logger.info('getRefreshToken error.', { error: err }) + }) +} + +getByTokenAndPopulateUser = function (bearerToken: string) { + const query = { + where: { + accessToken: bearerToken + }, + include: [ OAuthToken['sequelize'].models.User ] + } + + return OAuthToken.findOne(query).then(function (token) { + if (token) token['user'] = token.User + + return token + }) +} + +getByRefreshTokenAndPopulateUser = function (refreshToken: string) { + const query = { + where: { + refreshToken: refreshToken + }, + include: [ OAuthToken['sequelize'].models.User ] + } + + return OAuthToken.findOne(query).then(function (token) { + token['user'] = token.User + + return token + }) +} + +removeByUserId = function (userId, callback) { + const query = { + where: { + userId: userId + } + } + + return OAuthToken.destroy(query).asCallback(callback) +} -- cgit v1.2.3