aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib
diff options
context:
space:
mode:
authorChocobozzz <chocobozzz@framasoft.org>2020-11-20 15:36:43 +0100
committerChocobozzz <chocobozzz@framasoft.org>2020-11-20 15:36:43 +0100
commit74fd2643b43057c25558b3da79398efe104e2660 (patch)
tree4fd7dd84775780eed82bc4b3caaa328c3526a14c /server/lib
parent8f3ad70874f8769f5340632754dc2ca7f4c82733 (diff)
downloadPeerTube-74fd2643b43057c25558b3da79398efe104e2660.tar.gz
PeerTube-74fd2643b43057c25558b3da79398efe104e2660.tar.zst
PeerTube-74fd2643b43057c25558b3da79398efe104e2660.zip
Provide express request to onLogout call
+ pluginInfo related changes
Diffstat (limited to 'server/lib')
-rw-r--r--server/lib/auth.ts4
-rw-r--r--server/lib/oauth-model.ts10
-rw-r--r--server/lib/plugins/plugin-manager.ts12
3 files changed, 18 insertions, 8 deletions
diff --git a/server/lib/auth.ts b/server/lib/auth.ts
index 3f8e18633..acf0da18a 100644
--- a/server/lib/auth.ts
+++ b/server/lib/auth.ts
@@ -52,7 +52,7 @@ async function handleTokenRevocation (req: express.Request, res: express.Respons
52 const token = res.locals.oauth.token 52 const token = res.locals.oauth.token
53 53
54 res.locals.explicitLogout = true 54 res.locals.explicitLogout = true
55 await revokeToken(token) 55 const result = await revokeToken(token)
56 56
57 // FIXME: uncomment when https://github.com/oauthjs/node-oauth2-server/pull/289 is released 57 // FIXME: uncomment when https://github.com/oauthjs/node-oauth2-server/pull/289 is released
58 // oAuthServer.revoke(req, res, err => { 58 // oAuthServer.revoke(req, res, err => {
@@ -68,7 +68,7 @@ async function handleTokenRevocation (req: express.Request, res: express.Respons
68 // } 68 // }
69 // }) 69 // })
70 70
71 return res.json() 71 return res.json(result)
72} 72}
73 73
74async function onExternalUserAuthenticated (options: { 74async function onExternalUserAuthenticated (options: {
diff --git a/server/lib/oauth-model.ts b/server/lib/oauth-model.ts
index 3273c6c2d..f7ea98b41 100644
--- a/server/lib/oauth-model.ts
+++ b/server/lib/oauth-model.ts
@@ -141,13 +141,15 @@ async function getUser (usernameOrEmail?: string, password?: string) {
141 return user 141 return user
142} 142}
143 143
144async function revokeToken (tokenInfo: { refreshToken: string }) { 144async function revokeToken (tokenInfo: { refreshToken: string }): Promise<{ success: boolean, redirectUrl?: string }> {
145 const res: express.Response = this.request.res 145 const res: express.Response = this.request.res
146 const token = await OAuthTokenModel.getByRefreshTokenAndPopulateUser(tokenInfo.refreshToken) 146 const token = await OAuthTokenModel.getByRefreshTokenAndPopulateUser(tokenInfo.refreshToken)
147 147
148 if (token) { 148 if (token) {
149 let redirectUrl: string
150
149 if (res.locals.explicitLogout === true && token.User.pluginAuth && token.authName) { 151 if (res.locals.explicitLogout === true && token.User.pluginAuth && token.authName) {
150 PluginManager.Instance.onLogout(token.User.pluginAuth, token.authName, token.User) 152 redirectUrl = await PluginManager.Instance.onLogout(token.User.pluginAuth, token.authName, token.User, this.request)
151 } 153 }
152 154
153 clearCacheByToken(token.accessToken) 155 clearCacheByToken(token.accessToken)
@@ -155,10 +157,10 @@ async function revokeToken (tokenInfo: { refreshToken: string }) {
155 token.destroy() 157 token.destroy()
156 .catch(err => logger.error('Cannot destroy token when revoking token.', { err })) 158 .catch(err => logger.error('Cannot destroy token when revoking token.', { err }))
157 159
158 return true 160 return { success: true, redirectUrl }
159 } 161 }
160 162
161 return false 163 return { success: false }
162} 164}
163 165
164async function saveToken (token: TokenInfo, client: OAuthClientModel, user: UserModel) { 166async function saveToken (token: TokenInfo, client: OAuthClientModel, user: UserModel) {
diff --git a/server/lib/plugins/plugin-manager.ts b/server/lib/plugins/plugin-manager.ts
index 94b5ecc41..8e7491257 100644
--- a/server/lib/plugins/plugin-manager.ts
+++ b/server/lib/plugins/plugin-manager.ts
@@ -1,3 +1,4 @@
1import * as express from 'express'
1import { createReadStream, createWriteStream } from 'fs' 2import { createReadStream, createWriteStream } from 'fs'
2import { outputFile, readJSON } from 'fs-extra' 3import { outputFile, readJSON } from 'fs-extra'
3import { basename, join } from 'path' 4import { basename, join } from 'path'
@@ -166,18 +167,25 @@ export class PluginManager implements ServerHook {
166 167
167 // ###################### External events ###################### 168 // ###################### External events ######################
168 169
169 onLogout (npmName: string, authName: string, user: MUser) { 170 async onLogout (npmName: string, authName: string, user: MUser, req: express.Request) {
170 const auth = this.getAuth(npmName, authName) 171 const auth = this.getAuth(npmName, authName)
171 172
172 if (auth?.onLogout) { 173 if (auth?.onLogout) {
173 logger.info('Running onLogout function from auth %s of plugin %s', authName, npmName) 174 logger.info('Running onLogout function from auth %s of plugin %s', authName, npmName)
174 175
175 try { 176 try {
176 auth.onLogout(user) 177 // Force await, in case or onLogout returns a promise
178 const result = await auth.onLogout(user, req)
179
180 return typeof result === 'string'
181 ? result
182 : undefined
177 } catch (err) { 183 } catch (err) {
178 logger.warn('Cannot run onLogout function from auth %s of plugin %s.', authName, npmName, { err }) 184 logger.warn('Cannot run onLogout function from auth %s of plugin %s.', authName, npmName, { err })
179 } 185 }
180 } 186 }
187
188 return undefined
181 } 189 }
182 190
183 onSettingsChanged (name: string, settings: any) { 191 onSettingsChanged (name: string, settings: any) {