add initial module files

add first function for creating an access token
This commit is contained in:
2022-03-12 22:07:14 +00:00
parent a87159e41a
commit d6ce1b196e
5 changed files with 195 additions and 1 deletions

View File

@ -0,0 +1,45 @@
<#
.Synopsis
Login and retrieve an Access token.
.Description
Login and retrieve an Access token from _matrix/client/v3/login. See https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3login.
.Parameter ServerUrl
URL for the Matrix server to log into, for example "https://matrix.example.com".
.Parameter Credentials
PSCredentials Object that contains the Matrix Username and Password for the user you wish to log in with.
.Example
# Create a PSCredentials Object
$creds = Get-Credential
$token = Get-LoginToken -ServerUrl "https://matrix.example.com" -Credentials $creds
#>
function Get-MatrixLoginToken {
param(
[Parameter(Mandatory)]
[string]$ServerUrl,
[Parameter(Mandatory)]
[PSCredential]$Credentials
)
$apiPath = "_matrix/client/v3/login"
$apiMethod = "Post"
$reqBody = @{
identifier = @{
type = "m.id.user"
user = $Credentials.UserName
}
type = "m.login.password"
password = $Credentials.Password | ConvertFrom-SecureString -AsPlainText
} | ConvertTo-Json
$res = Invoke-RestMethod -Uri "$ServerUrl/$apiPath" -Method $apiMethod -Body $reqBody
$token = $res.access_token | ConvertTo-SecureString
return $res.access_token
}