diff options
author | Joe Rayhawk <jrayhawk@fairlystable.org> | 2022-11-04 17:22:55 -0700 |
---|---|---|
committer | Joe Rayhawk <jrayhawk@fairlystable.org> | 2022-11-04 19:01:33 -0700 |
commit | 9f8002b5b553a69e36ae506af4805b877939575e (patch) | |
tree | 0023b94c7593ef365a8cec77d21bbce0acb59d0b /src | |
parent | 97ade7d346043f72fcbb2acc8daf8ed687be862b (diff) | |
download | crystal-obs-websocket-9f8002b5b553a69e36ae506af4805b877939575e.tar.gz crystal-obs-websocket-9f8002b5b553a69e36ae506af4805b877939575e.zip |
Add authentication support.v5.0.1.20221104.alpha1
Diffstat (limited to 'src')
-rw-r--r-- | src/obswebsocket.cr | 23 |
1 files changed, 20 insertions, 3 deletions
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 |