AutomateAPI icon indicating copy to clipboard operation
AutomateAPI copied to clipboard

Credential handling needs reworked

Open jasonrush opened this issue 6 years ago • 1 comments

Credential storage to local disk does not include Control credentials (#32), and helper functions are not intuitive nor follow Microsoft Verb best practices. See example code below for possible additional ideas:

#region *-CwaCredential
function Set-CwaCredential {
    [CmdletBinding()]
    Param (
        [parameter(Mandatory=$true,ValueFromPipelineByPropertyName,ValueFromPipeline)]
        [ValidateNotNull()]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]
        $Credential = [System.Management.Automation.PSCredential]::Empty  
    )

    # Store credential for later use.
    Write-Verbose "Credential username = '$($Credential.UserName)'"
    $Script:CwaCredential = $credential
}

function Get-CwaCredential {
    [CmdletBinding()]
    Param (
    )

    $Script:CwaCredential.UserName
}

function Remove-CwaCredential {
    [CmdletBinding()]
    Param (
    )

    $Script:CwaCredential = $null
}

function Save-CwaCredential {
    [CmdletBinding()]
    Param (
        [String]
        $Path = "$env:USERPROFILE/.CwaCredential.xml"
    )

    $Script:CwaCredential | Export-CliXml -Path $path
}

function Load-CwaCredential {
    [CmdletBinding()]
    Param (
        [String]
        $Path = "$env:USERPROFILE/.CwaCredential.xml"
    )

    if( Test-Path -Path $Path ){
        $Script:CwaCredential = Import-CliXml -Path $Path
    }
}



#endregion *-CwaCredential

jasonrush avatar Oct 22 '19 02:10 jasonrush

Completely agree - they are significantly over complicated. I like your suggestions and their simplicity @jasonrush

gavsto avatar Dec 18 '19 01:12 gavsto