mirror of
https://github.com/Thumbscrew/PSMatrix.git
synced 2025-07-01 10:35:25 +00:00
add initial module files
add first function for creating an access token
This commit is contained in:
45
public/Get-MatrixLoginToken.ps1
Normal file
45
public/Get-MatrixLoginToken.ps1
Normal 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
|
||||
}
|
Reference in New Issue
Block a user