| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483 |
- const sinon = require('sinon')
- const { expect } = require('chai')
- const modulePath =
- '../../../../app/src/Features/Authentication/AuthenticationController.js'
- const SandboxedModule = require('sandboxed-module')
- const tk = require('timekeeper')
- const MockRequest = require('../helpers/MockRequest')
- const MockResponse = require('../helpers/MockResponse')
- const { ObjectId } = require('mongodb')
- const AuthenticationErrors = require('../../../../app/src/Features/Authentication/AuthenticationErrors')
- describe('AuthenticationController', function () {
- beforeEach(function () {
- tk.freeze(Date.now())
- this.UserModel = { findOne: sinon.stub() }
- this.httpAuthUsers = {
- 'valid-test-user': Math.random().toString(16).slice(2),
- }
- this.user = {
- _id: new ObjectId(),
- email: (this.email = 'USER@example.com'),
- first_name: 'bob',
- last_name: 'brown',
- referal_id: 1234,
- isAdmin: false,
- }
- this.staffUser = {
- ...this.user,
- staffAccess: {
- publisherMetrics: true,
- publisherManagement: false,
- institutionMetrics: true,
- institutionManagement: false,
- groupMetrics: true,
- groupManagement: false,
- adminMetrics: true,
- splitTestMetrics: false,
- splitTestManagement: true,
- },
- }
- this.noStaffAccessUser = {
- ...this.user,
- staffAccess: {
- publisherMetrics: false,
- publisherManagement: false,
- institutionMetrics: false,
- institutionManagement: false,
- groupMetrics: false,
- groupManagement: false,
- adminMetrics: false,
- splitTestMetrics: false,
- splitTestManagement: false,
- },
- }
- this.password = 'banana'
- this.req = new MockRequest()
- this.res = new MockResponse()
- this.callback = sinon.stub()
- this.next = sinon.stub()
- this.req.session.analyticsId = 'abc-123'
- this.AuthenticationController = SandboxedModule.require(modulePath, {
- requires: {
- '../Helpers/AdminAuthorizationHelper': (this.AdminAuthorizationHelper =
- {
- hasAdminAccess: sinon.stub().returns(false),
- }),
- './AuthenticationErrors': AuthenticationErrors,
- '../User/UserAuditLogHandler': (this.UserAuditLogHandler = {
- addEntry: sinon.stub().yields(null),
- }),
- '../Helpers/AsyncFormHelper': (this.AsyncFormHelper = {
- redirect: sinon.stub(),
- }),
- '../../infrastructure/RequestContentTypeDetection': {
- acceptsJson: (this.acceptsJson = sinon.stub().returns(false)),
- },
- './AuthenticationManager': (this.AuthenticationManager = {}),
- '../User/UserUpdater': (this.UserUpdater = {
- updateUser: sinon.stub(),
- }),
- '@overleaf/metrics': (this.Metrics = { inc: sinon.stub() }),
- '../Security/LoginRateLimiter': (this.LoginRateLimiter = {
- processLoginRequest: sinon.stub(),
- recordSuccessfulLogin: sinon.stub(),
- }),
- '../User/UserHandler': (this.UserHandler = {
- setupLoginData: sinon.stub(),
- }),
- '../Analytics/AnalyticsManager': (this.AnalyticsManager = {
- recordEventForUser: sinon.stub(),
- identifyUser: sinon.stub(),
- getIdsFromSession: sinon.stub().returns({ userId: this.user._id }),
- }),
- '@overleaf/settings': (this.Settings = {
- siteUrl: 'http://www.foo.bar',
- httpAuthUsers: this.httpAuthUsers,
- elevateAccountSecurityAfterFailedLogin: 90 * 24 * 60 * 60 * 1000,
- }),
- passport: (this.passport = {
- authenticate: sinon.stub().returns(sinon.stub()),
- }),
- '../User/UserSessionsManager': (this.UserSessionsManager = {
- trackSession: sinon.stub(),
- untrackSession: sinon.stub(),
- revokeAllUserSessions: sinon.stub().yields(null),
- }),
- '../../infrastructure/Modules': (this.Modules = {
- hooks: { fire: sinon.stub().yields(null, []) },
- }),
- '../Notifications/NotificationsBuilder': (this.NotificationsBuilder = {
- ipMatcherAffiliation: sinon.stub().returns({ create: sinon.stub() }),
- }),
- '../../models/User': { User: this.UserModel },
- '../../../../modules/oauth2-server/app/src/Oauth2Server':
- (this.Oauth2Server = {
- Request: sinon.stub(),
- Response: sinon.stub(),
- server: {
- authenticate: sinon.stub(),
- },
- }),
- '../Helpers/UrlHelper': (this.UrlHelper = {
- getSafeRedirectPath: sinon.stub(),
- }),
- './SessionManager': (this.SessionManager = {
- isUserLoggedIn: sinon.stub().returns(true),
- getSessionUser: sinon.stub().returns(this.user),
- }),
- },
- })
- this.UrlHelper.getSafeRedirectPath
- .withArgs('https://evil.com')
- .returns(undefined)
- this.UrlHelper.getSafeRedirectPath.returnsArg(0)
- })
- afterEach(function () {
- tk.reset()
- })
- describe('validateAdmin', function () {
- beforeEach(function () {
- this.Settings.adminDomains = ['good.example.com']
- this.goodAdmin = {
- email: 'alice@good.example.com',
- isAdmin: true,
- }
- this.badAdmin = {
- email: 'beatrice@bad.example.com',
- isAdmin: true,
- }
- this.normalUser = {
- email: 'claire@whatever.example.com',
- isAdmin: false,
- }
- })
- it('should skip when adminDomains are not configured', function (done) {
- this.Settings.adminDomains = []
- this.SessionManager.getSessionUser = sinon.stub().returns(this.normalUser)
- this.AuthenticationController.validateAdmin(this.req, this.res, err => {
- this.SessionManager.getSessionUser.called.should.equal(false)
- expect(err).to.not.exist
- done()
- })
- })
- it('should skip non-admin user', function (done) {
- this.SessionManager.getSessionUser = sinon.stub().returns(this.normalUser)
- this.AuthenticationController.validateAdmin(this.req, this.res, err => {
- this.SessionManager.getSessionUser.called.should.equal(true)
- expect(err).to.not.exist
- done()
- })
- })
- it('should permit an admin with the right doman', function (done) {
- this.SessionManager.getSessionUser = sinon.stub().returns(this.goodAdmin)
- this.AuthenticationController.validateAdmin(this.req, this.res, err => {
- this.SessionManager.getSessionUser.called.should.equal(true)
- expect(err).to.not.exist
- done()
- })
- })
- it('should block an admin with a missing email', function (done) {
- this.SessionManager.getSessionUser = sinon
- .stub()
- .returns({ isAdmin: true })
- this.AdminAuthorizationHelper.hasAdminAccess.returns(true)
- this.AuthenticationController.validateAdmin(this.req, this.res, err => {
- this.SessionManager.getSessionUser.called.should.equal(true)
- expect(err).to.exist
- done()
- })
- })
- it('should block an admin with a bad domain', function (done) {
- this.SessionManager.getSessionUser = sinon.stub().returns(this.badAdmin)
- this.AdminAuthorizationHelper.hasAdminAccess.returns(true)
- this.AuthenticationController.validateAdmin(this.req, this.res, err => {
- this.SessionManager.getSessionUser.called.should.equal(true)
- expect(err).to.exist
- done()
- })
- })
- })
- describe('serializeUser', function () {
- describe('when isAdmin is false', function () {
- it('does not return an isAdmin field', function () {
- const isAdminMatcher = sinon.match(value => {
- return !('isAdmin' in value)
- })
- this.AuthenticationController.serializeUser(this.user, this.callback)
- expect(this.callback).to.have.been.calledWith(null, isAdminMatcher)
- })
- })
- describe('when staffAccess fields are provided', function () {
- it('only returns the fields set to true', function () {
- const expectedStaffAccess = {
- publisherMetrics: true,
- institutionMetrics: true,
- groupMetrics: true,
- adminMetrics: true,
- splitTestManagement: true,
- }
- const staffAccessMatcher = sinon.match(value => {
- return (
- Object.keys(value.staffAccess).length ===
- Object.keys(expectedStaffAccess).length
- )
- })
- this.AuthenticationController.serializeUser(
- this.staffUser,
- this.callback
- )
- expect(this.callback).to.have.been.calledWith(null, staffAccessMatcher)
- })
- })
- describe('when all staffAccess fields are false', function () {
- it('no staffAccess attribute is set', function () {
- const staffAccessMatcher = sinon.match(value => {
- return !('staffAccess' in value)
- })
- this.AuthenticationController.serializeUser(
- this.noStaffAccessUser,
- this.callback
- )
- expect(this.callback).to.have.been.calledWith(null, staffAccessMatcher)
- })
- })
- })
- describe('passportLogin', function () {
- beforeEach(function () {
- this.info = null
- this.req.login = sinon.stub().yields(null)
- this.res.json = sinon.stub()
- this.req.session = {
- passport: { user: this.user },
- postLoginRedirect: '/path/to/redir/to',
- }
- this.req.session.destroy = sinon.stub().yields(null)
- this.req.session.save = sinon.stub().yields(null)
- this.req.sessionStore = { generate: sinon.stub() }
- this.AuthenticationController.finishLogin = sinon.stub()
- this.passport.authenticate.yields(null, this.user, this.info)
- this.err = new Error('woops')
- })
- it('should call passport.authenticate', function () {
- this.AuthenticationController.passportLogin(this.req, this.res, this.next)
- this.passport.authenticate.callCount.should.equal(1)
- })
- describe('when authenticate produces an error', function () {
- beforeEach(function () {
- this.passport.authenticate.yields(this.err)
- })
- it('should return next with an error', function () {
- this.AuthenticationController.passportLogin(
- this.req,
- this.res,
- this.next
- )
- this.next.calledWith(this.err).should.equal(true)
- })
- })
- describe('when authenticate produces a user', function () {
- beforeEach(function () {
- this.req.session.postLoginRedirect = 'some_redirect'
- this.passport.authenticate.yields(null, this.user, this.info)
- })
- afterEach(function () {
- delete this.req.session.postLoginRedirect
- })
- it('should call finishLogin', function () {
- this.AuthenticationController.passportLogin(
- this.req,
- this.res,
- this.next
- )
- this.AuthenticationController.finishLogin.callCount.should.equal(1)
- this.AuthenticationController.finishLogin
- .calledWith(this.user)
- .should.equal(true)
- })
- })
- describe('when authenticate does not produce a user', function () {
- beforeEach(function () {
- this.info = { text: 'a', type: 'b' }
- this.passport.authenticate.yields(null, false, this.info)
- })
- it('should not call finishLogin', function () {
- this.AuthenticationController.passportLogin(
- this.req,
- this.res,
- this.next
- )
- this.AuthenticationController.finishLogin.callCount.should.equal(0)
- })
- it('should not send a json response with redirect', function () {
- this.AuthenticationController.passportLogin(
- this.req,
- this.res,
- this.next
- )
- this.res.json.callCount.should.equal(1)
- this.res.json.should.have.been.calledWith({ message: this.info })
- expect(this.res.json.lastCall.args[0].redir != null).to.equal(false)
- })
- })
- })
- describe('doPassportLogin', function () {
- beforeEach(function () {
- this.AuthenticationController._recordFailedLogin = sinon.stub()
- this.AuthenticationController._recordSuccessfulLogin = sinon.stub()
- this.Modules.hooks.fire = sinon.stub().yields(null, [])
- this.req.body = {
- email: this.email,
- password: this.password,
- session: {
- postLoginRedirect: '/path/to/redir/to',
- },
- }
- this.req.__authAuditInfo = { captcha: 'disabled' }
- this.cb = sinon.stub()
- })
- describe('when the preDoPassportLogin hooks produce an info object', function () {
- beforeEach(function () {
- this.Modules.hooks.fire = sinon
- .stub()
- .yields(null, [null, { redir: '/somewhere' }, null])
- })
- it('should stop early and call done with this info object', function (done) {
- this.AuthenticationController.doPassportLogin(
- this.req,
- this.req.body.email,
- this.req.body.password,
- this.cb
- )
- this.cb.callCount.should.equal(1)
- this.cb
- .calledWith(null, false, { redir: '/somewhere' })
- .should.equal(true)
- this.LoginRateLimiter.processLoginRequest.callCount.should.equal(0)
- done()
- })
- })
- describe('when the users rate limit', function () {
- beforeEach(function () {
- this.LoginRateLimiter.processLoginRequest.yields(null, false)
- })
- it('should block the request if the limit has been exceeded', function (done) {
- this.AuthenticationController.doPassportLogin(
- this.req,
- this.req.body.email,
- this.req.body.password,
- this.cb
- )
- this.cb.callCount.should.equal(1)
- this.cb.calledWith(null, null).should.equal(true)
- done()
- })
- })
- describe('when the user is authenticated', function () {
- beforeEach(function () {
- this.cb = sinon.stub()
- this.LoginRateLimiter.processLoginRequest.yields(null, true)
- this.AuthenticationManager.authenticate = sinon
- .stub()
- .yields(null, this.user)
- this.req.sessionID = Math.random()
- })
- describe('happy path', function () {
- beforeEach(function () {
- this.AuthenticationController.doPassportLogin(
- this.req,
- this.req.body.email,
- this.req.body.password,
- this.cb
- )
- })
- it('should attempt to authorise the user', function () {
- this.AuthenticationManager.authenticate
- .calledWith({ email: this.email.toLowerCase() }, this.password)
- .should.equal(true)
- })
- it("should establish the user's session", function () {
- this.cb.calledWith(null, this.user).should.equal(true)
- })
- })
- describe('when authenticate flags a parallel login', function () {
- beforeEach(function () {
- this.AuthenticationManager.authenticate = sinon
- .stub()
- .yields(new AuthenticationErrors.ParallelLoginError())
- this.AuthenticationController.doPassportLogin(
- this.req,
- this.req.body.email,
- this.req.body.password,
- this.cb
- )
- })
- it('should send a 429', function () {
- this.cb.should.have.been.calledWith(null, false, { status: 429 })
- })
- })
- describe('with a user having a recent failed login ', function () {
- beforeEach(function () {
- this.user.lastFailedLogin = new Date()
- })
- describe('with captcha disabled', function () {
- beforeEach(function () {
- this.req.__authAuditInfo.captcha = 'disabled'
- this.AuthenticationController.doPassportLogin(
- this.req,
- this.req.body.email,
- this.req.body.password,
- this.cb
- )
- })
- it('should let the user log in', function () {
- this.cb.should.have.been.calledWith(null, this.user)
- })
- })
- describe('with a solved captcha', function () {
- beforeEach(function () {
- this.req.__authAuditInfo.captcha = 'solved'
- this.AuthenticationController.doPassportLogin(
- this.req,
- this.req.body.email,
- this.req.body.password,
- this.cb
- )
- })
- it('should let the user log in', function () {
- this.cb.should.have.been.calledWith(null, this.user)
- })
- })
- describe('with a skipped captcha', function () {
- beforeEach(function () {
- this.req.__authAuditInfo.captcha = 'skipped'
- this.AuthenticationController.doPassportLogin(
- this.req,
- this.req.body.email,
- this.req.body.password,
- this.cb
- )
- })
- it('should request a captcha', function () {
- this.cb.should.have.been.calledWith(null, false, {
- text: 'cannot_verify_user_not_robot',
- type: 'error',
- errorReason: 'cannot_verify_user_not_robot',
- status: 400,
- })
- })
- })
- })
- })
- describe('when the user is not authenticated', function () {
- beforeEach(function () {
- this.LoginRateLimiter.processLoginRequest.yields(null, true)
- this.AuthenticationManager.authenticate = sinon
- .stub()
- .yields(null, null)
- this.cb = sinon.stub()
- this.AuthenticationController.doPassportLogin(
- this.req,
- this.req.body.email,
- this.req.body.password,
- this.cb
- )
- })
- it('should not establish the login', function () {
- this.cb.callCount.should.equal(1)
- this.cb.calledWith(null, false)
- expect(this.cb.lastCall.args[2]).to.deep.equal({
- type: 'error',
- key: 'invalid-password-retry-or-reset',
- status: 401,
- })
- })
- it('should not setup the user data in the background', function () {
- this.UserHandler.setupLoginData.called.should.equal(false)
- })
- it('should record a failed login', function () {
- this.AuthenticationController._recordFailedLogin.called.should.equal(
- true
- )
- })
- it('should log the failed login', function () {
- this.logger.debug
- .calledWith({ email: this.email.toLowerCase() }, 'failed log in')
- .should.equal(true)
- })
- })
- })
- describe('requireLogin', function () {
- beforeEach(function () {
- this.user = {
- _id: 'user-id-123',
- email: 'user@overleaf.com',
- }
- this.middleware = this.AuthenticationController.requireLogin()
- })
- describe('when the user is logged in', function () {
- beforeEach(function () {
- this.req.session = {
- user: (this.user = {
- _id: 'user-id-123',
- email: 'user@overleaf.com',
- }),
- }
- this.middleware(this.req, this.res, this.next)
- })
- it('should call the next method in the chain', function () {
- this.next.called.should.equal(true)
- })
- })
- describe('when the user is not logged in', function () {
- beforeEach(function () {
- this.req.session = {}
- this.AuthenticationController._redirectToLoginOrRegisterPage =
- sinon.stub()
- this.req.query = {}
- this.SessionManager.isUserLoggedIn = sinon.stub().returns(false)
- this.middleware(this.req, this.res, this.next)
- })
- it('should redirect to the register or login page', function () {
- this.AuthenticationController._redirectToLoginOrRegisterPage
- .calledWith(this.req, this.res)
- .should.equal(true)
- })
- })
- })
- describe('requireOauth', function () {
- beforeEach(function () {
- this.res.json = sinon.stub()
- this.res.status = sinon.stub().returns(this.res)
- this.res.sendStatus = sinon.stub()
- this.middleware = this.AuthenticationController.requireOauth('scope')
- })
- describe('when Oauth2Server authenticates', function () {
- beforeEach(function (done) {
- this.token = {
- accessToken: 'token',
- user: 'user',
- }
- this.Oauth2Server.server.authenticate = sinon
- .stub()
- .resolves(this.token)
- this.middleware(this.req, this.res, () => done())
- })
- it('should set oauth_token on request', function () {
- this.req.oauth_token.should.equal(this.token)
- })
- it('should set oauth on request', function () {
- this.req.oauth.access_token.should.equal(this.token.accessToken)
- })
- it('should set oauth_user on request', function () {
- this.req.oauth_user.should.equal('user')
- })
- })
- describe('when Oauth2Server returns 401 error', function () {
- beforeEach(function (done) {
- this.res.json.callsFake(() => done())
- this.Oauth2Server.server.authenticate.rejects({ code: 401 })
- this.middleware(this.req, this.res, this.next)
- })
- it('should return 401 error', function () {
- this.res.status.should.have.been.calledWith(401)
- })
- it('should not call next', function () {
- this.next.should.have.not.been.calledOnce
- })
- })
- })
- describe('requireGlobalLogin', function () {
- beforeEach(function () {
- this.req.headers = {}
- this.middleware = sinon.stub()
- this.AuthenticationController.requirePrivateApiAuth = sinon
- .stub()
- .returns(this.middleware)
- this.setRedirect = sinon.spy(
- this.AuthenticationController,
- 'setRedirectInSession'
- )
- })
- afterEach(function () {
- this.setRedirect.restore()
- })
- describe('with white listed url', function () {
- beforeEach(function () {
- this.AuthenticationController.addEndpointToLoginWhitelist('/login')
- this.req._parsedUrl.pathname = '/login'
- this.AuthenticationController.requireGlobalLogin(
- this.req,
- this.res,
- this.next
- )
- })
- it('should call next() directly', function () {
- this.next.called.should.equal(true)
- })
- })
- describe('with white listed url and a query string', function () {
- beforeEach(function () {
- this.AuthenticationController.addEndpointToLoginWhitelist('/login')
- this.req._parsedUrl.pathname = '/login'
- this.req.url = '/login?query=something'
- this.AuthenticationController.requireGlobalLogin(
- this.req,
- this.res,
- this.next
- )
- })
- it('should call next() directly', function () {
- this.next.called.should.equal(true)
- })
- })
- describe('with http auth', function () {
- beforeEach(function () {
- this.req.headers.authorization = 'Mock Basic Auth'
- this.AuthenticationController.requireGlobalLogin(
- this.req,
- this.res,
- this.next
- )
- })
- it('should pass the request onto requirePrivateApiAuth middleware', function () {
- this.middleware
- .calledWith(this.req, this.res, this.next)
- .should.equal(true)
- })
- })
- describe('with a user session', function () {
- beforeEach(function () {
- this.req.session = { user: { mock: 'user', _id: 'some_id' } }
- this.AuthenticationController.requireGlobalLogin(
- this.req,
- this.res,
- this.next
- )
- })
- it('should call next() directly', function () {
- this.next.called.should.equal(true)
- })
- })
- describe('with no login credentials', function () {
- beforeEach(function () {
- this.req.session = {}
- this.SessionManager.isUserLoggedIn = sinon.stub().returns(false)
- this.AuthenticationController.requireGlobalLogin(
- this.req,
- this.res,
- this.next
- )
- })
- it('should have called setRedirectInSession', function () {
- this.setRedirect.callCount.should.equal(1)
- })
- it('should redirect to the /login page', function () {
- this.res.redirectedTo.should.equal('/login')
- })
- })
- })
- describe('requireBasicAuth', function () {
- beforeEach(function () {
- this.basicAuthUsers = {
- 'basic-auth-user': 'basic-auth-password',
- }
- this.middleware = this.AuthenticationController.requireBasicAuth(
- this.basicAuthUsers
- )
- })
- describe('with http auth', function () {
- it('should error with incorrect user', function (done) {
- this.req.headers = {
- authorization: `Basic ${Buffer.from('user:nope').toString('base64')}`,
- }
- this.req.sendStatus = status => {
- expect(status).to.equal(401)
- done()
- }
- this.middleware(this.req, this.req)
- })
- it('should error with incorrect password', function (done) {
- this.req.headers = {
- authorization: `Basic ${Buffer.from('basic-auth-user:nope').toString(
- 'base64'
- )}`,
- }
- this.req.sendStatus = status => {
- expect(status).to.equal(401)
- done()
- }
- this.middleware(this.req, this.req)
- })
- it('should fail with empty pass', function (done) {
- this.req.headers = {
- authorization: `Basic ${Buffer.from(`basic-auth-user:`).toString(
- 'base64'
- )}`,
- }
- this.req.sendStatus = status => {
- expect(status).to.equal(401)
- done()
- }
- this.middleware(this.req, this.req)
- })
- it('should fail with empty user and password of "undefined"', function (done) {
- this.req.headers = {
- authorization: `Basic ${Buffer.from(`:undefined`).toString(
- 'base64'
- )}`,
- }
- this.req.sendStatus = status => {
- expect(status).to.equal(401)
- done()
- }
- this.middleware(this.req, this.req)
- })
- it('should fail with empty user and empty password', function (done) {
- this.req.headers = {
- authorization: `Basic ${Buffer.from(`:`).toString('base64')}`,
- }
- this.req.sendStatus = status => {
- expect(status).to.equal(401)
- done()
- }
- this.middleware(this.req, this.req)
- })
- it('should fail with a user that is not a valid property', function (done) {
- this.req.headers = {
- authorization: `Basic ${Buffer.from(
- `constructor:[Function ]`
- ).toString('base64')}`,
- }
- this.req.sendStatus = status => {
- expect(status).to.equal(401)
- done()
- }
- this.middleware(this.req, this.req)
- })
- it('should succeed with correct user/pass', function (done) {
- this.req.headers = {
- authorization: `Basic ${Buffer.from(
- `basic-auth-user:${this.basicAuthUsers['basic-auth-user']}`
- ).toString('base64')}`,
- }
- this.middleware(this.req, this.res, done)
- })
- })
- })
- describe('requirePrivateApiAuth', function () {
- beforeEach(function () {
- this.AuthenticationController.requireBasicAuth = sinon.stub()
- })
- it('should call requireBasicAuth with the private API user details', function () {
- this.AuthenticationController.requirePrivateApiAuth()
- this.AuthenticationController.requireBasicAuth
- .calledWith(this.httpAuthUsers)
- .should.equal(true)
- })
- })
- describe('_redirectToLoginOrRegisterPage', function () {
- beforeEach(function () {
- this.middleware = this.AuthenticationController.requireLogin(
- (this.options = { load_from_db: false })
- )
- this.req.session = {}
- this.AuthenticationController._redirectToRegisterPage = sinon.stub()
- this.AuthenticationController._redirectToLoginPage = sinon.stub()
- this.req.query = {}
- })
- describe('they have come directly to the url', function () {
- beforeEach(function () {
- this.req.query = {}
- this.SessionManager.isUserLoggedIn = sinon.stub().returns(false)
- this.middleware(this.req, this.res, this.next)
- })
- it('should redirect to the login page', function () {
- this.AuthenticationController._redirectToRegisterPage
- .calledWith(this.req, this.res)
- .should.equal(false)
- this.AuthenticationController._redirectToLoginPage
- .calledWith(this.req, this.res)
- .should.equal(true)
- })
- })
- describe('they have come via a templates link', function () {
- beforeEach(function () {
- this.req.query.zipUrl = 'something'
- this.SessionManager.isUserLoggedIn = sinon.stub().returns(false)
- this.middleware(this.req, this.res, this.next)
- })
- it('should redirect to the register page', function () {
- this.AuthenticationController._redirectToRegisterPage
- .calledWith(this.req, this.res)
- .should.equal(true)
- this.AuthenticationController._redirectToLoginPage
- .calledWith(this.req, this.res)
- .should.equal(false)
- })
- })
- describe('they have been invited to a project', function () {
- beforeEach(function () {
- this.req.query.project_name = 'something'
- this.SessionManager.isUserLoggedIn = sinon.stub().returns(false)
- this.middleware(this.req, this.res, this.next)
- })
- it('should redirect to the register page', function () {
- this.AuthenticationController._redirectToRegisterPage
- .calledWith(this.req, this.res)
- .should.equal(true)
- this.AuthenticationController._redirectToLoginPage
- .calledWith(this.req, this.res)
- .should.equal(false)
- })
- })
- })
- describe('_redirectToRegisterPage', function () {
- beforeEach(function () {
- this.req.path = '/target/url'
- this.req.query = { extra_query: 'foo' }
- this.AuthenticationController._redirectToRegisterPage(this.req, this.res)
- })
- it('should redirect to the register page with a query string attached', function () {
- this.req.session.postLoginRedirect.should.equal(
- '/target/url?extra_query=foo'
- )
- this.res.redirectedTo.should.equal('/register?extra_query=foo')
- })
- it('should log out a message', function () {
- this.logger.debug
- .calledWith(
- { url: this.url },
- 'user not logged in so redirecting to register page'
- )
- .should.equal(true)
- })
- })
- describe('_redirectToLoginPage', function () {
- beforeEach(function () {
- this.req.path = '/target/url'
- this.req.query = { extra_query: 'foo' }
- this.AuthenticationController._redirectToLoginPage(this.req, this.res)
- })
- it('should redirect to the register page with a query string attached', function () {
- this.req.session.postLoginRedirect.should.equal(
- '/target/url?extra_query=foo'
- )
- this.res.redirectedTo.should.equal('/login?extra_query=foo')
- })
- })
- describe('_recordSuccessfulLogin', function () {
- beforeEach(function () {
- this.UserUpdater.updateUser = sinon.stub().yields()
- this.AuthenticationController._recordSuccessfulLogin(
- this.user._id,
- this.callback
- )
- })
- it('should increment the user.login.success metric', function () {
- this.Metrics.inc.calledWith('user.login.success').should.equal(true)
- })
- it("should update the user's login count and last logged in date", function () {
- this.UserUpdater.updateUser.args[0][1].$set.lastLoggedIn.should.not.equal(
- undefined
- )
- this.UserUpdater.updateUser.args[0][1].$inc.loginCount.should.equal(1)
- })
- it('should call the callback', function () {
- this.callback.called.should.equal(true)
- })
- })
- describe('_recordFailedLogin', function () {
- beforeEach(function () {
- this.AuthenticationController._recordFailedLogin(this.callback)
- })
- it('should increment the user.login.failed metric', function () {
- this.Metrics.inc.calledWith('user.login.failed').should.equal(true)
- })
- it('should call the callback', function () {
- this.callback.called.should.equal(true)
- })
- })
- describe('setRedirectInSession', function () {
- beforeEach(function () {
- this.req = { session: {} }
- this.req.path = '/somewhere'
- this.req.query = { one: '1' }
- })
- it('should set redirect property on session', function () {
- this.AuthenticationController.setRedirectInSession(this.req)
- expect(this.req.session.postLoginRedirect).to.equal('/somewhere?one=1')
- })
- it('should set the supplied value', function () {
- this.AuthenticationController.setRedirectInSession(
- this.req,
- '/somewhere/specific'
- )
- expect(this.req.session.postLoginRedirect).to.equal('/somewhere/specific')
- })
- it('should not allow open redirects', function () {
- this.AuthenticationController.setRedirectInSession(
- this.req,
- 'https://evil.com'
- )
- expect(this.req.session.postLoginRedirect).to.be.undefined
- })
- describe('with a png', function () {
- beforeEach(function () {
- this.req = { session: {} }
- })
- it('should not set the redirect', function () {
- this.AuthenticationController.setRedirectInSession(
- this.req,
- '/something.png'
- )
- expect(this.req.session.postLoginRedirect).to.equal(undefined)
- })
- })
- describe('with a js path', function () {
- beforeEach(function () {
- this.req = { session: {} }
- })
- it('should not set the redirect', function () {
- this.AuthenticationController.setRedirectInSession(
- this.req,
- '/js/something.js'
- )
- expect(this.req.session.postLoginRedirect).to.equal(undefined)
- })
- })
- })
- describe('getRedirectFromSession', function () {
- it('should get redirect property from session', function () {
- this.req = { session: { postLoginRedirect: '/a?b=c' } }
- expect(
- this.AuthenticationController.getRedirectFromSession(this.req)
- ).to.equal('/a?b=c')
- })
- it('should not allow open redirects', function () {
- this.req = { session: { postLoginRedirect: 'https://evil.com' } }
- expect(this.AuthenticationController.getRedirectFromSession(this.req)).to
- .be.null
- })
- it('handle null values', function () {
- this.req = { session: {} }
- expect(this.AuthenticationController.getRedirectFromSession(this.req)).to
- .be.null
- })
- })
- describe('_clearRedirectFromSession', function () {
- beforeEach(function () {
- this.req = { session: { postLoginRedirect: '/a?b=c' } }
- })
- it('should remove the redirect property from session', function () {
- this.AuthenticationController._clearRedirectFromSession(this.req)
- expect(this.req.session.postLoginRedirect).to.equal(undefined)
- })
- })
- describe('finishLogin', function () {
- // - get redirect
- // - async handlers
- // - afterLoginSessionSetup
- // - clear redirect
- // - issue redir, two ways
- beforeEach(function () {
- this.AuthenticationController.getRedirectFromSession = sinon
- .stub()
- .returns('/some/page')
- this.req.sessionID = 'thisisacryptographicallysecurerandomid'
- this.req.session = {
- passport: { user: { _id: 'one' } },
- }
- this.req.session.destroy = sinon.stub().yields(null)
- this.req.session.save = sinon.stub().yields(null)
- this.req.sessionStore = { generate: sinon.stub() }
- this.req.login = sinon.stub().yields(null)
- this.AuthenticationController._clearRedirectFromSession = sinon.stub()
- this.AuthenticationController._redirectToReconfirmPage = sinon.stub()
- this.UserSessionsManager.trackSession = sinon.stub()
- this.UserHandler.setupLoginData = sinon.stub()
- this.LoginRateLimiter.recordSuccessfulLogin = sinon.stub()
- this.AuthenticationController._recordSuccessfulLogin = sinon.stub()
- this.AnalyticsManager.recordEvent = sinon.stub()
- this.AnalyticsManager.identifyUser = sinon.stub()
- this.acceptsJson.returns(true)
- this.res.json = sinon.stub()
- this.res.redirect = sinon.stub()
- })
- it('should extract the redirect from the session', function () {
- this.AuthenticationController.finishLogin(
- this.user,
- this.req,
- this.res,
- this.next
- )
- expect(
- this.AuthenticationController.getRedirectFromSession.callCount
- ).to.equal(1)
- expect(
- this.AuthenticationController.getRedirectFromSession.calledWith(
- this.req
- )
- ).to.equal(true)
- })
- it('should clear redirect from session', function () {
- this.AuthenticationController.finishLogin(
- this.user,
- this.req,
- this.res,
- this.next
- )
- expect(
- this.AuthenticationController._clearRedirectFromSession.callCount
- ).to.equal(1)
- expect(
- this.AuthenticationController._clearRedirectFromSession.calledWith(
- this.req
- )
- ).to.equal(true)
- })
- it('should issue a json response with a redirect', function () {
- this.AuthenticationController.finishLogin(
- this.user,
- this.req,
- this.res,
- this.next
- )
- expect(
- this.AsyncFormHelper.redirect.calledWith(
- this.req,
- this.res,
- '/some/page'
- )
- ).to.equal(true)
- })
- describe('with a non-json request', function () {
- beforeEach(function () {
- this.acceptsJson.returns(false)
- this.res.json = sinon.stub()
- this.res.redirect = sinon.stub()
- })
- it('should issue a plain redirect', function () {
- this.AuthenticationController.finishLogin(
- this.user,
- this.req,
- this.res,
- this.next
- )
- expect(
- this.AsyncFormHelper.redirect.calledWith(
- this.req,
- this.res,
- '/some/page'
- )
- ).to.equal(true)
- })
- })
- describe('when user is flagged to reconfirm', function () {
- beforeEach(function () {
- this.req.session = {}
- this.user.must_reconfirm = true
- })
- it('should redirect to reconfirm page', function () {
- this.AuthenticationController.finishLogin(
- this.user,
- this.req,
- this.res,
- this.next
- )
- expect(
- this.AuthenticationController._redirectToReconfirmPage.calledWith(
- this.req
- )
- ).to.equal(true)
- })
- })
- describe('when user account is suspended', function () {
- beforeEach(function () {
- this.req.session = {}
- this.user.suspended = true
- })
- it('should not log in and instead redirect to suspended account page', function () {
- this.AuthenticationController.finishLogin(
- this.user,
- this.req,
- this.res,
- this.next
- )
- sinon.assert.notCalled(this.req.login)
- sinon.assert.calledWith(
- this.AsyncFormHelper.redirect,
- this.req,
- this.res,
- '/account-suspended'
- )
- })
- })
- 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.AsyncFormHelper.redirect.called).to.equal(true)
- })
- 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,
- this.next
- )
- expect(this.next.callCount).to.equal(0)
- 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()
- }
- )
- })
- })
- describe('UserAuditLog', function () {
- it('should add an audit log entry', function () {
- this.AuthenticationController.finishLogin(
- this.user,
- this.req,
- this.res,
- this.next
- )
- expect(this.UserAuditLogHandler.addEntry).to.have.been.calledWith(
- this.user._id,
- 'login',
- this.user._id,
- '42.42.42.42'
- )
- })
- it('should add an audit log entry before logging the user in', function () {
- this.AuthenticationController.finishLogin(
- this.user,
- this.req,
- this.res,
- this.next
- )
- expect(this.UserAuditLogHandler.addEntry).to.have.been.calledBefore(
- this.req.login
- )
- })
- it('should not log the user in without an audit log entry', function () {
- const theError = new Error()
- this.UserAuditLogHandler.addEntry.yields(theError)
- this.AuthenticationController.finishLogin(
- this.user,
- this.req,
- this.res,
- this.next
- )
- expect(this.next).to.have.been.calledWith(theError)
- expect(this.req.login).to.not.have.been.called
- })
- it('should pass along auditInfo when present', function () {
- this.AuthenticationController.setAuditInfo(this.req, {
- method: 'Login',
- })
- this.AuthenticationController.finishLogin(
- this.user,
- this.req,
- this.res,
- this.next
- )
- expect(this.UserAuditLogHandler.addEntry).to.have.been.calledWith(
- this.user._id,
- 'login',
- this.user._id,
- '42.42.42.42',
- { method: 'Login' }
- )
- })
- })
- describe('_afterLoginSessionSetup', function () {
- beforeEach(function () {})
- it('should call req.login', function () {
- this.AuthenticationController.finishLogin(
- this.user,
- this.req,
- this.res,
- this.next
- )
- this.req.login.callCount.should.equal(1)
- })
- it('should erase the CSRF secret', function () {
- this.AuthenticationController.finishLogin(
- this.user,
- this.req,
- this.res,
- this.next
- )
- expect(this.req.session.csrfSecret).to.not.exist
- })
- it('should call req.session.save', function () {
- this.AuthenticationController.finishLogin(
- this.user,
- this.req,
- this.res,
- this.next
- )
- this.req.session.save.callCount.should.equal(1)
- })
- it('should call UserSessionsManager.trackSession', function () {
- this.AuthenticationController.finishLogin(
- this.user,
- this.req,
- this.res,
- this.next
- )
- this.UserSessionsManager.trackSession.callCount.should.equal(1)
- })
- describe('when req.session.save produces an error', function () {
- beforeEach(function () {
- this.req.session.save = sinon.stub().yields(new Error('woops'))
- })
- it('should produce an error', function (done) {
- this.AuthenticationController.finishLogin(
- this.user,
- this.req,
- this.res,
- err => {
- expect(err).to.not.be.oneOf([null, undefined])
- expect(err).to.be.instanceof(Error)
- done()
- }
- )
- })
- it('should not call UserSessionsManager.trackSession', function (done) {
- this.AuthenticationController.finishLogin(
- this.user,
- this.req,
- this.res,
- err => {
- expect(err).to.exist
- this.UserSessionsManager.trackSession.callCount.should.equal(0)
- done()
- }
- )
- })
- })
- })
- describe('_loginAsyncHandlers', function () {
- beforeEach(function () {
- this.AuthenticationController.finishLogin(
- this.user,
- this.req,
- this.res,
- this.next
- )
- })
- it('should call identifyUser', function () {
- sinon.assert.calledWith(
- this.AnalyticsManager.identifyUser,
- this.user._id,
- this.req.session.analyticsId
- )
- })
- it('should setup the user data in the background', function () {
- this.UserHandler.setupLoginData.calledWith(this.user).should.equal(true)
- })
- it('should set res.session.justLoggedIn', function () {
- this.req.session.justLoggedIn.should.equal(true)
- })
- it('should record the successful login', function () {
- this.AuthenticationController._recordSuccessfulLogin
- .calledWith(this.user._id)
- .should.equal(true)
- })
- it('should tell the rate limiter that there was a success for that email', function () {
- this.LoginRateLimiter.recordSuccessfulLogin
- .calledWith(this.user.email)
- .should.equal(true)
- })
- it('should log the successful login', function () {
- this.logger.debug
- .calledWith(
- { email: this.user.email, userId: this.user._id.toString() },
- 'successful log in'
- )
- .should.equal(true)
- })
- it('should track the login event', function () {
- sinon.assert.calledWith(
- this.AnalyticsManager.recordEventForUser,
- this.user._id,
- 'user-logged-in'
- )
- })
- })
- })
- })
|