token.js 450 B

123456789101112131415161718192021222324
  1. /**
  2. * Validates a device token
  3. *
  4. * Will convert to string and removes invalid characters as required.
  5. */
  6. function token(input) {
  7. let token;
  8. if (typeof input === 'string') {
  9. token = input;
  10. } else if (Buffer.isBuffer(input)) {
  11. token = input.toString('hex');
  12. }
  13. token = token.replace(/[^0-9a-f]/gi, '');
  14. if (token.length === 0) {
  15. throw new Error('Token has invalid length');
  16. }
  17. return token;
  18. }
  19. module.exports = token;