Private/Connect-SF.ps1
|
function Connect-SF { [CmdletBinding()] [OutputType([void])] param ( [Parameter(Mandatory)] $ConnectorConfiguration, [switch]$ForceRenewal ) $baseUrl = [string]$ConnectorConfiguration.configuration.baseurl if ([string]::IsNullOrWhiteSpace($baseUrl)) { throw "baseurl is required." } $clientId = [string]$ConnectorConfiguration.configuration.clientid if ([string]::IsNullOrWhiteSpace($clientId)) { throw "clientid is required." } $companyId = [string]$ConnectorConfiguration.configuration.companyid if ([string]::IsNullOrWhiteSpace($companyId)) { throw "companyid is required." } $authFlow = [string]$ConnectorConfiguration.configuration.authflow if ([string]::IsNullOrWhiteSpace($authFlow)) { $authFlow = 'Assertion' } if ($authFlow -notin @('Assertion', 'ClientCredentials')) { throw "authflow must be 'Assertion' or 'ClientCredentials'." } if ($authFlow -eq 'Assertion') { $userId = [string]$ConnectorConfiguration.configuration.userid if ([string]::IsNullOrWhiteSpace($userId)) { throw "userid is required when authflow is 'Assertion'." } $privateKey = [string]$ConnectorConfiguration.secrets.privatekey if ([string]::IsNullOrWhiteSpace($privateKey)) { throw "secrets.privatekey is required when authflow is 'Assertion'." } $sfAssertion = Get-SFAssertion ` -BaseUrl $baseUrl ` -UserId $userId ` -ClientId $clientId ` -TokenEndpointUrl "$baseUrl/oauth/token" ` -PrivateKey $privateKey $sfToken = Get-SFToken ` -BaseUrl $baseUrl ` -ClientId $clientId ` -CompanyId $companyId ` -Assertion $sfAssertion ` -ForceNew:$ForceRenewal } else { $clientSecret = [string]$ConnectorConfiguration.secrets.clientsecret if ([string]::IsNullOrWhiteSpace($clientSecret)) { throw "secrets.clientsecret is required when authflow is 'ClientCredentials'." } $sfToken = Get-SFToken ` -BaseUrl $baseUrl ` -ClientId $clientId ` -CompanyId $companyId ` -ClientSecret $clientSecret ` -ForceNew:$ForceRenewal } if (-not $sfToken -or -not $sfToken.access_token) { throw "SuccessFactors token response did not contain 'access_token'." } $Script:ConnectorConfiguration = $ConnectorConfiguration $Script:AccessToken = $sfToken.access_token $expiresIn = $sfToken.expires_in ? [int]$sfToken.expires_in : 3600 $Script:AccessTokenExpiry = (Get-Date).AddSeconds([Math]::Max(0, $expiresIn - 60)) } |