AccessTokenEncryptorTests.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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.encrypted2023 =
  16. '2023.1-v3:a6dd3781dd6ce93a4134874b505a209c:9TdIDAc8V9SeR0ffSn63Jj4=:d8b2de0b733c81b949993dce229abb4c'
  17. this.badLabel = 'xxxxxx:c7a39310056b694c:jQf+Uh5Den3JREtvc82GW5Q='
  18. this.badKey = '2015.1:d7a39310056b694c:jQf+Uh5Den3JREtvc82GW5Q='
  19. this.badCipherText = '2015.1:c7a39310056b694c:xQf+Uh5Den3JREtvc82GW5Q='
  20. this.settings = {
  21. cipherLabel: '2023.1-v3',
  22. cipherPasswords: {
  23. '2023.1-v3': '44444444444444444444444444444444444444',
  24. },
  25. }
  26. this.AccessTokenEncryptor = SandboxedModule.require(modulePath, {
  27. globals: {
  28. Buffer,
  29. },
  30. })
  31. this.encryptor = new this.AccessTokenEncryptor(this.settings)
  32. })
  33. describe('invalid settings', function () {
  34. it('should flag missing label', function () {
  35. expect(
  36. () =>
  37. new this.AccessTokenEncryptor({
  38. cipherLabel: '',
  39. cipherPasswords: { '': '' },
  40. })
  41. ).to.throw(/cipherLabel cannot be empty/)
  42. })
  43. it('should flag invalid label with colon', function () {
  44. expect(
  45. () =>
  46. new this.AccessTokenEncryptor({
  47. cipherLabel: '2023:1-v2',
  48. cipherPasswords: { '2023:1-v2': '' },
  49. })
  50. ).to.throw(/colon/)
  51. })
  52. it('should flag missing password', function () {
  53. expect(
  54. () =>
  55. new this.AccessTokenEncryptor({
  56. cipherPasswords: { '2023.1-v3': '' },
  57. cipherVersions: { '2023.1-v3': 'v3' },
  58. })
  59. ).to.throw(/cipherPasswords.+ missing/)
  60. expect(
  61. () =>
  62. new this.AccessTokenEncryptor({
  63. cipherLabel: '2023.1-v3',
  64. cipherPasswords: { '2023.1-v3': undefined },
  65. })
  66. ).to.throw(/cipherPasswords.+ missing/)
  67. })
  68. it('should flag short password', function () {
  69. expect(
  70. () =>
  71. new this.AccessTokenEncryptor({
  72. cipherLabel: '2023.1-v3',
  73. cipherPasswords: { '2023.1-v3': 'foo' },
  74. })
  75. ).to.throw(/cipherPasswords.+ too short/)
  76. })
  77. it('should flag missing version', function () {
  78. expect(
  79. () =>
  80. new this.AccessTokenEncryptor({
  81. cipherLabel: '2023.1',
  82. cipherPasswords: { 2023.1: '11111111111111111111111111111111' },
  83. })
  84. ).to.throw(/must contain version suffix/)
  85. expect(
  86. () =>
  87. new this.AccessTokenEncryptor({
  88. cipherLabel: '2023.1-',
  89. cipherPasswords: { '2023.1-': '11111111111111111111111111111111' },
  90. })
  91. ).to.throw(/must contain version suffix/)
  92. })
  93. it('should flag invalid version', function () {
  94. expect(
  95. () =>
  96. new this.AccessTokenEncryptor({
  97. cipherLabel: '2023.1-v0',
  98. cipherPasswords: {
  99. '2023.1-v0': '11111111111111111111111111111111',
  100. },
  101. })
  102. ).to.throw(/unknown version/)
  103. })
  104. it('should flag unknown default scheme', function () {
  105. expect(
  106. () =>
  107. new this.AccessTokenEncryptor({
  108. cipherLabel: '2000.1-v3',
  109. cipherPasswords: {
  110. '2023.1-v3': '11111111111111111111111111111111',
  111. },
  112. })
  113. ).to.throw(/unknown default cipherLabel/)
  114. })
  115. })
  116. describe('encrypt', function () {
  117. it('should encrypt the object', function (done) {
  118. this.encryptor.encryptJson(this.testObject, (err, encrypted) => {
  119. expect(err).to.be.null
  120. encrypted.should.match(
  121. /^2023.1-v3:[0-9a-f]{32}:[a-zA-Z0-9=+/]+:[0-9a-f]{32}$/
  122. )
  123. done()
  124. })
  125. })
  126. it('should encrypt the object differently the next time', function (done) {
  127. this.encryptor.encryptJson(this.testObject, (err, encrypted1) => {
  128. expect(err).to.be.null
  129. this.encryptor.encryptJson(this.testObject, (err, encrypted2) => {
  130. expect(err).to.be.null
  131. encrypted1.should.not.equal(encrypted2)
  132. done()
  133. })
  134. })
  135. })
  136. })
  137. describe('decrypt', function () {
  138. it('should decrypt the string to get the same object', function (done) {
  139. this.encryptor.encryptJson(this.testObject, (err, encrypted) => {
  140. expect(err).to.be.null
  141. this.encryptor.decryptToJson(encrypted, (err, decrypted) => {
  142. expect(err).to.be.null
  143. expect(decrypted).to.deep.equal(this.testObject)
  144. done()
  145. })
  146. })
  147. })
  148. it('should not be able to decrypt 2015 string', function (done) {
  149. this.encryptor.decryptToJson(this.encrypted2015, (err, decrypted) => {
  150. expect(err).to.exist
  151. expect(err.message).to.equal(
  152. 'unknown access-token-encryptor label 2015.1'
  153. )
  154. expect(decrypted).to.not.exist
  155. done()
  156. })
  157. })
  158. it('should not be able to decrypt a 2016 string', function (done) {
  159. this.encryptor.decryptToJson(this.encrypted2016, (err, decrypted) => {
  160. expect(err).to.exist
  161. expect(err.message).to.equal(
  162. 'unknown access-token-encryptor label 2016.1'
  163. )
  164. expect(decrypted).to.not.exist
  165. done()
  166. })
  167. })
  168. it('should not be able to decrypt a 2019 string', function (done) {
  169. this.encryptor.decryptToJson(this.encrypted2019, (err, decrypted) => {
  170. expect(err).to.exist
  171. expect(err.message).to.equal(
  172. 'unknown access-token-encryptor label 2019.1'
  173. )
  174. expect(decrypted).to.not.exist
  175. done()
  176. })
  177. })
  178. it('should decrypt an 2023 string to get the same object', function (done) {
  179. this.encryptor.decryptToJson(this.encrypted2023, (err, decrypted) => {
  180. expect(err).to.be.null
  181. expect(decrypted).to.deep.equal(this.testObject)
  182. done()
  183. })
  184. })
  185. it('should return an error when decrypting an invalid label', function (done) {
  186. this.encryptor.decryptToJson(this.badLabel, (err, decrypted) => {
  187. expect(err).to.be.instanceof(Error)
  188. expect(decrypted).to.be.undefined
  189. done()
  190. })
  191. })
  192. it('should return an error when decrypting an invalid key', function (done) {
  193. this.encryptor.decryptToJson(this.badKey, (err, decrypted) => {
  194. expect(err).to.be.instanceof(Error)
  195. expect(decrypted).to.be.undefined
  196. done()
  197. })
  198. })
  199. it('should return an error when decrypting an invalid ciphertext', function (done) {
  200. this.encryptor.decryptToJson(this.badCipherText, (err, decrypted) => {
  201. expect(err).to.be.instanceof(Error)
  202. expect(decrypted).to.be.undefined
  203. done()
  204. })
  205. })
  206. })
  207. })