Skip to content

Commit

Permalink
Merge pull request #164 from ritzcrackr/AnonymousAndCertificateAuthen…
Browse files Browse the repository at this point in the history
…tication

Anonymous and certificate authentication
  • Loading branch information
lipkau authored Feb 22, 2019
2 parents b855d29 + 9fd7a16 commit c7daf91
Show file tree
Hide file tree
Showing 45 changed files with 733 additions and 344 deletions.
45 changes: 45 additions & 0 deletions ConfluencePS/Private/Copy-CommonParameter.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
function Copy-CommonParameter {
<#
.SYNOPSIS
This is a helper function to assist in creating a hashtable for splatting parameters to inner function calls.
.DESCRIPTION
This command copies all of the keys of a hashtable to a new hashtable if the key name matches the DefaultParameter
or the AdditionalParameter values. This function is designed to help select only function parameters that have been
set, so they can be passed to inner functions if and only if they have been set.
.EXAMPLE
PS C:\> Copy-CommonParameter -InputObject $PSBoundParameters
Returns a hashtable that contains all of the bound default parameters.
.EXAMPLE
PS C:\> Copy-CommonParameter -InputObject $PSBoundParameters -AdditionalParameter "ApiUri"
Returns a hashtable that contains all of the bound default parameters and the "ApiUri" parameter.
#>
[CmdletBinding( SupportsShouldProcess = $false )]
[OutputType(
[hashtable]
)]
param
(
[Parameter(Mandatory = $true)]
[hashtable]$InputObject,

[Parameter(Mandatory = $false)]
[string[]]$AdditionalParameter,

[Parameter(Mandatory = $false)]
[string[]]$DefaultParameter = @("Credential", "Certificate")
)

[hashtable]$ht = @{}
foreach ($key in $InputObject.Keys) {
if ($key -in ($DefaultParameter + $AdditionalParameter)) {
$ht[$key] = $InputObject[$key]
}
}

return $ht
}
27 changes: 14 additions & 13 deletions ConfluencePS/Public/Add-Attachment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ function Add-Attachment {
[OutputType([ConfluencePS.Attachment])]
param(
[Parameter( Mandatory = $true )]
[URi]$apiURi,
[uri]$ApiUri,

[Parameter( Mandatory = $true )]
[Parameter( Mandatory = $false )]
[PSCredential]$Credential,

[Parameter( Mandatory = $false )]
[ValidateNotNull()]
[System.Security.Cryptography.X509Certificates.X509Certificate]
$Certificate,

[Parameter(
Position = 0,
Mandatory = $true,
Expand Down Expand Up @@ -46,27 +51,23 @@ function Add-Attachment {

begin {
Write-Verbose "[$($MyInvocation.MyCommand.Name)] Function started"

$resourceApi = "$apiURi/content/{0}/child/attachment"
}

process {
Write-DebugMessage "[$($MyInvocation.MyCommand.Name)] ParameterSetName: $($PsCmdlet.ParameterSetName)"
Write-DebugMessage "[$($MyInvocation.MyCommand.Name)] PSBoundParameters: $($PSBoundParameters | Out-String)"

$parameter = @{
URI = $resourceApi -f $PageID
Method = "POST"
Credential = $Credential
OutputType = [ConfluencePS.Attachment]
Verbose = $false
}
$iwParameters = Copy-CommonParameter -InputObject $PSBoundParameters
$iwParameters['Uri'] = "$ApiUri/content/{0}/child/attachment" -f $PageID
$iwParameters['Method'] = 'Post'
$iwParameters['OutputType'] = [ConfluencePS.Attachment]

foreach ($file in $FilePath) {
$parameter["InFile"] = $file
$iwParameters["InFile"] = $file

Write-Debug "[$($MyInvocation.MyCommand.Name)] Invoking Add Attachment Method with `$parameter"
if ($PSCmdlet.ShouldProcess($PageID, "Adding attachment(s) '$($file)'.")) {
Invoke-Method @parameter
Invoke-Method @iwParameters
}
}
}
Expand Down
26 changes: 14 additions & 12 deletions ConfluencePS/Public/Add-Label.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ function Add-Label {
[OutputType([ConfluencePS.ContentLabelSet])]
param (
[Parameter( Mandatory = $true )]
[URi]$apiURi,
[uri]$ApiUri,

[Parameter( Mandatory = $true )]
[Parameter( Mandatory = $false )]
[PSCredential]$Credential,

[Parameter( Mandatory = $false )]
[ValidateNotNull()]
[System.Security.Cryptography.X509Certificates.X509Certificate]
$Certificate,

[Parameter(
Position = 0,
ValueFromPipeline = $true,
Expand All @@ -32,7 +37,7 @@ function Add-Label {
BEGIN {
Write-Verbose "[$($MyInvocation.MyCommand.Name)] Function started"

$resourceApi = "$apiURi/content/{0}/label"
$resourceApi = "$ApiUri/content/{0}/label"
}

PROCESS {
Expand Down Expand Up @@ -71,13 +76,9 @@ function Add-Label {
Throw $exception
}

$iwParameters = @{
Uri = ""
Method = 'Post'
Body = ""
OutputType = [ConfluencePS.Label]
Credential = $Credential
}
$iwParameters = Copy-CommonParameter -InputObject $PSBoundParameters
$iwParameters['Method'] = 'Post'
$iwParameters['OutputType'] = [ConfluencePS.Label]

# Extract name if an Object is provided
if (($Label -is [ConfluencePS.Label]) -or $Label -is [ConfluencePS.Label[]]) {
Expand All @@ -92,14 +93,15 @@ function Add-Label {
$InputObject = $_.Page
}
else {
$InputObject = Get-Page -PageID $_page -ApiURi $apiURi -Credential $Credential
$authAndApiUri = Copy-CommonParameter -InputObject $PSBoundParameters -AdditionalParameter "ApiUri"
$InputObject = Get-Page -PageID $_page @authAndApiUri
}

$iwParameters["Uri"] = $resourceApi -f $_page
$iwParameters["Body"] = ($Label | Foreach-Object {@{prefix = 'global'; name = $_}}) | ConvertTo-Json

Write-Debug "[$($MyInvocation.MyCommand.Name)] Content to be sent: $($iwParameters["Body"] | Out-String)"
If ($PSCmdlet.ShouldProcess("Label $Label, PageID $_page")) {
if ($PSCmdlet.ShouldProcess("Label $Label, PageID $_page")) {
$output = [ConfluencePS.ContentLabelSet]@{ Page = $InputObject }
$output.Labels += (Invoke-Method @iwParameters)
$output
Expand Down
26 changes: 15 additions & 11 deletions ConfluencePS/Public/ConvertTo-StorageFormat.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ function ConvertTo-StorageFormat {
[OutputType([String])]
param (
[Parameter( Mandatory = $true )]
[URi]$apiURi,
[uri]$ApiUri,

[Parameter( Mandatory = $true )]
[Parameter( Mandatory = $false )]
[PSCredential]$Credential,

[Parameter( Mandatory = $false )]
[ValidateNotNull()]
[System.Security.Cryptography.X509Certificates.X509Certificate]
$Certificate,

[Parameter(
Position = 0,
Mandatory = $true,
Expand All @@ -24,16 +29,15 @@ function ConvertTo-StorageFormat {
Write-Debug "[$($MyInvocation.MyCommand.Name)] ParameterSetName: $($PsCmdlet.ParameterSetName)"
Write-Debug "[$($MyInvocation.MyCommand.Name)] PSBoundParameters: $($PSBoundParameters | Out-String)"

$iwParameters = Copy-CommonParameter -InputObject $PSBoundParameters
$iwParameters['Uri'] = "$ApiUri/contentbody/convert/storage"
$iwParameters['Method'] = 'Post'

foreach ($_content in $Content) {
$iwParameters = @{
Uri = "$apiURi/contentbody/convert/storage"
Method = 'Post'
Body = @{
value = "$_content"
representation = 'wiki'
} | ConvertTo-Json
Credential = $Credential
}
$iwParameters['Body'] = @{
value = "$_content"
representation = 'wiki'
} | ConvertTo-Json

Write-Debug "[$($MyInvocation.MyCommand.Name)] Content to be sent: $($_content | Out-String)"
(Invoke-Method @iwParameters).value
Expand Down
13 changes: 7 additions & 6 deletions ConfluencePS/Public/ConvertTo-Table.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ function ConvertTo-Table {

# This ForEach needed if the content wasn't piped in
$Content | ForEach-Object {
If ($Vertical) {
If ($HeaderGenerated) {$pipe = '|'}
Else {$pipe = '||'}
if ($Vertical) {
if ($HeaderGenerated) {$pipe = '|'}
else {$pipe = '||'}

# Put an empty row between multiple tables (objects)
If ($Spacer) {
if ($Spacer) {
$null = $sb.AppendLine('')
}

Expand All @@ -42,9 +42,10 @@ function ConvertTo-Table {
}

$Spacer = $true
} Else {
}
else {
# Header row enclosed by ||
If (-not $HeaderGenerated) {
if (-not $HeaderGenerated) {
$null = $sb.AppendLine("|| {0} ||" -f ($_.PSObject.Properties.Name -join " || "))
$HeaderGenerated = $true
}
Expand Down
55 changes: 29 additions & 26 deletions ConfluencePS/Public/Get-Attachment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ function Get-Attachment {
[OutputType([ConfluencePS.Attachment])]
param (
[Parameter( Mandatory = $true )]
[URi]$apiURi,
[uri]$ApiUri,

[Parameter( Mandatory = $true )]
[Parameter( Mandatory = $false )]
[PSCredential]$Credential,

[Parameter( Mandatory = $false )]
[ValidateNotNull()]
[System.Security.Cryptography.X509Certificates.X509Certificate]
$Certificate,

[Parameter(
Position = 0,
Mandatory = $true,
Expand All @@ -28,7 +33,6 @@ function Get-Attachment {

BEGIN {
Write-Verbose "[$($MyInvocation.MyCommand.Name)] Function started"
$resourceApi = "$apiURi/content/{0}/child/attachment"
}

PROCESS {
Expand All @@ -41,30 +45,29 @@ function Get-Attachment {
Throw $exception
}

$iwParameters = Copy-CommonParameter -InputObject $PSBoundParameters
$iwParameters['Method'] = 'Get'
$iwParameters['GetParameters'] = @{
expand = "version"
limit = $PageSize
}
$iwParameters['OutputType'] = [ConfluencePS.Attachment]

if ($FileNameFilter) {
$iwParameters["GetParameters"]["filename"] = $FileNameFilter
}

if ($MediaTypeFilter) {
$iwParameters["GetParameters"]["mediaType"] = $MediaTypeFilter
}

# Paging
($PSCmdlet.PagingParameters | Get-Member -MemberType Property).Name | ForEach-Object {
$iwParameters[$_] = $PSCmdlet.PagingParameters.$_
}

foreach ($_PageID in $PageID) {
$iwParameters = @{
Uri = $resourceApi -f $_PageID
Method = 'Get'
GetParameters = @{
expand = "version"
limit = $PageSize
}
OutputType = [ConfluencePS.Attachment]
Credential = $Credential
}

if ($FileNameFilter) {
$iwParameters["GetParameters"]["filename"] = $FileNameFilter
}

if ($MediaTypeFilter) {
$iwParameters["GetParameters"]["mediaType"] = $MediaTypeFilter
}

# Paging
($PSCmdlet.PagingParameters | Get-Member -MemberType Property).Name | ForEach-Object {
$iwParameters[$_] = $PSCmdlet.PagingParameters.$_
}
$iwParameters['Uri'] = "$ApiUri/content/{0}/child/attachment" -f $_PageID

Invoke-Method @iwParameters
}
Expand Down
29 changes: 13 additions & 16 deletions ConfluencePS/Public/Get-AttachmentFile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ function Get-AttachmentFile {
[OutputType([Bool])]
param (
[Parameter( Mandatory = $true )]
[URi]$apiURi,
[uri]$ApiUri,

[Parameter( Mandatory = $true )]
[Parameter( Mandatory = $false )]
[PSCredential]$Credential,

[Parameter( Mandatory = $false )]
[ValidateNotNull()]
[System.Security.Cryptography.X509Certificates.X509Certificate]
$Certificate,

[Parameter(
Position = 0,
Mandatory = $true,
Expand Down Expand Up @@ -49,21 +54,13 @@ function Get-AttachmentFile {
Throw $exception
}

foreach ($_Attachment in $Attachment) {
if ($Path) {
$filename = Join-Path $Path $_Attachment.Filename
}
else {
$filename = $_Attachment.Filename
}
$iwParameters = Copy-CommonParameter -InputObject $PSBoundParameters
$iwParameters['Method'] = 'Get'

$iwParameters = @{
Uri = $_Attachment.URL
Method = 'Get'
Headers = @{"Accept" = $_Attachment.MediaType}
OutFile = $filename
Credential = $Credential
}
foreach ($_Attachment in $Attachment) {
$iwParameters['Uri'] = $_Attachment.URL
$iwParameters['Headers'] = @{"Accept" = $_Attachment.MediaType}
$iwParameters['OutFile'] = if ($Path) {Join-Path -Path $Path -ChildPath $_Attachment.Filename} else {$_Attachment.Filename}

$result = Invoke-Method @iwParameters
(-not $result)
Expand Down
Loading

0 comments on commit c7daf91

Please sign in to comment.