Skip to content

Return the standard token fields as credentials, and leave expiry to the app - #1

Merged
henrikbjorn merged 2 commits into
mainfrom
credentials-0.2.0
Nov 18, 2026
Merged

henrikbjorn merged 2 commits into
mainfrom
credentials-0.2.0

Conversation

@henrikbjorn

@henrikbjorn henrikbjorn commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

credentials now carries the token response's standard fields, with expiries as epoch seconds, and an app decides for itself when to refresh.

What changes

  • credentials has up to six keys: access_token, token_type, refresh_token, expires_at, refresh_token_expires_at and scope. Keys a vendor doesn't send are left out.
  • refresh_token_expires_at comes from refresh_token_timeout, the IETF draft's name, or refresh_token_expires_in, which GitHub, Google and Shopify send. Most vendors send neither, so a missing value doesn't mean the refresh token never runs out.
  • HubSpot's scopes list becomes scope, the standard space-separated string.
  • Expiries are epoch seconds, as oauth2 and OmniAuth keep them, instead of ISO 8601 text. An expires_at that 0.1.0 stored as text still refreshes, because oauth2's from_hash parses it.
  • refresh(credentials) always asks the vendor. expired? and refresh! are removed. The app renews a couple of minutes before expires_at, or before refresh_token_expires_at to keep idle credentials alive.
  • No expires flag, unlike OmniAuth: it repeats expires_at, and oauth2's from_hash crashes on expires: true.
  • The README, docs/how-a-sign-in-works.md, the Shopify and HubSpot strategy pages and the CHANGELOG are updated, and the version is 0.2.0.

Why

  • Other fields stay out on purpose. Some token responses carry secrets and personal data under names that log filters miss: Slack's nested authed_user tokens and webhook url, a Shopify staff member's email, Salesforce's signature. Auth.js, Spring, Go's x/oauth2, Google's libraries and MSAL all persist a fixed set for the same reason.
  • token_type and scope are kept because RFC 6749 says a client must not use a token type it doesn't understand, and a grant can come back with fewer scopes than were asked for.
  • Integers are what oauth2 hands over, so credentials_from stores its value as it is.
  • The app decides when to refresh because it knows its own schedule, for example a keep-alive sweep. The gem only does the part an app can't: asking the vendor and merging the answer.

Upgrading

  • refresh(credentials) keeps its name but now always asks the vendor. In 0.1.0 it asked only when the token was about to expire, so an app that calls it before every use would refresh on every call. Check expires_at first.
  • Replace strategy.expired?(credentials) with your own check, for example credentials['expires_at'] < 2.minutes.from_now.to_i, then call strategy.refresh(credentials).
  • Rows saved by 0.1.0 hold expires_at as ISO 8601 text until they've refreshed once. Convert them before comparing: Time.iso8601(expires_at).to_i if expires_at.is_a?(String).
  • Read stored expiries with Time.at(...) instead of .to_time.
  • Studio has these changes ready locally. Tavle's Connectors::Base calls expired? and needs the same.

Tests

138 runs, 0 failures (bundle exec rake test on Ruby 4.0).

…the app

credentials now carries the token response's standard fields: token_type
and scope as well as the tokens and their expiry, and the refresh token's
own expiry when the vendor says. That is read from refresh_token_timeout,
the IETF draft's name, or refresh_token_expires_in, which GitHub, Google
and Shopify send; most vendors send neither. HubSpot's scopes list becomes
scope. Other fields stay out, as the libraries that persist credentials
keep a fixed set: vendors put secrets and personal data under names log
filters miss.

Expiries are epoch seconds, as oauth2 and OmniAuth keep them, so the
credentials hash stores oauth2's value as it is. An expires_at that 0.1.0
stored as ISO 8601 text still refreshes.

refresh always asks the vendor, and expired? and refresh! are gone: an app
renews when it chooses, a couple of minutes before expires_at, or before
the refresh token runs out to keep idle credentials alive.
@henrikbjorn
henrikbjorn merged commit d1efe4c into main Nov 18, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant