2022-03-12 22:07:14 +00:00
|
|
|
<#
|
|
|
|
.Synopsis
|
2022-03-12 22:15:09 +00:00
|
|
|
Login and retrieve an Access token (returned as a SecureString).
|
2022-03-12 22:07:14 +00:00
|
|
|
|
|
|
|
.Description
|
2022-03-12 22:15:09 +00:00
|
|
|
Login and retrieve an Access token from _matrix/client/v3/login (returned as a SecureString). See https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3login.
|
2022-03-12 22:07:14 +00:00
|
|
|
|
|
|
|
.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.
|
|
|
|
|
2022-03-12 22:31:54 +00:00
|
|
|
.Parameter DeviceDisplayName
|
|
|
|
Display name to assign to this Access token.
|
|
|
|
|
2022-03-12 22:07:14 +00:00
|
|
|
.Example
|
|
|
|
# Create a PSCredentials Object
|
|
|
|
$creds = Get-Credential
|
|
|
|
$token = Get-LoginToken -ServerUrl "https://matrix.example.com" -Credentials $creds
|
|
|
|
#>
|
2022-03-12 22:27:27 +00:00
|
|
|
function New-MatrixAccessToken {
|
2022-03-12 22:07:14 +00:00
|
|
|
param(
|
|
|
|
[Parameter(Mandatory)]
|
|
|
|
[string]$ServerUrl,
|
|
|
|
|
|
|
|
[Parameter(Mandatory)]
|
2022-03-12 22:31:54 +00:00
|
|
|
[PSCredential]$Credentials,
|
|
|
|
|
|
|
|
[Parameter(Mandatory=$false)]
|
|
|
|
[string]$DeviceDisplayName="PSMatrix"
|
2022-03-12 22:07:14 +00:00
|
|
|
)
|
|
|
|
|
2022-03-27 14:29:44 +00:00
|
|
|
$url = New-MatrixUrl -ServerUrl $ServerUrl -ApiPath "_matrix/client/v3/login"
|
2022-03-12 22:07:14 +00:00
|
|
|
$apiMethod = "Post"
|
|
|
|
|
|
|
|
$reqBody = @{
|
|
|
|
identifier = @{
|
|
|
|
type = "m.id.user"
|
|
|
|
user = $Credentials.UserName
|
|
|
|
}
|
|
|
|
type = "m.login.password"
|
|
|
|
password = $Credentials.Password | ConvertFrom-SecureString -AsPlainText
|
2022-03-12 22:31:54 +00:00
|
|
|
initial_device_display_name = $DeviceDisplayName
|
2022-03-12 22:07:14 +00:00
|
|
|
} | ConvertTo-Json
|
|
|
|
|
2022-03-27 14:29:44 +00:00
|
|
|
$res = Invoke-RestMethod -Uri $url -Method $apiMethod -Body $reqBody
|
2022-03-12 22:07:14 +00:00
|
|
|
|
2022-03-12 22:15:09 +00:00
|
|
|
$token = $res.access_token | ConvertTo-SecureString -AsPlainText
|
2022-03-12 22:07:14 +00:00
|
|
|
|
2022-03-12 22:16:30 +00:00
|
|
|
return $token
|
2022-03-12 22:07:14 +00:00
|
|
|
}
|