From 7bfcba57c8ef15ff45ba9a1e073a03f91b506c29 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sat, 7 Oct 2023 15:01:43 +0200 Subject: [PATCH 1/5] Added Add-GitHubUserSigningKey --- .../Add-GitHubUserSigningKey.ps1 | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/GitHub/public/Users/SSH-Signing-Keys/Add-GitHubUserSigningKey.ps1 diff --git a/src/GitHub/public/Users/SSH-Signing-Keys/Add-GitHubUserSigningKey.ps1 b/src/GitHub/public/Users/SSH-Signing-Keys/Add-GitHubUserSigningKey.ps1 new file mode 100644 index 000000000..cb55dc119 --- /dev/null +++ b/src/GitHub/public/Users/SSH-Signing-Keys/Add-GitHubUserSigningKey.ps1 @@ -0,0 +1,49 @@ +filter Add-GitHubUserSigningKey { + <# + .SYNOPSIS + Create a SSH signing key for the authenticated user + + .DESCRIPTION + Creates an SSH signing key for the authenticated user's GitHub account. + You must authenticate with Basic Authentication, or you must authenticate with OAuth with at least `write:ssh_signing_key` scope. For more information, see "[Understanding scopes for OAuth apps](https://docs.github.com/apps/building-oauth-apps/understanding-scopes-for-oauth-apps/)." + + .EXAMPLE + Add-GitHubUserSigningKey -Title 'ssh-rsa AAAAB3NzaC1yc2EAAA' -Key '2Sg8iYjAxxmI2LvUXpJjkYrMxURPc8r+dB7TJyvv1234' + + Creates a new SSH signing key for the authenticated user's GitHub account. + + .NOTES + https://docs.github.com/rest/users/ssh-signing-keys#create-a-ssh-signing-key-for-the-authenticated-user + + #> + [OutputType([pscustomobject])] + [CmdletBinding()] + param ( + # A descriptive name for the new key. + [Parameter( + ValueFromPipeline, + ValueFromPipelineByPropertyName + )] + [Alias('name')] + [string] $Title, + + # The public SSH key to add to your GitHub account. For more information, see [Checking for existing SSH keys](https://docs.github.com/authentication/connecting-to-github-with-ssh/checking-for-existing-ssh-keys)." + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName + )] + [string] $Key + + ) + + $body = $PSBoundParameters | ConvertFrom-HashTable | ConvertTo-HashTable -NameCasingStyle snake_case + + $inputObject = @{ + APIEndpoint = "/user/ssh_signing_keys" + Method = 'POST' + Body = $body + } + + (Invoke-GitHubAPI @inputObject).Response + +} From a3e1b624c3e04443b6f09f577c9c26441559b009 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sat, 7 Oct 2023 15:04:59 +0200 Subject: [PATCH 2/5] Added Remove-GitHubUserSigningKey --- .../Remove-GitHubUserSigningKey.ps1 | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/GitHub/public/Users/SSH-Signing-Keys/Remove-GitHubUserSigningKey.ps1 diff --git a/src/GitHub/public/Users/SSH-Signing-Keys/Remove-GitHubUserSigningKey.ps1 b/src/GitHub/public/Users/SSH-Signing-Keys/Remove-GitHubUserSigningKey.ps1 new file mode 100644 index 000000000..48a283c3d --- /dev/null +++ b/src/GitHub/public/Users/SSH-Signing-Keys/Remove-GitHubUserSigningKey.ps1 @@ -0,0 +1,37 @@ +filter Remove-GitHubUserSigningKey { + <# + .SYNOPSIS + Delete an SSH signing key for the authenticated user + + .DESCRIPTION + Deletes an SSH signing key from the authenticated user's GitHub account. + You must authenticate with Basic Authentication, or you must authenticate with OAuth with at least `admin:ssh_signing_key` scope. For more information, see "[Understanding scopes for OAuth apps](https://docs.github.com/apps/building-oauth-apps/understanding-scopes-for-oauth-apps/)." + + .EXAMPLE + Remove-GitHubUserSigningKey -ID '1234567' + + Removes the SSH signing key with the ID of `1234567` from the authenticated user's GitHub account. + + .NOTES + https://docs.github.com/rest/users/ssh-signing-keys#delete-an-ssh-signing-key-for-the-authenticated-user + + #> + [OutputType([pscustomobject])] + [CmdletBinding()] + param ( + # The unique identifier of the SSH signing key. + [Parameter( + Mandatory + )] + [Alias('ssh_signing_key_id')] + [string] $ID + ) + + $inputObject = @{ + APIEndpoint = "/user/ssh_signing_keys/$ID" + Method = 'DELETE' + } + + $null = (Invoke-GitHubAPI @inputObject).Response + +} From d4aef4f26049bcade7bae6d67cd1f4304e7e23a3 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sat, 7 Oct 2023 15:05:08 +0200 Subject: [PATCH 3/5] doc fix --- src/GitHub/public/Users/Keys/Remove-GitHubUserKey.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GitHub/public/Users/Keys/Remove-GitHubUserKey.ps1 b/src/GitHub/public/Users/Keys/Remove-GitHubUserKey.ps1 index 5151c069e..0c79573f6 100644 --- a/src/GitHub/public/Users/Keys/Remove-GitHubUserKey.ps1 +++ b/src/GitHub/public/Users/Keys/Remove-GitHubUserKey.ps1 @@ -19,7 +19,7 @@ [OutputType([pscustomobject])] [CmdletBinding()] param ( - # The ID of the GPG key. + # The unique identifier of the key. [Parameter( Mandatory )] From f0be3bb34aaa5e34a64a1706e8b09dfe0f0f7321 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sat, 7 Oct 2023 15:19:57 +0200 Subject: [PATCH 4/5] doc fixes --- .../public/Users/GPG-Keys/Get-GitHubUserGpgKey.ps1 | 5 +++++ src/GitHub/public/Users/Keys/Get-GitHubUserKey.ps1 | 11 +++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/GitHub/public/Users/GPG-Keys/Get-GitHubUserGpgKey.ps1 b/src/GitHub/public/Users/GPG-Keys/Get-GitHubUserGpgKey.ps1 index 6f11ace0f..0ccb50c2b 100644 --- a/src/GitHub/public/Users/GPG-Keys/Get-GitHubUserGpgKey.ps1 +++ b/src/GitHub/public/Users/GPG-Keys/Get-GitHubUserGpgKey.ps1 @@ -11,6 +11,11 @@ Gets all GPG keys for the authenticated user. + .EXAMPLE + Get-GitHubUserGpgKey -ID '1234567' + + Gets the GPG key with ID '1234567' for the authenticated user. + .EXAMPLE Get-GitHubUserGpgKey -Username 'octocat' diff --git a/src/GitHub/public/Users/Keys/Get-GitHubUserKey.ps1 b/src/GitHub/public/Users/Keys/Get-GitHubUserKey.ps1 index 4706d9745..cdbfca733 100644 --- a/src/GitHub/public/Users/Keys/Get-GitHubUserKey.ps1 +++ b/src/GitHub/public/Users/Keys/Get-GitHubUserKey.ps1 @@ -1,16 +1,23 @@ filter Get-GitHubUserKey { <# .SYNOPSIS - List GPG keys for a given user or the authenticated user + List public SSH keys for a given user or the authenticated user. .DESCRIPTION - Lists a given user's or the current user's GPG keys. + Lists a given user's or the current user's public SSH keys. + For the authenticated users keys, it requires that you are authenticated via Basic Auth or via OAuth with at least `read:public_key` [scope](https://docs.github.com/apps/building-oauth-apps/understanding-scopes-for-oauth-apps/). + Keys from a given user are accessible by anyone. .EXAMPLE Get-GitHubUserKey Gets all GPG keys for the authenticated user. + .EXAMPLE + Get-GitHubUserKey -ID '1234567' + + Gets the public SSH key with the ID '1234567' for the authenticated user. + .EXAMPLE Get-GitHubUserKey -Username 'octocat' From 7aa15a4e50be9f3991f5eefba3fc7e495eb4e953 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sat, 7 Oct 2023 15:20:11 +0200 Subject: [PATCH 5/5] Add Get-GitHubUserSigningKey --- .../Get-GitHubUserMySigningKey.ps1 | 37 +++++++++++ .../Get-GitHubUserMySigningKeyById.ps1 | 37 +++++++++++ .../Get-GitHubUserSigningKeyForUser.ps1 | 44 +++++++++++++ .../Get-GitHubUserSigningKey.ps1 | 63 +++++++++++++++++++ tools/utilities/GitHubAPI.ps1 | 4 +- 5 files changed, 183 insertions(+), 2 deletions(-) create mode 100644 src/GitHub/private/Users/SSH-Signing-Keys/Get-GitHubUserMySigningKey.ps1 create mode 100644 src/GitHub/private/Users/SSH-Signing-Keys/Get-GitHubUserMySigningKeyById.ps1 create mode 100644 src/GitHub/private/Users/SSH-Signing-Keys/Get-GitHubUserSigningKeyForUser.ps1 create mode 100644 src/GitHub/public/Users/SSH-Signing-Keys/Get-GitHubUserSigningKey.ps1 diff --git a/src/GitHub/private/Users/SSH-Signing-Keys/Get-GitHubUserMySigningKey.ps1 b/src/GitHub/private/Users/SSH-Signing-Keys/Get-GitHubUserMySigningKey.ps1 new file mode 100644 index 000000000..ad6f7f7bf --- /dev/null +++ b/src/GitHub/private/Users/SSH-Signing-Keys/Get-GitHubUserMySigningKey.ps1 @@ -0,0 +1,37 @@ +filter Get-GitHubUserMySigningKey { + <# + .SYNOPSIS + List SSH signing keys for the authenticated user + + .DESCRIPTION + Lists the SSH signing keys for the authenticated user's GitHub account. + You must authenticate with Basic Authentication, or you must authenticate with OAuth with at least `read:ssh_signing_key` scope. For more information, see "[Understanding scopes for OAuth apps](https://docs.github.com/apps/building-oauth-apps/understanding-scopes-for-oauth-apps/)." + + .EXAMPLE + Get-GitHubUserMySigningKey + + Lists the SSH signing keys for the authenticated user's GitHub account. + + .NOTES + https://docs.github.com/rest/users/ssh-signing-keys#list-ssh-signing-keys-for-the-authenticated-user + + #> + [OutputType([pscustomobject])] + [CmdletBinding()] + param ( + # The number of results per page (max 100). + [Parameter()] + [int] $PerPage = 30 + ) + + $body = $PSBoundParameters | ConvertFrom-HashTable | ConvertTo-HashTable -NameCasingStyle snake_case + + $inputObject = @{ + APIEndpoint = '/user/ssh_signing_keys' + Method = 'GET' + Body = $body + } + + (Invoke-GitHubAPI @inputObject).Response + +} diff --git a/src/GitHub/private/Users/SSH-Signing-Keys/Get-GitHubUserMySigningKeyById.ps1 b/src/GitHub/private/Users/SSH-Signing-Keys/Get-GitHubUserMySigningKeyById.ps1 new file mode 100644 index 000000000..677d197de --- /dev/null +++ b/src/GitHub/private/Users/SSH-Signing-Keys/Get-GitHubUserMySigningKeyById.ps1 @@ -0,0 +1,37 @@ +filter Get-GitHubUserMySigningKeyById { + <# + .SYNOPSIS + Get an SSH signing key for the authenticated user + + .DESCRIPTION + Gets extended details for an SSH signing key. + You must authenticate with Basic Authentication, or you must authenticate with OAuth with at least `read:ssh_signing_key` scope. For more information, see "[Understanding scopes for OAuth apps](https://docs.github.com/apps/building-oauth-apps/understanding-scopes-for-oauth-apps/)." + + .EXAMPLE + Get-GitHubUserMySigningKeyById -ID '1234567' + + Gets the SSH signing key with the ID '1234567' for the authenticated user. + + .NOTES + https://docs.github.com/rest/users/ssh-signing-keys#get-an-ssh-signing-key-for-the-authenticated-user + + #> + [OutputType([pscustomobject])] + [CmdletBinding()] + param ( + # The unique identifier of the SSH signing key. + [Parameter( + Mandatory + )] + [Alias('ssh_signing_key_id')] + [string] $ID + ) + + $inputObject = @{ + APIEndpoint = "/user/ssh_signing_keys/$ID" + Method = 'GET' + } + + (Invoke-GitHubAPI @inputObject).Response + +} diff --git a/src/GitHub/private/Users/SSH-Signing-Keys/Get-GitHubUserSigningKeyForUser.ps1 b/src/GitHub/private/Users/SSH-Signing-Keys/Get-GitHubUserSigningKeyForUser.ps1 new file mode 100644 index 000000000..78aa25598 --- /dev/null +++ b/src/GitHub/private/Users/SSH-Signing-Keys/Get-GitHubUserSigningKeyForUser.ps1 @@ -0,0 +1,44 @@ +filter Get-GitHubUserSigningKeyForUser { + <# + .SYNOPSIS + List SSH signing keys for a user + + .DESCRIPTION + List SSH signing keys for a user + + .EXAMPLE + Get-GitHubUserSigningKeyForUser -Username 'octocat' + + Gets the SSH signing keys for the user 'octocat'. + + .NOTES + https://docs.github.com/rest/users/ssh-signing-keys#list-ssh-signing-keys-for-a-user + + #> + [OutputType([pscustomobject])] + [CmdletBinding()] + param ( + # The handle for the GitHub user account. + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName + )] + [string] $Username, + + # The number of results per page (max 100). + [Parameter()] + [int] $PerPage = 30 + ) + + $body = $PSBoundParameters | ConvertFrom-HashTable | ConvertTo-HashTable -NameCasingStyle snake_case + Remove-HashtableEntries -Hashtable $body -RemoveNames 'username' + + $inputObject = @{ + APIEndpoint = "/users/$Username/ssh_signing_keys" + Method = 'GET' + Body = $body + } + + (Invoke-GitHubAPI @inputObject).Response + +} diff --git a/src/GitHub/public/Users/SSH-Signing-Keys/Get-GitHubUserSigningKey.ps1 b/src/GitHub/public/Users/SSH-Signing-Keys/Get-GitHubUserSigningKey.ps1 new file mode 100644 index 000000000..e7c60b53e --- /dev/null +++ b/src/GitHub/public/Users/SSH-Signing-Keys/Get-GitHubUserSigningKey.ps1 @@ -0,0 +1,63 @@ +filter Get-GitHubUserSigningKey { + <# + .SYNOPSIS + List SSH signing keys for a given user or the authenticated user. + + .DESCRIPTION + Lists a given user's or the current user's SSH signing keys. + + .EXAMPLE + Get-GitHubUserSigningKey + + Gets all SSH signing keys for the authenticated user. + + .EXAMPLE + Get-GitHubUserSigningKey -ID '1234567' + + Gets the SSH signing key with the ID '1234567' for the authenticated user. + + .EXAMPLE + Get-GitHubUserSigningKey -Username 'octocat' + + Gets all SSH signing keys for the 'octocat' user. + + .NOTES + https://docs.github.com/rest/users/ssh-signing-keys#list-ssh-signing-keys-for-the-authenticated-user + https://docs.github.com/rest/users/ssh-signing-keys#get-an-ssh-signing-key-for-the-authenticated-user + https://docs.github.com/rest/users/ssh-signing-keys#list-ssh-signing-keys-for-a-user + + #> + [OutputType([pscustomobject])] + [CmdletBinding()] + param ( + # The handle for the GitHub user account. + [Parameter( + Mandatory, + ValueFromPipeline, + ValueFromPipelineByPropertyName, + ParameterSetName = 'Username' + )] + [string] $Username, + + # The unique identifier of the SSH signing key. + [Parameter( + ParameterSetName = 'Me' + )] + [Alias('gpg_key_id')] + [string] $ID, + + # The number of results per page (max 100). + [Parameter()] + [int] $PerPage = 30 + ) + + if ($Username) { + Get-GitHubUserSigningKeyForUser -Username $Username -PerPage $PerPage + } else { + if ($ID) { + Get-GitHubUserMySigningKeyById -ID $ID + } else { + Get-GitHubUserMySigningKey -PerPage $PerPage + } + } +} diff --git a/tools/utilities/GitHubAPI.ps1 b/tools/utilities/GitHubAPI.ps1 index d4f716447..daea6fc91 100644 --- a/tools/utilities/GitHubAPI.ps1 +++ b/tools/utilities/GitHubAPI.ps1 @@ -21,8 +21,8 @@ $response = Invoke-RestMethod -Uri $APIDocURI -Method Get # @{n = 'PUT'; e = { (($_.value.psobject.Properties.Name) -contains 'PUT') } }, ` # @{n = 'PATCH'; e = { (($_.value.psobject.Properties.Name) -contains 'PATCH') } } | Format-Table -$path = '/user/gpg_keys/{gpg_key_id}' -$method = 'DELETE' +$path = '/users/{username}/ssh_signing_keys' +$method = 'get' $response.paths.$path.$method $response.paths.$path.$method.tags | clip # -> Namespace/foldername $response.paths.$path.$method.operationId | clip # -> FunctionName