Classes/VMWare.Auth.psm1

using module .\VMWare.Settings.psm1
class VMWareAuth {
    # Singleton Storage
    static [VMWareAuth] $Instance

    # Parameters
    [PSCredential]$Credential
    [Object]$VSphere

    # Instatiator
    static [VMWareAuth] NewInstance([PSCredential]$Credential, [String]$VSphere) {
        # Remove existing instance
        [VMWareAuth]::ClearInstance()

        # Create new instance and validate
        $Auth = [VMWareAuth]::new()
        $Auth.Credential = $Credential
        $Auth.VSphere = $VSphere
        $Auth.ValidateLogin()

        [VMWareAuth]::instance = $Auth
        return [VMWareAuth]::instance
    }

    static [Void] ClearInstance() {
        [VMWareAuth]::instance = $null
    }

    # Get Instance or return error requesting login
    static [VMWareAuth] GetInstance() {
        if ($null -eq [VMWareAuth]::instance) {
            # Try loading saved settings
            if ([VMWareAuth]::load()) {
                return [VMWareAuth]::instance
            }
            else {
                throw 'You are not logged in. Please login with Login-VSphere.'
            }
        }
        else {
            return [VMWareAuth]::instance
        }
    }

    # Test the credentials and save the user object
    [Void] ValidateLogin() {
        try {
            Connect-VIServer -Server $this.VSphere -User $this.Credential.GetNetworkCredential().UserName -Password $this.Credential.GetNetworkCredential().Password -ErrorAction Stop
        }
        catch {
            Write-Error $_
            throw 'Login Failed - credentials are invalid. Use Login-VSphere to re-authenticate.'
        }
    }

    [Void] UpdateVSphere([String]$VSphere) {
        $this.VSphere = $VSphere
        $this.ValidateLogin()
    }

    # Save the settings to the local system
    [void] Save() {
        if (!(Test-Path([VMWareSettings]::SAVE_DIR))) {
            New-Item -Type Directory -Path ([VMWareSettings]::SAVE_DIR)
        }

        # Create a filtered object to save
        $Save = [PSCustomObject]@{
            Credential = $this.Credential
            VSphere    = $this.VSphere
        }

        $Save | Export-CliXml -Path ([VMWareSettings]::SAVE_PATH)  -Encoding 'utf8' -Force
    }

    # Save the settings to the local system
    static [void] Delete() {
        # Validate we are deleting a file and things match...
        $file = Get-Item -Type File -path [VMWareSettings]::SAVE_PATH
        if ($null -ne $file && $file.name -eq [VMWareSettings]::SAVE_FILE) {
            Remove-Item [VMWareSettings]::SAVE_PATH
        }
    }

    # Load Saved Credentials
    hidden static [VMWareAuth] Load() {
        if (Test-Path([VMWareSettings]::SAVE_PATH)) {
            try {
                $Import = Import-CliXml -Path ([VMWareSettings]::SAVE_PATH)
            }
            catch {
                Write-Warning 'Failed to load settings'
                Return $null
            }

            $Auth = [VMWareAuth]::new()
            $Auth.Credential = $Import.Credential
            $Auth.VSphere = $Import.VSphere
            $Auth.ValidateLogin()

            [VMWareAuth]::instance = $Auth
            return [VMWareAuth]::instance
        }
        return $null
    }
}