Skip to content

Commit

Permalink
Feat/authentication (#2)
Browse files Browse the repository at this point in the history
* add ability to authneticate to pwpush and review active pushes

* add linting on errors

* catch error

* more info
  • Loading branch information
thekamilpro committed Feb 27, 2024
1 parent 3e4346b commit 8531a71
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 27 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ jobs:
- name: Install modules
shell: pwsh
run : |
Install-Module -Name ModuleBuilder -Force
Install-Module -Name ModuleBuilder, PSScriptAnalyzer -Force
- name: Lint
shell: pwsh
run : |
. ./test/lint.ps1
- name: Build module
shell: pwsh
Expand Down
18 changes: 13 additions & 5 deletions src/public/Connect-Pwpush.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Connect-Pwpush
function Connect-Pwpush
{
<#
.SYNOPSIS
Expand All @@ -8,23 +8,31 @@ function Connect-Pwpush
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/"
[string]$Server = "https://pwpush.com/",

[pscredential]$Credential
)

if ($Server[-1] -eq "/")
{
$Server = $server.Substring(0, $Server.Length - 1)
$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
}
}
8 changes: 4 additions & 4 deletions src/public/Get-Pwpush.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Get-Pwpush
function Get-Pwpush
{
<#
.SYNOPSIS
Expand All @@ -18,12 +18,12 @@ function Get-Pwpush
[Parameter(Mandatory)]
[string]$UrlToken
)

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

$params = @{
Endpoint = $endpoint
Method = "Get"
Endpoint = $endpoint
Method = "Get"
}
Invoke-PwpushRequest @params
}
18 changes: 18 additions & 0 deletions src/public/Get-PwpushActive.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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
}
18 changes: 18 additions & 0 deletions src/public/Get-PwpushExpired.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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
}
8 changes: 4 additions & 4 deletions src/public/Get-PwpushPreview.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Get-PwpushPreview
function Get-PwpushPreview
{
<#
.SYNOPSIS
Expand All @@ -18,12 +18,12 @@ function Get-PwpushPreview
[Parameter(Mandatory)]
[string]$UrlToken
)

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

$params = @{
Endpoint = $endpoint
Method = "Get"
Endpoint = $endpoint
Method = "Get"
}
Invoke-PwpushRequest @params
}
23 changes: 20 additions & 3 deletions src/public/Invoke-PwpushRequest.ps1
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
function Invoke-PwpushRequest
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.
This function takes care of all headers, pagination, body formatting etc. for pwpush api calls.
#>
[cmdletbinding()]
param(
Expand Down Expand Up @@ -38,7 +38,24 @@ function Invoke-PwpushRequest
$params.Add("ContentType", "application/json")
}

$reposne = Invoke-WebRequest @params
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
Expand Down
19 changes: 9 additions & 10 deletions src/public/New-Pwpush.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function New-Pwpush
function New-Pwpush
{
<#
.SYNOPSIS
Expand All @@ -11,26 +11,25 @@ function New-Pwpush
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 = @{
Expand All @@ -45,8 +44,8 @@ function New-Pwpush
} | ConvertTo-Json

$params = @{
Endpoint = $endpoint
Method = "Post"
Endpoint = $endpoint
Method = "Post"
Body = $body
}
Invoke-PwpushRequest @params
Expand Down
1 change: 1 addition & 0 deletions test/lint.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Invoke-ScriptAnalyzer -Path "$PSScriptRoot/../src" -Recurse -Severity @("Error")

0 comments on commit 8531a71

Please sign in to comment.