diff --git a/.crystal-version b/.crystal-version index 8b95abd..f176c94 100644 --- a/.crystal-version +++ b/.crystal-version @@ -1 +1 @@ -0.24.2 +0.31.1 diff --git a/README.md b/README.md index 7ea4f92..7264243 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ Create a JWT that can be sent to the client side for your end user to connect to get "/api/bifrost-token" do authenticate_user! payload = { channels: ["user:#{current_user.id}", "global"] } - jwt = JWT.encode(payload, ENV["JWT_SECRET"], "HS512") + jwt = JWT.encode(payload, ENV["JWT_SECRET"], JWT::Algorithm::HS512) { token: jwt }.to_json end ``` @@ -114,7 +114,7 @@ data = { }, exp: Time.zone.now.to_i + 1.hour } -jwt = JWT.encode(data, ENV["JWT_SECRET"], "HS512") +jwt = JWT.encode(data, ENV["JWT_SECRET"], JWT::Algorithm::HS512) url = ENV.fetch("BIFROST_URL") url += "/broadcast" diff --git a/shard.lock b/shard.lock index f96d397..6908bf2 100644 --- a/shard.lock +++ b/shard.lock @@ -2,25 +2,33 @@ version: 1.0 shards: dotenv: github: gdotdesign/cr-dotenv - version: 0.1.0 + version: 0.3.1 + + exception_page: + github: crystal-loot/exception_page + version: 0.1.2 jwt: github: crystal-community/jwt - version: 0.2.2 + version: 1.1.1 kemal: github: kemalcr/kemal - version: 0.22.0 + version: 0.26.0 kilt: github: jeromegn/kilt version: 0.4.0 + openssl_ext: + github: stakach/openssl_ext + version: 1.0.0 + radix: github: luislavena/radix - version: 0.3.8 + version: 0.3.9 spec-kemal: github: kemalcr/spec-kemal - commit: 93ab608228ea6619d15555d1c77a3bcff29f1dba + version: 0.5.0 diff --git a/shard.yml b/shard.yml index 86042a2..b073ef5 100644 --- a/shard.yml +++ b/shard.yml @@ -11,15 +11,13 @@ targets: dependencies: spec-kemal: github: kemalcr/spec-kemal - branch: master kemal: github: kemalcr/kemal - version: ~> 0.22.0 jwt: github: crystal-community/jwt dotenv: github: gdotdesign/cr-dotenv -crystal: 0.24.2 +crystal: 0.31.1 license: MIT diff --git a/spec/bifrost_spec.cr b/spec/bifrost_spec.cr index 4aad7d7..64533b3 100644 --- a/spec/bifrost_spec.cr +++ b/spec/bifrost_spec.cr @@ -23,9 +23,9 @@ describe Bifrost do context "invalid JWT" do it "returns bad request" do payload = { - exp: Time.now.epoch + 3600, # 1 hour + exp: Time.local.to_unix + 3600, # 1 hour } - jwt = JWT.encode(payload, "bad-secret-key", "HS512") + jwt = JWT.encode(payload, "bad-secret-key", JWT::Algorithm::HS512) post "/broadcast", headers: HTTP::Headers{"Content-Type" => "application/json"}, body: {token: jwt}.to_json response.status_code.should eq 400 @@ -37,9 +37,9 @@ describe Bifrost do context "expired JWT" do it "returns bad request" do payload = { - exp: Time.now.epoch - 10, + exp: Time.local.to_unix - 10, } - jwt = JWT.encode(payload, ENV["JWT_SECRET"], "HS512") + jwt = JWT.encode(payload, ENV["JWT_SECRET"], JWT::Algorithm::HS512) post "/broadcast", headers: HTTP::Headers{"Content-Type" => "application/json"}, body: {token: jwt}.to_json response.status_code.should eq 400 @@ -51,11 +51,11 @@ describe Bifrost do context "valid JWT" do it "returns success" do payload = { - exp: Time.now.epoch + 3600, + exp: Time.local.to_unix + 3600, channel: "user:12", message: {test: "test"}.to_json, } - jwt = JWT.encode(payload, ENV["JWT_SECRET"], "HS512") + jwt = JWT.encode(payload, ENV["JWT_SECRET"], JWT::Algorithm::HS512) post "/broadcast", headers: HTTP::Headers{"Content-Type" => "application/json"}, body: {token: jwt}.to_json response.status_code.should eq 200 diff --git a/src/bifrost.cr b/src/bifrost.cr index ca34b90..b249957 100644 --- a/src/bifrost.cr +++ b/src/bifrost.cr @@ -22,7 +22,7 @@ module Bifrost if Kemal.config.env == "development" env.response.content_type = "text/html" allowed_channels = {channels: ["user:1"]} - test_token = JWT.encode(allowed_channels, ENV["JWT_SECRET"], "HS512") + test_token = JWT.encode(allowed_channels, ENV["JWT_SECRET"], JWT::Algorithm::HS512) render "src/views/test.ecr" else render_404 @@ -58,12 +58,12 @@ module Bifrost token = env.params.json["token"].as(String) payload = self.decode_jwt(token) - channel = payload["channel"].as(String) + channel = payload["channel"].as_s deliveries = 0 if SOCKETS.has_key?(channel) SOCKETS[channel].each do |socket| - socket.send(payload["message"].as(Hash).to_json) + socket.send(payload["message"].as_h.to_json) deliveries += 1 end end @@ -92,8 +92,8 @@ module Bifrost begin payload = self.decode_jwt(token) - payload["channels"].as(Array).each do |channel| - channel = channel.as(String) + payload["channels"].as_a.each do |channel| + channel = channel.as_s if SOCKETS.has_key?(channel) SOCKETS[channel] << socket else @@ -149,7 +149,7 @@ module Bifrost end def self.decode_jwt(token : String) - payload, header = JWT.decode(token, ENV["JWT_SECRET"], "HS512") + payload, header = JWT.decode(token, ENV["JWT_SECRET"], JWT::Algorithm::HS512) payload end end