AuthCommands.ps1

#requires -Version 5.1

# Handles required actions after login, such as changing an expired or recovered Master Password.
# This lets the user complete these steps interactively instead of having the login fail with
# an "expired, you are required to change it" message.
class KeeperPostLoginUI : KeeperSecurity.Authentication.Sync.IAuthSyncCallback,
    KeeperSecurity.Authentication.IPostLoginTaskUI, KeeperSecurity.Authentication.IAuthSecurityKeyUI,
    KeeperSecurity.Utils.IMasterPasswordStrengthUI, KeeperSecurity.Utils.IMasterPasswordReentryUI {
    [string] $WebAuthnOrigin

    KeeperPostLoginUI([string] $webAuthnOrigin) {
        $this.WebAuthnOrigin = $webAuthnOrigin
    }

    [void] OnNextStep() {
    }

    [System.Threading.Tasks.Task[string]] AuthenticatePublicKeyRequest(
        [KeeperSecurity.Authentication.PublicKeyCredentialRequestOptions] $request) {
        $typeName = 'KeeperSecurity.Authentication.WindowsWebAuthnSecurityKeyUI'
        $handlerType = [AppDomain]::CurrentDomain.GetAssemblies() |
            ForEach-Object { $_.GetType($typeName, $false) } |
            Where-Object { $null -ne $_ } |
            Select-Object -First 1
        if ($null -eq $handlerType) {
            throw [System.InvalidOperationException]::new('Windows WebAuthn support was not initialized.')
        }
        $arguments = [object[]]::new(2)
        $arguments[0] = $request
        $arguments[1] = $this.WebAuthnOrigin
        $task = $handlerType.GetMethod('AuthenticatePublicKeyRequest').Invoke($null, $arguments)
        return [System.Threading.Tasks.Task[string]]$task
    }

    [System.Threading.Tasks.Task[bool]] Confirmation([string] $information) {
        Write-Host ''
        Write-Host $information -ForegroundColor Yellow

        $answer = Read-Host 'Do you want to continue? (y/n)'
        return [System.Threading.Tasks.Task]::FromResult([bool]($answer -match '^y(es)?$'))
    }

    [System.Threading.Tasks.Task[string]] GetNewPassword([KeeperSecurity.Utils.PasswordRuleMatcher] $matcher) {
        Write-Host ''
        Write-Host 'Please set a new Master Password.' -ForegroundColor Yellow
        while ($true) {
            $secured = Read-Host -Prompt 'New Master Password' -AsSecureString
            $newPassword = [Net.NetworkCredential]::new('', $secured).Password

            if ($null -eq $newPassword) {
                throw [System.OperationCanceledException]::new('Input cancelled.')
            }

            if ([string]::IsNullOrEmpty($newPassword)) {
                Write-Warning 'Password cannot be empty.'
                continue
            }

            $confirmSecured = Read-Host -Prompt 'Confirm New Master Password' -AsSecureString
            $confirmPassword = [Net.NetworkCredential]::new('', $confirmSecured).Password

            if ($null -eq $confirmPassword) {
                throw [System.OperationCanceledException]::new('Input cancelled.')
            }

            if ($newPassword -ne $confirmPassword) {
                Write-Warning 'Passwords do not match.'
                continue
            }

            $failedRules = $matcher.MatchFailedRules($newPassword)
            if ($failedRules -and $failedRules.Count -gt 0) {
                $failedRules | ForEach-Object { Write-Warning $_ }
                continue
            }

            return [System.Threading.Tasks.Task]::FromResult($newPassword)
        }
        throw [System.InvalidOperationException]::new('unreachable')
    }

    [System.Threading.Tasks.Task[string]] GetCurrentPassword([System.Func[string, bool]] $validatePassword) {
        if ($null -eq $validatePassword) {
            throw [System.ArgumentNullException]::new('validatePassword')
        }

        while ($true) {
            $secured = Read-Host -Prompt 'Current Master Password' -AsSecureString
            $currentPassword = [Net.NetworkCredential]::new('', $secured).Password

            if ($null -eq $currentPassword) {
                throw [System.OperationCanceledException]::new('Input cancelled.')
            }

            if ($validatePassword.Invoke($currentPassword)) {
                return [System.Threading.Tasks.Task]::FromResult($currentPassword)
            }

            Write-Warning 'Current Master Password is incorrect. Please try again.'
        }
        throw [System.InvalidOperationException]::new('unreachable')
    }

    [System.Threading.Tasks.Task] ReportMasterPasswordStrength([int] $score, [string] $category) {
        if ($category -eq 'UNKNOWN') {
            Write-Host 'Master password strength could not be determined.' -ForegroundColor Gray
            return [System.Threading.Tasks.Task]::CompletedTask
        }
        $color = switch ($category) {
            'WEAK' { 'Red' }
            'FAIR' { 'Yellow' }
            'MEDIUM' { 'Cyan' }
            'STRONG' { 'Green' }
            default { 'Gray' }
        }
        Write-Host "Master password strength: $category ($score/100)" -ForegroundColor $color
        return [System.Threading.Tasks.Task]::CompletedTask
    }
}

function New-SqliteIdbConnectionFunc {
    param([Parameter(Mandatory)][string] $ConnectionString)
    $connType = [Microsoft.Data.Sqlite.SqliteConnection]
    $dm = [System.Reflection.Emit.DynamicMethod]::new(
        'PcOpenSqliteConnection',
        [System.Data.IDbConnection],
        [Type[]]@(),
        [KeeperSecurity.Vault.SqlKeeperStorage])
    $il = $dm.GetILGenerator()
    $il.Emit([System.Reflection.Emit.OpCodes]::Ldstr, $ConnectionString)
    $il.Emit([System.Reflection.Emit.OpCodes]::Newobj, $connType.GetConstructor(@([string])))
    $il.Emit([System.Reflection.Emit.OpCodes]::Dup)
    $openMi = [System.Data.Common.DbConnection].GetMethod('Open', [Type[]]@())
    $il.Emit([System.Reflection.Emit.OpCodes]::Callvirt, $openMi)
    $il.Emit([System.Reflection.Emit.OpCodes]::Ret)
    $dm.CreateDelegate([Func[System.Data.IDbConnection]])
}

function Initialize-SqliteStorageDependencies {
    param(
        [Parameter(Mandatory = $true)][string] $ErrorContext
    )
    $moduleRoot = $PSScriptRoot
    if ($MyInvocation.MyCommand.Module) { $moduleRoot = $MyInvocation.MyCommand.Module.ModuleBase }

    $storageUtilsRoot = Join-Path $moduleRoot 'StorageUtils'
    $requiredStorageDlls = @(
        'Microsoft.Data.Sqlite.dll',
        'SQLitePCLRaw.batteries_v2.dll',
        'SQLitePCLRaw.core.dll',
        'SQLitePCLRaw.provider.dynamic_cdecl.dll',
        'e_sqlite3.dll'
    )
    $missingFiles = [System.Collections.Generic.List[string]]::new()
    foreach ($fileName in $requiredStorageDlls) {
        $filePath = Join-Path $storageUtilsRoot $fileName
        if (-not (Test-Path -LiteralPath $filePath -PathType Leaf)) {
            $missingFiles.Add($fileName)
        }
    }

    $nativeSqlitePath = Join-Path $storageUtilsRoot 'e_sqlite3.dll'
    if (-not (Test-Path -LiteralPath $nativeSqlitePath -PathType Leaf)) {
        $missingFiles.Add('e_sqlite3.dll')
    }

    if ($missingFiles.Count -gt 0) {
        $missingList = $missingFiles -join ', '
        throw "Offline storage dependencies were not found in '$storageUtilsRoot'. Missing: $missingList. When using -UseOfflineStorage, copy the SQLite assemblies from a Commander net8.0 build into the 'StorageUtils' folder under the PowerCommander module directory."
    }

    if (-not $script:StorageUtilsAssemblyResolveRegistered) {
        $script:StorageUtilsAssemblyResolveRegistered = $true
        $sur = $storageUtilsRoot
        $mr = $moduleRoot
        $handler = [System.ResolveEventHandler] {
            param($AssemblyResolveSource, $AssemblyResolveEventArgs)
            $simpleName = ($AssemblyResolveEventArgs.Name -split ',')[0]
            foreach ($root in @($sur, $mr)) {
                $candidate = [System.IO.Path]::Combine($root, "$simpleName.dll")
                if ([System.IO.File]::Exists($candidate)) {
                    return [System.Reflection.Assembly]::LoadFrom($candidate)
                }
            }
            return $null
        }
        [System.AppDomain]::CurrentDomain.add_AssemblyResolve($handler)
    }

    if (-not $script:PcSqlitePclInitialized) {
        $batteriesPath = Join-Path $storageUtilsRoot 'SQLitePCLRaw.batteries_v2.dll'
        $batteriesAsm = [System.Reflection.Assembly]::LoadFrom($batteriesPath)
        $batteriesType = $batteriesAsm.GetType('SQLitePCL.Batteries_V2')
        if (-not $batteriesType) { throw "Could not load type SQLitePCL.Batteries_V2 from $batteriesPath" }
        $initMethod = $batteriesType.GetMethod('Init', [System.Reflection.BindingFlags]::Public -bor [System.Reflection.BindingFlags]::Static)
        [void]$initMethod.Invoke($null, @())
        $script:PcSqlitePclInitialized = $true
    }

    [void][System.Reflection.Assembly]::LoadFrom((Join-Path $storageUtilsRoot 'Microsoft.Data.Sqlite.dll'))
}

