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 | |
parent | 97ade7d346043f72fcbb2acc8daf8ed687be862b (diff) | |
download | crystal-obs-websocket-5.0.1.20221104.alpha1.tar.gz crystal-obs-websocket-5.0.1.20221104.alpha1.zip |
Add authentication support.v5.0.1.20221104.alpha1
-rw-r--r-- | shard.yml | 2 | ||||
-rw-r--r-- | src/obswebsocket.cr | 23 |
2 files changed, 21 insertions, 4 deletions
@@ -1,5 +1,5 @@ name: obswebsocket -version: 5.0.1.20221029.alpha1 +version: 5.0.1.20221104.alpha1 repository: "git://fairlystable.org/crystal-obs-websocket" authors: 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 |