Skip to content

Commit dcd8bbd

Browse files
author
James Brundage
committed
feat: Get-WebSocket -Authenticate ( Fixes #69 )
1 parent 9c755ca commit dcd8bbd

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

Commands/Get-WebSocket.ps1

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,16 @@ function Get-WebSocket {
246246
[ScriptBlock]
247247
$OnWarning,
248248

249+
# If provided, will authenticate the WebSocket.
250+
# Many websockets require an initial authentication handshake
251+
# after an initial message is received.
252+
# This parameter can be either a ScriptBlock or any other object.
253+
# If it is a ScriptBlock, it will be run with the output of the WebSocket passed as the first argument.
254+
[Parameter(ParameterSetName='WebSocketClient')]
255+
[Alias('Authorize','Identify')]
256+
[PSObject]
257+
$Authenticate,
258+
249259
# If set, will watch the output of the WebSocket job, outputting results continuously instead of outputting a websocket job.
250260
[Parameter(ParameterSetName='WebSocketClient')]
251261
[Alias('Tail')]
@@ -256,7 +266,7 @@ function Get-WebSocket {
256266
[Parameter(ParameterSetName='WebSocketClient')]
257267
[Alias('Raw')]
258268
[switch]
259-
$RawText,
269+
$RawText,
260270

261271
# If set, will output the raw bytes that come out of the WebSocket.
262272
[Parameter(ParameterSetName='WebSocketClient')]
@@ -466,6 +476,25 @@ function Get-WebSocket {
466476
}
467477
}
468478
}
479+
480+
if ($Authenticate) {
481+
# a number of websockets require some handshaking to authenticate
482+
$authenticationMessage =
483+
if ($Authenticate -is [ScriptBlock]) {
484+
& $Authenticate $MessageObject
485+
} else {
486+
$authenticate
487+
}
488+
489+
if ($authenticationMessage) {
490+
if ($authenticationMessage -isnot [string]) {
491+
$ws.SendAsync([ArraySegment[byte]]::new(
492+
$OutputEncoding.GetBytes((ConvertTo-Json -InputObject $authenticationMessage -Depth 10))
493+
), 'Text', $true, $CT)
494+
}
495+
}
496+
}
497+
469498
if ($Skip -and ($SkipCount -le $Skip)) {
470499
$SkipCount++
471500
continue WebSocketMessageLoop
@@ -482,7 +511,7 @@ function Get-WebSocket {
482511
@($MessageObject),
483512
$MessageObject
484513
)
485-
}
514+
}
486515

487516
if ($First -and ($MessageCount - $FilteredCount - $SkipCount) -ge $First) {
488517
$Maximum = $first

0 commit comments

Comments
 (0)