Public/Invoke-IamAuthenticate.ps1

<#
    .SYNOPSIS
    Authenticate agaist HSDP IAM as configured for the current Salesforce org
 
    .DESCRIPTION
    Uses the current org configuration to autenticate against the phecc__Access_Token_URL__c URL
    configured in the current configured Salesforce org.
 
    .INPUTS
    None. You cannot pipe objects to Invoke-IamAuthenticate.
 
    .OUTPUTS
    Returns as PSCustomObject with the following memebers:
        access_token
        expires_in
        refresh_token
        scope
        token_type
 
    .EXAMPLE
    PS> $auth = Invoke-IamAuthenticate
 
    .LINK
    Set-FileConfig
 
    .NOTES
    Assumes config is initialized for org access.
#>

function Invoke-IamAuthenticate {
    $config = Get-Config
    $sfHsdpConfig = Get-SfHsdpConfig
    $authForToken = [convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($sfHsdpConfig.phecc__Key__c):$($sfHsdpConfig.phecc__Secret__c)"))
    $Headers = @{
        "api-version"="2"
        "Content-Type"="application/x-www-form-urlencoded; charset=UTF-8"
        "Accept"="application/json"
        "Authorization"="Basic $($authForToken)"
    }
    $Form = @{
        "grant_type"= "password"
        "username"=$config.IamCredentials.GetNetworkCredential().username
        "password"=$config.IamCredentials.GetNetworkCredential().password
        "scope"=($config.scopes -Join " ")
    }
    Invoke-RestMethod -Uri $sfHsdpConfig.phecc__Access_Token_URL__c -Method Post -Body $Form -Headers $Headers
}