• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ringcentral / bugsnag-notification-app / 9987907705

18 Jul 2024 08:11AM UTC coverage: 88.77% (-0.6%) from 89.386%
9987907705

push

github

web-flow
misc: encode auth token in DB (#73)

251 of 311 branches covered (80.71%)

Branch coverage included in aggregate %.

47 of 55 new or added lines in 4 files covered. (85.45%)

658 of 713 relevant lines covered (92.29%)

17.09 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.38
/src/server/models/authToken.js
1
const crypto = require('crypto');
7✔
2
const Sequelize = require('sequelize');
7✔
3
const { sequelize } = require('./sequelize');
7✔
4

5
const AuthToken = sequelize.define('authTokens', {
7✔
6
  id: {
7
    type: Sequelize.STRING,
8
    primaryKey: true,
9
  },
10
  data: {
11
    type: Sequelize.STRING
12
  },
13
  encryptedData: {
14
    type: Sequelize.STRING
15
  },
16
});
17

18
function getCipherKey() {
19
  if (!process.env.APP_SERVER_SECRET_KEY) {
33!
NEW
20
    throw new Error('APP_SERVER_SECRET_KEY is not defined');
×
21
  }
22
  if (process.env.APP_SERVER_SECRET_KEY.length < 32) {
33!
23
    // pad secret key with spaces if it is less than 32 bytes
24
    return process.env.APP_SERVER_SECRET_KEY.padEnd(32, ' ');
33✔
25
  }
NEW
26
  if (process.env.APP_SERVER_SECRET_KEY.length > 32) {
×
27
    // truncate secret key if it is more than 32 bytes
NEW
28
    return process.env.APP_SERVER_SECRET_KEY.slice(0, 32);
×
29
  }
NEW
30
  return process.env.APP_SERVER_SECRET_KEY;
×
31
}
32

33
const originalSave = AuthToken.prototype.save;
7✔
34
AuthToken.prototype.save = async function () {
7✔
35
  if (this.data) {
18✔
36
    // encode data to encryptedData
37
    const cipher = crypto
10✔
38
      .createCipheriv('aes-256-cbc', getCipherKey(), Buffer.alloc(16, 0))
39
    this.encryptedData = cipher.update(this.data, 'utf8', 'hex') + cipher.final('hex');
10✔
40
    this.data = '';
10✔
41
  }
42
  return originalSave.call(this);
18✔
43
}
44

45
AuthToken.prototype.getDecryptedData = function () {
7✔
46
  if (!this.encryptedData) {
28✔
47
    // for backward compatibility
48
    return this.data;
5✔
49
  }
50
  // decode encryptedData to data
51
  const decipher = crypto
23✔
52
    .createDecipheriv('aes-256-cbc', getCipherKey(), Buffer.alloc(16, 0))
53
  return decipher.update(this.encryptedData, 'hex', 'utf8') + decipher.final('utf8');
23✔
54
}
55

56
AuthToken.prototype.removeData = function () {
7✔
57
  this.data = '';
4✔
58
  this.encryptedData = '';
4✔
59
}
60

61
exports.AuthToken = AuthToken;
7✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc