add -Unencrypted parameter

this enables unencrypted connections to $HomeServer, which is
often the case if the bot runs in an internal network with direct
access to the homeserver c-s listener
master
lub 5 years ago
parent d5b5778c7a
commit 82bce8415d

@ -14,10 +14,26 @@ param (
[ValidateNotNullOrEmpty()] [ValidateNotNullOrEmpty()]
[securestring] [securestring]
$AccessToken = ((Get-Content -Path $env:ACCESSTOKEN_FILE -Raw).Trim() ` $AccessToken = ((Get-Content -Path $env:ACCESSTOKEN_FILE -Raw).Trim() `
| ConvertTo-SecureString -AsPlainText -Force) | ConvertTo-SecureString -AsPlainText -Force),
[switch]
$Unencrypted = $false #is alternatively set with $env:UNENCRYPTED
) )
$ErrorActionPreference = 'Stop' $ErrorActionPreference = 'Stop'
if($env:UNENCRYPTED -eq 'TRUE') {
$Unencrypted = $true
}
#used in Invoke-RestMethod
$authentication_headers = @{
Authentication = 'Bearer'
Token = $AccessToken
ContentType = 'application/json'
AllowUnencryptedAuthentication = $Unencrypted
}
$account_data_type_prefix = 'de.lubiland.pingposh.' $account_data_type_prefix = 'de.lubiland.pingposh.'
$account_data_types = @{ $account_data_types = @{
next_batch = $account_data_type_prefix+'next_batch' next_batch = $account_data_type_prefix+'next_batch'
@ -44,18 +60,13 @@ function Send-MatrixEvent {
$uri = '{0}_matrix/client/r0/rooms/{1}/send/{2}/{3}' -f $HomeServer,$RoomId,$EventType,$txn_id $uri = '{0}_matrix/client/r0/rooms/{1}/send/{2}/{3}' -f $HomeServer,$RoomId,$EventType,$txn_id
$header_splat = @{
Authentication = 'Bearer'
Token = $AccessToken
ContentType = 'application/json'
}
$http_splat = @{ $http_splat = @{
Uri = $uri Uri = $uri
Method = 'Put' Method = 'Put'
Body = $Event Body = $Event
} }
$response = Invoke-RestMethod @header_splat @http_splat $response = Invoke-RestMethod @authentication_headers @http_splat
if($response.event_id) { if($response.event_id) {
Write-Host ('Event {0} sent to room {1}' -f $response.event_id,$RoomId) Write-Host ('Event {0} sent to room {1}' -f $response.event_id,$RoomId)
} }
@ -215,17 +226,12 @@ function Join-MatrixRoom {
$uri = '{0}_matrix/client/r0/rooms/{1}/join' -f $HomeServer,$RoomId $uri = '{0}_matrix/client/r0/rooms/{1}/join' -f $HomeServer,$RoomId
$header_splat = @{
Authentication = 'Bearer'
Token = $AccessToken
ContentType = 'application/json'
}
$http_splat = @{ $http_splat = @{
Uri = $uri Uri = $uri
Method = 'Post' Method = 'Post'
} }
$response = Invoke-RestMethod @header_splat @http_splat $response = Invoke-RestMethod @authentication_headers @http_splat
if($response.room_id) { if($response.room_id) {
Write-Host ('Joined room {0}' -f $RoomId) Write-Host ('Joined room {0}' -f $RoomId)
} else { } else {
@ -241,17 +247,12 @@ function Get-MatrixAccountData {
$uri = '{0}_matrix/client/r0/user/{1}/account_data/{2}' -f $HomeServer,$UserId,$Type $uri = '{0}_matrix/client/r0/user/{1}/account_data/{2}' -f $HomeServer,$UserId,$Type
$header_splat = @{
Authentication = 'Bearer'
Token = $AccessToken
ContentType = 'application/json'
}
$http_splat = @{ $http_splat = @{
Uri = $uri Uri = $uri
Method = 'Get' Method = 'Get'
} }
$response = Invoke-RestMethod @header_splat @http_splat $response = Invoke-RestMethod @authentication_headers @http_splat
return $response return $response
} }
function Set-MatrixAccountData { function Set-MatrixAccountData {
@ -267,18 +268,13 @@ function Set-MatrixAccountData {
$uri = '{0}_matrix/client/r0/user/{1}/account_data/{2}' -f $HomeServer,$UserId,$Type $uri = '{0}_matrix/client/r0/user/{1}/account_data/{2}' -f $HomeServer,$UserId,$Type
$header_splat = @{
Authentication = 'Bearer'
Token = $AccessToken
ContentType = 'application/json'
}
$http_splat = @{ $http_splat = @{
Uri = $uri Uri = $uri
Method = 'Put' Method = 'Put'
Body = $Content | ConvertTo-Json -Compress Body = $Content | ConvertTo-Json -Compress
} }
Invoke-RestMethod @header_splat @http_splat | Out-Null Invoke-RestMethod @authentication_headers @http_splat | Out-Null
} }
function Get-NextBatchToken { function Get-NextBatchToken {
$account_data = Get-MatrixAccountData -Type $account_data_types.next_batch $account_data = Get-MatrixAccountData -Type $account_data_types.next_batch
@ -313,16 +309,11 @@ function Invoke-MatrixSync {
$uri += '&filter={0}' -f $FilterId $uri += '&filter={0}' -f $FilterId
} }
$header_splat = @{
Authentication = 'Bearer'
Token = $AccessToken
ContentType = 'application/json'
}
$http_splat = @{ $http_splat = @{
Uri = $uri Uri = $uri
} }
$response = Invoke-RestMethod @header_splat @http_splat $response = Invoke-RestMethod @authentication_headers @http_splat
#.PSObject.Properties because the rooms under .join are [NoteProperty] #.PSObject.Properties because the rooms under .join are [NoteProperty]
$room_ids = $response.rooms.join.PSObject.Properties.Name $room_ids = $response.rooms.join.PSObject.Properties.Name
@ -352,11 +343,6 @@ function Get-MatrixFilterId {
$uri = '{0}_matrix/client/r0/user/{1}/filter' -f $HomeServer,$UserId $uri = '{0}_matrix/client/r0/user/{1}/filter' -f $HomeServer,$UserId
$header_splat = @{
Authentication = 'Bearer'
Token = $AccessToken
ContentType = 'application/json'
}
$http_splat = @{ $http_splat = @{
Uri = $uri Uri = $uri
Method = 'Post' Method = 'Post'
@ -365,7 +351,7 @@ function Get-MatrixFilterId {
Body = $Filter | ConvertTo-Json -Compress -Depth 3 Body = $Filter | ConvertTo-Json -Compress -Depth 3
} }
$response = Invoke-RestMethod @header_splat @http_splat $response = Invoke-RestMethod @authentication_headers @http_splat
return $response.filter_id return $response.filter_id
} }

Loading…
Cancel
Save