Private/Test-Captcha.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
function Test-Captcha {
    [CmdletBinding()]
    param(
        # Response of Invoke-WebRequest
        [Parameter( ValueFromPipeline )]
        [Microsoft.PowerShell.Commands.WebResponseObject]
        $InputObject
    )

    begin {
        $tokenRequiresCaptcha = "AUTHENTICATION_DENIED"
        $LoginReason = "X-Seraph-LoginReason"
    }

    process {
        Write-Verbose "[$($MyInvocation.MyCommand.Name)] Checking response headers for authentication errors"
        Write-Debug "[$($MyInvocation.MyCommand.Name)] Investigating `$InputObject.Headers['$LoginReason']"

        if ($InputObject.Headers -and $InputObject.Headers[$LoginReason]) {
            if ( ($InputObject.Headers[$LoginReason] -split ",") -contains $tokenRequiresCaptcha ) {
                $errorMessage = @{
                    Category         = "AuthenticationError"
                    CategoryActivity = "Authentication"
                    Message          = "JIRA requires you to log on to the website before continuing for security reasons."
                }
                Write-Error @errorMessage
            }
        }
    }

    end {
    }
}