Private/ConvertFrom-NSPFortinetTokenText.ps1
|
function ConvertFrom-NSPFortinetTokenText { # The portal token and User-Agent from whatever was copied out of the browser's DevTools: a # request copied as cURL (bash or cmd), its request headers, or the bare token. [CmdletBinding()] [OutputType([pscustomobject])] param([Parameter(Mandatory)][AllowEmptyString()][string]$Text) $text = $Text -replace '\^(.)', '$1' # cURL (cmd) escapes with '^' $find = { param([string]$Header) foreach ($pattern in "(?i)-H\s+(['`"])$Header`:\s*(?<value>.*?)\1", "(?im)^\s*$Header`:?\s*(?<value>.+?)\s*$") { $match = [regex]::Match($text, $pattern) if ($match.Success) { return $match.Groups['value'].Value.Trim() } } } $token = & $find 'authorization' if (-not $token) { $token = ([regex]::Match($text, 'eyJ[\w-]+\.eyJ[\w-]+\.[\w-]+')).Value } if (-not $token) { throw 'No portal token found. In the browser DevTools Network tab, copy a request to support.fortinet.com/app/ as cURL (it carries an Authorization header).' } $userAgent = & $find 'user-agent' if (-not $userAgent) { $userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0' } [pscustomobject]@{ Token = $token; UserAgent = $userAgent } } |