KpPwpush.psm1

#Region './public/Connect-Pwpush.ps1' -1

function Connect-Pwpush
{
    <#
    .SYNOPSIS
        Connects to pwpush server and initialises the module
    .DESCRIPTION
        This must be first command used before running any other commands from this module. If no parameters are specified,
        defaults to pwpush.com server.
    .PARAMETER Server
        Server to connect to to
    .PARAMETER Credential
        User email and API token - token can be created at https://pwpush.com/en/users/token
    .EXAMPLE
        Connect-KpPwpush
        Connects to https://pwpush.com server
    #>

    [cmdletbinding()]
    param(
        [string]$Server = "https://pwpush.com/",

        [pscredential]$Credential
    )

    if ($Server[-1] -eq "/")
    {
        $Server = $server.Substring(0, $Server.Length - 1)
    }

    $Script:Connection = @{
        Server = $server
    }

    if ($PSBoundParameters.ContainsKey('Credential'))
    {
        $Script:Connection["userEmail"] = $Credential.UserName
        $Script:Connection["userToken"] = $Credential.GetNetworkCredential().Password
    }
}
#EndRegion './public/Connect-Pwpush.ps1' 39
#Region './public/Get-Pwpush.ps1' -1

function Get-Pwpush
{
    <#
    .SYNOPSIS
        Retrieves a push.
    .DESCRIPTION
        Retrieves a push including it’s payload and details. If the push is still active, this will burn a view and the transaction will be logged in the push audit log.
    .PARAMETER UrlToken
        Token of the push
    .LINK
        https://pwpush.com/api/1.0/passwords/show.en.html
    .EXAMPLE
        Get-KpPwpush -UrlToken abc123
        Retrieves push with token abc123
    #>

    [cmdletbinding()]
    param (
        [Parameter(Mandatory)]
        [string]$UrlToken
    )

    $endpoint = "p/$($UrlToken).json"

    $params = @{
        Endpoint = $endpoint
        Method   = "Get"
    }
    Invoke-PwpushRequest @params
}
#EndRegion './public/Get-Pwpush.ps1' 30
#Region './public/Get-PwpushActive.ps1' -1

function Get-PwpushActive
{
    <#
    .SYNOPSIS
        Returns the list of password pushes that you previously pushed which are still active.
    #>

    [cmdletbinding()]
    param (
    )

    $endpoint = "p/active.json"

    $params = @{
        Endpoint = $endpoint
        Method   = "Get"
    }
    Invoke-PwpushRequest @params
}
#EndRegion './public/Get-PwpushActive.ps1' 19
#Region './public/Get-PwpushExpired.ps1' -1

function Get-PwpushExpired
{
     <#
    .SYNOPSIS
        Returns the list of password pushes that you previously pushed which have expired.
    #>

    [cmdletbinding()]
    param (
    )

    $endpoint = "p/expired.json"

    $params = @{
        Endpoint = $endpoint
        Method   = "Get"
    }
    Invoke-PwpushRequest @params
}
#EndRegion './public/Get-PwpushExpired.ps1' 19
#Region './public/Get-PwpushPreview.ps1' -1

function Get-PwpushPreview
{
    <#
    .SYNOPSIS
        Retrieves the URL of a push.
    .DESCRIPTION
        Helper endpoint to retrieve the fully qualified secret URL of a push.
    .PARAMETER UrlToken
        Token of a secret
    .LINK
        https://pwpush.com/api/1.0/passwords/preview.en.html
    .EXAMPLE
        Get-KpPwpushPreview -UrlToken 123abc
        Retrieves URL for token 123abc
    #>

    [cmdletbinding()]
    param (
        [Parameter(Mandatory)]
        [string]$UrlToken
    )

    $endpoint = "p/$($UrlToken)/preview.json"

    $params = @{
        Endpoint = $endpoint
        Method   = "Get"
    }
    Invoke-PwpushRequest @params
}
#EndRegion './public/Get-PwpushPreview.ps1' 30
#Region './public/Invoke-PwpushRequest.ps1' -1

