aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/oauth/oauth-token.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/models/oauth/oauth-token.ts')
-rw-r--r--server/models/oauth/oauth-token.ts259
1 files changed, 129 insertions, 130 deletions
diff --git a/server/models/oauth/oauth-token.ts b/server/models/oauth/oauth-token.ts
index a82bff130..0d21c42fd 100644
--- a/server/models/oauth/oauth-token.ts
+++ b/server/models/oauth/oauth-token.ts
@@ -1,164 +1,163 @@
1import * as Sequelize from 'sequelize' 1import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript'
2
3import { logger } from '../../helpers' 2import { logger } from '../../helpers'
3import { AccountModel } from '../account/account'
4import { UserModel } from '../account/user'
5import { OAuthClientModel } from './oauth-client'
6
7export type OAuthTokenInfo = {
8 refreshToken: string
9 refreshTokenExpiresAt: Date,
10 client: {
11 id: number
12 },
13 user: {
14 id: number
15 }
16}
4 17
5import { addMethodsToModel } from '../utils' 18@Table({
6import { OAuthTokenAttributes, OAuthTokenInfo, OAuthTokenInstance, OAuthTokenMethods } from './oauth-token-interface' 19 tableName: 'oAuthToken',
7 20 indexes: [
8let OAuthToken: Sequelize.Model<OAuthTokenInstance, OAuthTokenAttributes>
9let getByRefreshTokenAndPopulateClient: OAuthTokenMethods.GetByRefreshTokenAndPopulateClient
10let getByTokenAndPopulateUser: OAuthTokenMethods.GetByTokenAndPopulateUser
11let getByRefreshTokenAndPopulateUser: OAuthTokenMethods.GetByRefreshTokenAndPopulateUser
12
13export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
14 OAuthToken = sequelize.define<OAuthTokenInstance, OAuthTokenAttributes>('OAuthToken',
15 { 21 {
16 accessToken: { 22 fields: [ 'refreshToken' ],
17 type: DataTypes.STRING, 23 unique: true
18 allowNull: false
19 },
20 accessTokenExpiresAt: {
21 type: DataTypes.DATE,
22 allowNull: false
23 },
24 refreshToken: {
25 type: DataTypes.STRING,
26 allowNull: false
27 },
28 refreshTokenExpiresAt: {
29 type: DataTypes.DATE,
30 allowNull: false
31 }
32 }, 24 },
33 { 25 {
34 indexes: [ 26 fields: [ 'accessToken' ],
35 { 27 unique: true
36 fields: [ 'refreshToken' ], 28 },
37 unique: true 29 {
38 }, 30 fields: [ 'userId' ]
39 { 31 },
40 fields: [ 'accessToken' ], 32 {
41 unique: true 33 fields: [ 'oAuthClientId' ]
42 },
43 {
44 fields: [ 'userId' ]
45 },
46 {
47 fields: [ 'oAuthClientId' ]
48 }
49 ]
50 } 34 }
51 ) 35 ]
36})
37export class OAuthTokenModel extends Model<OAuthTokenModel> {
52 38
53 const classMethods = [ 39 @AllowNull(false)
54 associate, 40 @Column
41 accessToken: string
55 42
56 getByRefreshTokenAndPopulateClient, 43 @AllowNull(false)
57 getByTokenAndPopulateUser, 44 @Column
58 getByRefreshTokenAndPopulateUser 45 accessTokenExpiresAt: Date
59 ]
60 addMethodsToModel(OAuthToken, classMethods)
61 46
62 return OAuthToken 47 @AllowNull(false)
63} 48 @Column
49 refreshToken: string
64 50
65// --------------------------------------------------------------------------- 51 @AllowNull(false)
52 @Column
53 refreshTokenExpiresAt: Date
66 54
67function associate (models) { 55 @CreatedAt
68 OAuthToken.belongsTo(models.User, { 56 createdAt: Date
57
58 @UpdatedAt
59 updatedAt: Date
60
61 @ForeignKey(() => UserModel)
62 @Column
63 userId: number
64
65 @BelongsTo(() => UserModel, {
69 foreignKey: { 66 foreignKey: {
70 name: 'userId',
71 allowNull: false 67 allowNull: false
72 }, 68 },
73 onDelete: 'cascade' 69 onDelete: 'cascade'
74 }) 70 })
71 User: UserModel
75 72
76 OAuthToken.belongsTo(models.OAuthClient, { 73 @ForeignKey(() => OAuthClientModel)
74 @Column
75 oAuthClientId: number
76
77 @BelongsTo(() => OAuthClientModel, {
77 foreignKey: { 78 foreignKey: {
78 name: 'oAuthClientId',
79 allowNull: false 79 allowNull: false
80 }, 80 },
81 onDelete: 'cascade' 81 onDelete: 'cascade'
82 }) 82 })
83} 83 OAuthClients: OAuthClientModel[]
84 84
85getByRefreshTokenAndPopulateClient = function (refreshToken: string) { 85 static getByRefreshTokenAndPopulateClient (refreshToken: string) {
86 const query = { 86 const query = {
87 where: { 87 where: {
88 refreshToken: refreshToken 88 refreshToken: refreshToken
89 }, 89 },
90 include: [ OAuthToken['sequelize'].models.OAuthClient ] 90 include: [ OAuthClientModel ]
91 }
92
93 return OAuthTokenModel.findOne(query)
94 .then(token => {
95 if (!token) return null
96
97 return {
98 refreshToken: token.refreshToken,
99 refreshTokenExpiresAt: token.refreshTokenExpiresAt,
100 client: {
101 id: token.oAuthClientId
102 },
103 user: {
104 id: token.userId
105 }
106 } as OAuthTokenInfo
107 })
108 .catch(err => {
109 logger.info('getRefreshToken error.', err)
110 throw err
111 })
91 } 112 }
92 113
93 return OAuthToken.findOne(query) 114 static getByTokenAndPopulateUser (bearerToken: string) {
94 .then(token => { 115 const query = {
95 if (!token) return null 116 where: {
96 117 accessToken: bearerToken
97 const tokenInfos: OAuthTokenInfo = { 118 },
98 refreshToken: token.refreshToken, 119 include: [
99 refreshTokenExpiresAt: token.refreshTokenExpiresAt, 120 {
100 client: { 121 model: UserModel,
101 id: token.oAuthClientId 122 include: [
102 }, 123 {
103 user: { 124 model: AccountModel,
104 id: token.userId 125 required: true
126 }
127 ]
105 } 128 }
106 } 129 ]
130 }
107 131
108 return tokenInfos 132 return OAuthTokenModel.findOne(query).then(token => {
109 }) 133 if (token) token['user'] = token.User
110 .catch(err => {
111 logger.info('getRefreshToken error.', err)
112 throw err
113 })
114}
115 134
116getByTokenAndPopulateUser = function (bearerToken: string) { 135 return token
117 const query = { 136 })
118 where: {
119 accessToken: bearerToken
120 },
121 include: [
122 {
123 model: OAuthToken['sequelize'].models.User,
124 include: [
125 {
126 model: OAuthToken['sequelize'].models.Account,
127 required: true
128 }
129 ]
130 }
131 ]
132 } 137 }
133 138
134 return OAuthToken.findOne(query).then(token => { 139 static getByRefreshTokenAndPopulateUser (refreshToken: string) {
135 if (token) token['user'] = token.User 140 const query = {
141 where: {
142 refreshToken: refreshToken
143 },
144 include: [
145 {
146 model: UserModel,
147 include: [
148 {
149 model: AccountModel,
150 required: true
151 }
152 ]
153 }
154 ]
155 }
136 156
137 return token 157 return OAuthTokenModel.findOne(query).then(token => {
138 }) 158 token['user'] = token.User
139}
140 159
141getByRefreshTokenAndPopulateUser = function (refreshToken: string) { 160 return token
142 const query = { 161 })
143 where: {
144 refreshToken: refreshToken
145 },
146 include: [
147 {
148 model: OAuthToken['sequelize'].models.User,
149 include: [
150 {
151 model: OAuthToken['sequelize'].models.Account,
152 required: true
153 }
154 ]
155 }
156 ]
157 } 162 }
158
159 return OAuthToken.findOne(query).then(token => {
160 token['user'] = token.User
161
162 return token
163 })
164} 163}