Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix compatibility with lastest version of Crystal #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .crystal-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.24.2
0.31.1
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down Expand Up @@ -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"

Expand Down
18 changes: 13 additions & 5 deletions shard.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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

4 changes: 1 addition & 3 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 6 additions & 6 deletions spec/bifrost_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions src/bifrost.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down