From e7355a5474ae24e1b534eda03ce7aced44d4b5b3 Mon Sep 17 00:00:00 2001 From: Thumbscrew Date: Sat, 12 Mar 2022 22:53:06 +0000 Subject: [PATCH] add Remove-MatrixAccessToken --- PSMatrix.psd1 | 3 ++- public/Remove-MatrixAccessToken.ps1 | 42 +++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 public/Remove-MatrixAccessToken.ps1 diff --git a/PSMatrix.psd1 b/PSMatrix.psd1 index 3ad5768..9a65448 100644 --- a/PSMatrix.psd1 +++ b/PSMatrix.psd1 @@ -70,7 +70,8 @@ PowerShellVersion = '7.0' # Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. FunctionsToExport = @( - 'New-MatrixAccessToken' + 'New-MatrixAccessToken', + 'Remove-MatrixAccessToken' ) # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. diff --git a/public/Remove-MatrixAccessToken.ps1 b/public/Remove-MatrixAccessToken.ps1 new file mode 100644 index 0000000..a436669 --- /dev/null +++ b/public/Remove-MatrixAccessToken.ps1 @@ -0,0 +1,42 @@ +<# + .Synopsis + Invalidate the provided Access token. + + .Description + Invalidate the provided Access token. See https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3logout. + + .Parameter ServerUrl + URL for the Matrix server to log into, for example "https://matrix.example.com". + + .Parameter AccessToken + The Access token to invalidate. + + .Example + Remove-MatrixAccessToken -ServerUrl "https://matrix.example.com" -AccessToken $token +#> + +function Remove-MatrixAccessToken { + param( + [Parameter(Mandatory)] + [string]$ServerUrl, + + [Parameter(Mandatory)] + [SecureString]$AccessToken + ) + + $apiPath = "_matrix/client/v3/logout" + $apiMethod = "Post" + + $headers = @{ + Authorization="Bearer " + ($AccessToken | ConvertFrom-SecureString -AsPlainText) + } + + Try { + Invoke-RestMethod -Uri "$ServerUrl/$apiPath" -Method $apiMethod -Headers $headers + return $true + } + Catch { + Write-Error $_ + return $false + } +} \ No newline at end of file