aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/oauth-token.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/models/oauth-token.ts')
-rw-r--r--server/models/oauth-token.ts75
1 files changed, 46 insertions, 29 deletions
diff --git a/server/models/oauth-token.ts b/server/models/oauth-token.ts
index 74c9180eb..567df1c12 100644
--- a/server/models/oauth-token.ts
+++ b/server/models/oauth-token.ts
@@ -1,9 +1,24 @@
1import * as Sequelize from 'sequelize'
2
1import { logger } from '../helpers' 3import { logger } from '../helpers'
2 4
3// --------------------------------------------------------------------------- 5import { addMethodsToModel } from './utils'
6import {
7 OAuthTokenClass,
8 OAuthTokenInstance,
9 OAuthTokenAttributes,
10
11 OAuthTokenMethods
12} from './oauth-token-interface'
13
14let OAuthToken: Sequelize.Model<OAuthTokenInstance, OAuthTokenAttributes>
15let getByRefreshTokenAndPopulateClient: OAuthTokenMethods.GetByRefreshTokenAndPopulateClient
16let getByTokenAndPopulateUser: OAuthTokenMethods.GetByTokenAndPopulateUser
17let getByRefreshTokenAndPopulateUser: OAuthTokenMethods.GetByRefreshTokenAndPopulateUser
18let removeByUserId: OAuthTokenMethods.RemoveByUserId
4 19
5module.exports = function (sequelize, DataTypes) { 20export default function (sequelize, DataTypes) {
6 const OAuthToken = sequelize.define('OAuthToken', 21 OAuthToken = sequelize.define('OAuthToken',
7 { 22 {
8 accessToken: { 23 accessToken: {
9 type: DataTypes.STRING, 24 type: DataTypes.STRING,
@@ -38,25 +53,27 @@ module.exports = function (sequelize, DataTypes) {
38 { 53 {
39 fields: [ 'oAuthClientId' ] 54 fields: [ 'oAuthClientId' ]
40 } 55 }
41 ], 56 ]
42 classMethods: {
43 associate,
44
45 getByRefreshTokenAndPopulateClient,
46 getByTokenAndPopulateUser,
47 getByRefreshTokenAndPopulateUser,
48 removeByUserId
49 }
50 } 57 }
51 ) 58 )
52 59
60 const classMethods = [
61 associate,
62
63 getByRefreshTokenAndPopulateClient,
64 getByTokenAndPopulateUser,
65 getByRefreshTokenAndPopulateUser,
66 removeByUserId
67 ]
68 addMethodsToModel(OAuthToken, classMethods)
69
53 return OAuthToken 70 return OAuthToken
54} 71}
55 72
56// --------------------------------------------------------------------------- 73// ---------------------------------------------------------------------------
57 74
58function associate (models) { 75function associate (models) {
59 this.belongsTo(models.User, { 76 OAuthToken.belongsTo(models.User, {
60 foreignKey: { 77 foreignKey: {
61 name: 'userId', 78 name: 'userId',
62 allowNull: false 79 allowNull: false
@@ -64,7 +81,7 @@ function associate (models) {
64 onDelete: 'cascade' 81 onDelete: 'cascade'
65 }) 82 })
66 83
67 this.belongsTo(models.OAuthClient, { 84 OAuthToken.belongsTo(models.OAuthClient, {
68 foreignKey: { 85 foreignKey: {
69 name: 'oAuthClientId', 86 name: 'oAuthClientId',
70 allowNull: false 87 allowNull: false
@@ -73,25 +90,25 @@ function associate (models) {
73 }) 90 })
74} 91}
75 92
76function getByRefreshTokenAndPopulateClient (refreshToken) { 93getByRefreshTokenAndPopulateClient = function (refreshToken) {
77 const query = { 94 const query = {
78 where: { 95 where: {
79 refreshToken: refreshToken 96 refreshToken: refreshToken
80 }, 97 },
81 include: [ this.associations.OAuthClient ] 98 include: [ OAuthToken['sequelize'].models.OAuthClient ]
82 } 99 }
83 100
84 return this.findOne(query).then(function (token) { 101 return OAuthToken.findOne(query).then(function (token) {
85 if (!token) return token 102 if (!token) return token
86 103
87 const tokenInfos = { 104 const tokenInfos = {
88 refreshToken: token.refreshToken, 105 refreshToken: token.refreshToken,
89 refreshTokenExpiresAt: token.refreshTokenExpiresAt, 106 refreshTokenExpiresAt: token.refreshTokenExpiresAt,
90 client: { 107 client: {
91 id: token.client.id 108 id: token['client'].id
92 }, 109 },
93 user: { 110 user: {
94 id: token.user 111 id: token['user']
95 } 112 }
96 } 113 }
97 114
@@ -101,42 +118,42 @@ function getByRefreshTokenAndPopulateClient (refreshToken) {
101 }) 118 })
102} 119}
103 120
104function getByTokenAndPopulateUser (bearerToken) { 121getByTokenAndPopulateUser = function (bearerToken) {
105 const query = { 122 const query = {
106 where: { 123 where: {
107 accessToken: bearerToken 124 accessToken: bearerToken
108 }, 125 },
109 include: [ this.sequelize.models.User ] 126 include: [ OAuthToken['sequelize'].models.User ]
110 } 127 }
111 128
112 return this.findOne(query).then(function (token) { 129 return OAuthToken.findOne(query).then(function (token) {
113 if (token) token.user = token.User 130 if (token) token['user'] = token.User
114 131
115 return token 132 return token
116 }) 133 })
117} 134}
118 135
119function getByRefreshTokenAndPopulateUser (refreshToken) { 136getByRefreshTokenAndPopulateUser = function (refreshToken) {
120 const query = { 137 const query = {
121 where: { 138 where: {
122 refreshToken: refreshToken 139 refreshToken: refreshToken
123 }, 140 },
124 include: [ this.sequelize.models.User ] 141 include: [ OAuthToken['sequelize'].models.User ]
125 } 142 }
126 143
127 return this.findOne(query).then(function (token) { 144 return OAuthToken.findOne(query).then(function (token) {
128 token.user = token.User 145 token['user'] = token.User
129 146
130 return token 147 return token
131 }) 148 })
132} 149}
133 150
134function removeByUserId (userId, callback) { 151removeByUserId = function (userId, callback) {
135 const query = { 152 const query = {
136 where: { 153 where: {
137 userId: userId 154 userId: userId
138 } 155 }
139 } 156 }
140 157
141 return this.destroy(query).asCallback(callback) 158 return OAuthToken.destroy(query).asCallback(callback)
142} 159}