function Get-SqliteVaultStorageFromHelper {
    param([Parameter(Mandatory = $true)][string] $ConnectionString, [Parameter(Mandatory = $true)][string] $OwnerUid)

    Initialize-SqliteStorageDependencies -ErrorContext 'Offline storage'

    $getConnection = New-SqliteIdbConnectionFunc -ConnectionString $ConnectionString
    $dialect = [KeeperSecurity.Storage.SqliteDialect]::Instance
    $vaultStorage = New-Object KeeperSecurity.Vault.SqlKeeperStorage($getConnection, $dialect, $OwnerUid)

    $verifyConn = New-Object Microsoft.Data.Sqlite.SqliteConnection($ConnectionString)
    $verifyConn.Open()
    try {
        $schemas = @($vaultStorage.GetStorages() | ForEach-Object { $_.Schema })
        $failed = [KeeperSecurity.Storage.DatabaseUtils]::VerifyDatabase($verifyConn, $dialect, $schemas)
        if ($failed -and $failed.Count -gt 0) {
            [System.Diagnostics.Trace]::TraceError(($failed -join "`n"))
        }
    }
    finally {
        $verifyConn.Dispose()
    }

    return $vaultStorage
}

function Get-SqliteComplianceStorageFromHelper {
    param([Parameter(Mandatory = $true)][string] $ConnectionString)

    Initialize-SqliteStorageDependencies -ErrorContext 'Compliance storage'

    $getConnection = New-SqliteIdbConnectionFunc -ConnectionString $ConnectionString
    $dialect = [KeeperSecurity.Storage.SqliteDialect]::Instance
    $complianceStorage = New-Object KeeperSecurity.Compliance.SqlComplianceStorage($getConnection, $dialect)

    $verifyConn = New-Object Microsoft.Data.Sqlite.SqliteConnection($ConnectionString)
    $verifyConn.Open()
    try {
        [KeeperSecurity.Compliance.SqlComplianceStorage]::VerifyDatabase($verifyConn, $dialect)
    }
    finally {
        $verifyConn.Dispose()
    }

    return $complianceStorage
}

$expires = @(
    [KeeperSecurity.Authentication.TwoFactorDuration]::EveryLogin,
    [KeeperSecurity.Authentication.TwoFactorDuration]::Every30Days,
    [KeeperSecurity.Authentication.TwoFactorDuration]::Forever)

function Test-InteractiveSession {
    if ($psISE) {
        return $true
    }
    if ($PSIsInteractive) {
        return $true
    }
    if ($PSPrivateMetadata.JobId) {
        return $false
    }
    return $Host.UI.SupportsVirtualTerminal
}

function twoFactorChannelToText ([KeeperSecurity.Authentication.TwoFactorChannel] $channel) {
    if ($channel -eq [KeeperSecurity.Authentication.TwoFactorChannel]::Authenticator) {
        return 'authenticator'
    }
    if ($channel -eq [KeeperSecurity.Authentication.TwoFactorChannel]::TextMessage) {
        return 'sms'
    }
    if ($channel -eq [KeeperSecurity.Authentication.TwoFactorChannel]::DuoSecurity) {
        return 'duo'
    }
    if ($channel -eq [KeeperSecurity.Authentication.TwoFactorChannel]::RSASecurID) {
        return 'rsa'
    }
    if ($channel -eq [KeeperSecurity.Authentication.TwoFactorChannel]::KeeperDNA) {
        return 'dna'
    }
    if ($channel -eq [KeeperSecurity.Authentication.TwoFactorChannel]::SecurityKey) {
        return 'security key'
    }
    return ''
}

function deviceApprovalChannelToText ([KeeperSecurity.Authentication.DeviceApprovalChannel]$channel) {
    if ($channel -eq [KeeperSecurity.Authentication.DeviceApprovalChannel]::Email) {
        return 'email'
    }
    if ($channel -eq [KeeperSecurity.Authentication.DeviceApprovalChannel]::KeeperPush) {
        return 'keeper'
    }
    if ($channel -eq [KeeperSecurity.Authentication.DeviceApprovalChannel]::TwoFactorAuth) {
        return '2fa'
    }
    return ''
}

function twoFactorDurationToExpire ([KeeperSecurity.Authentication.TwoFactorDuration] $duration) {
    if ($duration -eq [KeeperSecurity.Authentication.TwoFactorDuration]::EveryLogin) {
        return 'now'
    }
    if ($duration -eq [KeeperSecurity.Authentication.TwoFactorDuration]::Forever) {
        return 'never'
    }
    if ($duration -eq [KeeperSecurity.Authentication.TwoFactorDuration]::Every30Days) {
        return '30_days'
    }
    return "---"
}


function getStepPrompt ([KeeperSecurity.Authentication.IAuthentication] $auth) {
    $prompt = "`nUnsupported ($($auth.step.State.ToString()))"
    if ($auth.step -is [KeeperSecurity.Authentication.Sync.DeviceApprovalStep]) {
        $prompt = "`nDevice Approval ($(deviceApprovalChannelToText $auth.step.DefaultChannel))"
    }
    elseif ($auth.step -is [KeeperSecurity.Authentication.Sync.TwoFactorStep]) {
        $channelText = twoFactorChannelToText $auth.step.DefaultChannel
        $prompt = "`n2FA channel($($channelText)) expire[$(twoFactorDurationToExpire $auth.step.Duration)]"
    }

    elseif ($auth.step -is [KeeperSecurity.Authentication.Sync.PasswordStep]) {
        $prompt = "`nMaster Password"
    }
    elseif ($auth.step -is [KeeperSecurity.Authentication.Sync.AccountRecoveryStep]) {
        if ($null -eq $auth.step.RecoveryType) {
            $prompt = "`nAccount Recovery: Verification Code"
        }
        elseif ($auth.step.RecoveryType -eq [Authentication.BackupKeyType]::BktSecAnswer) {
            $prompt = "`nSecurity Question: $($auth.step.SecurityQuestion)"
        }
        else {
            $prompt = "`nRecovery Phrase (24 words)"
        }
    }
    elseif ($auth.step -is [KeeperSecurity.Authentication.Sync.SsoTokenStep]) {
        $prompt = "`nSSO Token"
    }
    elseif ($auth.step -is [KeeperSecurity.Authentication.Sync.SsoDataKeyStep]) {
        $prompt = "`nSSO Login Approval"
    }
    elseif ($auth.step -is [KeeperSecurity.Authentication.Sync.ReadyToLoginStep]) {
        $prompt = "`nLogin"
    }

    return $prompt
}

