SecretManagement.PleasantPasswordServer.Extension/Private/Invoke-LoginToPleasant.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
function Invoke-LoginToPleasant
{

    <#
        .SYNOPSIS
         Login to Pleasant Password Server

        .DESCRIPTION
         Login to Pleasant Password Server

        .PARAMETER AdditionalParameters
         The following values need to be in there:
           ServerURL
           Port

        .EXAMPLE

           $var = @{
              ServerURL = "https://ppsdc1.pps.net"
              Port = "10001"
           }

           Invoke-LoginToPleasant -AdditionalParameters $var

        .NOTES
           Author: Constantin Hager
           Date: 2020-12-31
    #>


    [CmdletBinding()]
    param (
        [Parameter()]
        [Hashtable]
        $AdditionalParameters
    )

    $PasswordServerURL = [string]::Concat($AdditionalParameters.ServerURL, ":", $AdditionalParameters.Port)

    $SecretFile = Get-SecretFile

    # Create OAuth2 token params
    $tokenParams = @{
        grant_type = 'password';
        username   = $SecretFile.UserName;
        password   = $SecretFile.GetNetworkCredential().password;
    }

    $splat = @{
        Uri         = "$PasswordServerURL/OAuth2/Token"
        Method      = "POST"
        Body        = $tokenParams
        ContentType = "application/x-www-form-urlencoded"
        ErrorAction = "SilentlyContinue"
    }

    # Authenticate to Pleasant Password Server
    $JSON = Invoke-WebRequest @splat

    if ($null -eq $JSON)
    {
        return $null
    }
    else
    {
        # Generate JSON token
        $Token = (ConvertFrom-Json $JSON.Content).access_token

        return $Token
    }

}