Public/Tokens/Get-CIF3Token.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
function Get-CIF3Token {
    <#
    .SYNOPSIS
        Gets list of tokens from CIF3 API.
    .DESCRIPTION
        This cmdlet calls the CIF API /tokens method to list all available tokens on the CIF instance.
    .EXAMPLE
        # Get token list with current session token
        PS C:\> Get-CIF3Token
         
        # Get token list using a specified token
        PS C:\> Get-CIF3Token -Token 0000111222333456789abcdef
 
        # Get token list using a specified CIF API URI.
        PS C:\> Get-CIF3Token -Uri http://cif.domain.local
 
        # Get token information for a token with name 'readonly'
        PS C:\> Get-CIF3Token -Name 'readonly'
 
    .OUTPUTS
        A an array of PSCustomObjects from CIF instance's API composed of token list properties.
        Properties of each PSCustomObject are acl, admin, expires, groups, id, last_activity_at, read, token,
            username, write
    .PARAMETER Token
        The API token to use when communicating with the CIF API (uses session token if not specified).
    .PARAMETER Uri
        The Base Uri to use for the CIF instance API (uses session Uri if not specified).
    .PARAMETER Name
        Username to search for.
    .PARAMETER TokenToFind
        Token to search for.
        'username' or 'token'
    .PARAMETER Raw
        Return the raw response object from the CIF API, versus parsing it and returning a hashtable.
    .FUNCTIONALITY
        CIF3
    .LINK
        https://github.com/csirtgadgets/bearded-avenger/blob/master/cif/httpd/views/tokens.py
    #>

    [CmdletBinding()]
    param (
        [string]$Token = $Script:CIF3.Token,

        [string]$Uri = $Script:CIF3.Uri,

        [string]$Name,

        [string]$TokenToFind,

        [switch]$Raw
    )

    begin {
        $Uri += '/tokens'

        $Body = @{ }

        switch($PSBoundParameters.Keys) {
            'Name'          { $Body.Add('username', $Name) }
            'TokenToFind'   { $Body.Add('token', $TokenToFind) }
        }
    }

    process {
        Write-Verbose 'Token listing from CIF API'

        $Params = @{
            Body    = $Body
            Method  = 'GET'
            Uri     = $Uri
        }

        Write-Verbose 'Adding token to request'
        $Params.Token = $Token
        Write-Verbose "Calling CIF API $Uri ..."
        
        $Response = Send-CIF3Api @Params -ErrorAction Stop
        
        if ($Raw) {
            return $Response
        } 
        else { 
            return Format-CIF3ApiResponse -InputObject $Response 
        }
    }
}