AccessTokenEncryptor.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Sanity-check the conversion and remove this comment.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS101: Remove unnecessary use of Array.from
  6. * DS102: Remove unnecessary code created because of implicit returns
  7. * DS207: Consider shorter variations of null checks
  8. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  9. */
  10. const crypto = require('crypto');
  11. const ALGORITHM = 'aes-256-ctr';
  12. const keyFn = (password, salt, callback)=> crypto.pbkdf2(password, salt, 10000, 64, 'sha1', callback);
  13. const keyFn32 = (password, salt, keyLength, callback)=> crypto.pbkdf2(password, salt, 10000, 32, 'sha1', callback);
  14. class AccessTokenEncryptor {
  15. constructor(settings) {
  16. this.settings = settings;
  17. this.cipherLabel = this.settings.cipherLabel;
  18. if (this.cipherLabel != null ? this.cipherLabel.match(/:/) : undefined) { throw Error("cipherLabel must not contain a colon (:)"); }
  19. this.cipherPassword = this.settings.cipherPasswords[this.cipherLabel];
  20. if ((this.cipherPassword == null)) { throw Error("cipherPassword not set"); }
  21. if (this.cipherPassword.length < 16) { throw Error("cipherPassword too short"); }
  22. }
  23. encryptJson(json, callback) {
  24. const string = JSON.stringify(json);
  25. return crypto.randomBytes(32, (err, bytes) => {
  26. if (err) { return callback(err); }
  27. const salt = bytes.slice(0, 16);
  28. const iv = bytes.slice(16, 32);
  29. return keyFn32(this.cipherPassword, salt, 32, (err, key) => {
  30. if (err != null) {
  31. logger.err({err}, "error getting Fn key");
  32. return callback(err);
  33. }
  34. const cipher = crypto.createCipheriv(ALGORITHM, key, iv);
  35. const crypted = cipher.update(string, 'utf8', 'base64') + cipher.final('base64');
  36. return callback(null, `${this.cipherLabel}:${salt.toString('hex')}:${crypted}:${iv.toString('hex')}`);
  37. });
  38. });
  39. }
  40. decryptToJson(encryptedJson, callback) {
  41. const [label, salt, cipherText, iv] = Array.from(encryptedJson.split(':', 4));
  42. const password = this.settings.cipherPasswords[label];
  43. if ((password == null) || (password.length < 16)) { return callback(new Error("invalid password")); }
  44. if (iv) {
  45. return this.decryptToJsonV2(password, salt, cipherText, iv, callback);
  46. } else {
  47. return this.decryptToJsonV1(password, salt, cipherText, callback);
  48. }
  49. }
  50. decryptToJsonV1(password, salt, cipherText, callback) {
  51. return keyFn(password, Buffer.from(salt, 'hex'), (err, key) => {
  52. let json;
  53. if (err != null) {
  54. logger.err({err}, "error getting Fn key");
  55. return callback(err);
  56. }
  57. const decipher = crypto.createDecipher(ALGORITHM, key);
  58. const dec = decipher.update(cipherText, 'base64', 'utf8') + decipher.final('utf8');
  59. try {
  60. json = JSON.parse(dec);
  61. } catch (e) {
  62. return callback(new Error("error decrypting token"));
  63. }
  64. return callback(null, json, true);
  65. });
  66. }
  67. decryptToJsonV2(password, salt, cipherText, iv, callback) {
  68. return keyFn32(password, Buffer.from(salt, 'hex'), 32, (err, key) => {
  69. let json;
  70. if (err != null) {
  71. logger.err({err}, "error getting Fn key");
  72. return callback(err);
  73. }
  74. const decipher = crypto.createDecipheriv(ALGORITHM, key, Buffer.from(iv, 'hex'));
  75. const dec = decipher.update(cipherText, 'base64', 'utf8') + decipher.final('utf8');
  76. try {
  77. json = JSON.parse(dec);
  78. } catch (e) {
  79. return callback(new Error("error decrypting token"));
  80. }
  81. return callback(null, json);
  82. });
  83. }
  84. }
  85. module.exports = AccessTokenEncryptor;