function printStepHelp ([KeeperSecurity.Authentication.IAuthentication] $auth) {
    $commands = @()
    if ($auth.step -is [KeeperSecurity.Authentication.Sync.DeviceApprovalStep]) {
        $channels = @()
        foreach ($ch in $auth.step.Channels) {
            $channels += deviceApprovalChannelToText $ch
        }
        if ($channels) {
            $commands += "channel=<$($channels -join ' | ')> to change channel."
        }
        $commands += "`"push`" to send a push to the channel"
        $commands += '<code> to send a code to the channel'
    }
    elseif ($auth.step -is [KeeperSecurity.Authentication.Sync.TwoFactorStep]) {
        $channels = @()
        foreach ($ch in $auth.step.Channels) {
            $channelText = twoFactorChannelToText $ch
            if ($channelText) {
                $channels += $channelText
            }
        }
        if ($channels) {
            $commands += "channel=<$($channels -join ' | ')> to change channel."
        }

        $channels = @()
        foreach ($ch in $auth.step.Channels) {
            $pushes = $auth.step.GetChannelPushActions($ch)
            if ($null -ne $pushes) {
                foreach ($push in $pushes) {
                    $channels += [KeeperSecurity.Authentication.AuthUIExtensions]::GetPushActionText($push)
                }
            }
        }
        if ($channels) {
            $commands += "`"$($channels -join ' | ')`" to send a push/code"
        }

        $channels = @()
        foreach ($exp in $expires) {
            $channels += twoFactorDurationToExpire $exp
        }
        $commands += "expire=<$($channels -join ' | ')> to set 2fa expiration."
        $commands += '<code> to send a 2fa code.'
    }

    elseif ($auth.step -is [KeeperSecurity.Authentication.Sync.PasswordStep]) {
        $commands += '<password> to send a master password.'
        $commands += '"recover" if you forgot your password and want to recover your account.'
    }
    elseif ($auth.step -is [KeeperSecurity.Authentication.Sync.AccountRecoveryStep]) {
        if ($null -eq $auth.step.RecoveryType) {
            $commands += '<code> the verification code emailed to you.'
        }
        elseif ($auth.step.RecoveryType -eq [Authentication.BackupKeyType]::BktSecAnswer) {
            $commands += '<answer> the answer to the security question above.'
        }
        else {
            $commands += '<recovery phrase> your 24-word recovery phrase.'
        }
        $commands += '"cancel" to go back to entering your master password.'
    }
    elseif ($auth.step -is [KeeperSecurity.Authentication.Sync.SsoTokenStep]) {
        $commands += $auth.step.SsoLoginUrl
        $commands += ''
        if (-not $auth.step.LoginAsProvider) {
            $commands += '"password" to login using master password.'
        }
        $commands += '<sso token> paste SSO login token.'
    }
    elseif ($auth.step -is [KeeperSecurity.Authentication.Sync.SsoDataKeyStep]) {
        $channels = @()
        foreach ($ch in $auth.step.Channels) {
            $channels += [KeeperSecurity.Authentication.AuthUIExtensions]::SsoDataKeyShareChannelText($ch)
        }
        if ($channels) {
            $commands += "`"$($channels -join ' | ')`" to request login approval"
        }
    }
    elseif ($auth.step -is [KeeperSecurity.Authentication.Sync.ReadyToLoginStep]) {
        $commands += '"login <Keeper Email>" login to Keeper as user'
        $commands += '"login_sso <Enterprise Domain>" login to Enterprise Domain'
    }

    if ($commands) {
        Write-Output "`nAvailable Commands`n"
        foreach ($command in $commands) {
            Write-Output $command
        }
        Write-Output '<Enter> to resume'
    }
}

function executeStepAction ([KeeperSecurity.Authentication.IAuthentication] $auth, [string] $action) {

    function tryExpireToTwoFactorDuration ([string] $expire, [ref] [KeeperSecurity.Authentication.TwoFactorDuration] $duration) {
        $result = $true
        if ($expire -eq 'now') {
            $duration.Value = [KeeperSecurity.Authentication.TwoFactorDuration]::EveryLogin
        }
        elseif ($expire -eq 'never') {
            $duration.Value = [KeeperSecurity.Authentication.TwoFactorDuration]::Forever
        }
        elseif ($expire -eq '30_days') {
            $duration.Value = [KeeperSecurity.Authentication.TwoFactorDuration]::Every30Days
        }
        else {
            $duration.Value = [KeeperSecurity.Authentication.TwoFactorDuration]::EveryLogin
        }

        return $result
    }

    function tryTextToDeviceApprovalChannel ([string] $text, [ref] [KeeperSecurity.Authentication.DeviceApprovalChannel] $channel) {
        $result = $true
        if ($text -eq 'email') {
            $channel.Value = [KeeperSecurity.Authentication.DeviceApprovalChannel]::Email
        }
        elseif ($text -eq 'keeper') {
            $channel.Value = [KeeperSecurity.Authentication.DeviceApprovalChannel]::KeeperPush
        }
        elseif ($text -eq '2fa') {
            $channel.Value = [KeeperSecurity.Authentication.DeviceApprovalChannel]::TwoFactorAuth
        }
        else {
            Write-Output 'Unsupported device approval channel:', $text
            $result = $false
        }

        return $result
    }

    function tryTextToTwoFactorChannel ([string] $text, [ref] [KeeperSecurity.Authentication.TwoFactorChannel] $channel) {
        $result = $true
        if ($text -eq 'authenticator') {
            $channel.Value = [KeeperSecurity.Authentication.TwoFactorChannel]::Authenticator
        }
        elseif ($text -eq 'sms') {
            $channel.Value = [KeeperSecurity.Authentication.TwoFactorChannel]::TextMessage
        }
        elseif ($text -eq 'duo') {
            $channel.Value = [KeeperSecurity.Authentication.TwoFactorChannel]::DuoSecurity
        }
        elseif ($text -eq 'rsa') {
            $channel.Value = [KeeperSecurity.Authentication.TwoFactorChannel]::RSASecurID
        }
        elseif ($text -eq 'dna') {
            $channel.Value = [KeeperSecurity.Authentication.TwoFactorChannel]::KeeperDNA
        }
        elseif ($text -eq 'security key' -or $text -eq 'key') {
            $channel.Value = [KeeperSecurity.Authentication.TwoFactorChannel]::SecurityKey
        }
        else {
            Write-Output 'Unsupported 2FA channel:', $text
            $result = $false
        }

        return $result
    }

    if ($auth.step -is [KeeperSecurity.Authentication.Sync.DeviceApprovalStep]) {
        if ($action -eq 'push') {
            $auth.step.SendPush($auth.step.DefaultChannel).GetAwaiter().GetResult() | Out-Null
        }
        elseif ($action -match 'channel\s*=\s*(.*)') {
            $ch = $Matches.1
            [KeeperSecurity.Authentication.DeviceApprovalChannel]$cha = $auth.step.DefaultChannel
            if (tryTextToDeviceApprovalChannel ($ch) ([ref]$cha)) {
                $auth.step.DefaultChannel = $cha
            }
        }
        else {
            Try {
                $auth.step.SendCode($auth.step.DefaultChannel, $action).GetAwaiter().GetResult() | Out-Null
            }
            Catch [KeeperSecurity.Authentication.KeeperApiException] {
                Write-Warning $_
            }
            Catch {
                Write-Error $_
            }
        }
    }
    elseif ($auth.step -is [KeeperSecurity.Authentication.Sync.TwoFactorStep]) {
        if ($action -match 'channel\s*=\s*(.*)') {
            $ch = $Matches.1
            [KeeperSecurity.Authentication.TwoFactorChannel]$cha = $auth.step.DefaultChannel
            if (tryTextToTwoFactorChannel($ch) ([ref]$cha)) {
                $auth.step.DefaultChannel = $cha
            }
        }
        elseif ($action -match 'expire\s*=\s*(.*)') {
            $exp = $Matches.1
            [KeeperSecurity.Authentication.TwoFactorDuration]$dur = $auth.step.Duration
            if (tryExpireToTwoFactorDuration($exp) ([ref]$dur)) {
                $auth.step.Duration = $dur
            }
        }
        else {
            $selectedPush = $null
            foreach ($cha in $auth.step.Channels) {
                $pushes = $auth.step.GetChannelPushActions($cha)
                if ($null -ne $pushes) {
                    foreach ($push in $pushes) {
                        if ($action -eq [KeeperSecurity.Authentication.AuthUIExtensions]::GetPushActionText($push)) {
                            $selectedPush = $push
                            break
                        }
                    }
                }
                if ($null -ne $selectedPush) {
                    break
                }
            }
            if ($null -ne $selectedPush) {
                $auth.step.SendPush($selectedPush).GetAwaiter().GetResult() | Out-Null
                return
            }
            try {
                $auth.step.SendCode($auth.step.DefaultChannel, $action).GetAwaiter().GetResult() | Out-Null
            }
            catch {
                Write-Error $_
            }
        }
    }
    elseif ($auth.step -is [KeeperSecurity.Authentication.Sync.PasswordStep]) {
        if ($action -eq 'recover') {
            $auth.step.RecoverAccount().GetAwaiter().GetResult() | Out-Null
            if ($auth.step -is [KeeperSecurity.Authentication.Sync.AccountRecoveryStep]) {
                Try {
                    $auth.step.RequestVerificationCode().GetAwaiter().GetResult() | Out-Null
                    Write-Host 'A verification code has been emailed to you.' -ForegroundColor Cyan
                }
                Catch {
                    Write-Error $_
                }
            }
            return
        }
        Try {
            $auth.step.VerifyPassword($action).GetAwaiter().GetResult() | Out-Null
        }
        Catch [KeeperSecurity.Authentication.KeeperAuthFailed] {
            Write-Warning 'Invalid password'
        }
        Catch [KeeperSecurity.Authentication.KeeperApiException] {
            if ($_.Exception.Message -match 'Invalid|Credential|password|authentication failed' -or $_.Exception.Code -match 'invalid') {
                Write-Warning "Invalid credentials: $($_.Exception.Message)"
            } else {
                Write-Error $_
            }
        }
        Catch {
            Write-Error $_
        }
    }
    elseif ($auth.step -is [KeeperSecurity.Authentication.Sync.AccountRecoveryStep]) {
        if ($action -eq 'cancel') {
            $auth.step.Resume().GetAwaiter().GetResult() | Out-Null
            return
        }
        Try {
            if ($null -eq $auth.step.RecoveryType) {
                $auth.step.SubmitVerificationCode($action).GetAwaiter().GetResult() | Out-Null
            }
            elseif ($auth.step.RecoveryType -eq [Authentication.BackupKeyType]::BktSecAnswer) {
                $auth.step.SubmitSecurityAnswer($action).GetAwaiter().GetResult() | Out-Null
            }
            else {
                $auth.step.SubmitRecoveryPhrase($action).GetAwaiter().GetResult() | Out-Null
            }
        }
        Catch [KeeperSecurity.Authentication.KeeperApiException] {
            Write-Warning "Account recovery failed: $($_.Exception.Message)"
        }
        Catch {
            Write-Error $_
        }
    }
    elseif ($auth.step -is [KeeperSecurity.Authentication.Sync.SsoTokenStep]) {
        if ($action -eq 'password') {
            $auth.step.LoginWithPassword().GetAwaiter().GetResult() | Out-Null
        }
        else {
            $auth.step.SetSsoToken($action).GetAwaiter().GetResult() | Out-Null
        }
    }
    elseif ($auth.step -is [KeeperSecurity.Authentication.Sync.SsoDataKeyStep]) {
        [KeeperSecurity.Authentication.DataKeyShareChannel]$channel = [KeeperSecurity.Authentication.DataKeyShareChannel]::KeeperPush
        if ([KeeperSecurity.Authentication.AuthUIExtensions]::TryParseDataKeyShareChannel($action, [ref]$channel)) {
            $auth.step.RequestDataKey($channel).GetAwaiter().GetResult() | Out-Null
        }
    }
    elseif ($auth.step -is [KeeperSecurity.Authentication.Sync.ReadyToLoginStep]) {
        if ($action -match '^login\s+(.*)$') {
            $username = $Matches.1
            $auth.Login($username).GetAwaiter().GetResult() | Out-Null
        }
        elseif ($action -match '^login_sso\s+(.*)$') {
            $providerName = $Matches.1
            $auth.LoginSso($providerName).GetAwaiter().GetResult() | Out-Null
        }
    }
}

function Resolve-KeeperServer {
    <#
    .Synopsis
    Resolves a Keeper region code or hostname to a valid Keeper server hostname.
    Returns $null for empty input and throws an error for unknown values.
    Host names of self hosted Keeper instances are passed through.
    Uses KeeperRegions from the SDK as the single source of truth for region mappings.
    #>

    param(
        [Parameter()] [string] $Server
    )

    if ([string]::IsNullOrWhiteSpace($Server)) {
        return $null
    }

    $resolved = [KeeperSecurity.Authentication.KeeperRegions]::ResolveServer($Server)
    if ($resolved) {
        return $resolved
    }

    # Not a known region: a self hosted Keeper instance.
    if ($Server.Contains('.')) {
        return $Server.Trim().ToLowerInvariant()
    }

    $validRegions = ([KeeperSecurity.Authentication.KeeperRegions]::Servers.Keys | Sort-Object) -join ', '
    Write-Error "Invalid region: $Server`nValid regions: $validRegions`nSelf hosted instances are set by their host name." -ErrorAction Stop
}

function getConfigurationForDevice {
    param(
        [Parameter(Mandatory=$true)] [string] $Device,
        [Parameter()] [string] $Username,
        [Parameter()] [string] $Server
    )

    $deviceToken = $Device.Trim()
    if (-not $deviceToken) {
        Write-Error "Device parameter requires a device_token value." -ErrorAction Stop
    }

    $configuration = New-Object KeeperSecurity.Configuration.KeeperConfiguration
    $configuration.LastLogin = $Username
    $configuration.LastServer = $Server

    $deviceConf = New-Object KeeperSecurity.Configuration.DeviceConfiguration $deviceToken
    if ($Server) {
        $deviceConf.ServerInfo.Put((New-Object KeeperSecurity.Configuration.DeviceServerConfiguration $Server))
    }
    $configuration.Devices.Put($deviceConf)

    if ($Username) {
        $userConf = New-Object KeeperSecurity.Configuration.UserConfiguration $Username
        $userConf.Server = $Server
        $userConf.LastDevice = New-Object KeeperSecurity.Configuration.UserDeviceConfiguration $deviceToken
        $configuration.Users.Put($userConf)
    }

    if ($Server) {
        $configuration.Servers.Put((New-Object KeeperSecurity.Configuration.ServerConfiguration $Server))
    }

    return New-Object KeeperSecurity.Configuration.InMemoryConfigurationStorage $configuration
}

function Connect-Keeper {
    <#
    .Synopsis
    Login to Keeper

   .Parameter Username
    User email

    .Parameter Password
    User password

    .Parameter NewLogin
    Do not resume the stored session (full login). When omitted, resume is also skipped if -Username
    differs from the stored LastLogin (switching accounts).

    .Parameter SsoPassword
    Use Master Password for SSO account

    .Parameter SsoProvider
    Login using SSO provider

    .Parameter Server
    Change default keeper server

    .Parameter Device
    Device token. When provided, skips loading configuration from file.

    .Parameter Config
    Config file name

    .Parameter UseOfflineStorage
    Use SQLite file for vault cache (persists between sessions).

    .Parameter VaultDatabasePath
    Path to the SQLite database file for vault storage. Default: keeper_db.sqlite in the same directory as the config file (or current directory).

   .Parameter SkipSync
    After a successful login, do not call SyncDown. The authenticated session and VaultOnline instance are available. The local vault stays empty until you run Sync-Keeper. AutoSync is disabled until then.

    .Parameter KeepAlive
    Enable automatic session keep-alive during login.

    .Parameter PushNotifications
    Enable push notifications during login.

    .Parameter IgnoreCertificateErrors
    Accept any TLS certificate presented by the Keeper server. The server is not authenticated.
    Self hosted Keeper instances with self signed certificates only.
#>

    [CmdletBinding(DefaultParameterSetName = 'regular')]
    Param(
        [Parameter(Position = 0)][string] $Username,
        [Parameter()] [SecureString]$Password,
        [Parameter()][switch] $NewLogin,
        [Parameter(ParameterSetName = 'sso_password')][switch] $SsoPassword,
        [Parameter(ParameterSetName = 'sso_provider')][switch] $SsoProvider,
        [Parameter()][string] $Server,
        [Parameter()][string] $Device,
        [Parameter()][string] $Config,
        [Parameter()][switch] $UseOfflineStorage,
        [Parameter()][string] $VaultDatabasePath,
        [Parameter()][switch] $SkipSync,
        [Parameter()][switch] $KeepAlive,
        [Parameter()][switch] $PushNotifications,
        [Parameter()][switch] $IgnoreCertificateErrors
    )

    if ($Server) {
        $Server = Resolve-KeeperServer -Server $Server
    }

    Disconnect-Keeper -Resume | Out-Null
    $deviceTokenOnly = $false
    if ($Device) {
        if ($Config) {
            Write-Error "The Device and Config parameters cannot be used together." -ErrorAction Stop
        }
        $storage = getConfigurationForDevice -Device $Device -Username $Username -Server $Server
        $deviceTokenOnly = $true
    }
    elseif ($Config) {
        $storage = New-Object KeeperSecurity.Configuration.JsonConfigurationStorage $Config
    } else {
        $storage = New-Object KeeperSecurity.Configuration.JsonConfigurationStorage
    }

    $endpoint = New-Object KeeperSecurity.Authentication.KeeperEndpoint($storage, $Server)
    $endpoint.DeviceName = 'PowerShell Commander'
    $endpoint.ClientVersion = 'c18.0.0'
    if ($IgnoreCertificateErrors.IsPresent) {
        $endpoint.IgnoreCertificateErrors = $true
        Write-Warning "TLS certificate verification is disabled for Keeper server `"$($endpoint.Server)`". The server is not authenticated."
    }

    Write-Information -MessageData "`nUsing Keeper Server: $($endpoint.Server)`n" -InformationAction Continue

    $authFlow = New-Object KeeperSecurity.Authentication.Sync.AuthSync($storage, $endpoint)
    $authFlow.UiCallback = [KeeperPostLoginUI]::new($endpoint.Server)
    $authFlow.SupportRestrictedSession = $true

    $authFlow.AlternatePassword = $SsoPassword.IsPresent
    $authFlow.NoNewDevice = $deviceTokenOnly
    $authFlow.AutoKeepAlive = $KeepAlive.IsPresent
    $authFlow.UsePushNotifications = $PushNotifications.IsPresent

    if (-not $NewLogin.IsPresent -and -not $SsoProvider.IsPresent) {
        if (-not $Username) {
            $conf = $storage.Get()
            if ($conf.LastServer -eq $endpoint.Server) {
                $Username = $conf.LastLogin
            }
        }
    }

    $namePrompt = 'Keeper Username'
    if ($SsoProvider.IsPresent) {
        $namePrompt = 'Enterprise Domain'
    }

    if ($Username) {
        Write-Output "$(($namePrompt + ': ').PadLeft(21, ' ')) $Username"
    }
    elseif (Test-InteractiveSession) {
        while (-not $Username) {
            $Username = Read-Host -Prompt $namePrompt.PadLeft(20, ' ')
        }
    } else {
        Write-Error "Non-interactive session detected" -ErrorAction Stop 
    }

    $canResume = -not ($NewLogin.IsPresent -or $Password)
    if ($canResume -and $PSBoundParameters.ContainsKey('Username')) {
        $cfgForResume = $storage.Get()
        if ($cfgForResume.LastLogin -and $Username -and
            [string]::Compare($Username, $cfgForResume.LastLogin, $true) -ne 0) {
            $canResume = $false
            Write-Verbose "Username differs from stored LastLogin; starting a new session (no resume)."
        }
    }
    $authFlow.ResumeSession = $canResume
    if ($deviceTokenOnly) {
        $authFlow.ResumeSession = $false
    }    
    Write-Verbose "Resume Session: $($authFlow.ResumeSession)"

    $biometricPresent = $false
    try {
        $windowsHelloAvailable = Test-WindowsHelloCapabilities
        if ($windowsHelloAvailable) {
            $biometricPresent = Test-WindowsHelloBiometricPreviouslyUsed -Username $Username
            if (-not $biometricPresent) {
                Write-Debug "Windows Hello biometric authentication not available for this user"
            }
        } else {
            Write-Debug "Windows Hello not available on this system"
        }
    }
    catch {
        Write-Debug "Failed to check Windows Hello capabilities or biometric credential status: $($_.Exception.Message)"
    }

    if ($SsoProvider.IsPresent) {
        $authFlow.LoginSso($Username).GetAwaiter().GetResult() | Out-Null
    }
    else {
        $passwords = @()
        if ($Password) {
            if ($Password -is [SecureString]) {
                $passwords += [Net.NetworkCredential]::new('', $Password).Password
            }
            elseif ($Password -is [String]) {
                $passwords += $Password
            }
        }
        $authFlow.Login($Username, $passwords).GetAwaiter().GetResult() | Out-Null
    }

    while (-not $authFlow.IsCompleted) {
        if ($lastStep -ne $authFlow.Step.State) {
            if (-not $biometricPresent) {
                printStepHelp $authFlow
            }
            $lastStep = $authFlow.Step.State
        }
        if ($biometricPresent) {
            try {
                Write-Host "Attempting Keeper biometric authentication..."

                $biometricResult = Assert-KeeperBiometricCredential -AuthSyncObject $authFlow -Username $Username -PassThru
                if ($biometricResult.Success -and $biometricResult.IsValid) {
                    $authFlow.ResumeLoginWithToken($biometricResult.EncryptedLoginToken).GetAwaiter().GetResult() | Out-Null 
                    if ($authFlow.IsCompleted) {
                        Write-Debug "Authentication completed successfully!"
                        break
                    }
                    Write-Debug "Biometric authentication succeeded, but additional authentication steps required"
                    $biometricPresent = $false
                } else {
                    $isCancelled = $biometricResult.ErrorMessage -match "cancelled|cancel" -or 
                                   $biometricResult.ErrorType -eq "OperationCanceledException"
                    
                    if ($isCancelled) {
                        Write-Host "Windows Hello authentication was cancelled. Falling back to default login method." -ForegroundColor Yellow
                    } else {
                        Write-Host "Biometric authentication failed, falling back to default login method. Device might not be registered"
                        Write-Host "Please try running 'Set-KeeperDeviceSettings -Register' to register the device"
                    }
                    $biometricPresent = $false 
                }
            }
            catch {
                Write-Host "Error logging in with biometric authentication, did you register the device using 'Set-KeeperDeviceSettings -Register'?"
                Write-Host "Biometric authentication error: $($_.Exception.Message), falling back to default input"
                $biometricPresent = $false 
            }
        }
        $prompt = getStepPrompt $authFlow

        $maskInput = $authFlow.Step -is [KeeperSecurity.Authentication.Sync.PasswordStep] -or
            ($authFlow.Step -is [KeeperSecurity.Authentication.Sync.AccountRecoveryStep] -and $null -ne $authFlow.Step.RecoveryType)
        if ($maskInput) {
            if (Test-InteractiveSession) {
                $securedPassword = Read-Host -Prompt $prompt -AsSecureString
                if ($securedPassword.Length -gt 0) {
                    $action = [Net.NetworkCredential]::new('', $securedPassword).Password
                }
                else {
                    $action = ''
                }
            } else {
                Write-Error "Non-interactive session detected" -ErrorAction Stop
            }
        }
        else {
            if (Test-InteractiveSession) {
                $action = Read-Host -Prompt $prompt
            } else {
                Write-Error "Non-interactive session detected" -ErrorAction Stop 
            }
        }

        if ($action) {
            if ($action -eq '?') {
            }
            else {
                executeStepAction $authFlow $action
            }
        }
    }

    if ($authFlow.Step.State -ne [KeeperSecurity.Authentication.Sync.AuthState]::Connected) {
        if ($authFlow.Step -is [KeeperSecurity.Authentication.Sync.ErrorStep]) {
            Write-Warning $authFlow.Step.Message
        }
        return
    }

    $auth = $authFlow
    if ([KeeperSecurity.Authentication.AuthExtensions]::IsAuthenticated($auth)) {
        Write-Information -MessageData "Connected to Keeper as $($auth.Username)" -InformationAction Continue

        $vaultStorage = $null
        if ($UseOfflineStorage) {
            $ownerUid = [KeeperSecurity.Utils.CryptoUtils]::Base64UrlEncode($auth.AuthContext.AccountUid)
            if ($VaultDatabasePath) {
                $dbPath = $VaultDatabasePath
            } elseif ($Config) {
                $resolved = $null
                try { $resolved = Resolve-Path -LiteralPath $Config -ErrorAction Stop } catch { }
                $configDir = if ($resolved) { [System.IO.Path]::GetDirectoryName($resolved.Path) } else { [System.IO.Path]::GetDirectoryName([System.IO.Path]::GetFullPath($Config)) }
                $dbPath = Join-Path $configDir 'keeper_db.sqlite'
            } else {
                $dbPath = Join-Path (Get-Location).Path 'keeper_db.sqlite'
            }
            $dbPath = $PSCmdlet.SessionState.Path.GetUnresolvedProviderPathFromPSPath($dbPath)
            $connectionString = "Data Source=$dbPath;Pooling=True;"
            Write-Information -MessageData "Using vault database: $dbPath"
            $vaultStorage = Get-SqliteVaultStorageFromHelper -ConnectionString $connectionString -OwnerUid $ownerUid
        }
        $vault = New-Object KeeperSecurity.Vault.VaultOnline($auth, $vaultStorage)
        try {
            if ($SkipSync.IsPresent) {
                $vault.AutoSync = $false
                Write-Information -MessageData 'SkipSync: vault SyncDown skipped. Local folder tree and records are empty until you run Sync-Keeper.' -InformationAction Continue
            }
            else {
                $task = $vault.SyncDown()
                Write-Information -MessageData 'Syncing ...' -InformationAction Continue
                $task.GetAwaiter().GetResult() | Out-Null
                $vault.AutoSync = $true
            }
        }
        catch {
            $isRecoverySessionError = $auth.AuthContext.IsAccountRecoverySession -or
                $_.Exception.Message -like '*ACCOUNT_RECOVERY*'
            if ($isRecoverySessionError) {
                Write-Host ''
                Write-Host 'Your Master Password has been changed successfully.' -ForegroundColor Green
                Write-Host 'This session was created during account recovery and cannot be used to access the vault - you need to log in again with your new password.' -ForegroundColor Yellow
                $reconnect = Read-Host 'Reconnect now with your new password? (y/n)'
                if ($reconnect -match '^y(es)?$') {
                    $PSBoundParameters.Remove('Password') | Out-Null
                    Connect-Keeper @PSBoundParameters
                }
                return
            }
            throw
        }

        $Script:Context.Auth = $auth
        $Script:Context.Vault = $vault

        [KeeperSecurity.Vault.VaultData]$vaultData = $vault
        Write-Information -MessageData "Decrypted $($vaultData.RecordCount) record(s)" -InformationAction Continue
        Set-KeeperLocation -Path '\' | Out-Null
        
        $encryptionModeProperty = $endpoint.PSObject.Properties['LastEncryptionMode']
        if ($null -ne $encryptionModeProperty -and $endpoint.LastEncryptionMode) {
            Write-Verbose $endpoint.LastEncryptionMode
        }
    }
}

$Keeper_ConfigServerCompleter = {
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)

    $prefixes = @('', 'dev.', 'qa.')
    $suffixes = $('.com', '.eu')

    $prefixes | ForEach-Object { $p = $_; $suffixes | ForEach-Object { $s = $_; "${p}keepersecurity${s}" } } | Where-Object { $_.StartsWith($wordToComplete) }
}
Register-ArgumentCompleter -Command Connect-Keeper -ParameterName Server -ScriptBlock $Keeper_ConfigServerCompleter
New-Alias -Name kc -Value Connect-Keeper

function Disconnect-Keeper {
    <#
    .Synopsis
    Logout from Keeper
#>


    [CmdletBinding()]
    Param(
        [Parameter()][switch] $Resume
    )

    $Script:Context.AvailableTeams = $null
    $Script:Context.AvailableUsers = $null
    
    $Script:Context.ManagedCompanyId = 0
    $Script:Context.Enterprise = $null

    $vault = $Script:Context.Vault
    if ($vault) {
        $vault.Dispose() | Out-Null
    }
    $Script:Context.Vault = $null

    [KeeperSecurity.Authentication.IAuthentication] $auth = $Script:Context.Auth
    if ($auth) {
        if (-not $Resume.IsPresent) {
            $auth.Logout().GetAwaiter().GetResult() | Out-Null
        }
        $auth.Dispose() | Out-Null

    }
    $Script:Context.Auth = $null
}
New-Alias -Name kq -Value Disconnect-Keeper

function Sync-Keeper {
    <#
    .SYNOPSIS
    Synchronizes local Keeper vault state with the server.

    .DESCRIPTION
    Downloads updates from the Keeper and refreshes the local vault cache.
    Optionally clears and re-syncs record types if the -SyncRecordTypes switch is used.

    .PARAMETER SyncRecordTypes
    If specified, clears locally cached record types and forces a fresh sync from the server.

    .EXAMPLE
    Sync-Keeper
    Performs a standard vault sync from the Keeper server.

    .EXAMPLE
    Sync-Keeper -SyncRecordTypes
    Clears local record type definitions and syncs them anew from the Keeper server.

    .NOTES
    Requires a valid Keeper session. Run `Connect-Keeper` or equivalent before using this command.
    #>


    [CmdletBinding()]
    param (
        [Parameter()]
        [switch] $SyncRecordTypes
    )

    $vault = $Script:Context.Vault
    if ($vault -is [KeeperSecurity.Vault.VaultOnline]) {
        if ($SyncRecordTypes) {
            $vault.Storage.Clear()
            Write-Host "Cleared local record type cache for re-sync."
        }

        Write-Host "Syncing vault with Keeper server..."
        $task = $vault.SyncDown()
        $task.GetAwaiter().GetResult() | Out-Null
        $vault.AutoSync = $true

        try {
            $plugin = getPamPlugin
            if ($plugin -and $vault.RecordRotationCache) {
                $replaceAll = $vault.ConsumeRotationsCleared()
                $plugin.MergeRecordRotationsFromVault($vault, $replaceAll)
            }
        }
        catch {
            Write-Verbose "PAM record rotation refresh skipped: $($_.Exception.Message)"
        }

        Write-Host "Vault sync completed."
    }
    else {
        Write-Error -Message "Not connected to a Keeper vault. Please authenticate first." -ErrorAction Stop
    }
}
New-Alias -Name ks -Value Sync-Keeper

function Set-KeeperAccountRecovery {
    <#
    .SYNOPSIS
        Sets up, delays, verifies, or previews Account Recovery via a Recovery Phrase

    .PARAMETER Generate
        Generates a new recovery phrase, shows it, and asks you to confirm you saved it
        before setting it on the account

    .PARAMETER Snooze
        Delays the account recovery setup prompt for 30 days

    .PARAMETER Verify
        Checks a phrase against the one already on file (does not set anything)

    .EXAMPLE
        C:\PS> Set-KeeperAccountRecovery -Generate
    .EXAMPLE
        C:\PS> Set-KeeperAccountRecovery -Snooze
    .EXAMPLE
        C:\PS> Set-KeeperAccountRecovery -Verify $phrase
    #>


    [CmdletBinding()]
    Param (
        [Parameter()][Switch] $Generate,
        [Parameter()][Switch] $Snooze,
        [Parameter()][String] $Verify
    )

    $specified = @('Generate', 'Snooze', 'Verify') | Where-Object { $PSBoundParameters.ContainsKey($_) }
    if ($specified.Count -eq 0) {
        Write-Error "Specify one of -Generate, -Snooze, or -Verify." -ErrorAction Stop
        return
    }
    if ($specified.Count -gt 1) {
        Write-Error "-$($specified -join ', -') cannot be used together. Specify only one." -ErrorAction Stop
        return
    }

    $vault = getVault
    $auth = $vault.Auth

    if ($Snooze.IsPresent) {
        [KeeperSecurity.Authentication.AccountRecoveryExtensions]::SnoozeAccountRecoverySetup($auth).GetAwaiter().GetResult() | Out-Null
        Write-Host 'Account recovery prompt delayed for 30 days.'
        return
    }

    if ($Verify) {
        try {
            return [KeeperSecurity.Authentication.AccountRecoveryExtensions]::VerifyAccountRecoveryPhrase($auth, $Verify).GetAwaiter().GetResult()
        }
        catch {
            $message = if ($_.Exception.InnerException) { $_.Exception.InnerException.Message } else { $_.Exception.Message }
            Write-Error $message -ErrorAction Stop
            return
        }
    }

    $phrase = [KeeperSecurity.Utils.RecoveryPhrase]::Generate()
    Write-Host ''
    Write-Host 'Your new Recovery Phrase:' -ForegroundColor Yellow
    Write-Host $phrase -ForegroundColor Yellow
    Write-Host ''

    if (-not (Test-InteractiveSession)) {
        Write-Error "Non-interactive session detected. Cannot confirm the recovery phrase was saved." -ErrorAction Stop
        return
    }

    $confirm = Read-Host 'Have you saved this recovery phrase somewhere safe? (y/n)'
    if ($confirm -notmatch '^y(es)?$') {
        Write-Warning 'Recovery phrase was NOT saved on the server. Run Set-KeeperAccountRecovery -Generate again when ready.'
        return
    }

    [KeeperSecurity.Authentication.AccountRecoveryExtensions]::SetupAccountRecovery($auth, $phrase).GetAwaiter().GetResult() | Out-Null
    Write-Host 'Recovery phrase has been set on your account.' -ForegroundColor Green
    return $phrase
}

