Functions/New-Token/New-Token.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
function New-Token {
    <#
      .Synopsis
       Logs into Trello and returns a token that may be used to make calls.
      .Description
       Logs into Trello and returns a token that may be used to make calls. Use with other commands to work with private boards
      .Parameter BoardId
       The id of the board
      .Example
       # Get all cards on a private board
       $auth = New-TrelloToken -Key abc -AppName "My App"
       Get-TrelloCardsInBoard -BoardId fDsPBXFt -Token $auth
    #>

    [cmdletbinding()]
    param(
        [Parameter(
            Mandatory=$true,
            Position=0
        )]
        $Key,
        [Parameter(
            Mandatory=$true,
            Position=1
        )]
        $AppName,
        [Parameter(
            Mandatory=$false,
            Position=2
        )]
        $Expiration="30days",
        [Parameter(
            Mandatory=$false,
            Position=3
        )]
        [ValidateSet("read","read,write")]
        $Scope="read"
    )
    begin
    {
        function Get-oAuth2AccessToken { 
            <#
                .Synopsis
                Retrieves an oAuth 2.0 access token from the specified base authorization
                URL, client application ID, and callback URL.
   
                .Parameter AuthUrl
                The base authorization URL defined by the service provider.
   
                .Parameter ClientId
                The client ID (aka. app ID, consumer ID, etc.).
   
                .Parameter RedirectUri
                The callback URL configured on your application's registration with the
                service provider.
   
                .Parameter SleepInterval
                The number of seconds to sleep while waiting for the user to authorize the
                application.
   
                .Parameter Scope
                A string array of "scopes" (permissions) that your application will be
                requesting from the user's account.
            #>
 
            [CmdletBinding()] 
            param ( 
                [Parameter(
                    Mandatory=$true,
                    Position=0
                )]
                [string]$AuthUrl,
                [Parameter(
                    Mandatory=$false,
                    Position=1
                )]
                [int]$SleepInterval = 2 
            )
            begin
            {
                try {
                    # Create the Internet Explorer object
                    $IE = New-Object -ComObject InternetExplorer.Application
                    $IE.Visible = $true
                }
                catch
                {
                    Write-Error $_
                }
            }
            process
            {
                try {
                    # Navigate to the constructed authorization URL
                    $IE.Navigate($AuthUrl)
 
                    # Sleep the script for $X seconds until callback URL has been reached
                    # NOTE: If user cancels authorization, this condition will not be satisifed
                    while ($IE.LocationUrl -notmatch ‘token=’) {
                        Write-Debug -Message (‘Sleeping {0} seconds for access URL’ -f $SleepInterval)
                        Start-Sleep -Seconds $SleepInterval
                    } 
 
                    # Parse the access token from the callback URL and exit Internet Explorer
                    Write-Debug -Message (‘Callback URL is: {0}’ -f $IE.LocationUrl)
                    [Void]($IE.LocationUrl -match ‘=([\w\.]+)’)
                    $AccessToken = $Matches[1]
  
                    # Write the access token to the pipeline inside of a HashTable (in case we want to return other properties later)
                    Write-Debug -Message (‘Access token is: {0}’ -f $AccessToken)
                }
                catch
                {
                    Write-Error $_
                }
                Write-Output $AccessToken
            }
            end
            {
                try {
                    $IE.Quit()
                }
                catch
                {
                    Write-Error $_
                }
            }
        }
    }
    process
    {
        $Output = @{}
        try
        {
            [string]$token = Get-oAuth2AccessToken -AuthUrl "https://trello.com/1/authorize?key=$Key&name=$AppName&expiration=$Expiration&scope=$Scope&response_type=token&callback_method=fragment&return_url=https://trello.com?"

            $Output.Add("Token",$token.Replace(' ',''))
            $Output.Add("AccessKey",$Key)
        }
        catch
        {
            Write-Error $_
        }
        Write-Output $Output
    }
    end
    {
    }
}

Export-ModuleMember New-Token