diff options
author | Chocobozzz <me@florianbigard.com> | 2020-04-24 11:33:01 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2020-05-04 16:21:39 +0200 |
commit | e307e4fce39853d445d086f92b8c556c363ee15d (patch) | |
tree | 0f3faaf3c73222db0fb55b72260c787aeeeb05eb /server/models/oauth | |
parent | e1c5503114deef954731904695cd40dccfcef555 (diff) | |
download | PeerTube-e307e4fce39853d445d086f92b8c556c363ee15d.tar.gz PeerTube-e307e4fce39853d445d086f92b8c556c363ee15d.tar.zst PeerTube-e307e4fce39853d445d086f92b8c556c363ee15d.zip |
Add ability for auth plugins to hook tokens validity
Diffstat (limited to 'server/models/oauth')
-rw-r--r-- | server/models/oauth/oauth-token.ts | 55 |
1 files changed, 33 insertions, 22 deletions
diff --git a/server/models/oauth/oauth-token.ts b/server/models/oauth/oauth-token.ts index e73c4be7d..3541b6103 100644 --- a/server/models/oauth/oauth-token.ts +++ b/server/models/oauth/oauth-token.ts | |||
@@ -30,6 +30,7 @@ export type OAuthTokenInfo = { | |||
30 | user: { | 30 | user: { |
31 | id: number | 31 | id: number |
32 | } | 32 | } |
33 | token: MOAuthTokenUser | ||
33 | } | 34 | } |
34 | 35 | ||
35 | enum ScopeNames { | 36 | enum ScopeNames { |
@@ -136,33 +137,43 @@ export class OAuthTokenModel extends Model<OAuthTokenModel> { | |||
136 | return clearCacheByToken(token.accessToken) | 137 | return clearCacheByToken(token.accessToken) |
137 | } | 138 | } |
138 | 139 | ||
140 | static loadByRefreshToken (refreshToken: string) { | ||
141 | const query = { | ||
142 | where: { refreshToken } | ||
143 | } | ||
144 | |||
145 | return OAuthTokenModel.findOne(query) | ||
146 | } | ||
147 | |||
139 | static getByRefreshTokenAndPopulateClient (refreshToken: string) { | 148 | static getByRefreshTokenAndPopulateClient (refreshToken: string) { |
140 | const query = { | 149 | const query = { |
141 | where: { | 150 | where: { |
142 | refreshToken: refreshToken | 151 | refreshToken |
143 | }, | 152 | }, |
144 | include: [ OAuthClientModel ] | 153 | include: [ OAuthClientModel ] |
145 | } | 154 | } |
146 | 155 | ||
147 | return OAuthTokenModel.findOne(query) | 156 | return OAuthTokenModel.scope(ScopeNames.WITH_USER) |
148 | .then(token => { | 157 | .findOne(query) |
149 | if (!token) return null | 158 | .then(token => { |
150 | 159 | if (!token) return null | |
151 | return { | 160 | |
152 | refreshToken: token.refreshToken, | 161 | return { |
153 | refreshTokenExpiresAt: token.refreshTokenExpiresAt, | 162 | refreshToken: token.refreshToken, |
154 | client: { | 163 | refreshTokenExpiresAt: token.refreshTokenExpiresAt, |
155 | id: token.oAuthClientId | 164 | client: { |
156 | }, | 165 | id: token.oAuthClientId |
157 | user: { | 166 | }, |
158 | id: token.userId | 167 | user: { |
159 | } | 168 | id: token.userId |
160 | } as OAuthTokenInfo | 169 | }, |
161 | }) | 170 | token |
162 | .catch(err => { | 171 | } as OAuthTokenInfo |
163 | logger.error('getRefreshToken error.', { err }) | 172 | }) |
164 | throw err | 173 | .catch(err => { |
165 | }) | 174 | logger.error('getRefreshToken error.', { err }) |
175 | throw err | ||
176 | }) | ||
166 | } | 177 | } |
167 | 178 | ||
168 | static getByTokenAndPopulateUser (bearerToken: string): Bluebird<MOAuthTokenUser> { | 179 | static getByTokenAndPopulateUser (bearerToken: string): Bluebird<MOAuthTokenUser> { |
@@ -184,14 +195,14 @@ export class OAuthTokenModel extends Model<OAuthTokenModel> { | |||
184 | static getByRefreshTokenAndPopulateUser (refreshToken: string): Bluebird<MOAuthTokenUser> { | 195 | static getByRefreshTokenAndPopulateUser (refreshToken: string): Bluebird<MOAuthTokenUser> { |
185 | const query = { | 196 | const query = { |
186 | where: { | 197 | where: { |
187 | refreshToken: refreshToken | 198 | refreshToken |
188 | } | 199 | } |
189 | } | 200 | } |
190 | 201 | ||
191 | return OAuthTokenModel.scope(ScopeNames.WITH_USER) | 202 | return OAuthTokenModel.scope(ScopeNames.WITH_USER) |
192 | .findOne(query) | 203 | .findOne(query) |
193 | .then(token => { | 204 | .then(token => { |
194 | if (!token) return new OAuthTokenModel() | 205 | if (!token) return undefined |
195 | 206 | ||
196 | return Object.assign(token, { user: token.User }) | 207 | return Object.assign(token, { user: token.User }) |
197 | }) | 208 | }) |