■ ■ ■ ■ ■ ■
opencti-platform/opencti-graphql/src/http/httpPlatform.js
| skipped 239 lines |
240 | 240 | | |
241 | 241 | | // -- Passport callback |
242 | 242 | | const urlencodedParser = bodyParser.urlencoded({ extended: true }); |
243 | | - | app.all(`${basePath}/auth/:provider/callback`, urlencodedParser, passport.initialize({}), (req, res, next) => { |
| 243 | + | app.all(`${basePath}/auth/:provider/callback`, urlencodedParser, passport.initialize({}), async (req, res, next) => { |
244 | 244 | | const { referer } = req.session; |
245 | | - | try { |
246 | | - | const { provider } = req.params; |
247 | | - | const context = executionContext(`${provider}_strategy`); |
248 | | - | passport.authenticate(provider, {}, async (err, user) => { |
| 245 | + | const { provider } = req.params; |
| 246 | + | const callbackLogin = () => new Promise((accept, reject) => { |
| 247 | + | passport.authenticate(provider, {}, (err, user) => { |
249 | 248 | | if (err || !user) { |
250 | | - | logAudit.error(userWithOrigin(req, {}), LOGIN_ACTION, { provider, error: err?.message }); |
251 | | - | setCookieError(res, err?.message); |
252 | | - | res.redirect(referer ?? '/'); |
| 249 | + | reject(err); |
| 250 | + | } else { |
| 251 | + | accept(user); |
253 | 252 | | } |
254 | | - | // noinspection UnnecessaryLocalVariableJS |
255 | | - | await authenticateUser(context, req, user, provider); |
256 | | - | req.session.referer = null; |
257 | | - | res.redirect(referer ?? '/'); |
258 | 253 | | })(req, res, next); |
259 | | - | } catch (e) { |
260 | | - | setCookieError(res, e?.message); |
| 254 | + | }); |
| 255 | + | try { |
| 256 | + | const context = executionContext(`${provider}_strategy`); |
| 257 | + | const logged = await callbackLogin(); |
| 258 | + | await authenticateUser(context, req, logged, provider); |
| 259 | + | } catch (err) { |
| 260 | + | logAudit.error(userWithOrigin(req, {}), LOGIN_ACTION, { provider, error: err?.message }); |
| 261 | + | setCookieError(res, 'Invalid authentication, please ask your administrator'); |
| 262 | + | } finally { |
261 | 263 | | res.redirect(referer ?? '/'); |
262 | 264 | | } |
263 | 265 | | }); |
| skipped 33 lines |