Skip to content

Commit 00cfaa7

Browse files
committed
Report a fresh access token's whole lifetime in expires_in
The token response counted expires_in down from the clock at response time and truncated it, so a 1-hour token minted a few milliseconds earlier was reported as 3599 seconds. Clients that require an hour-long token refuse a provider that says so. A token minted or rotated by the instance answering now reports its lifetime from issuance, with the expiry taken from the same clock reading, so a 1-hour token reports 3600 and a delegated one its whole capped 300. A token loaded later, such as a refresh retried within the grace window, still reports what it has left, rounded up. No TTL changes.
1 parent 0d5fbef commit 00cfaa7

4 files changed

Lines changed: 58 additions & 9 deletions

File tree

‎app/models/identity/access_token.rb‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ class Identity::AccessToken < ApplicationRecord
9292
attribute :raw_token, :string
9393
attribute :raw_refresh_token, :string
9494

95-
before_create :set_token
95+
# When this instance minted the access token it holds, by issuance or
96+
# rotation. Never stored: a token loaded later has none (see #expires_in).
97+
attr_accessor :issued_at
98+
99+
before_create :set_issued_at, :set_token
96100
before_create :set_expiry_and_refresh_token, if: :refreshable?
97101

98102
# Issuing or destroying a grant restarts its client's retention period
@@ -352,8 +356,15 @@ def expired?
352356
expires_at? && expires_at.past?
353357
end
354358

359+
# RFC 6749 §5.1: the token's lifetime in seconds. A token this instance
360+
# just minted reports its whole lifetime, measured from issuance rather than
361+
# from the clock now: the request runs on after the mint, and counting those
362+
# milliseconds against a one-hour token reports 3599, under the hour some
363+
# clients require. A token loaded later, such as a refresh retried within
364+
# the grace window, reports what it has left. Rounded up so a fraction of a
365+
# second never costs a whole one.
355366
def expires_in
356-
(expires_at - Time.current).to_i if expires_at?
367+
(expires_at - (issued_at || Time.current)).ceil if expires_at?
357368
end
358369

359370
def lapsed?
@@ -377,17 +388,18 @@ def revoke(reason:, actor:, **details)
377388
def refresh(permission: self.permission)
378389
narrowed = permission.to_s != self.permission
379390
raw_token, raw_refresh_token = self.class.generate_token(:access), self.class.generate_token(:refresh)
391+
now = Time.current
380392
rotated = { token: nil, token_digest: TokenDigester.digest(raw_token),
381393
refresh_token_digest: TokenDigester.digest(raw_refresh_token),
382-
expires_at: EXPIRES_IN.from_now, permission: permission, updated_at: Time.current }
394+
expires_at: EXPIRES_IN.since(now), permission: permission, updated_at: now }
383395

384396
rotated_now = transaction do
385397
if self.class.unlapsed.where(id: id, refresh_token_digest: refresh_token_digest).update_all(rotated) == 1
386398
retired_refresh_tokens.create! \
387399
refresh_token_digest: refresh_token_digest,
388400
successor_refresh_token_digest: rotated[:refresh_token_digest],
389401
successor: { access_token: raw_token, refresh_token: raw_refresh_token }
390-
assign_attributes rotated.merge(raw_token: raw_token, raw_refresh_token: raw_refresh_token)
402+
assign_attributes rotated.merge(raw_token: raw_token, raw_refresh_token: raw_refresh_token, issued_at: now)
391403
true
392404
end
393405
end
@@ -469,6 +481,10 @@ def lock_grant
469481
self.class.lock.where(id: id).pluck(:id)
470482
end
471483

484+
def set_issued_at
485+
self.issued_at = Time.current
486+
end
487+
472488
# Personal access tokens are written in plaintext as well as digested,
473489
# for production code that reads only plaintext. An explicitly given
474490
# token is digested like a generated one.
@@ -489,7 +505,7 @@ def token_kind
489505
end
490506