function Get-KeeperInformation {
    <#
    .Synopsis
    Prints account license information
    #>


    $vault = getVault
    [KeeperSecurity.Authentication.IAuthentication]$auth = $vault.Auth

    [KeeperSecurity.Authentication.AccountLicense]$license = $auth.AuthContext.License
    switch ($license.AccountType) {
        0 { $accountType = $license.ProductTypeName }
        1 { $accountType = 'Family Plan'}
        2 { $accountType = 'Enterprise' }
        Default { $accountType = $license.ProductTypeName }
    }
    $accountType = 'Enterprise'
    [PSCustomObject]@{
        PSTypeName  = "KeeperSecurity.License.Info"
        User        = $auth.Username
        Server      = $auth.Endpoint.Server
        Admin       = $auth.AuthContext.IsEnterpriseAdmin
        AccountType = $accountType 
        RenewalDate = $license.ExpirationDate
        StorageCapacity = [int] [Math]::Truncate($license.BytesTotal / (1024 * 1024 * 1024))
        StorageUsage = $license.StorageUsagePercent
        StorageExpires = $license.StorageExpirationDate
    }

    if ($license.AccountType -eq 2) {
        $enterprise = getEnterprise
        if ($enterprise) {
            $enterpriseLicense = $enterprise.enterpriseData.EnterpriseLicense
            $productTypeId = $enterpriseLicense.ProductTypeId
            if ($productTypeId -in @(2, 5)) {
                $tier = $enterpriseLicense.Tier
                if ($tier -eq 1) {
                    $plan = 'Enterprise'
                } else {
                    $plan = 'Business'
                }
            }
            elseif ($productTypeId -in @(9, 10)) {
                $distributor = $enterpriseLicense.Distributor
                if ($distributor -eq $true) {
                    $plan = 'Distributor'
                } else {
                    $plan = 'Managed MSP'
                }
            }
            elseif ($productTypeId -in @(11, 12)) {
                $plan = 'Keeper MSP'
            }
            elseif ($productTypeId -eq 8) {
                $tier = $enterpriseLicense.Tier
                if ($tier -eq 1) {
                    $plan = 'Enterprise'
                } else {
                    $plan = 'Business'
                }
                $plan = "MC $plan"
            } else {
                $plan = 'Unknown'
            }
            if ($productTypeId -in @(5, 10, 12)) {
                $plan = "$plan Trial"
            }

            $enterpriseInfo = [PSCustomObject]@{
                PSTypeName  = "KeeperSecurity.License.EnterpriseInfo"
                LicenseType = 'Enterprise'
                EnterpriseName = $enterprise.loader.EnterpriseName
                BasePlan    = $plan
            }
            if ($enterpriseLicense.Paid) {
                $expiration = $enterpriseLicense.Expiration
                if ($expiration -gt 0) {
                    $exp = [KeeperSecurity.Utils.DateTimeOffsetExtensions]::FromUnixTimeMilliseconds($expiration)
                    $expDate = $exp.ToString('d')
                    Add-Member -InputObject $enterpriseInfo -MemberType NoteProperty -Name 'Expires' -Value $expDate
                }
                
                switch ($enterpriseLicense.filePlanTypeId) {
                    -1 { $filePlan = 'No Storage' }
                    0 { $filePlan = 'Trial' }
                    1 { $filePlan = '1GB' }
                    2 { $filePlan = '10GB' }
                    3 { $filePlan = '50GB' }
                    4 { $filePlan = '100GB' }
                    5 { $filePlan = '250GB' }
                    6 { $filePlan = '500GB' }
                    7 { $filePlan = '1TB' }
                    8 { $filePlan = '10TB' }
                    Default { $filePlan = '???' }
                }
                Add-Member -InputObject $enterpriseInfo -MemberType NoteProperty -Name 'StorageCapacity' -Value $filePlan

                $numberOfSeats = $enterpriseLicense.NumberOfSeats
                if ($numberOfSeats -gt 0) {
                    Add-Member -InputObject $enterpriseInfo -MemberType NoteProperty -Name 'TotalUsers' -Value $numberOfSeats
                }
                $seatsAllocated = $enterpriseLicense.SeatsAllocated
                if ($seatsAllocated -gt 0) {
                    Add-Member -InputObject $enterpriseInfo -MemberType NoteProperty -Name 'ActiveUsers' -Value $seatsAllocated
                }
                $seatsPending = $enterpriseLicense.SeatsPending
                if ($seatsAllocated -gt 0) {
                    Add-Member -InputObject $enterpriseInfo -MemberType NoteProperty -Name 'InvitedUsers' -Value $SeatsPending
                }

            }
            $enterpriseInfo
        }
    }
}
New-Alias -Name kwhoami -Value Get-KeeperInformation

function compareArrays {
    param ($array1, $array2)

    if ($array1.Length -eq $array2.Length) {
        foreach ($i in 0..($array1.Length-1)) {
            if ($array1[$i] -ne $array2[$i]) {
                return $false
            }
        }
        return $true
    }
    return $false
}

function formatTimeout {
    param ($timeout)

    if ($timeout -gt 0) {
        $dayMillis = [TimeSpan]::FromDays(1).TotalMilliseconds
        if ($logoutTimer -gt $dayMillis) { 
            return "$([Math]::Round($logoutTimer / $dayMillis)) day(s)"
        }

        $hourMillis = [TimeSpan]::FromHours(1).TotalMilliseconds
        if ($logoutTimer -gt $hourMillis) { 
            return "$([Math]::Round($logoutTimer / $hourMillis)) hour(s)"
        }

        $minuteMillis = [TimeSpan]::FromMinutes(1).TotalMilliseconds
        return "$([Math]::Round($logoutTimer / $minuteMillis)) minute(s)"
    }
}

