diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-05-22 20:58:25 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-05-25 17:32:16 +0200 |
commit | e02643f32e4c97ca307f8fc5b69be79c40d70a3b (patch) | |
tree | b7f6269913cd5a0e4f26a9461a043deb0c168be0 /server/models/oauth-token.ts | |
parent | 65fcc3119c334b75dd13bcfdebf186afdc580a8f (diff) | |
download | PeerTube-e02643f32e4c97ca307f8fc5b69be79c40d70a3b.tar.gz PeerTube-e02643f32e4c97ca307f8fc5b69be79c40d70a3b.tar.zst PeerTube-e02643f32e4c97ca307f8fc5b69be79c40d70a3b.zip |
Type models
Diffstat (limited to 'server/models/oauth-token.ts')
-rw-r--r-- | server/models/oauth-token.ts | 75 |
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 @@ | |||
1 | import * as Sequelize from 'sequelize' | ||
2 | |||
1 | import { logger } from '../helpers' | 3 | import { logger } from '../helpers' |
2 | 4 | ||
3 | // --------------------------------------------------------------------------- | 5 | import { addMethodsToModel } from './utils' |
6 | import { | ||
7 | OAuthTokenClass, | ||
8 | OAuthTokenInstance, | ||
9 | OAuthTokenAttributes, | ||
10 | |||
11 | OAuthTokenMethods | ||
12 | } from './oauth-token-interface' | ||
13 | |||
14 | let OAuthToken: Sequelize.Model<OAuthTokenInstance, OAuthTokenAttributes> | ||
15 | let getByRefreshTokenAndPopulateClient: OAuthTokenMethods.GetByRefreshTokenAndPopulateClient | ||
16 | let getByTokenAndPopulateUser: OAuthTokenMethods.GetByTokenAndPopulateUser | ||
17 | let getByRefreshTokenAndPopulateUser: OAuthTokenMethods.GetByRefreshTokenAndPopulateUser | ||
18 | let removeByUserId: OAuthTokenMethods.RemoveByUserId | ||
4 | 19 | ||
5 | module.exports = function (sequelize, DataTypes) { | 20 | export 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 | ||
58 | function associate (models) { | 75 | function 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 | ||
76 | function getByRefreshTokenAndPopulateClient (refreshToken) { | 93 | getByRefreshTokenAndPopulateClient = 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 | ||
104 | function getByTokenAndPopulateUser (bearerToken) { | 121 | getByTokenAndPopulateUser = 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 | ||
119 | function getByRefreshTokenAndPopulateUser (refreshToken) { | 136 | getByRefreshTokenAndPopulateUser = 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 | ||
134 | function removeByUserId (userId, callback) { | 151 | removeByUserId = 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 | } |