function Invoke-PwpushRequest
{
    <#
    .SYNOPSIS
        Main function for interacting with pwpush API
    .DESCRIPTION
        This function takes care of all headers, pagination, body formatting etc. for pwpush api calls.
    #>

    [cmdletbinding()]
    param(
        [Parameter(Mandatory)]
        [string]$Endpoint,

        [object]$Body,

        [ValidateSet("Get", "Post", "Delete")]
        [string]$Method = "Get",

        [switch]$ReturnRaw
    )

    if ( -not ($Script:Connection))
    {
        Throw "Please connect to Pwpush via Connect-KpPwpush"
    }

    $url = "$($Script:Connection.Server)/$Endpoint"

    $params = @{
        UseBasicParsing = $true
        Method = $Method
        Uri = $Url
    }

    if ($PSBoundParameters.ContainsKey('Body'))
    {
        $params.Add("Body", $Body)
        $params.Add("ContentType", "application/json")
    }

    if ($Script:Connection.ContainsKey('userEmail'))
    {
        $headers = @{
            "X-User-Email" = $Script:Connection.userEmail
            "X-User-Token" = $Script:Connection.userToken
        }
        $params.Add("headers", $headers)
    }

    try
    {
        $reposne = Invoke-WebRequest @params
    }
    catch
    {
        throw
    }
    
    if ($ReturnRaw.IsPresent)
    {
        return $reposne
    }

    return $reposne.Content | ConvertFrom-Json
}
#EndRegion './public/Invoke-PwpushRequest.ps1' 66
#Region './public/New-Pwpush.ps1' -1

function New-Pwpush
{
    <#
    .SYNOPSIS
        Create a new push.
    .DESCRIPTION
        Create a new push.
    .LINK
        https://pwpush.com/api/1.0/passwords/create.en.html
    .EXAMPLE
        New-KpPwpush -Payload mySecret
        Creates a new push with payload of mySecret
    #>


    [cmdletbinding()]
    param (
        [Parameter(Mandatory)]
        [string]$Payload,

        [string]$Note = "",

        [ValidateRange(1, 90)]
        [int]$ExpireAfterDays = 7,

        [ValidateRange(1, 100)]
        [int]$ExpireAfterViews = 5,

        [bool]$DeletableByViewer = $true,

        [bool]$RetrievalStep = $false
    )

    $endpoint = "p.json"

    $body = @{
        password = @{
            "payload"             = $Payload
            "note"                = $Note
            "expire_after_days"   = $ExpireAfterDays
            "expire_after_views"  = $ExpireAfterViews
            "deletable_by_viewer" = $DeletableByViewer
            "retrieval_step"      = $RetrievalStep
        }
    } | ConvertTo-Json

    $params = @{
        Endpoint = $endpoint
        Method   = "Post"
        Body     = $body
    }
    Invoke-PwpushRequest @params
}
#EndRegion './public/New-Pwpush.ps1' 53
#Region './public/Remove-Pwpush.ps1' -1

function Remove-Pwpush
{
    <#
    .SYNOPSIS
        Expires a push
    .DESCRIPTION
        Expires a push immediately. Must be authenticated & owner of the push or the push must have been created with deleteable_by_viewer.
    .LINK
        https://pwpush.com/api/1.0/passwords/destroy.en.html
    .PARAMETER UrlToken
        Token of a push
    .EXAMPLE
        Remove-KpPwPush -UrlToken abc123
        Expires token abc123
    #>

    [cmdletbinding()]
    param (
        [Parameter(Mandatory)]
        [string]$UrlToken
    )
    
    $endpoint = "p/$($UrlToken).json"

    $params = @{
        Endpoint = $endpoint 
        Method   = "Delete" 
    }
    Invoke-PwpushRequest @params
}
#EndRegion './public/Remove-Pwpush.ps1' 30
#Region './public/URL/Get-PwpushUrl.ps1' -1

function Get-PwpushUrl
{
    <#
    .SYNOPSIS
        Retrieves a URL push.
    .DESCRIPTION
        Retrieves a push including it’s payload and details. If the push is still active, this will burn a view and the transaction will be logged in the push audit log.
    .PARAMETER UrlToken
        Token of the push
    .LINK
        https://pwpush.com/api/1.0/urls/show.en.html
    .EXAMPLE
        Get-KpPwpushUrl -UrlToken abc123
        Retrieves push with token abc123
    #>

    [cmdletbinding()]
    param (
        [Parameter(Mandatory)]
        [string]$UrlToken,

        [string]$Passphrase
    )

    $endpoint = "r/$($UrlToken).json"

    if ($PSBoundParameters.ContainsKey('Passphrase'))
    {
        $endpoint += "?passphrase=$Passphrase"
    }

    $params = @{
        Endpoint = $endpoint
        Method   = "Get"
    }
    Invoke-PwpushRequest @params
}
#EndRegion './public/URL/Get-PwpushUrl.ps1' 37
#Region './public/URL/Get-PwpushUrlActive.ps1' -1

