Private/Invoke-LPLogin.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<#
.Synopsis
   Logs in to LastPass
.DESCRIPTION
   Sends the login request to LastPass and throws an error if it fails.
.EXAMPLE
   Invoke-LPLogin
#>

function Invoke-LPLogin
{
    [CmdletBinding()]
    Param()

    Begin
    {
        if (!$LPKeys)
        {
            $LPKeys = Get-LPKeys
        }
    }
    Process
    {
        Write-Verbose "Setting up common variables"
        $WebRequestSettings = @{
            "UserAgent" = $LPUserAgent;
            "WebSession" = $LPSession;
            "UseBasicParsing" = $true;
            "ErrorAction" = "Stop";
        }

        Write-Verbose "Attempting to login"
        try
        {
            $LoginBody = @{
                "xml" = 2;
                "username" = $LPLogin.UserName.ToLower();
                "hash" = $LPKeys.UserName;
                "iterations" = $LPIterations;
                "includeprivatekeyenc" = 1;
                "method" = "cli";
                "outofbandsupported" = 1;
            }

            $LoginResponse = Invoke-WebRequest -Uri "$LPUrl/login.php" -Method Post -Body $LoginBody @WebRequestSettings
            Write-Debug $($LoginResponse | Out-String)

            switch ($([xml]$LoginResponse.Content).response.error.cause) {
                $null
                {
                    if ($([xml]$LoginResponse.Content).response.ok)
                    {
                        Write-Verbose "Sucessful login"
                    }
                    else
                    {
                        throw "Malformed response from server"
                    }
                }
                "outofbandrequired"
                {
                    Write-Host "Out of band authentication is required"
                    Write-Verbose "Trying login again with out of band request"
                    $LoginBody.Add("outofbandrequest",1)
                    $LoginResponse = Invoke-WebRequest -Uri "$LPUrl/login.php" -Method Post -Body $LoginBody @WebRequestSettings
                    Write-Debug $($LoginResponse | Out-String)

                    if ($([xml]$LoginResponse.Content).response.error)
                    {
                        throw "$($([xml]$LoginResponse.Content).response.error.message)"
                    }
                    if ($([xml]$LoginResponse.Content).response.ok)
                    {
                        Write-Verbose "Sucessful login"
                    }
                    else
                    {
                        throw "Malformed response from server"
                    }
                }
                "unknownpassword"
                {
                    Write-Host "Invalid LastPass password"
                    $script:LPLogin = $null
                }
                Default
                {
                    throw "$($([xml]$LoginResponse.Content).response.error.message)"
                }
            }

            $script:LPSession = $LPSession
            $LPSession
        }
        catch
        {
            throw "Failed to login: $_"
        }
    }
}