AccessTokenEncryptorTests.js 8.3 KB

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