Functions/Server/Set-BambooAuthentication.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
<#
.SYNOPSIS
    Sets the Authentication credential ad mode for the target Bamboo Server.
.DESCRIPTION
    All further cmdlets from PsBamboo will be executed with the Authentication
    details passed by this command.
.PARAMETER Credential
    Mandatory - PSCredential to be used to login to the target Bamboo server
.PARAMETER AuthenticationToken
    Optional - Authentication Token to be directly set for further authentication
.PARAMETER AuthenticationMode
    Optional - Type of the Authentication process - currently Basic only
.EXAMPLE
    Set-BambooAuthentication -Credential (Get-Credential)
.EXAMPLE
    Set-BambooAuthentication -AuthenticationToken 'VXNlck5hbWU6UGFzc3dvcmQ='
#>


function Set-BambooAuthentication {
    [CmdletBinding(DefaultParameterSetName="ByCredential")]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUsePSCredentialType", "Credential")]
    param(
        [Parameter(Mandatory=$true,ParameterSetName='ByCredential')]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.CredentialAttribute()]
        $Credential,

        [Parameter(Mandatory=$true,ParameterSetName='ByAuthenticationToken')]
        [ValidateNotNullOrEmpty()]
        [Alias('Token')]
        [string]$AuthenticationToken,

        [ValidateSet('Basic')]
        [string]$AuthenticationMode='Basic'
    )
    # Set AuthenticationMode
    $script:AuthenticationMode = $AuthenticationMode

    # Directly set token
    if ($AuthenticationToken) {
        $script:AuthenticationToken=$AuthenticationToken
        return
    }

    # Get UserName and Password from Credential
    $UserName=$Credential.UserName
    $Password = $null
    if ($Credential.GetNetworkCredential()) {
        $Password=$Credential.GetNetworkCredential().password
    } else {
        $Password=[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($Credential.password))
    }

    # Construct the AuthToken by UserName and Password
    $script:AuthenticationToken=[Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $UserName,$Password)))
}