-
Notifications
You must be signed in to change notification settings - Fork 170
Add support for authenticating with OAuth Client Credential Grant #292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
gdbarron
merged 3 commits into
Snow-Shell:master
from
jmunroBT:oauth_client_credential_grant
Jul 18, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| $ProjectRoot = Resolve-Path "$PSScriptRoot/.." | ||
| $ModulePsd = (Resolve-Path "$ProjectRoot/ServiceNow/ServiceNow.psd1").Path | ||
|
|
||
| Get-Module 'ServiceNow' | Remove-Module -Force -ErrorAction SilentlyContinue | ||
| Import-Module $ModulePsd -Force | ||
|
|
||
| Describe 'New-ServiceNowSession OAuth Client Credentials Grant' { | ||
|
|
||
| BeforeAll { | ||
| $script:testClientCredential = [PSCredential]::new('myClientId', ([System.Net.NetworkCredential]::new('', 'myClientSecret').SecurePassword)) | ||
| } | ||
|
|
||
| Context 'Client credentials grant without proxy' { | ||
|
|
||
| It 'Authenticates using client_credentials grant type' { | ||
| Mock Invoke-WebRequest -ModuleName 'ServiceNow' { | ||
| [PSCustomObject]@{ | ||
| StatusCode = 200 | ||
| Content = '{"access_token": "abc123", "expires_in": 1800}' | ||
| } | ||
| } | ||
|
|
||
| $session = New-ServiceNowSession -Url 'test.service-now.com' -ClientCredential $script:testClientCredential -PassThru | ||
|
|
||
| $session.GrantType | Should -Be 'client_credentials' | ||
| $session.AccessToken | Should -Not -BeNullOrEmpty | ||
| $session.RefreshToken | Should -BeNullOrEmpty | ||
| $session.ClientCredential | Should -Be $script:testClientCredential | ||
|
|
||
| Should -Invoke Invoke-WebRequest -ModuleName 'ServiceNow' -Times 1 -Exactly -ParameterFilter { | ||
| $Body.grant_type -eq 'client_credentials' | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Context 'Client credentials grant with proxy' { | ||
|
|
||
| It 'Accepts ClientCredential and Proxy without requiring Credential' { | ||
| Mock Invoke-WebRequest -ModuleName 'ServiceNow' { | ||
| [PSCustomObject]@{ | ||
| StatusCode = 200 | ||
| Content = '{"access_token": "abc123", "expires_in": 1800}' | ||
| } | ||
| } | ||
|
|
||
| $session = New-ServiceNowSession -Url 'test.service-now.com' -ClientCredential $script:testClientCredential -Proxy 'http://proxy.example.com:8080' -PassThru | ||
|
|
||
| $session.GrantType | Should -Be 'client_credentials' | ||
| $session.Proxy | Should -Be 'http://proxy.example.com:8080' | ||
|
|
||
| Should -Invoke Invoke-WebRequest -ModuleName 'ServiceNow' -Times 1 -Exactly -ParameterFilter { | ||
| $Proxy -eq 'http://proxy.example.com:8080' | ||
| } | ||
| } | ||
|
|
||
| It 'Accepts ClientCredential, Proxy, and ProxyCredential together' { | ||
| Mock Invoke-WebRequest -ModuleName 'ServiceNow' { | ||
| [PSCustomObject]@{ | ||
| StatusCode = 200 | ||
| Content = '{"access_token": "abc123", "expires_in": 1800}' | ||
| } | ||
| } | ||
|
|
||
| $proxyCred = [PSCredential]::new('proxyuser', ([System.Net.NetworkCredential]::new('', 'proxypass').SecurePassword)) | ||
|
|
||
| { New-ServiceNowSession -Url 'test.service-now.com' -ClientCredential $script:testClientCredential -Proxy 'http://proxy.example.com:8080' -ProxyCredential $proxyCred -PassThru } | Should -Not -Throw | ||
| } | ||
| } | ||
|
|
||
| Context 'Password grant still works alongside client credentials support' { | ||
|
|
||
| It 'Authenticates using password grant type when Credential and ClientCredential are both provided' { | ||
| Mock Invoke-WebRequest -ModuleName 'ServiceNow' { | ||
| [PSCustomObject]@{ | ||
| StatusCode = 200 | ||
| Content = '{"access_token": "abc123", "refresh_token": "refresh456", "expires_in": 1800}' | ||
| } | ||
| } | ||
|
|
||
| $userCred = [PSCredential]::new('user', ([System.Net.NetworkCredential]::new('', 'pass').SecurePassword)) | ||
|
|
||
| $session = New-ServiceNowSession -Url 'test.service-now.com' -Credential $userCred -ClientCredential $script:testClientCredential -PassThru | ||
|
|
||
| $session.GrantType | Should -Be 'password' | ||
| $session.RefreshToken | Should -Not -BeNullOrEmpty | ||
|
|
||
| Should -Invoke Invoke-WebRequest -ModuleName 'ServiceNow' -Times 1 -Exactly -ParameterFilter { | ||
| $Body.grant_type -eq 'password' | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Describe 'Get-ServiceNowAuth Token Refresh Logic' { | ||
|
|
||
| Context 'Client credentials grant refresh' { | ||
|
|
||
| It 'Re-authenticates using client_credentials when token expired and no refresh token exists' { | ||
| Mock Invoke-RestMethod -ModuleName 'ServiceNow' { | ||
| [PSCustomObject]@{ | ||
| access_token = 'newtoken123' | ||
| expires_in = 1800 | ||
| } | ||
| } | ||
|
|
||
| $session = @{ | ||
| Domain = 'test.service-now.com' | ||
| BaseUri = 'https://test.service-now.com/api/' | ||
| Version = '' | ||
| ClientCredential = [PSCredential]::new('clientid', ([System.Net.NetworkCredential]::new('', 'secret').SecurePassword)) | ||
| GrantType = 'client_credentials' | ||
| ExpiresOn = (Get-Date).AddMinutes(-5) | ||
| } | ||
|
|
||
| InModuleScope 'ServiceNow' -Parameters @{ session = $session } { | ||
| param($session) | ||
| $result = Get-ServiceNowAuth -ServiceNowSession $session | ||
| $result | Should -Not -BeNullOrEmpty | ||
| $session.AccessToken | Should -Not -BeNullOrEmpty | ||
| } | ||
|
|
||
| Should -Invoke Invoke-RestMethod -ModuleName 'ServiceNow' -Times 1 -Exactly -ParameterFilter { | ||
| $Body.grant_type -eq 'client_credentials' | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Context 'Password grant refresh' { | ||
|
|
||
| It 'Uses refresh_token grant when a refresh token is available' { | ||
| Mock Invoke-RestMethod -ModuleName 'ServiceNow' { | ||
| [PSCustomObject]@{ | ||
| access_token = 'newtoken123' | ||
| refresh_token = 'newrefresh456' | ||
| expires_in = 1800 | ||
| } | ||
| } | ||
|
|
||
| $session = @{ | ||
| Domain = 'test.service-now.com' | ||
| BaseUri = 'https://test.service-now.com/api/' | ||
| Version = '' | ||
| ClientCredential = [PSCredential]::new('clientid', ([System.Net.NetworkCredential]::new('', 'secret').SecurePassword)) | ||
| RefreshToken = [PSCredential]::new('RefreshToken', ([System.Net.NetworkCredential]::new('', 'oldrefresh').SecurePassword)) | ||
| GrantType = 'password' | ||
| ExpiresOn = (Get-Date).AddMinutes(-5) | ||
| } | ||
|
|
||
| InModuleScope 'ServiceNow' -Parameters @{ session = $session } { | ||
| param($session) | ||
| $null = Get-ServiceNowAuth -ServiceNowSession $session | ||
| $session.RefreshToken | Should -Not -BeNullOrEmpty | ||
| } | ||
|
|
||
| Should -Invoke Invoke-RestMethod -ModuleName 'ServiceNow' -Times 1 -Exactly -ParameterFilter { | ||
| $Body.grant_type -eq 'refresh_token' | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Context 'No refresh method available' { | ||
|
|
||
| It 'Warns and does not call Invoke-RestMethod when no refresh token or client credentials grant type exists' { | ||
| Mock Invoke-RestMethod -ModuleName 'ServiceNow' {} | ||
| Mock Write-Warning -ModuleName 'ServiceNow' {} | ||
|
|
||
| $session = @{ | ||
| Domain = 'test.service-now.com' | ||
| BaseUri = 'https://test.service-now.com/api/' | ||
| Version = '' | ||
| ClientCredential = [PSCredential]::new('clientid', ([System.Net.NetworkCredential]::new('', 'secret').SecurePassword)) | ||
| AccessToken = [PSCredential]::new('AccessToken', ([System.Net.NetworkCredential]::new('', 'staletoken').SecurePassword)) | ||
| GrantType = 'password' | ||
| ExpiresOn = (Get-Date).AddMinutes(-5) | ||
| } | ||
|
|
||
| InModuleScope 'ServiceNow' -Parameters @{ session = $session } { | ||
| param($session) | ||
| $null = Get-ServiceNowAuth -ServiceNowSession $session | ||
| } | ||
|
|
||
| Should -Invoke Invoke-RestMethod -ModuleName 'ServiceNow' -Times 0 | ||
| Should -Invoke Write-Warning -ModuleName 'ServiceNow' -Times 1 | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we'll need to add a OAuthClientCredentialProxy paramset for ClientCredential, Proxy, and ProxyCredential params.