Public/Initialize-iPilotSession.ps1

Function Initialize-iPilotSession {
    <#
        .Synopsis
        Prompts the user for iPilot credentials including APIKey and storing securely for later sessions

        .Description
        Helper function for iPilot module to retrieve api and credentials either from file, global or prompting the user

        .Parameter ApiUrl
        URL of iPilot API

        .Parameter Credential
        Credentials for iPilot API

        .Parameter Refresh
        Ask and store new credentials instead of using saved

        .Parameter SaveToFile
        Will save Credentials and ApiKey to iPilot Data Directory

        .Example
        # Get iPilot ApiKey and iPilot Credential and save to iPilot Data Directory
        Initialize-iPilotSession -ApiUrl https://api.nuwave.com -SaveToFile
    #>

    Param (
        [System.String]
            $ApiUrl = "https://api.nuwave.com",   
        [System.String]
            $ApiVersion = "v1",
        [System.String]
            $ApiKey, # if not specified, will prompt user
        [System.String]
            $iPilotDataDirectory = "${env:APPDATA}\NuWave",
        [System.Management.Automation.PSCredential]
            $Credential, # if not specified, will prompt user
        [Switch] 
            $Refresh,
        [Switch] 
            $SaveToFile,
        [Switch]
            $Verbose,
        [Switch]
            $Debug
    )

    # Verbose Switch
    if($PSBoundParameters.containskey("Verbose")) {
        $PreviousVerbosePreference = $VerbosePreference
        $VerbosePreference = "continue"
    }

    # Debug Switch
    if($PSBoundParameters.containskey("Debug")) {
        $PreviousDebugPreference = $DebugPreference
        $DebugPreference = "continue"
    }

    # if oauth iPilotOAuthToken is valid and not expired, nothing for us to do unless explicitly told to refresh
    if ($global:iPilotOAuthToken -and $global:iPilotApiKey) {
        if (!$Refresh.IsPresent) {
            $now = Get-Date
            if ($global:iPilotOAuthToken.OAuthTokenExpirationDateTime -ge ($now.ToUniversalTime())) {
                Write-Debug "iPilotOAuthToken not expired"
                # nothing to do
                return 
            } else {
                Write-Debug "iPilotOAuthToken expired, renewing token"
            }
        } else {
            Write-Debug "Refreshing iPilotOAuthToken"
            $global:iPilotOAuthToken = $iPilotOAuthToken = $null
        }
    } else {
        Write-Debug "Missing iPilotOAuthToken token"
    }

    # make company directory in user profile to store files
    if ( !(Test-Path -Path "$iPilotDataDirectory") ) {
        New-Item -ItemType Directory -Force -Path $iPilotDataDirectory | Out-Null
    }

    if ($ApiKey -or $Credential) {
        Write-Debug "Using explicit credentials and/or ApiKey"
        if (!$ApiKey) {
            Write-Debug "Prompt for ApiKey"
            $ApiKeyCredential = Get-Credential -UserName "ApiKey" -Message "Enter API Key provided to you by NuWave Communications"
            # decrypt the apikey
            $ApiKey = $ApiKeyCredential.GetNetworkCredential().Password
        } 
        if (!$Credential) {
            Write-Debug "Prompt user for NuWave iPilot credentials"
            $Credential = Get-Credential -Message "Enter NuWave iPilot username and password"
        } 

        # Save ApiKey and iPilot credential
        if ($SaveToFile) {
            $ApiKeyCredential, $Credential | Export-Clixml -Path "$iPilotDataDirectory\iPilot.cred" -Force
        }
    } else {
        Write-Debug "Prompt or retrieve from iPilot.cred"

        # They didnt specify credentials so either read or prompt the user for apikey and ipilot credentials
        if ( (!$Refresh.IsPresent) -and (Test-Path -Path "$iPilotDataDirectory\iPilot.cred")) {
            Write-Verbose "Reading ApiKey and Credential from iPilot.cred"
            # load credentials from file
            $ApiKeyCredential, $Credential = Import-Clixml -Path "$iPilotDataDirectory\iPilot.cred"
            $global:iPilotApiKeyCredential = $ApiKeyCredential
            $global:iPilotCredential = $Credential
        } else {
            Write-Debug "Asking for ApiKey and Credential"
            $ApiKeyCredential = Get-Credential -UserName "ApiKey" -Message "Enter API Key provided to you by NuWave Communications"
            $Credential = Get-Credential                          -Message "Enter NuWave iPilot username and password"

            # Save ApiKey and iPilot credential
            if ($SaveToFile) {
                $ApiKeyCredential, $Credential | Export-Clixml -Path "$iPilotDataDirectory\iPilot.cred" -Force
            }
        }
        # Decrypt the ApiKey
        $ApiKey = $ApiKeyCredential.GetNetworkCredential().Password
    }

    # Create printable version of ApiKey
    if ($ApiKey.length -gt 9) {
        $PrintableApiKey = "..." + $ApiKey.SubString($ApiKey.length - 6) # only grab last 6
    } else {
        $PrintableApiKey = $ApiKey  # not really an ApiKey so print all of it
    }

    # Attempt to log in and get iPilotOAuthToken
    Write-Debug "Logging in user $($Credential.UserName) with ApiKey ending in $PrintableApiKey"
    $global:iPilotOAuthToken = Get-iPilotOAuthToken -Credential $Credential -ApiKey $ApiKey -ApiUrl $ApiUrl -ApiVersion $ApiVersion
    Write-Verbose "iPilot OAuth Token Expires on: $(Get-Date $global:iPilotOAuthToken.OAuthTokenExpirationDateTime -Format g)"

    # and store the api key since commands need it to prevent user from having to type or specify everytime.
    $global:iPilotApiKey = $ApiKey

    # Verbose Switch
    if($PSBoundParameters.containskey("Verbose")) {
        $VerbosePreference = $PreviousVerbosePreference
    }

    # Debug Switch
    if($PSBoundParameters.containskey("Debug")) {
        $DebugPreference = $PreviousDebugPreference
    }
}