From 9f8002b5b553a69e36ae506af4805b877939575e Mon Sep 17 00:00:00 2001 From: Joe Rayhawk Date: Fri, 4 Nov 2022 17:22:55 -0700 Subject: Add authentication support. --- src/obswebsocket.cr | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'src/obswebsocket.cr') diff --git a/src/obswebsocket.cr b/src/obswebsocket.cr index 3ab7836..968957f 100644 --- a/src/obswebsocket.cr +++ b/src/obswebsocket.cr @@ -119,7 +119,7 @@ module OBSWebSocket end self.reqchan.send( { reschan, request.to_s } ) end - def initialize( uri : String ) + def initialize( uri : String, password : String | Nil = nil ) spawn do loop do obs_pubsub = HTTP::WebSocket.new( URI.parse( uri ), HTTP::Headers{"Cookie" => "SESSIONID=1235", "Sec-WebSocket-Protocol" => "obswebsocket.json"} ) @@ -177,8 +177,25 @@ module OBSWebSocket end case json["op"].as_i64 when 0 # hello - json_text = %({"op":1,"d":{"rpcVersion":1,"eventSubscriptions":526335}}) - self.send( json_text ) + + hello = JSON.build do |j| + j.object do + j.field "op", 1 + j.field "d" do + j.object do + j.field "rpcVersion", 1 + j.field "eventSubscriptions", 526335 + if json["d"]["authentication"]? && password + secret = Base64.encode(OpenSSL::Digest.new("sha256").update( password + json["d"]["authentication"]["salt"].as_s ).final).chomp + auth = Base64.encode(OpenSSL::Digest.new("sha256").update( secret + json["d"]["authentication"]["challenge"].as_s ).final).chomp + j.field "authentication", auth + end + end + end + end + end + + self.send( hello.to_s ) when 2 # identified @negotiated = true @connecterror = 0 -- cgit v1.2.3