AuthenticationManagerTests.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. /* eslint-disable
  2. handle-callback-err,
  3. max-len,
  4. no-return-assign,
  5. no-unused-vars,
  6. no-useless-escape,
  7. */
  8. // TODO: This file was created by bulk-decaffeinate.
  9. // Fix any style issues and re-enable lint.
  10. /*
  11. * decaffeinate suggestions:
  12. * DS102: Remove unnecessary code created because of implicit returns
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. const sinon = require('sinon')
  16. const chai = require('chai')
  17. const should = chai.should()
  18. const { expect } = chai
  19. const modulePath =
  20. '../../../../app/src/Features/Authentication/AuthenticationManager.js'
  21. const SandboxedModule = require('sandboxed-module')
  22. const events = require('events')
  23. const { ObjectId } = require('mongojs')
  24. const Errors = require('../../../../app/src/Features/Errors/Errors')
  25. describe('AuthenticationManager', function() {
  26. beforeEach(function() {
  27. this.settings = { security: { bcryptRounds: 12 } }
  28. this.AuthenticationManager = SandboxedModule.require(modulePath, {
  29. globals: {
  30. console: console
  31. },
  32. requires: {
  33. '../../models/User': {
  34. User: (this.User = {})
  35. },
  36. '../../infrastructure/mongojs': {
  37. db: (this.db = { users: {} }),
  38. ObjectId
  39. },
  40. bcrypt: (this.bcrypt = {}),
  41. 'settings-sharelatex': this.settings,
  42. '../V1/V1Handler': (this.V1Handler = {}),
  43. '../User/UserGetter': (this.UserGetter = {})
  44. }
  45. })
  46. return (this.callback = sinon.stub())
  47. })
  48. describe('with real bcrypt', function() {
  49. beforeEach(function() {
  50. const bcrypt = require('bcrypt')
  51. this.bcrypt.compare = bcrypt.compare
  52. this.bcrypt.getRounds = bcrypt.getRounds
  53. this.bcrypt.genSalt = bcrypt.genSalt
  54. this.bcrypt.hash = bcrypt.hash
  55. // Hash of 'testpassword'
  56. return (this.testPassword =
  57. '$2a$12$zhtThy3R5tLtw5sCwr5XD.zhPENGn4ecjeMcP87oYSYrIICFqBpei')
  58. })
  59. describe('authenticate', function() {
  60. beforeEach(function() {
  61. this.user = {
  62. _id: 'user-id',
  63. email: (this.email = 'USER@sharelatex.com')
  64. }
  65. return (this.User.findOne = sinon
  66. .stub()
  67. .callsArgWith(1, null, this.user))
  68. })
  69. describe('when the hashed password matches', function() {
  70. beforeEach(function(done) {
  71. this.unencryptedPassword = 'testpassword'
  72. this.user.hashedPassword = this.testPassword
  73. return this.AuthenticationManager.authenticate(
  74. { email: this.email },
  75. this.unencryptedPassword,
  76. (error, user) => {
  77. this.callback(error, user)
  78. return done()
  79. }
  80. )
  81. })
  82. it('should look up the correct user in the database', function() {
  83. return this.User.findOne
  84. .calledWith({ email: this.email })
  85. .should.equal(true)
  86. })
  87. it('should return the user', function() {
  88. return this.callback.calledWith(null, this.user).should.equal(true)
  89. })
  90. })
  91. describe('when the encrypted passwords do not match', function() {
  92. beforeEach(function() {
  93. return this.AuthenticationManager.authenticate(
  94. { email: this.email },
  95. 'notthecorrectpassword',
  96. this.callback
  97. )
  98. })
  99. it('should not return the user', function() {
  100. return this.callback.calledWith(null, null).should.equal(true)
  101. })
  102. })
  103. })
  104. describe('setUserPasswordInV2', function() {
  105. beforeEach(function() {
  106. this.user = {
  107. _id: '5c8791477192a80b5e76ca7e',
  108. email: (this.email = 'USER@sharelatex.com')
  109. }
  110. return (this.db.users.update = sinon
  111. .stub()
  112. .callsArgWith(2, null, { nModified: 1 }))
  113. })
  114. it('should not produce an error', function(done) {
  115. return this.AuthenticationManager.setUserPasswordInV2(
  116. this.user._id,
  117. 'testpassword',
  118. (err, updated) => {
  119. expect(err).to.not.exist
  120. expect(updated).to.equal(true)
  121. return done()
  122. }
  123. )
  124. })
  125. it('should set the hashed password', function(done) {
  126. return this.AuthenticationManager.setUserPasswordInV2(
  127. this.user._id,
  128. 'testpassword',
  129. (err, updated) => {
  130. expect(err).to.not.exist
  131. const {
  132. hashedPassword
  133. } = this.db.users.update.lastCall.args[1].$set
  134. expect(hashedPassword).to.exist
  135. expect(hashedPassword.length).to.equal(60)
  136. expect(hashedPassword).to.match(/^\$2a\$12\$[a-zA-Z0-9\/.]{53}$/)
  137. return done()
  138. }
  139. )
  140. })
  141. })
  142. })
  143. describe('authenticate', function() {
  144. describe('when the user exists in the database', function() {
  145. beforeEach(function() {
  146. this.user = {
  147. _id: 'user-id',
  148. email: (this.email = 'USER@sharelatex.com')
  149. }
  150. this.unencryptedPassword = 'banana'
  151. return (this.User.findOne = sinon
  152. .stub()
  153. .callsArgWith(1, null, this.user))
  154. })
  155. describe('when the hashed password matches', function() {
  156. beforeEach(function(done) {
  157. this.user.hashedPassword = this.hashedPassword = 'asdfjadflasdf'
  158. this.bcrypt.compare = sinon.stub().callsArgWith(2, null, true)
  159. this.bcrypt.getRounds = sinon.stub().returns(12)
  160. return this.AuthenticationManager.authenticate(
  161. { email: this.email },
  162. this.unencryptedPassword,
  163. (error, user) => {
  164. this.callback(error, user)
  165. return done()
  166. }
  167. )
  168. })
  169. it('should look up the correct user in the database', function() {
  170. return this.User.findOne
  171. .calledWith({ email: this.email })
  172. .should.equal(true)
  173. })
  174. it('should check that the passwords match', function() {
  175. return this.bcrypt.compare
  176. .calledWith(this.unencryptedPassword, this.hashedPassword)
  177. .should.equal(true)
  178. })
  179. it('should return the user', function() {
  180. return this.callback.calledWith(null, this.user).should.equal(true)
  181. })
  182. })
  183. describe('when the encrypted passwords do not match', function() {
  184. beforeEach(function() {
  185. return this.AuthenticationManager.authenticate(
  186. { email: this.email },
  187. this.unencryptedPassword,
  188. this.callback
  189. )
  190. })
  191. it('should not return the user', function() {
  192. return this.callback.calledWith(null, null).should.equal(true)
  193. })
  194. })
  195. describe('when the hashed password matches but the number of rounds is too low', function() {
  196. beforeEach(function(done) {
  197. this.user.hashedPassword = this.hashedPassword = 'asdfjadflasdf'
  198. this.bcrypt.compare = sinon.stub().callsArgWith(2, null, true)
  199. this.bcrypt.getRounds = sinon.stub().returns(7)
  200. this.AuthenticationManager.setUserPassword = sinon
  201. .stub()
  202. .callsArgWith(2, null)
  203. return this.AuthenticationManager.authenticate(
  204. { email: this.email },
  205. this.unencryptedPassword,
  206. (error, user) => {
  207. this.callback(error, user)
  208. return done()
  209. }
  210. )
  211. })
  212. it('should look up the correct user in the database', function() {
  213. return this.User.findOne
  214. .calledWith({ email: this.email })
  215. .should.equal(true)
  216. })
  217. it('should check that the passwords match', function() {
  218. return this.bcrypt.compare
  219. .calledWith(this.unencryptedPassword, this.hashedPassword)
  220. .should.equal(true)
  221. })
  222. it('should check the number of rounds', function() {
  223. return this.bcrypt.getRounds.called.should.equal(true)
  224. })
  225. it('should set the users password (with a higher number of rounds)', function() {
  226. return this.AuthenticationManager.setUserPassword
  227. .calledWith('user-id', this.unencryptedPassword)
  228. .should.equal(true)
  229. })
  230. it('should return the user', function() {
  231. return this.callback.calledWith(null, this.user).should.equal(true)
  232. })
  233. })
  234. describe('when the hashed password matches but the number of rounds is too low, but upgrades disabled', function() {
  235. beforeEach(function(done) {
  236. this.settings.security.disableBcryptRoundsUpgrades = true
  237. this.user.hashedPassword = this.hashedPassword = 'asdfjadflasdf'
  238. this.bcrypt.compare = sinon.stub().callsArgWith(2, null, true)
  239. this.bcrypt.getRounds = sinon.stub().returns(7)
  240. this.AuthenticationManager.setUserPassword = sinon
  241. .stub()
  242. .callsArgWith(2, null)
  243. return this.AuthenticationManager.authenticate(
  244. { email: this.email },
  245. this.unencryptedPassword,
  246. (error, user) => {
  247. this.callback(error, user)
  248. return done()
  249. }
  250. )
  251. })
  252. it('should not check the number of rounds', function() {
  253. return this.bcrypt.getRounds.called.should.equal(false)
  254. })
  255. it('should not set the users password (with a higher number of rounds)', function() {
  256. return this.AuthenticationManager.setUserPassword
  257. .calledWith('user-id', this.unencryptedPassword)
  258. .should.equal(false)
  259. })
  260. it('should return the user', function() {
  261. return this.callback.calledWith(null, this.user).should.equal(true)
  262. })
  263. })
  264. })
  265. describe('when the user does not exist in the database', function() {
  266. beforeEach(function() {
  267. this.User.findOne = sinon.stub().callsArgWith(1, null, null)
  268. return this.AuthenticationManager.authenticate(
  269. { email: this.email },
  270. this.unencrpytedPassword,
  271. this.callback
  272. )
  273. })
  274. it('should not return a user', function() {
  275. return this.callback.calledWith(null, null).should.equal(true)
  276. })
  277. })
  278. })
  279. describe('validateEmail', function() {
  280. describe('valid', () =>
  281. it('should return null', function() {
  282. const result = this.AuthenticationManager.validateEmail(
  283. 'foo@example.com'
  284. )
  285. return expect(result).to.equal(null)
  286. }))
  287. describe('invalid', function() {
  288. it('should return validation error object for no email', function() {
  289. const result = this.AuthenticationManager.validateEmail('')
  290. expect(result).to.not.equal(null)
  291. return expect(result.message).to.equal('email not valid')
  292. })
  293. it('should return validation error object for invalid', function() {
  294. const result = this.AuthenticationManager.validateEmail('notanemail')
  295. expect(result).to.not.equal(null)
  296. return expect(result.message).to.equal('email not valid')
  297. })
  298. })
  299. })
  300. describe('validatePassword', function() {
  301. beforeEach(function() {
  302. // 73 characters:
  303. return (this.longPassword =
  304. '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef012345678')
  305. })
  306. describe('with a null password', () =>
  307. it('should return an error', function() {
  308. return expect(this.AuthenticationManager.validatePassword()).to.eql({
  309. message: 'password not set'
  310. })
  311. }))
  312. describe('password length', function() {
  313. describe('with the default password length options', function() {
  314. it('should reject passwords that are too short', function() {
  315. expect(this.AuthenticationManager.validatePassword('')).to.eql({
  316. message: 'password is too short'
  317. })
  318. return expect(
  319. this.AuthenticationManager.validatePassword('foo')
  320. ).to.eql({ message: 'password is too short' })
  321. })
  322. it('should reject passwords that are too long', function() {
  323. return expect(
  324. this.AuthenticationManager.validatePassword(this.longPassword)
  325. ).to.eql({ message: 'password is too long' })
  326. })
  327. it('should accept passwords that are a good length', function() {
  328. return expect(
  329. this.AuthenticationManager.validatePassword('l337h4x0r')
  330. ).to.equal(null)
  331. })
  332. })
  333. describe('when the password length is specified in settings', function() {
  334. beforeEach(function() {
  335. return (this.settings.passwordStrengthOptions = {
  336. length: {
  337. min: 10,
  338. max: 12
  339. }
  340. })
  341. })
  342. it('should reject passwords that are too short', function() {
  343. return expect(
  344. this.AuthenticationManager.validatePassword('012345678')
  345. ).to.eql({ message: 'password is too short' })
  346. })
  347. it('should accept passwords of exactly minimum length', function() {
  348. return expect(
  349. this.AuthenticationManager.validatePassword('0123456789')
  350. ).to.equal(null)
  351. })
  352. it('should reject passwords that are too long', function() {
  353. return expect(
  354. this.AuthenticationManager.validatePassword('0123456789abc')
  355. ).to.eql({ message: 'password is too long' })
  356. })
  357. it('should accept passwords of exactly maximum length', function() {
  358. return expect(
  359. this.AuthenticationManager.validatePassword('0123456789ab')
  360. ).to.equal(null)
  361. })
  362. })
  363. describe('when the maximum password length is set to >72 characters in settings', function() {
  364. beforeEach(function() {
  365. return (this.settings.passwordStrengthOptions = {
  366. length: {
  367. max: 128
  368. }
  369. })
  370. })
  371. it('should still reject passwords > 72 characters in length', function() {
  372. return expect(
  373. this.AuthenticationManager.validatePassword(this.longPassword)
  374. ).to.eql({ message: 'password is too long' })
  375. })
  376. })
  377. })
  378. describe('allowed characters', function() {
  379. describe('with the default settings for allowed characters', function() {
  380. it('should allow passwords with valid characters', function() {
  381. expect(
  382. this.AuthenticationManager.validatePassword(
  383. 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
  384. )
  385. ).to.equal(null)
  386. return expect(
  387. this.AuthenticationManager.validatePassword(
  388. '1234567890@#$%^&*()-_=+[]{};:<>/?!£€.,'
  389. )
  390. ).to.equal(null)
  391. })
  392. it('should not allow passwords with invalid characters', function() {
  393. return expect(
  394. this.AuthenticationManager.validatePassword(
  395. 'correct horse battery staple'
  396. )
  397. ).to.eql({ message: 'password contains an invalid character' })
  398. })
  399. })
  400. describe('when valid characters are overridden in settings', function() {
  401. beforeEach(function() {
  402. return (this.settings.passwordStrengthOptions = {
  403. chars: {
  404. symbols: ' '
  405. }
  406. })
  407. })
  408. it('should allow passwords with valid characters', function() {
  409. return expect(
  410. this.AuthenticationManager.validatePassword(
  411. 'correct horse battery staple'
  412. )
  413. ).to.equal(null)
  414. })
  415. it('should disallow passwords with invalid characters', function() {
  416. return expect(
  417. this.AuthenticationManager.validatePassword(
  418. '1234567890@#$%^&*()-_=+[]{};:<>/?!£€.,'
  419. )
  420. ).to.eql({ message: 'password contains an invalid character' })
  421. })
  422. })
  423. describe('when allowAnyChars is set', function() {
  424. beforeEach(function() {
  425. return (this.settings.passwordStrengthOptions = {
  426. allowAnyChars: true
  427. })
  428. })
  429. it('should allow any characters', function() {
  430. expect(
  431. this.AuthenticationManager.validatePassword(
  432. 'correct horse battery staple'
  433. )
  434. ).to.equal(null)
  435. return expect(
  436. this.AuthenticationManager.validatePassword(
  437. '1234567890@#$%^&*()-_=+[]{};:<>/?!£€.,'
  438. )
  439. ).to.equal(null)
  440. })
  441. })
  442. })
  443. })
  444. describe('setUserPassword', function() {
  445. beforeEach(function() {
  446. this.user_id = ObjectId()
  447. this.password = 'banana'
  448. this.hashedPassword = 'asdkjfa;osiuvandf'
  449. this.salt = 'saltaasdfasdfasdf'
  450. this.bcrypt.genSalt = sinon.stub().callsArgWith(2, null, this.salt)
  451. this.bcrypt.hash = sinon.stub().callsArgWith(2, null, this.hashedPassword)
  452. return (this.db.users.update = sinon.stub().callsArg(2))
  453. })
  454. describe('too long', function() {
  455. beforeEach(function() {
  456. this.settings.passwordStrengthOptions = {
  457. length: {
  458. max: 10
  459. }
  460. }
  461. return (this.password = 'dsdsadsadsadsadsadkjsadjsadjsadljs')
  462. })
  463. it('should return and error', function(done) {
  464. return this.AuthenticationManager.setUserPassword(
  465. this.user_id,
  466. this.password,
  467. function(err) {
  468. expect(err).to.exist
  469. return done()
  470. }
  471. )
  472. })
  473. it('should not start the bcrypt process', function(done) {
  474. return this.AuthenticationManager.setUserPassword(
  475. this.user_id,
  476. this.password,
  477. err => {
  478. this.bcrypt.genSalt.called.should.equal(false)
  479. this.bcrypt.hash.called.should.equal(false)
  480. return done()
  481. }
  482. )
  483. })
  484. })
  485. describe('too short', function() {
  486. beforeEach(function() {
  487. this.settings.passwordStrengthOptions = {
  488. length: {
  489. max: 10,
  490. min: 6
  491. }
  492. }
  493. return (this.password = 'dsd')
  494. })
  495. it('should return and error', function(done) {
  496. return this.AuthenticationManager.setUserPassword(
  497. this.user_id,
  498. this.password,
  499. function(err) {
  500. expect(err).to.exist
  501. return done()
  502. }
  503. )
  504. })
  505. it('should not start the bcrypt process', function(done) {
  506. return this.AuthenticationManager.setUserPassword(
  507. this.user_id,
  508. this.password,
  509. err => {
  510. this.bcrypt.genSalt.called.should.equal(false)
  511. this.bcrypt.hash.called.should.equal(false)
  512. return done()
  513. }
  514. )
  515. })
  516. })
  517. describe('successful password set attempt', function() {
  518. beforeEach(function() {
  519. this.UserGetter.getUser = sinon.stub().yields(null, { overleaf: null })
  520. this.AuthenticationManager.setUserPassword(
  521. this.user_id,
  522. this.password,
  523. this.callback
  524. )
  525. })
  526. it("should update the user's password in the database", function() {
  527. const { args } = this.db.users.update.lastCall
  528. expect(args[0]).to.deep.equal({
  529. _id: ObjectId(this.user_id.toString())
  530. })
  531. return expect(args[1]).to.deep.equal({
  532. $set: {
  533. hashedPassword: this.hashedPassword
  534. },
  535. $unset: {
  536. password: true
  537. }
  538. })
  539. })
  540. it('should hash the password', function() {
  541. this.bcrypt.genSalt.calledWith(12).should.equal(true)
  542. return this.bcrypt.hash
  543. .calledWith(this.password, this.salt)
  544. .should.equal(true)
  545. })
  546. it('should call the callback', function() {
  547. return this.callback.called.should.equal(true)
  548. })
  549. })
  550. })
  551. })