Skip to content

Commit baaad16

Browse files
Add ExpiresOn property to ServiceNow session (#276)
While this change doesn't handle re-authentication automatically, it makes session management significantly easier in code by providing clear token expiration visibility and a consistent way to check if a new session is needed.
1 parent 6280337 commit baaad16

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

ServiceNow/Public/New-ServiceNowSession.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ function New-ServiceNowSession {
197197
$token = $response.Content | ConvertFrom-Json
198198
$newSession.Add('AccessToken', (New-Object System.Management.Automation.PSCredential('AccessToken', ($token.access_token | ConvertTo-SecureString -AsPlainText -Force))))
199199
$newSession.Add('RefreshToken', (New-Object System.Management.Automation.PSCredential('RefreshToken', ($token.refresh_token | ConvertTo-SecureString -AsPlainText -Force))))
200+
if ($token.expires_in) {
201+
$expiryTime = (Get-Date).AddSeconds($token.expires_in)
202+
$newSession.Add('ExpiresOn', $expiryTime)
203+
Write-Verbose "Token will expire at $expiryTime"
204+
}
200205
}
201206
else {
202207
# invoke-webrequest didn't throw an error, but we didn't get a token back either

0 commit comments

Comments
 (0)