Skip to content

Commit b09bad3

Browse files
authored
Report a fresh access token's whole lifetime in expires_in (#3288)
* 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. * Clear a token's issuance time when it reloads A reloaded token reads its expiry afresh, so it must report what it has left, not the lifetime it was minted with. A virtual attribute is reset by reload; an instance variable outlived it.
1 parent 0d5fbef commit b09bad3

4 files changed

Lines changed: 60 additions & 9 deletions

File tree

‎app/models/identity/access_token.rb‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,12 @@ 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 or reloaded later has none (see
97+
# #expires_in).
98+
attribute :issued_at, :datetime
99+
100+
before_create :set_issued_at, :set_token
96101
before_create :set_expiry_and_refresh_token, if: :refreshable?
97102

98103
# Issuing or destroying a grant restarts its client's retention period
@@ -352,8 +357,15 @@ def expired?
352357
expires_at? && expires_at.past?
353358
end
354359

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

359371
def lapsed?
@@ -377,17 +389,18 @@ def revoke(reason:, actor:, **details)
377389
def refresh(permission: self.permission)
378390
narrowed = permission.to_s != self.permission
379391
raw_token, raw_refresh_token = self.class.generate_token(:access), self.class.generate_token(:refresh)
392+
now = Time.current
380393
rotated = { token: nil, token_digest: TokenDigester.digest(raw_token),
381394
refresh_token_digest: TokenDigester.digest(raw_refresh_token),
382-
expires_at: EXPIRES_IN.from_now, permission: permission, updated_at: Time.current }
395+
expires_at: EXPIRES_IN.since(now), permission: permission, updated_at: now }
383396

384397
rotated_now = transaction do
385398
if self.class.unlapsed.where(id: id, refresh_token_digest: refresh_token_digest).update_all(rotated) == 1
386399
retired_refresh_tokens.create! \
387400
refresh_token_digest: refresh_token_digest,
388401
successor_refresh_token_digest: rotated[:refresh_token_digest],
389402
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)
403+
assign_attributes rotated.merge(raw_token: raw_token, raw_refresh_token: raw_refresh_token, issued_at: now)
391404
true
392405
end
393406
end
@@ -469,6 +482,10 @@ def lock_grant
469482
self.class.lock.where(id: id).pluck(:id)
470483
end
471484

485+
def set_issued_at
486+
self.issued_at = Time.current
487+
end
488+
472489
# Personal access tokens are written in plaintext as well as digested,
473490
# for production code that reads only plaintext. An explicitly given
474491
# token is digested like a generated one.
@@ -489,7 +506,7 @@ def token_kind
489506
end
490507

491508
def set_expiry_and_refresh_token
492-
self.expires_at ||= EXPIRES_IN.from_now
509+
self.expires_at ||= EXPIRES_IN.since(issued_at)
493510
self.raw_refresh_token ||= self.class.generate_token(:refresh)
494511
self.refresh_token_digest = TokenDigester.digest(raw_refresh_token)
495512
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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,41 @@ 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+
assert_equal 3000, token.reload.expires_in
99+
end
100+
66101
test "refresh fails when the presented refresh token was already rotated" do
67102
token = identities(:david).access_tokens.create!(oauth_client: oauth_clients(:mcp_client))
68103
stale = Identity::AccessToken.find(token.id)

0 commit comments

Comments
 (0)