From a7d23b631f26ae220dddbe9a3680503ece48deb9 Mon Sep 17 00:00:00 2001 From: iamibi <8592115+iamibi@users.noreply.github.com> Date: Sun, 27 Sep 2026 22:09:57 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Guard=20expires=5Flatency=20when?= =?UTF-8?q?=20a=20token=20has=20no=20known=20expiry?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit AccessToken#initialize subtracted expires_latency from expires_at whenever expires_latency was set, without checking that expires_at was set. So a token without expires_in/expires_at (RFC 6749 section 5.1 makes expires_in RECOMMENDED, not REQUIRED), or with expires_in: 0, raised NoMethodError (undefined method '-' for nil). This broke client.get_token(params, expires_latency: N) against providers that issue non-expiring tokens and omit expires_in. Only apply the latency when an expiry is known. Co-Authored-By: Claude Opus 5.5 Signed-off-by: iamibi <8592115+iamibi@users.noreply.github.com> --- CHANGELOG.md | 2 ++ lib/oauth2/access_token.rb | 2 +- spec/oauth2/access_token_spec.rb | 50 ++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fff1198a..715428bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,8 @@ Please file a bug if you notice a violation of semantic versioning. ### Fixed +- `OAuth2::AccessToken` no longer raises `NoMethodError` when `expires_latency` is set but the token has no known expiry (no `expires_in`/`expires_at`, or `expires_in: 0`), e.g. `client.get_token(params, expires_latency: 30)` against a provider that omits `expires_in` by @iamibi + ### Security ## [2.0.25] - 2026-07-14 diff --git a/lib/oauth2/access_token.rb b/lib/oauth2/access_token.rb index 47893ab3..df482c5f 100644 --- a/lib/oauth2/access_token.rb +++ b/lib/oauth2/access_token.rb @@ -171,7 +171,7 @@ def initialize(client, token, opts = {}) @expires_at &&= convert_expires_at(@expires_at) @expires_latency &&= @expires_latency.to_i @expires_at ||= Time.now.to_i + @expires_in if @expires_in && !@expires_in.zero? - @expires_at -= @expires_latency if @expires_latency + @expires_at -= @expires_latency if @expires_at && @expires_latency @options = { mode: opts.delete(:mode) || :header, header_format: opts.delete(:header_format) || "Bearer %s", diff --git a/spec/oauth2/access_token_spec.rb b/spec/oauth2/access_token_spec.rb index 8348592f..a3e8d051 100644 --- a/spec/oauth2/access_token_spec.rb +++ b/spec/oauth2/access_token_spec.rb @@ -795,6 +795,56 @@ def assert_initialized_token(target) target = described_class.from_hash(client, hash.merge(expires_at: expires_at)) expect(target.expires_at).to eq(expires_at - expires_latency) end + + context "when neither expires_in nor expires_at is provided" do + let(:hash) do + { + access_token: token, + expires_latency: expires_latency, + } + end + + it "does not raise" do + expect { described_class.from_hash(client, hash) }.not_to raise_error + end + + it "does not expire" do + target = described_class.from_hash(client, hash) + expect(target.expires_at).to be_nil + expect(target).not_to be_expires + expect(target).not_to be_expired + end + + it "retains expires_latency" do + target = described_class.from_hash(client, hash) + expect(target.expires_latency).to eq(expires_latency) + end + end + + context "when expires_in is zero" do + let(:expires_in) { 0 } + + it "does not raise" do + expect { described_class.from_hash(client, hash) }.not_to raise_error + end + + it "does not expire" do + target = described_class.from_hash(client, hash) + expect(target.expires_at).to be_nil + expect(target).not_to be_expires + end + end + + context "when the token endpoint response omits expires_in" do + let(:refresh_body) { JSON.dump(access_token: "refreshed_foo", refresh_token: "refresh_bar") } + + it "builds a non-expiring token via Client#get_token" do + target = client.get_token({grant_type: "refresh_token", refresh_token: "abaca"}, {expires_latency: expires_latency}) + expect(target.token).to eq("refreshed_foo") + expect(target.expires_latency).to eq(expires_latency) + expect(target).not_to be_expires + end + end end end