function Get-PwpushUrlActive
{
    <#
    .SYNOPSIS
        Returns the list of URL pushes that you previously pushed which are still active.
    #>

    [cmdletbinding()]
    param (
    )

    $endpoint = "r/active.json"

    $params = @{
        Endpoint = $endpoint
        Method   = "Get"
    }
    Invoke-PwpushRequest @params
}
#EndRegion './public/URL/Get-PwpushUrlActive.ps1' 19
#Region './public/URL/Get-PwpushUrlExpired.ps1' -1

function Get-PwpushUrlExpired
{
     <#
    .SYNOPSIS
        Retrieves a push including it’s payload and details. If the push is still active, this will burn a view and the transaction will be logged in the push audit log.
    #>

    [cmdletbinding()]
    param (
    )

    $endpoint = "r/expired.json"

    $params = @{
        Endpoint = $endpoint
        Method   = "Get"
    }
    Invoke-PwpushRequest @params
}
#EndRegion './public/URL/Get-PwpushUrlExpired.ps1' 19
#Region './public/URL/Get-PwpushUrlPreview.ps1' -1

function Get-PwpushUrlPreview
{
    <#
    .SYNOPSIS
        Helper endpoint to retrieve the fully qualified secret URL of a push.
    .DESCRIPTION
        Helper endpoint to retrieve the fully qualified secret URL of a push.
    .PARAMETER UrlToken
        Secret URL token of a previously created push
    .LINK
        https://pwpush.com/api/1.0/urls/preview.en.html
    .EXAMPLE
        Get-KpPwpushPreviewUrl -UrlToken 123abc
        Retrieves URL for token 123abc
    #>

    [cmdletbinding()]
    param (
        [Parameter(Mandatory)]
        [string]$UrlToken
    )

    $endpoint = "r/$($UrlToken)/preview.json"

    $params = @{
        Endpoint = $endpoint
        Method   = "Get"
    }
    Invoke-PwpushRequest @params
}
#EndRegion './public/URL/Get-PwpushUrlPreview.ps1' 30
#Region './public/URL/New-PwpushUrl.ps1' -1

function New-PwpushUrl
{
    <#
    .SYNOPSIS
        Create a new URL push.
    .DESCRIPTION
        Create a new URL push.
    .LINK
        https://pwpush.com/api/1.0/urls/create.en.html
    .EXAMPLE
        New-KpPwpushFile -Payload mySecret
        Creates a new push with payload of mySecret
    #>


    [cmdletbinding()]
    param (
        [Parameter(Mandatory)]
        [string]$Payload,

        [string]$Passphrase,

        [string]$Note = "",

        [ValidateRange(1, 90)]
        [int]$ExpireAfterDays = 7,

        [ValidateRange(1, 100)]
        [int]$ExpireAfterViews = 5,

        [bool]$DeletableByViewer = $true,

        [bool]$RetrievalStep = $false
    )

    $endpoint = "r.json"

    $data = @{
        url = @{
            "payload"             = $Payload
            "expire_after_days"   = $ExpireAfterDays
            "expire_after_views"  = $ExpireAfterViews
            "deletable_by_viewer" = $DeletableByViewer
            "retrieval_step"      = $RetrievalStep
        }
    }

    switch ($PSBoundParameters.GetEnumerator() )
    {
        { $_.Key -eq "Note" } { $data.url[$_.Key.ToLower()] = $_.Value }
        { $_.Key -eq "Passphrase" } { $data.url[$_.Key.ToLower()] = $_.Value }
    }

    $body = $data | ConvertTo-Json

    $params = @{
        Endpoint = $endpoint
        Method   = "Post"
        Body     = $body
    }
    Invoke-PwpushRequest @params
}
#EndRegion './public/URL/New-PwpushUrl.ps1' 62
#Region './public/URL/Remove-PwpushUrl.ps1' -1

function Remove-PwpushUrl
{
    <#
    .SYNOPSIS
        Expires a push
    .DESCRIPTION
        Expires a push immediately. Must be authenticated & owner of the push or the push must have been created with deleteable_by_viewer
    .LINK
        https://pwpush.com/api/1.0/passwords/destroy.en.html
    .PARAMETER UrlToken
        Token of a push
    .EXAMPLE
        Remove-KpPwPush -UrlToken abc123
        Expires token abc123
    #>

    [cmdletbinding()]
    param (
        [Parameter(Mandatory)]
        [string]$UrlToken
    )
    
    $endpoint = "r/$($UrlToken).json"

    $params = @{
        Endpoint = $endpoint 
        Method   = "Delete" 
    }
    Invoke-PwpushRequest @params
}
#EndRegion './public/URL/Remove-PwpushUrl.ps1' 30