Source: database/Models/JWT/TokenRepository.js

  1. const Token = require('./Token.js');
  2. /**
  3. * DB Connection to the invalidToken Table
  4. */
  5. class TokenRepository {
  6. /**
  7. * @constructor
  8. * @param {Database} db sqlite connection
  9. */
  10. constructor(db) {
  11. this.db = db;
  12. }
  13. /**
  14. * Insert a new invalid Token to the DB
  15. * @param {Token} token
  16. * @returns {object} an info object describing any changes made
  17. */
  18. async insert(token) {
  19. await this.cleanup();
  20. const sql = "INSERT INTO invalidToken(token) VALUES (?)";
  21. return await this.db.run(sql, [token.token]);
  22. }
  23. /**
  24. * Search for a token in the db
  25. * @param {Token} token
  26. * @returns Token
  27. */
  28. async selectToken(token) {
  29. await this.cleanup();
  30. const sql = "SELECT * FROM invalidToken WHERE invalidToken.token = ?"
  31. return await this.db.get(sql, [token.token]);
  32. }
  33. /**
  34. * Get all invalid token in the DB
  35. * @returns Array.<Token>
  36. */
  37. async selectAllToken() {
  38. await this.cleanup();
  39. const sql = "SELECT * FROM invalidToken"
  40. return await this.db.getAll(sql, []);
  41. }
  42. /**
  43. * Remove all token that are invalid by them self
  44. */
  45. async cleanup() {
  46. const currentUnixTime = Date.now();
  47. const sql = "DELETE FROM invalidToken WHERE expiration > ?";
  48. await this.db.run(sql, [currentUnixTime]);
  49. }
  50. }
  51. module.exports = TokenRepository;