SecretManagement.Chromium.Extension/Public/Find-Chromium.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
function Find-Chromium {
    <#
    .SYNOPSIS
    Attempts to detect a chromium profile to work with. Includes common presets
    #>

    param(
        #The name of the preset folder to search for
        $Preset
    )
    #TODO: Automatic Detection
    $Presets = [ordered]@{
        EdgeCanary = 'Microsoft/Edge SxS'
        EdgeBeta = 'Microsoft/Edge Beta'
        Edge = 'Microsoft/Edge'
        ChromeCanary = 'Google/Chrome SxS'
        ChromeBeta = 'Google/Chrome Beta'
        Chrome = 'Google/Chrome'
    }

    $ErrorActionPreference = 'Stop'
    
    function getUserDataFolderPath ($Preset) {
        $localAppDataPath = [Environment]::GetFolderPath('LocalApplicationData')
        return (
            Resolve-Path -Path (
                [IO.Path]::Combine(
                    $localAppDataPath,
                    $Preset,
                    'User Data'
                )
            )
        )
    }

    function getProfileNames ($UserDataFolderPath) {
        try {
            $localStatePath = Join-Path -Resolve $UserDataFolderPath 'Local State' -ErrorAction stop
        } catch {
            Write-Warning "$UserDataFolderPath exists but has no Local State file."
        }
        [String[]]$ProfileNames = (Get-Content -raw $localStatePath | ConvertFrom-Json).profile.info_cache.psobject.properties.name
        if (-not $ProfileNames) {Write-Warning "Local State file exists but no profile information was found"}
        return $ProfileNames
    }

    function getLoginDataPath ($Preset, $ProfileName) {
        return (
            Resolve-Path -ErrorAction Stop -Path (
                [io.path]::Combine(
                    (getUserDataFolderPath $Preset),
                    $ProfileName,
                    'Login Data'
                )   
            )
        )
    }

    if ($Preset -and $Presets.$Preset) {
        #Narrow the scope to just the selected preset
        $Presets = [ordered]@{
            $Preset = $Presets.$Preset
        }
    }

    foreach ($PresetItem in $Presets.keys) {
        try {
            $userDataFolderPath = getUserDataFolderPath $Presets[$PresetItem]
            foreach ($ProfileNameItem in (getProfileNames $UserDataFolderPath)) {
                $LoginDataPath = getLoginDataPath $Presets[$PresetItem] $ProfileNameItem
                [PSCustomObject][ordered]@{
                    Name = $PresetItem
                    Profile = $ProfileNameItem
                    LocalStatePath = Join-Path $userDataFolderPath 'Local State'
                    LoginDataPath = $LoginDataPath
                } | Write-Output
                Write-Verbose "SecretManagement.Chromium: Discovery FOUND $PresetItem profile at $($Presets[$PresetItem])"
            }
        } catch {
            Write-Verbose "SecretManagement.Chromium: Discovery NOT FOUND $PresetItem profile at $($Presets[$PresetItem])"
        }
    }
}