AccessTokenEncryptorTests.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. const chai = require('chai')
  2. chai.should()
  3. const { expect } = chai
  4. const modulePath = '../../../index.js'
  5. const SandboxedModule = require('sandboxed-module')
  6. describe('AccessTokenEncryptor', function () {
  7. beforeEach(function () {
  8. this.testObject = { hello: 'world' }
  9. this.encrypted2015 =
  10. '2015.1:473a66fb5d816bc716f278ab819d88a5:+mTg7O9sgUND8pNQFG6h2GE='
  11. this.encrypted2016 =
  12. '2016.1:76a7d64a444ccee1a515b49c44844a69:m5YSkexUsLjcF4gLncm72+k='
  13. this.encrypted2019 =
  14. '2019.1:627143b2ab185a020c8720253a4c984e:7gnY6Ez3/Y3UWgLHLfBtJsE=:bf75cecb6aeea55b3c060e1122d2a82d'
  15. this.badLabel = 'xxxxxx:c7a39310056b694c:jQf+Uh5Den3JREtvc82GW5Q='
  16. this.badKey = '2015.1:d7a39310056b694c:jQf+Uh5Den3JREtvc82GW5Q='
  17. this.badCipherText = '2015.1:c7a39310056b694c:xQf+Uh5Den3JREtvc82GW5Q='
  18. this.settings = {
  19. cipherLabel: '2019.1',
  20. cipherPasswords: {
  21. 2016.1: '11111111111111111111111111111111111111',
  22. 2015.1: '22222222222222222222222222222222222222',
  23. 2019.1: '33333333333333333333333333333333333333',
  24. },
  25. }
  26. this.AccessTokenEncryptor = SandboxedModule.require(modulePath, {
  27. globals: {
  28. Buffer,
  29. },
  30. requires: {
  31. '@overleaf/logger': {
  32. err() {},
  33. },
  34. },
  35. })
  36. this.encryptor = new this.AccessTokenEncryptor(this.settings)
  37. })
  38. describe('encrypt', function () {
  39. it('should encrypt the object', function (done) {
  40. this.encryptor.encryptJson(this.testObject, (err, encrypted) => {
  41. expect(err).to.be.null
  42. encrypted.should.match(
  43. /^2019.1:[0-9a-f]{32}:[a-zA-Z0-9=+/]+:[0-9a-f]{32}$/
  44. )
  45. done()
  46. })
  47. })
  48. it('should encrypt the object differently the next time', function (done) {
  49. this.encryptor.encryptJson(this.testObject, (err, encrypted1) => {
  50. expect(err).to.be.null
  51. this.encryptor.encryptJson(this.testObject, (err, encrypted2) => {
  52. expect(err).to.be.null
  53. encrypted1.should.not.equal(encrypted2)
  54. done()
  55. })
  56. })
  57. })
  58. })
  59. describe('decrypt', function () {
  60. it('should decrypt the string to get the same object', function (done) {
  61. this.encryptor.encryptJson(this.testObject, (err, encrypted) => {
  62. expect(err).to.be.null
  63. this.encryptor.decryptToJson(encrypted, (err, decrypted) => {
  64. expect(err).to.be.null
  65. expect(decrypted).to.deep.equal(this.testObject)
  66. done()
  67. })
  68. })
  69. })
  70. it('should not be able to decrypt 2015 string', function (done) {
  71. this.encryptor.decryptToJson(this.encrypted2015, (err, decrypted) => {
  72. expect(err).to.exist
  73. expect(err.message).to.equal('token scheme v1 is not supported anymore')
  74. expect(decrypted).to.not.exist
  75. done()
  76. })
  77. })
  78. it('should not be able to decrypt a 2016 string', function (done) {
  79. this.encryptor.decryptToJson(this.encrypted2016, (err, decrypted) => {
  80. expect(err).to.exist
  81. expect(err.message).to.equal('token scheme v1 is not supported anymore')
  82. expect(decrypted).to.not.exist
  83. done()
  84. })
  85. })
  86. it('should decrypt an 2019 string to get the same object', function (done) {
  87. this.encryptor.decryptToJson(this.encrypted2019, (err, decrypted) => {
  88. expect(err).to.be.null
  89. expect(decrypted).to.deep.equal(this.testObject)
  90. done()
  91. })
  92. })
  93. it('should return an error when decrypting an invalid label', function (done) {
  94. this.encryptor.decryptToJson(this.badLabel, (err, decrypted) => {
  95. expect(err).to.be.instanceof(Error)
  96. expect(decrypted).to.be.undefined
  97. done()
  98. })
  99. })
  100. it('should return an error when decrypting an invalid key', function (done) {
  101. this.encryptor.decryptToJson(this.badKey, (err, decrypted) => {
  102. expect(err).to.be.instanceof(Error)
  103. expect(decrypted).to.be.undefined
  104. done()
  105. })
  106. })
  107. it('should return an error when decrypting an invalid ciphertext', function (done) {
  108. this.encryptor.decryptToJson(this.badCipherText, (err, decrypted) => {
  109. expect(err).to.be.instanceof(Error)
  110. expect(decrypted).to.be.undefined
  111. done()
  112. })
  113. })
  114. })
  115. })