function Get-KeeperDeviceSettings {
    <#
    .SYNOPSIS
    Display settings of the current device
    
    .PARAMETER Auth
    Optional authentication object. If not provided, uses the current vault's auth.
    #>

    [CmdletBinding()]
    Param (
        [Parameter()][object] $Auth
    )

    if (-not $Auth) {
        $vault = getVault
        $auth = $vault.Auth
    }
    else {
        $auth = $Auth
    }

    $accountSummary = [KeeperSecurity.Authentication.AuthExtensions]::LoadAccountSummary($auth).GetAwaiter().GetResult()
    $device = $accountSummary.Devices | Where-Object { compareArrays $_.EncryptedDeviceToken $auth.DeviceToken } | Select-Object -First 1
    if (-not $device) {
        Write-Error -Message "The current device could not be found" -ErrorAction Stop
    }

    $logoutTimer = $accountSummary.Settings.LogoutTimer
    if ($logoutTimer -gt 0) {
        $logoutTimerText = formatTimeout $logoutTimer
    } else {
        $logoutTimerText = '1 hour(s)'
    }

    $persistentLoginRestricted = $false
    if ($accountSummary.Enforcements.Booleans) {
        $plp = $accountSummary.Enforcements.Booleans | Where-Object { $_.Key -eq 'restrict_persistent_login' } | Select-Object -First 1
        if ($plp) {
            $persistentLoginRestricted = $plp.Value
        }
    }
    $persistentLoginEnabled = $false
    if (-not $persistentLoginRestricted) {
        $persistentLoginEnabled = $accountSummary.Settings.PersistentLogin
    }

    $settings = [PSCustomObject]@{
        PSTypeName  = "KeeperSecurity.Authentication.DeviceInfo"
        DeviceName = $device.DeviceName
        PersistentLogin = $persistentLoginEnabled
        DataKeyPresent = $device.EncryptedDataKeyPresent
        IpAutoApprove = -not $accountSummary.Settings.IpDisableAutoApprove
        IsSsoUser = $accountSummary.Settings.SsoUser
        DeviceLogoutTimeout = $logoutTimerText
        NoYubiKeyPin = [bool]$accountSummary.Settings.SecurityKeysNoUserVerify
    }

    if ($accountSummary.Enforcements.Longs) {
        $enf = $accountSummary.Enforcements.Longs | Where-Object { $_.Key -eq 'logout_timer_desktop' } | Select-Object -First 1
        if ($enf.Length -eq 1) {
            $entLogoutTimer = $enf.Value
            if ($entLogoutTimer -gt 0) {
                $entLogoutTimerText = formatTimeout $entLogoutTimer
                Add-Member -InputObject $settings -MemberType NoteProperty -Name 'EnterpriseLogoutTimeout' -Value $entLogoutTimerText
            }
        }
    }
    $settings
}

function Set-KeeperDeviceSettings {
    <#
    .SYNOPSIS
        Modifies the current device settings

    .PARAMETER NewName
        Modifies device name

    .PARAMETER Timeout
        Sets inactivity timeout. Format: NUMBER[h|d]
        default - minutes, h - hours, d - days
    
    .PARAMETER Register
        Register current device for Persistent Login

    .PARAMETER PersistentLogin
        Enables or disables Persistent login for account
        ON | OFF

    .PARAMETER IpAutoApprove
        Enables or disables Automatic Approval by IP address for account
        ON | OFF

    .PARAMETER NoYubiKeyPin
        ON - security-key (YubiKey) authentication no longer requires a PIN.
        OFF - security-key authentication requires a PIN.
        This is an account setting: it applies to ALL devices of the account.
        Turning it ON lowers the strength of the security-key factor, so it asks for confirmation unless -Force is specified.

    .PARAMETER Force
        Do not ask for confirmation (used with -NoYubiKeyPin ON)

    .EXAMPLE
        C:\PS> Set-KeeperDeviceSettings -NewName 'Azure' -Timeout 30d -PersistentLogin ON -Register

    .EXAMPLE
        C:\PS> Set-KeeperDeviceSettings -NoYubiKeyPin OFF
    #>


    [CmdletBinding()]
    Param (
        [Parameter()][String] $NewName,
        [Parameter(HelpMessage='NUMBER[h|d]')][String] $Timeout,
        [Parameter()][Switch] $Register,
        [Parameter()][ValidateSet('ON', 'OFF')][String] $PersistentLogin,
        [Parameter()][ValidateSet('ON', 'OFF')][String] $IpAutoApprove,
        [Parameter()][ValidateSet('ON', 'OFF')][String] $NoYubiKeyPin,
        [Parameter()][Switch] $Force
    )

    $vault = getVault
    $auth = $vault.Auth

    $accountSummary = [KeeperSecurity.Authentication.AuthExtensions]::LoadAccountSummary($auth).GetAwaiter().GetResult()
    $device = $accountSummary.Devices | Where-Object { compareArrays $_.EncryptedDeviceToken $auth.DeviceToken } | Select-Object -First 1
    if (-not $device) {
        Write-Error -Message "The current device could not be found" -ErrorAction Stop
    }

    $changed = $false

    if ($NoYubiKeyPin) {
        $noPin = ($NoYubiKeyPin -eq 'ON')
        if ($noPin -and -not $Force.IsPresent) {
            Write-Warning "This turns off the YubiKey PIN requirement for security-key authentication on your whole account, on ALL devices. A stolen security key could then be used without a PIN."
            $answer = Read-Host "Do you want to proceed? (y/N)"
            if ($answer -notin @('y', 'yes')) {
                Write-Host "Canceled."
                return
            }
        }
        [KeeperSecurity.Authentication.AuthExtensions]::SetSecurityKeysNoUserVerify($auth, $noPin).GetAwaiter().GetResult() | Out-Null
        if ($noPin) {
            Write-Information "Security-key authentication no longer requires a YubiKey PIN (applies to all devices on this account)."
        }
        else {
            Write-Information "Security-key authentication requires a YubiKey PIN (applies to all devices on this account)."
        }
        $changed = $true
    }

    if ($NewName) {
        $request = New-Object Authentication.DeviceUpdateRequest
        $request.ClientVersion = $auth.Endpoint.ClientVersion
        $request.DeviceStatus = [Authentication.DeviceStatus]::DeviceOk
        $request.DeviceName = $NewName
        $request.EncryptedDeviceToken = $device.EncryptedDeviceToken

        $auth.ExecuteAuthRest("authentication/update_device", $request, $null, 0).GetAwaiter().GetResult() | Out-Null
        Write-Information "Device name was changed to `"$NewName`""
        $changed = $true
    }

    $persistentLoginRestricted = $false
    if ($accountSummary.Enforcements.Booleans) {
        $plp = $accountSummary.Enforcements.Booleans | Where-Object { $_.Key -eq 'restrict_persistent_login' } | Select-Object -First 1
        if ($plp) {
            $persistentLoginRestricted = $plp.Value
        }
    }

    if ($Register.IsPresent) {
        $registered = [KeeperSecurity.Authentication.AuthExtensions]::RegisterDataKeyForDevice($auth, $device).GetAwaiter().GetResult()
        if ($registered) {
            Write-Information "Device is registered for Persistent Login"
        }
        $changed = $true
    }

    if ($PersistentLogin) {
        if ($persistentLoginRestricted -eq $true) {
            Write-Error "Persistent Login feature is restricted by Enterprise Administrator" -ErrorAction Stop
        }
        $value = '0'
        if ($PersistentLogin -eq 'ON') {
            $value = '1'
        }
        [KeeperSecurity.Authentication.AuthExtensions]::SetSessionParameter($auth, 'persistent_login', $value).GetAwaiter().GetResult() | Out-Null
        $changed = $true
    }

    if ($IpAutoApprove) {
        $value = '1'
        if ($IpAutoApprove -eq 'ON') {
            $value = '0'
        }
        [KeeperSecurity.Authentication.AuthExtensions]::SetSessionParameter($auth, 'ip_disable_auto_approve', $value).GetAwaiter().GetResult() | Out-Null
        $changed = $true
    }

    if ($Timeout) {
        $lastLetter = $Timeout[-1]
        if ($lastLetter -eq 'd') {
            $timeoutInt = $Timeout.Substring(0, $Timeout.Length - 1)
        }
        elseif ($lastLetter -eq 'h') {
            $timeoutInt = $Timeout.Substring(0, $Timeout.Length - 1)
        } else {
            $lastLetter = ''
            $timeoutInt = $Timeout
        }

        $minutes = $null
        $b = [int]::TryParse($timeoutInt, [ref]$minutes)
        if (-not $b) {
            Write-Error "Invalid timeout value `"$Timeout`". Format NUMBER[h|d]. d-days, h-hours. default minutes " -ErrorAction Stop
        }
        if ($lastLetter -eq 'h') {
            $minutes = $minutes * 60
        }
        elseif ($lastLetter -eq 'd') {
            $minutes = $minutes * (60 * 24) 
        }
        [KeeperSecurity.Authentication.AuthExtensions]::SetSessionInactivityTimeout($auth, $minutes).GetAwaiter().GetResult() | Out-Null
        $changed = $true
    }

    if (-not $changed) {
        Get-KeeperDeviceSettings
    }
}
New-Alias -Name this-device -Value Set-KeeperDeviceSettings

# SIG # Begin signature block
# MIInvgYJKoZIhvcNAQcCoIInrzCCJ6sCAQExDzANBglghkgBZQMEAgEFADB5Bgor
# BgEEAYI3AgEEoGswaTA0BgorBgEEAYI3AgEeMCYCAwEAAAQQH8w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