1. /**
  2. * Cleans authentication tokens for scheduled runs, with defensive checks.
  3. *
  4. * @param {Array<string>} tokens An array of authentication tokens.
  5. * @returns {Array<string>} A cleaned array of tokens, with invalid or expired tokens removed.
  6. */
  7. function cleanAuthTokens(tokens) {
  8. if (!Array.isArray(tokens)) {
  9. console.error("Invalid input: tokens must be an array."); // Defensive check
  10. return [];
  11. }
  12. const cleanedTokens = [];
  13. for (const token of tokens) {
  14. if (typeof token !== 'string') {
  15. console.warn("Invalid token type: Skipping non-string token."); // Defensive check
  16. continue;
  17. }
  18. const trimmedToken = token.trim(); // Remove leading/trailing whitespace
  19. if (!trimmedToken) {
  20. console.warn("Empty token after trimming: Skipping."); // Defensive check
  21. continue;
  22. }
  23. if (isValidToken(trimmedToken)) {
  24. cleanedTokens.push(trimmedToken);
  25. } else {
  26. console.warn("Invalid token: Skipping:", trimmedToken); // Defensive check
  27. }
  28. }
  29. return cleanedTokens;
  30. /**
  31. * Placeholder function for validating a token. Replace with your actual validation logic.
  32. * This is a simplified example.
  33. * @param {string} token
  34. * @returns {boolean}
  35. */
  36. function isValidToken(token) {
  37. // Replace with your token validation logic (e.g., JWT verification, API call)
  38. // This is a placeholder - adjust based on your token type and validation method
  39. if (token.startsWith("valid_")) { //Example check
  40. return true;
  41. }
  42. return false;
  43. }
  44. }

Add your comment