491507
def set_expiry_and_refresh_token
492-
self.expires_at ||= EXPIRES_IN.from_now
508+
self.expires_at ||= EXPIRES_IN.since(issued_at)
493509
self.raw_refresh_token ||= self.class.generate_token(:refresh)
494510
self.refresh_token_digest = TokenDigester.digest(raw_refresh_token)
495511
end

‎test/integration/oauth_flow_test.rb‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ class OauthFlowTest < ActionDispatch::IntegrationTest
557557

558558
assert body["access_token"].start_with?("fizzy_at_")
559559
assert body["refresh_token"].start_with?("fizzy_rt_")
560-
assert_operator body["expires_in"], :>, 0
560+
assert_equal 3600, body["expires_in"], "a fresh 1-hour token reports the whole hour"
561561
assert_equal "Bearer", body["token_type"]
562562
assert_equal "read", body["scope"]
563563

@@ -1245,7 +1245,7 @@ class OauthFlowTest < ActionDispatch::IntegrationTest
12451245

12461246
assert_not_nil body["access_token"]
12471247
assert_not_nil body["refresh_token"]
1248-
assert_operator body["expires_in"], :>, 0
1248+
assert_equal 3600, body["expires_in"], "a fresh 1-hour token reports the whole hour"
12491249
assert_not_equal old_access_token, body["access_token"]
12501250
assert_not_equal old_refresh_token, body["refresh_token"]
12511251
end

‎test/integration/oauth_token_exchange_test.rb‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ class OauthTokenExchangeTest < ActionDispatch::IntegrationTest
3434
assert_equal "Bearer", body["token_type"]
3535
assert_equal ACCESS_TOKEN_TYPE, body["issued_token_type"]
3636
assert_equal "read write", body["scope"]
37-
assert_operator body["expires_in"], :>, 0
38-
assert_operator body["expires_in"], :<=, 300
37+
assert_equal Oauth::TokenDelegation::TTL_CAP.to_i, body["expires_in"], "a fresh delegated token reports its whole capped lifetime"
3938
assert_not body.key?("refresh_token")
4039

4140
delegated = Identity::AccessToken.find_by_raw_token(body["access_token"])

‎test/models/identity/access_token_test.rb‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,40 @@ class Identity::AccessTokenTest < ActiveSupport::TestCase
6363
end
6464
end
6565

66+
test "a fresh token reports its whole lifetime with the clock frozen" do
67+
freeze_time
68+
token = identities(:david).access_tokens.create!(oauth_client: oauth_clients(:mcp_client))
69+
70+
assert_equal 3600, token.expires_in
71+
end
72+
73+
test "a fresh token reports its whole lifetime when the request runs on after the mint" do
74+
token = identities(:david).access_tokens.create!(oauth_client: oauth_clients(:mcp_client))
75+
76+
travel_to token.issued_at + 0.4.seconds, with_usec: true
77+
assert_equal 3600, token.expires_in, "a 1-hour token told it has 3599s fails clients that require an hour"
78+
79+
travel_to token.issued_at + 2.seconds, with_usec: true
80+
assert_equal 3600, token.expires_in
81+
end
82+
83+
test "a refreshed token reports its whole lifetime when the request runs on after the rotation" do
84+
token = identities(:david).access_tokens.create!(oauth_client: oauth_clients(:mcp_client))
85+
86+
travel 10.minutes
87+
assert token.refresh
88+
89+
travel_to token.issued_at + 0.4.seconds, with_usec: true
90+
assert_equal 3600, token.expires_in
91+
end
92+
93+
test "a token loaded later reports what it has left" do
94+
token = identities(:david).access_tokens.create!(oauth_client: oauth_clients(:mcp_client))
95+
96+
travel_to token.issued_at + 10.minutes + 0.4.seconds, with_usec: true
97+
assert_equal 3000, Identity::AccessToken.find(token.id).expires_in
98+
end
99+
66100
test "refresh fails when the presented refresh token was already rotated" do
67101
token = identities(:david).access_tokens.create!(oauth_client: oauth_clients(:mcp_client))
68102
stale = Identity::AccessToken.find(token.id)

0 commit comments

Comments
 (0)