mirror of
https://github.com/overleaf/overleaf.git
synced 2025-04-22 21:09:42 +00:00
Merge pull request #2689 from overleaf/ta-pre-finish-login-hook
Add preFinishLogin Hook GitOrigin-RevId: 8379e0643866feef95c648a2db4d8665420e615b
This commit is contained in:
parent
694267603a
commit
05a2529881
2 changed files with 88 additions and 23 deletions
services/web
app/src/Features/Authentication
test/unit/src/Authentication
|
@ -113,31 +113,46 @@ const AuthenticationController = (module.exports = {
|
|||
if (user === false) {
|
||||
return res.redirect('/login')
|
||||
} // OAuth2 'state' mismatch
|
||||
if (user.must_reconfirm) {
|
||||
return AuthenticationController._redirectToReconfirmPage(req, res, user)
|
||||
}
|
||||
const redir =
|
||||
AuthenticationController._getRedirectFromSession(req) || '/project'
|
||||
AuthenticationController._loginAsyncHandlers(req, user)
|
||||
AuthenticationController.afterLoginSessionSetup(req, user, function(err) {
|
||||
if (err) {
|
||||
return next(err)
|
||||
|
||||
const Modules = require('../../infrastructure/Modules')
|
||||
Modules.hooks.fire('preFinishLogin', req, res, user, function(
|
||||
error,
|
||||
results
|
||||
) {
|
||||
if (error) {
|
||||
return next(error)
|
||||
}
|
||||
SudoModeHandler.activateSudoMode(user._id, function(err) {
|
||||
if (results.some(result => result && result.doNotFinish)) {
|
||||
return next()
|
||||
}
|
||||
|
||||
if (user.must_reconfirm) {
|
||||
return AuthenticationController._redirectToReconfirmPage(req, res, user)
|
||||
}
|
||||
|
||||
const redir =
|
||||
AuthenticationController._getRedirectFromSession(req) || '/project'
|
||||
AuthenticationController._loginAsyncHandlers(req, user)
|
||||
AuthenticationController.afterLoginSessionSetup(req, user, function(err) {
|
||||
if (err) {
|
||||
logger.err(
|
||||
{ err, user_id: user._id },
|
||||
'Error activating Sudo Mode on login, continuing'
|
||||
)
|
||||
}
|
||||
AuthenticationController._clearRedirectFromSession(req)
|
||||
if (
|
||||
_.get(req, ['headers', 'accept'], '').match(/^application\/json.*$/)
|
||||
) {
|
||||
res.json({ redir })
|
||||
} else {
|
||||
res.redirect(redir)
|
||||
return next(err)
|
||||
}
|
||||
SudoModeHandler.activateSudoMode(user._id, function(err) {
|
||||
if (err) {
|
||||
logger.err(
|
||||
{ err, user_id: user._id },
|
||||
'Error activating Sudo Mode on login, continuing'
|
||||
)
|
||||
}
|
||||
AuthenticationController._clearRedirectFromSession(req)
|
||||
if (
|
||||
_.get(req, ['headers', 'accept'], '').match(/^application\/json.*$/)
|
||||
) {
|
||||
res.json({ redir })
|
||||
} else {
|
||||
res.redirect(redir)
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
|
|
|
@ -58,7 +58,7 @@ describe('AuthenticationController', function() {
|
|||
revokeAllUserSessions: sinon.stub().callsArgWith(1, null)
|
||||
}),
|
||||
'../../infrastructure/Modules': (this.Modules = {
|
||||
hooks: { fire: sinon.stub().callsArgWith(2, null, []) }
|
||||
hooks: { fire: sinon.stub().yields(null, []) }
|
||||
}),
|
||||
'../SudoMode/SudoModeHandler': (this.SudoModeHandler = {
|
||||
activateSudoMode: sinon.stub().callsArgWith(1, null)
|
||||
|
@ -1210,5 +1210,55 @@ describe('AuthenticationController', function() {
|
|||
).to.equal(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('preFinishLogin hook', function() {
|
||||
it('call hook and proceed', function() {
|
||||
this.Modules.hooks.fire = sinon.stub().yields(null, [])
|
||||
this.AuthenticationController.finishLogin(
|
||||
this.user,
|
||||
this.req,
|
||||
this.res,
|
||||
this.next
|
||||
)
|
||||
sinon.assert.calledWith(
|
||||
this.Modules.hooks.fire,
|
||||
'preFinishLogin',
|
||||
this.req,
|
||||
this.res,
|
||||
this.user
|
||||
)
|
||||
expect(this.res.json.callCount).to.equal(1)
|
||||
})
|
||||
|
||||
it('stop if hook has redirected', function(done) {
|
||||
this.Modules.hooks.fire = sinon
|
||||
.stub()
|
||||
.yields(null, [{ doNotFinish: true }])
|
||||
this.AuthenticationController.finishLogin(
|
||||
this.user,
|
||||
this.req,
|
||||
this.res,
|
||||
error => {
|
||||
expect(error).to.not.exist
|
||||
expect(this.res.json.callCount).to.equal(0)
|
||||
done()
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
it('call next with hook errors', function(done) {
|
||||
this.Modules.hooks.fire = sinon.stub().yields(new Error())
|
||||
this.AuthenticationController.finishLogin(
|
||||
this.user,
|
||||
this.req,
|
||||
this.res,
|
||||
error => {
|
||||
expect(error).to.exist
|
||||
expect(this.res.json.callCount).to.equal(0)
|
||||
done()
|
||||
}
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Add table
Reference in a new issue