AuthenticationManagerTests.coffee 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/Features/Authentication/AuthenticationManager.js"
  6. SandboxedModule = require('sandboxed-module')
  7. events = require "events"
  8. ObjectId = require("mongojs").ObjectId
  9. Errors = require "../../../../app/js/Features/Errors/Errors"
  10. describe "AuthenticationManager", ->
  11. beforeEach ->
  12. @settings = { security: { bcryptRounds: 12 } }
  13. @AuthenticationManager = SandboxedModule.require modulePath, requires:
  14. "../../models/User": User: @User = {}
  15. "../../infrastructure/mongojs":
  16. db: @db =
  17. users: {}
  18. ObjectId: ObjectId
  19. "bcrypt": @bcrypt = {}
  20. "settings-sharelatex": @settings
  21. "../V1/V1Handler": @V1Handler = {}
  22. "../User/UserGetter": @UserGetter = {}
  23. @callback = sinon.stub()
  24. describe "with real bcrypt", ->
  25. beforeEach ->
  26. bcrypt = require('bcrypt')
  27. @bcrypt.compare = bcrypt.compare
  28. @bcrypt.getRounds = bcrypt.getRounds
  29. @bcrypt.genSalt = bcrypt.genSalt
  30. @bcrypt.hash = bcrypt.hash
  31. # Hash of 'testpassword'
  32. @testPassword = '$2a$12$zhtThy3R5tLtw5sCwr5XD.zhPENGn4ecjeMcP87oYSYrIICFqBpei'
  33. describe "authenticate", ->
  34. beforeEach ->
  35. @user =
  36. _id: "user-id"
  37. email: @email = "USER@sharelatex.com"
  38. @User.findOne = sinon.stub().callsArgWith(1, null, @user)
  39. describe "when the hashed password matches", ->
  40. beforeEach (done) ->
  41. @unencryptedPassword = "testpassword"
  42. @user.hashedPassword = @testPassword
  43. @AuthenticationManager.authenticate email: @email, @unencryptedPassword, (error, user) =>
  44. @callback(error, user)
  45. done()
  46. it "should look up the correct user in the database", ->
  47. @User.findOne.calledWith(email: @email).should.equal true
  48. it "should return the user", ->
  49. @callback.calledWith(null, @user).should.equal true
  50. describe "when the encrypted passwords do not match", ->
  51. beforeEach ->
  52. @AuthenticationManager.authenticate(email: @email, "notthecorrectpassword", @callback)
  53. it "should not return the user", ->
  54. @callback.calledWith(null, null).should.equal true
  55. describe "setUserPasswordInV2", ->
  56. beforeEach ->
  57. @user =
  58. _id: "5c8791477192a80b5e76ca7e"
  59. email: @email = "USER@sharelatex.com"
  60. @db.users.update = sinon.stub().callsArgWith(2, null, {nModified: 1})
  61. it "should not produce an error", (done) ->
  62. @AuthenticationManager.setUserPasswordInV2 @user._id, "testpassword", (err, updated) =>
  63. expect(err).to.not.exist
  64. expect(updated).to.equal true
  65. done()
  66. it "should set the hashed password", (done) ->
  67. @AuthenticationManager.setUserPasswordInV2 @user._id, "testpassword", (err, updated) =>
  68. expect(err).to.not.exist
  69. hashedPassword = @db.users.update.lastCall.args[1].$set.hashedPassword
  70. expect(hashedPassword).to.exist
  71. expect(hashedPassword.length).to.equal 60
  72. expect(hashedPassword).to.match /^\$2a\$12\$[a-zA-Z0-9\/.]{53}$/
  73. done()
  74. describe "authenticate", ->
  75. describe "when the user exists in the database", ->
  76. beforeEach ->
  77. @user =
  78. _id: "user-id"
  79. email: @email = "USER@sharelatex.com"
  80. @unencryptedPassword = "banana"
  81. @User.findOne = sinon.stub().callsArgWith(1, null, @user)
  82. describe "when the hashed password matches", ->
  83. beforeEach (done) ->
  84. @user.hashedPassword = @hashedPassword = "asdfjadflasdf"
  85. @bcrypt.compare = sinon.stub().callsArgWith(2, null, true)
  86. @bcrypt.getRounds = sinon.stub().returns 12
  87. @AuthenticationManager.authenticate email: @email, @unencryptedPassword, (error, user) =>
  88. @callback(error, user)
  89. done()
  90. it "should look up the correct user in the database", ->
  91. @User.findOne.calledWith(email: @email).should.equal true
  92. it "should check that the passwords match", ->
  93. @bcrypt.compare
  94. .calledWith(@unencryptedPassword, @hashedPassword)
  95. .should.equal true
  96. it "should return the user", ->
  97. @callback.calledWith(null, @user).should.equal true
  98. describe "when the encrypted passwords do not match", ->
  99. beforeEach ->
  100. @AuthenticationManager.authenticate(email: @email, @unencryptedPassword, @callback)
  101. it "should not return the user", ->
  102. @callback.calledWith(null, null).should.equal true
  103. describe "when the hashed password matches but the number of rounds is too low", ->
  104. beforeEach (done) ->
  105. @user.hashedPassword = @hashedPassword = "asdfjadflasdf"
  106. @bcrypt.compare = sinon.stub().callsArgWith(2, null, true)
  107. @bcrypt.getRounds = sinon.stub().returns 7
  108. @AuthenticationManager.setUserPassword = sinon.stub().callsArgWith(2, null)
  109. @AuthenticationManager.authenticate email: @email, @unencryptedPassword, (error, user) =>
  110. @callback(error, user)
  111. done()
  112. it "should look up the correct user in the database", ->
  113. @User.findOne.calledWith(email: @email).should.equal true
  114. it "should check that the passwords match", ->
  115. @bcrypt.compare
  116. .calledWith(@unencryptedPassword, @hashedPassword)
  117. .should.equal true
  118. it "should check the number of rounds", ->
  119. @bcrypt.getRounds.called.should.equal true
  120. it "should set the users password (with a higher number of rounds)", ->
  121. @AuthenticationManager.setUserPassword
  122. .calledWith("user-id", @unencryptedPassword)
  123. .should.equal true
  124. it "should return the user", ->
  125. @callback.calledWith(null, @user).should.equal true
  126. describe "when the user does not exist in the database", ->
  127. beforeEach ->
  128. @User.findOne = sinon.stub().callsArgWith(1, null, null)
  129. @AuthenticationManager.authenticate(email: @email, @unencrpytedPassword, @callback)
  130. it "should not return a user", ->
  131. @callback.calledWith(null, null).should.equal true
  132. describe "validateEmail", ->
  133. describe "valid", ->
  134. it "should return null", ->
  135. result = @AuthenticationManager.validateEmail 'foo@example.com'
  136. expect(result).to.equal null
  137. describe "invalid", ->
  138. it "should return validation error object for no email", ->
  139. result = @AuthenticationManager.validateEmail ''
  140. expect(result).to.not.equal null
  141. expect(result.message).to.equal 'email not valid'
  142. it "should return validation error object for invalid", ->
  143. result = @AuthenticationManager.validateEmail 'notanemail'
  144. expect(result).to.not.equal null
  145. expect(result.message).to.equal 'email not valid'
  146. describe "validatePassword", ->
  147. beforeEach ->
  148. # 73 characters:
  149. @longPassword = '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef012345678'
  150. describe "with a null password", ->
  151. it "should return an error", ->
  152. expect(@AuthenticationManager.validatePassword()).to.eql { message: 'password not set' }
  153. describe "password length", ->
  154. describe "with the default password length options", ->
  155. it "should reject passwords that are too short", ->
  156. expect(@AuthenticationManager.validatePassword('')).to.eql { message: 'password is too short' }
  157. expect(@AuthenticationManager.validatePassword('foo')).to.eql { message: 'password is too short' }
  158. it "should reject passwords that are too long", ->
  159. expect(@AuthenticationManager.validatePassword(@longPassword)).to.eql { message: 'password is too long' }
  160. it "should accept passwords that are a good length", ->
  161. expect(@AuthenticationManager.validatePassword('l337h4x0r')).to.equal null
  162. describe "when the password length is specified in settings", ->
  163. beforeEach ->
  164. @settings.passwordStrengthOptions =
  165. length:
  166. min: 10
  167. max: 12
  168. it "should reject passwords that are too short", ->
  169. expect(@AuthenticationManager.validatePassword('012345678')).to.eql { message: 'password is too short' }
  170. it "should accept passwords of exactly minimum length", ->
  171. expect(@AuthenticationManager.validatePassword('0123456789')).to.equal null
  172. it "should reject passwords that are too long", ->
  173. expect(@AuthenticationManager.validatePassword('0123456789abc')).to.eql { message: 'password is too long' }
  174. it "should accept passwords of exactly maximum length", ->
  175. expect(@AuthenticationManager.validatePassword('0123456789ab')).to.equal null
  176. describe "when the maximum password length is set to >72 characters in settings", ->
  177. beforeEach ->
  178. @settings.passwordStrengthOptions =
  179. length:
  180. max: 128
  181. it "should still reject passwords > 72 characters in length", ->
  182. expect(@AuthenticationManager.validatePassword(@longPassword)).to.eql { message: 'password is too long' }
  183. describe "allowed characters", ->
  184. describe "with the default settings for allowed characters", ->
  185. it "should allow passwords with valid characters", ->
  186. expect(@AuthenticationManager.validatePassword("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")).to.equal null
  187. expect(@AuthenticationManager.validatePassword("1234567890@#$%^&*()-_=+[]{};:<>/?!£€.,")).to.equal null
  188. it "should not allow passwords with invalid characters", ->
  189. expect(@AuthenticationManager.validatePassword("correct horse battery staple")).to.eql { message: 'password contains an invalid character' }
  190. describe "when valid characters are overridden in settings", ->
  191. beforeEach ->
  192. @settings.passwordStrengthOptions =
  193. chars:
  194. symbols: " "
  195. it "should allow passwords with valid characters", ->
  196. expect(@AuthenticationManager.validatePassword("correct horse battery staple")).to.equal null
  197. it "should disallow passwords with invalid characters", ->
  198. expect(@AuthenticationManager.validatePassword("1234567890@#$%^&*()-_=+[]{};:<>/?!£€.,")).to.eql { message: 'password contains an invalid character' }
  199. describe "when allowAnyChars is set", ->
  200. beforeEach ->
  201. @settings.passwordStrengthOptions =
  202. allowAnyChars: true
  203. it "should allow any characters", ->
  204. expect(@AuthenticationManager.validatePassword("correct horse battery staple")).to.equal null
  205. expect(@AuthenticationManager.validatePassword("1234567890@#$%^&*()-_=+[]{};:<>/?!£€.,")).to.equal null
  206. describe "setUserPassword", ->
  207. beforeEach ->
  208. @user_id = ObjectId()
  209. @password = "banana"
  210. @hashedPassword = "asdkjfa;osiuvandf"
  211. @salt = "saltaasdfasdfasdf"
  212. @bcrypt.genSalt = sinon.stub().callsArgWith(2, null, @salt)
  213. @bcrypt.hash = sinon.stub().callsArgWith(2, null, @hashedPassword)
  214. @db.users.update = sinon.stub().callsArg(2)
  215. describe "too long", ->
  216. beforeEach ->
  217. @settings.passwordStrengthOptions =
  218. length:
  219. max:10
  220. @password = "dsdsadsadsadsadsadkjsadjsadjsadljs"
  221. it "should return and error", (done)->
  222. @AuthenticationManager.setUserPassword @user_id, @password, (err)->
  223. expect(err).to.exist
  224. done()
  225. it "should not start the bcrypt process", (done)->
  226. @AuthenticationManager.setUserPassword @user_id, @password, (err)=>
  227. @bcrypt.genSalt.called.should.equal false
  228. @bcrypt.hash.called.should.equal false
  229. done()
  230. describe "too short", ->
  231. beforeEach ->
  232. @settings.passwordStrengthOptions =
  233. length:
  234. max:10
  235. min:6
  236. @password = "dsd"
  237. it "should return and error", (done)->
  238. @AuthenticationManager.setUserPassword @user_id, @password, (err)->
  239. expect(err).to.exist
  240. done()
  241. it "should not start the bcrypt process", (done)->
  242. @AuthenticationManager.setUserPassword @user_id, @password, (err)=>
  243. @bcrypt.genSalt.called.should.equal false
  244. @bcrypt.hash.called.should.equal false
  245. done()
  246. describe "password set attempt", ->
  247. describe "with SL user in SL", ->
  248. beforeEach ->
  249. @UserGetter.getUser = sinon.stub().yields(null, { overleaf: null })
  250. @AuthenticationManager.setUserPassword(@user_id, @password, @callback)
  251. it 'should look up the user', ->
  252. @UserGetter.getUser.calledWith(@user_id).should.equal true
  253. it "should update the user's password in the database", ->
  254. args = @db.users.update.lastCall.args
  255. expect(args[0]).to.deep.equal {_id: ObjectId(@user_id.toString())}
  256. expect(args[1]).to.deep.equal {
  257. $set: {
  258. "hashedPassword": @hashedPassword
  259. }
  260. $unset: password: true
  261. }
  262. it "should hash the password", ->
  263. @bcrypt.genSalt
  264. .calledWith(12)
  265. .should.equal true
  266. @bcrypt.hash
  267. .calledWith(@password, @salt)
  268. .should.equal true
  269. it "should call the callback", ->
  270. @callback.called.should.equal true
  271. describe "with SL user in v2", ->
  272. beforeEach (done) ->
  273. @settings.overleaf = true
  274. @UserGetter.getUser = sinon.stub().yields(null, { overleaf: null })
  275. @AuthenticationManager.setUserPassword @user_id, @password, (err, changed) =>
  276. @callback(err, changed)
  277. done()
  278. it "should error", ->
  279. @callback.calledWith(new Errors.SLInV2Error("Password Reset Attempt")).should.equal true
  280. describe "with v2 user in SL", ->
  281. beforeEach (done) ->
  282. @UserGetter.getUser = sinon.stub().yields(null, { overleaf: {id: 1} })
  283. @AuthenticationManager.setUserPassword @user_id, @password, (err, changed) =>
  284. @callback(err, changed)
  285. done()
  286. it "should error", ->
  287. @callback.calledWith(new Errors.NotInV2Error("Password Reset Attempt")).should.equal true
  288. describe "with v2 user in v2", ->
  289. beforeEach (done) ->
  290. @settings.overleaf = true
  291. @UserGetter.getUser = sinon.stub().yields(null, { overleaf: {id: 1} })
  292. @V1Handler.doPasswordReset = sinon.stub().yields(null, true)
  293. @AuthenticationManager.setUserPassword @user_id, @password, (err, changed) =>
  294. @callback(err, changed)
  295. done()
  296. it "should set the password in v2", ->
  297. @callback.calledWith(null, true).should.equal true