AccessTokenEncryptorTests.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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('sync', function () {
  117. describe('encrypt', function () {
  118. it('should encrypt the object', function (done) {
  119. this.encryptor.encryptJson(this.testObject, (err, encrypted) => {
  120. expect(err).to.be.null
  121. encrypted.should.match(
  122. /^2023.1-v3:[0-9a-f]{32}:[a-zA-Z0-9=+/]+:[0-9a-f]{32}$/
  123. )
  124. done()
  125. })
  126. })
  127. it('should encrypt the object differently the next time', function (done) {
  128. this.encryptor.encryptJson(this.testObject, (err, encrypted1) => {
  129. expect(err).to.be.null
  130. this.encryptor.encryptJson(this.testObject, (err, encrypted2) => {
  131. expect(err).to.be.null
  132. encrypted1.should.not.equal(encrypted2)
  133. done()
  134. })
  135. })
  136. })
  137. })
  138. describe('decrypt', function () {
  139. it('should decrypt the string to get the same object', function (done) {
  140. this.encryptor.encryptJson(this.testObject, (err, encrypted) => {
  141. expect(err).to.be.null
  142. this.encryptor.decryptToJson(encrypted, (err, decrypted) => {
  143. expect(err).to.be.null
  144. expect(decrypted).to.deep.equal(this.testObject)
  145. done()
  146. })
  147. })
  148. })
  149. it('should not be able to decrypt 2015 string', function (done) {
  150. this.encryptor.decryptToJson(this.encrypted2015, (err, decrypted) => {
  151. expect(err).to.exist
  152. expect(err.message).to.equal(
  153. 'unknown access-token-encryptor label 2015.1'
  154. )
  155. expect(decrypted).to.not.exist
  156. done()
  157. })
  158. })
  159. it('should not be able to decrypt a 2016 string', function (done) {
  160. this.encryptor.decryptToJson(this.encrypted2016, (err, decrypted) => {
  161. expect(err).to.exist
  162. expect(err.message).to.equal(
  163. 'unknown access-token-encryptor label 2016.1'
  164. )
  165. expect(decrypted).to.not.exist
  166. done()
  167. })
  168. })
  169. it('should not be able to decrypt a 2019 string', function (done) {
  170. this.encryptor.decryptToJson(this.encrypted2019, (err, decrypted) => {
  171. expect(err).to.exist
  172. expect(err.message).to.equal(
  173. 'unknown access-token-encryptor label 2019.1'
  174. )
  175. expect(decrypted).to.not.exist
  176. done()
  177. })
  178. })
  179. it('should decrypt an 2023 string to get the same object', function (done) {
  180. this.encryptor.decryptToJson(this.encrypted2023, (err, decrypted) => {
  181. expect(err).to.be.null
  182. expect(decrypted).to.deep.equal(this.testObject)
  183. done()
  184. })
  185. })
  186. it('should return an error when decrypting an invalid label', function (done) {
  187. this.encryptor.decryptToJson(this.badLabel, (err, decrypted) => {
  188. expect(err).to.be.instanceof(Error)
  189. expect(decrypted).to.be.undefined
  190. done()
  191. })
  192. })
  193. it('should return an error when decrypting an invalid key', function (done) {
  194. this.encryptor.decryptToJson(this.badKey, (err, decrypted) => {
  195. expect(err).to.be.instanceof(Error)
  196. expect(decrypted).to.be.undefined
  197. done()
  198. })
  199. })
  200. it('should return an error when decrypting an invalid ciphertext', function (done) {
  201. this.encryptor.decryptToJson(this.badCipherText, (err, decrypted) => {
  202. expect(err).to.be.instanceof(Error)
  203. expect(decrypted).to.be.undefined
  204. done()
  205. })
  206. })
  207. })
  208. })
  209. describe('async', function () {
  210. describe('encrypt', function () {
  211. it('should encrypt the object', async function () {
  212. const encrypted = await this.encryptor.promises.encryptJson(
  213. this.testObject
  214. )
  215. encrypted.should.match(
  216. /^2023.1-v3:[0-9a-f]{32}:[a-zA-Z0-9=+/]+:[0-9a-f]{32}$/
  217. )
  218. })
  219. it('should encrypt the object differently the next time', async function () {
  220. const encrypted1 = await this.encryptor.promises.encryptJson(
  221. this.testObject
  222. )
  223. const encrypted2 = await this.encryptor.promises.encryptJson(
  224. this.testObject
  225. )
  226. encrypted1.should.not.equal(encrypted2)
  227. })
  228. })
  229. describe('decrypt', function () {
  230. it('should decrypt the string to get the same object', async function () {
  231. const encrypted = await this.encryptor.promises.encryptJson(
  232. this.testObject
  233. )
  234. const decrypted = await this.encryptor.promises.decryptToJson(encrypted)
  235. expect(decrypted).to.deep.equal(this.testObject)
  236. })
  237. it('should not be able to decrypt 2015 string', async function () {
  238. await expect(
  239. this.encryptor.promises.decryptToJson(this.encrypted2015)
  240. ).to.eventually.be.rejectedWith(
  241. 'unknown access-token-encryptor label 2015.1'
  242. )
  243. })
  244. it('should not be able to decrypt a 2016 string', async function () {
  245. await expect(
  246. this.encryptor.promises.decryptToJson(this.encrypted2016)
  247. ).to.be.rejectedWith('unknown access-token-encryptor label 2016.1')
  248. })
  249. it('should not be able to decrypt a 2019 string', async function () {
  250. await expect(
  251. this.encryptor.promises.decryptToJson(this.encrypted2019)
  252. ).to.be.rejectedWith('unknown access-token-encryptor label 2019.1')
  253. })
  254. it('should decrypt an 2023 string to get the same object', async function () {
  255. const decrypted = await this.encryptor.promises.decryptToJson(
  256. this.encrypted2023
  257. )
  258. expect(decrypted).to.deep.equal(this.testObject)
  259. })
  260. it('should return an error when decrypting an invalid label', async function () {
  261. await expect(
  262. this.encryptor.promises.decryptToJson(this.badLabel)
  263. ).to.be.rejectedWith('unknown access-token-encryptor label xxxxxx')
  264. })
  265. it('should return an error when decrypting an invalid key', async function () {
  266. await expect(
  267. this.encryptor.promises.decryptToJson(this.badKey)
  268. ).to.be.rejectedWith('unknown access-token-encryptor label 2015.1')
  269. })
  270. it('should return an error when decrypting an invalid ciphertext', async function () {
  271. await expect(
  272. this.encryptor.promises.decryptToJson(this.badCipherText)
  273. ).to.be.rejectedWith('unknown access-token-encryptor label 2015.1')
  274. })
  275. })
  276. })
  277. })