diff --git a/Server-Side Components/Scheduled Jobs/API Token Expiry Warning/API Token Expiry Warning.js b/Server-Side Components/Scheduled Jobs/API Token Expiry Warning/API Token Expiry Warning.js new file mode 100644 index 0000000000..6b43987d6b --- /dev/null +++ b/Server-Side Components/Scheduled Jobs/API Token Expiry Warning/API Token Expiry Warning.js @@ -0,0 +1,53 @@ +(function() { + + // Configuration via system properties + var warningDays = parseInt(gs.getProperty('api.token.expiry.warning.days', '7'), 10); // Days before expiry to warn + var emailRecipients = gs.getProperty('api.token.expiry.email.recipients', 'admin@example.com'); // Comma-separated emails + + // Current time and warning threshold time + var now = new GlideDateTime(); + var warningDate = new GlideDateTime(); + warningDate.addDays(warningDays); + + // Query oauth_credential records with expires_on between now and warningDate + var gr = new GlideRecord('oauth_credential'); + gr.addQuery('expires_on', '>=', now); + gr.addQuery('expires_on', '<=', warningDate); + gr.addQuery('active', '=', true); // Only active tokens + gr.orderBy('expires_on'); + gr.query(); + + if (!gr.hasNext()) { + gs.info('No OAuth credentials nearing expiry within ' + warningDays + ' days.'); + return; + } + + // Build notification email body + var emailBody = '

API Token Expiry Warning

'; + emailBody += '

The following OAuth credentials are set to expire within ' + warningDays + ' days:

'; + emailBody += ''; + emailBody += ''; + + while (gr.next()) { + emailBody += ''; + emailBody += ''; + emailBody += ''; + emailBody += ''; + emailBody += ''; + emailBody += ''; + } + emailBody += '
NameUserClient IDExpires On
' + gr.getDisplayValue('name') + '' + gr.getDisplayValue('user') + '' + gr.getValue('client_id') + '' + gr.getDisplayValue('expires_on') + '
'; + emailBody += '

Please review and renew tokens to avoid integration failures.

'; + + // Send the email + var mail = new GlideEmailOutbound(); + mail.setFrom('no-reply@yourdomain.com'); + mail.setSubject('[ServiceNow] OAuth API Token Expiry Warning'); + mail.setTo(emailRecipients); + mail.setBody(emailBody); + mail.setContentType('text/html'); + mail.send(); + + gs.info('OAuth token expiry warning email sent to: ' + emailRecipients); + +})(); diff --git a/Server-Side Components/Scheduled Jobs/API Token Expiry Warning/Readme.md b/Server-Side Components/Scheduled Jobs/API Token Expiry Warning/Readme.md new file mode 100644 index 0000000000..4ec5e8a490 --- /dev/null +++ b/Server-Side Components/Scheduled Jobs/API Token Expiry Warning/Readme.md @@ -0,0 +1,5 @@ +API Token Expiry Warning Script +**Overview** +This script provides proactive monitoring and alerting for OAuth API tokens stored in the ServiceNow oauth_credential table. It identifies tokens nearing expiry within a configurable countdown window and sends notification emails to administrators, helping prevent sudden integration failures due to expired credentials. +**Problem Solved** +API tokens used for integrations have expiration timestamps after which they become invalid. Without early warnings, tokens can expire unnoticed, causing integration outages, failed API calls, and increased support incidents. This solution enables administrators to receive timely alerts allowing proactive token renewal and smoother integration continuity.