Skip to content

Commit b8d4d87

Browse files
authored
Require exp when verifying access tokens (#97)
1 parent 941e192 commit b8d4d87

4 files changed

Lines changed: 23 additions & 3 deletions

File tree

‎Gemfile.lock‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ PATH
44
livekit-server-sdk (1.0.0)
55
faraday (>= 2.0, < 3.0)
66
google-protobuf (~> 4.30, >= 4.30.2)
7-
jwt (>= 2.2.3, < 3.0)
7+
jwt (>= 2.3.0, < 3.0)
88
twirp (~> 1.13, >= 1.13.1)
99

1010
GEM

‎lib/livekit/token_verifier.rb‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@ def initialize(api_key: nil, api_secret: nil)
1010
end
1111

1212
def verify(token)
13-
decoded_token = JWT.decode(token, @api_secret, true, algorithm: AccessToken::SIGNING_ALGORITHM)
13+
decoded_token = JWT.decode(
14+
token,
15+
@api_secret,
16+
true,
17+
{
18+
algorithm: AccessToken::SIGNING_ALGORITHM,
19+
required_claims: ["exp"],
20+
},
21+
)
1422
decoded = decoded_token.first
1523
if decoded["iss"] != @api_key
1624
raise "Invalid issuer"

‎livekit_server_sdk.gemspec‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Gem::Specification.new do |spec|
2929

3030
spec.add_dependency "faraday", ">= 2.0", "< 3.0"
3131
spec.add_dependency "google-protobuf", "~> 4.30", ">= 4.30.2"
32-
spec.add_dependency "jwt", ">= 2.2.3", "< 3.0"
32+
spec.add_dependency "jwt", ">= 2.3.0", "< 3.0"
3333
spec.add_dependency "twirp", "~> 1.13", ">= 1.13.1"
3434

3535
# For more information and examples about making a new gem, checkout our

‎spec/livekit/token_verifier_spec.rb‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@
1717
expect(grant.attributes["mykey"]).to eq("myvalue")
1818
end
1919

20+
it "fails when exp is missing" do
21+
payload = {
22+
"iss" => TEST_KEY,
23+
"sub" => "user",
24+
"nbf" => Time.now.to_i - 5,
25+
"video" => { "roomJoin" => true, "room" => "testroom" },
26+
}
27+
jwt = JWT.encode(payload, TEST_SECRET, "HS256")
28+
v = described_class.new(api_key: TEST_KEY, api_secret: TEST_SECRET)
29+
expect { v.verify(jwt) }.to raise_error(JWT::MissingRequiredClaim)
30+
end
31+
2032
it "fails on expired tokens" do
2133
token = LiveKit::AccessToken.new(api_key: TEST_KEY, api_secret: TEST_SECRET,
2234
identity: "test_identity", ttl: -10)

0 commit comments

Comments
 (0)