Project

any_auth

0.0
The project is in a healthy, maintained state
OmniAuth's strategies without the Rack: a vendor's fields and sign-in round trip as plain objects.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

>= 0
>= 1.1.6
>= 0
>= 0
>= 1.18.1

Runtime

>= 0
>= 2.14.3
>= 3.2, < 4
~> 2.0, >= 2.0.22
 Project Readme

AnyAuth

Sign people in with Google, Microsoft, Apple and others, and connect your app to vendors' APIs, from your own Rails controllers. One strategy class per vendor, with the request and callback phases OmniAuth names, and no Rack middleware.

Needs Ruby 3.4 and Rails 8.

  • Sign in with Google and Microsoft
  • Connect to a vendor's API
  • Reference
  • Writing a strategy
  • Coming from OmniAuth
  • Testing
  • Developer docs: adding a strategy, how a sign-in works, each strategy

Sign in with Google and Microsoft

1. Install

# Gemfile
gem "any_auth"

2. Register the providers

Create an OAuth client at each vendor and add your callback address as a redirect URI: https://example.com/auth/google/callback. Each strategy page says where.

# config/initializers/any_auth.rb
AnyAuth.configure do |config|
  config.provider :google,
    client_id: ENV["GOOGLE_CLIENT_ID"], client_secret: ENV["GOOGLE_CLIENT_SECRET"]
  config.provider :microsoft,
    client_id: ENV["MICROSOFT_CLIENT_ID"], client_secret: ENV["MICROSOFT_CLIENT_SECRET"]
end

3. Add the routes

# config/routes.rb
scope "auth/:provider", module: :auth, as: :auth do
  resource :authorization, only: :create
  resource :callback, only: :show
end

Two resources: POST /auth/google/authorization starts the sign-in, and GET /auth/google/callback is where the vendor sends the person back.

4. Write the controllers

The examples build on the Rails 8 authentication generator (bin/rails generate authentication), which gives you start_new_session_for and after_authentication_url.

# app/controllers/auth/base_controller.rb
class Auth::BaseController < ApplicationController
  include AnyAuth::ControllerHelpers

  allow_unauthenticated_access
  before_action :set_strategy
  rescue_from AnyAuth::AuthenticationError, with: :refused

  private
    def set_strategy
      @strategy = AnyAuth.strategy(params[:provider])
    end

    def refused
      redirect_to new_session_path, alert: "Try another way to sign in."
    end
end
# app/controllers/auth/authorizations_controller.rb
class Auth::AuthorizationsController < Auth::BaseController
  def create
    redirect_to_provider @strategy, redirect_uri: auth_callback_url(params[:provider])
  end
end
# app/controllers/auth/callbacks_controller.rb
class Auth::CallbacksController < Auth::BaseController
  def show
    if user = User.from_any_auth(authenticate_with_provider(@strategy))
      start_new_session_for user
      redirect_to after_authentication_url
    else
      refused
    end
  end
end

redirect_to_provider keeps what the callback needs in a short-lived encrypted cookie of its own and sends the person to the vendor. authenticate_with_provider checks the callback, swaps the code for tokens, asks who signed in and returns an auth. A failed or refused sign-in raises an AnyAuth::AuthenticationError, and the person lands back on the sign-in page. A setup mistake the vendor reports, such as a wrong client secret, is an AnyAuth::ConfigurationError: not caught here, so it becomes an error page and reaches your error tracker.

after_authentication_url already returns people to the page they were asked to sign in on: the generator keeps it in the session, and the trip to the vendor leaves it there.

auth_callback_url is built from the request's Host header, which anyone can forge. Vendors that match redirect URIs exactly refuse a forged one, but not all are strict, so tell Rails which hosts are yours; it then refuses any other before your controller runs:

# config/environments/production.rb
config.hosts = %w[ example.com ]

5. Find the person

# app/models/user.rb
class User < ApplicationRecord
  has_many :sessions, dependent: :destroy
  has_many :identities, dependent: :destroy

  def self.from_any_auth(auth)
    if identity = Identity.find_by(provider: auth.provider, uid: auth.uid)
      identity.user
    elsif auth.info.email_verified
      find_by(email_address: auth.info.email)&.tap do |user|
        user.identities.create!(provider: auth.provider, uid: auth.uid)
      end
    end
  end
end

Key a person on provider and uid, in an Identity that belongs to the user. Link to an existing account by email only when auth.info.email_verified is true: AnyAuth sets it only when the vendor can vouch for the mailbox, which is stricter than the vendor's own flag (see Google and Microsoft).

6. Add the buttons

<%= button_to "Sign in with Google", auth_authorization_path(:google), form: { data: { turbo: false } } %>
<%= button_to "Sign in with Microsoft", auth_authorization_path(:microsoft), form: { data: { turbo: false } } %>

The sign-in starts with a POST, and not through Turbo: the browser has to follow the redirect to the vendor itself.

Carrying something through the sign-in

Anything passed as params: comes back on the auth, such as an invitation the person was accepting:

redirect_to_provider @strategy, redirect_uri: auth_callback_url(params[:provider]),
  params: { invitation: params[:invitation] }

auth = authenticate_with_provider(@strategy)
auth.params["invitation"]

It goes through a cookie as JSON, so its keys come back as strings. Keep it small: a cookie holds 4 KB, and a large params: raises ActionDispatch::Cookies::CookieOverflow when the person leaves.

Connect to a vendor's API

The same two helpers connect an account at a vendor, here Shopify, whose sign-in needs the shop's address first.

config.provider :shopify, client_id: ENV["SHOPIFY_CLIENT_ID"],
  client_secret: ENV["SHOPIFY_CLIENT_SECRET"], scope: "read_orders"

Ask for the fields

A strategy is an ActiveModel. Its attributes are what a person types before signing in:

AnyAuth.strategy_class(:shopify).attribute_names   # => ["shop"]
<%= form_with model: @strategy, scope: :strategy, url: shops_path do |form| %>
  <%= form.label :shop %>
  <%= form.text_field :shop %>
  <%= tag.p @strategy.errors.full_messages_for(:shop).to_sentence %>
  <%= form.submit "Connect" %>
<% end %>
# config/routes.rb
resources :shops, only: %i[ index new create ]
namespace :shops do
  resource :callback, only: :show
end
# app/controllers/shops_controller.rb
class ShopsController < ApplicationController
  include AnyAuth::ControllerHelpers

  def new
    @strategy = AnyAuth.strategy(:shopify)
  end

  def create
    @strategy = AnyAuth.strategy(:shopify, params.expect(strategy: [ :shop ]))

    if @strategy.valid?
      redirect_to_provider @strategy, redirect_uri: shops_callback_url
    else
      render :new, status: :unprocessable_content
    end
  end
end
# app/controllers/shops/callbacks_controller.rb
class Shops::CallbacksController < ApplicationController
  include AnyAuth::ControllerHelpers

  def show
    shopify = AnyAuth.strategy(:shopify)
    auth = authenticate_with_provider(shopify)

    Current.user.shops.create!(credentials: shopify.attributes.merge(auth.credentials))
    redirect_to shops_path, notice: "#{shopify.shop} is connected."
  end
end

authenticate_with_provider puts the shop back on the strategy, so shopify.attributes is what was typed. Permit the attribute names and nothing else. Economic and Adversus have fields and no sign-in at all: check valid? and store strategy.attributes.

Store and refresh the credentials

Store auth.credentials (encrypted), and renew it when it is about to run out:

class Shop < ApplicationRecord
  serialize :credentials, coder: JSON
  encrypts :credentials

  def access_token
    renew if expiring?
    credentials["access_token"]
  end

  private
    def expiring?
      expires_at = credentials["expires_at"]
      expires_at.present? && expires_at < 2.minutes.from_now.to_i
    end

    def renew
      update!(credentials: strategy.refresh(credentials))
    end

    def strategy
      AnyAuth.strategy(:shopify, shop: credentials["shop"])
    end
end

refresh always asks the vendor, and keeps a refresh token the vendor did not replace; a strategy with nothing to refresh, such as OAuth 1, answers the credentials as they are. When to refresh is yours to decide. Renew a little before expires_at, since a token used to its last second can run out mid-request. refresh_token_expires_at says when the refresh token itself runs out, for a keep-alive that renews idle credentials in time, but most vendors don't say, so its absence does not mean it never runs out. Both are epoch seconds; credentials stored by 0.1.0 hold expires_at as ISO 8601 text until their next refresh. A vendor that rotates its refresh token, as Shopify does, must not be refreshed twice at once. How a vendor wants the token sent on an API call is on its strategy page.

Configuring each sign-in (OmniAuth's setup phase)

OmniAuth runs a setup proc before each phase to change a strategy's options for that request, such as a customer's own client or IdP. In AnyAuth you build the strategy yourself, so the setup is the arguments you build it with. Options given to AnyAuth.strategy win over the configured ones:

config.provider :shopify, scope: "read_orders"
class Auth::BaseController < ApplicationController
  # …

  private
    def set_strategy
      @strategy = AnyAuth.strategy(params[:provider], **Current.account.sign_in_options)
    end
end

Build it the same way in both actions; a before_action does that. Only the strategy's fields (such as Shopify's shop) travel to the callback with the sign-in. Its options do not, so a client id or certificate from your database must be looked up again, as above.

Reference

Providers

config.provider :google, client_id: -> { ENV["GOOGLE_CLIENT_ID"] }
config.provider :google_drive, from: :google, scope: "https://www.googleapis.com/auth/drive.file"
config.provider :google_workspace, strategy: :google, client_id: "…", client_secret: "…"

AnyAuth.providers   # => ["google", "google_drive", "google_workspace"]
from: another provider's strategy and options, here the same Google client asking for Drive
strategy: another provider's code only, with options of its own
a callable value asked each time a strategy is built
scope: replaces the strategy's default; OpenID Connect always adds openid

Options sent to the vendor are declared per strategy (scope everywhere, prompt and login_hint for Google and Microsoft, and others on each strategy page). An unknown option raises ActiveModel::UnknownAttributeError when the strategy is built.

Controller helpers

redirect_to_provider(strategy, redirect_uri:, params: {}) keeps the sign-in in its own cookie and redirects to the vendor
authenticate_with_provider(strategy) checks the callback, swaps the code and returns the auth
any_auth_params(strategy) the params: you passed, readable before the code is swapped and when the sign-in fails
delete_any_auth_cookie forgets a sign-in under way; call it when someone signs out

The sign-in is kept for ten minutes and used once. One at a time: a second replaces the first. It lives in its own cookie rather than the session, so reset_session leaves it; call delete_any_auth_cookie on sign-out so the next person at the browser cannot finish it.

What comes back

auth.provider the name the provider is configured under
auth.uid who the vendor's token is for, as the vendor identifies them
auth.info name, email, image, nil where the vendor does not say; email_verified, false unless it vouches
auth.credentials access_token, and when sent token_type, refresh_token, expires_at, refresh_token_expires_at (epoch seconds) and scope
auth.raw what the vendor said about who signed in: the ID token's claims, or its answer without the tokens
auth.params what you passed as params:

What uid is, and what each vendor puts in raw, is on its strategy page.

Errors

AnyAuth::Error
├── AnyAuth::AuthenticationError   the sign-in or refresh didn't work: rescue this
│   ├── InvalidCallback            the answer can't be proven ours
│   ├── AccessDenied               the person or the vendor said no, or the grant is dead
│   └── Unavailable                the vendor couldn't answer now
└── AnyAuth::ConfigurationError    the vendor says your setup is wrong: let it raise
Error Means Next
InvalidCallback wrong state, a bad signature or ID token, no code, nothing under way start again
AccessDenied access_denied, invalid_grant, unauthorized_client, invalid_scope, a refused refresh sign in again
Unavailable a timeout, a server error, a rate limit, an answer that cannot be read try again later
ConfigurationError invalid_client, invalid_request, an unsupported grant or response type fix the setup

Every error carries what the vendor said: code (its own word, such as invalid_grant, or nil), description (its explanation, for logs; never show it to people, it can be forged) and retry_after (seconds, when the vendor says when to try again). reason is the vendor's code, or the kind of failure when it gave none: invalid_callback, access_denied, unavailable.

rescue_from AnyAuth::AuthenticationError, with: :refused
rescue_from AnyAuth::Unavailable, with: :try_again_later

# a background refresh
retry_on AnyAuth::Unavailable, wait: ->(executions, error) { error.retry_after || executions**4 }

Mistakes AnyAuth can see before asking anyone are ordinary Ruby errors: an unconfigured provider is a KeyError, a missing client_id an ArgumentError.

A failure page, as with OmniAuth

To send every failed sign-in to one page, as OmniAuth's /auth/failure does, write the rescue once in the controller that signs people in:

rescue_from AnyAuth::AuthenticationError, with: :redirect_to_failure

private
  def redirect_to_failure(error)
    Rails.error.report(error, handled: true)
    redirect_to auth_failure_path(message: error.reason, provider: params[:provider])
  end
# config/routes.rb
get "auth/failure", to: "auth/failures#show", as: :auth_failure

Keep error.description out of the address: it is the vendor's text, for logs. Unlike OmniAuth's failure page, this never receives a ConfigurationError, and it behaves the same in every environment.

Strategies

Strategy Kind
Google OpenID Connect sign-in and Google APIs
Microsoft OpenID Connect work, school and personal accounts
Apple OpenID Connect signed client secret; the callback is a POST
Hubspot OAuth 2 one more request, to name the user
Relatel OAuth 2 one more request, to name the employee
Tavle OAuth 2 one more request, to name the member
Shopify OAuth 2 with a field per-shop endpoints, signed callback
Signicat OpenID Connect national eIDs, with the eID and level checked
Economic, Adversus fields only
Saml SAML 2.0 a customer's IdP; needs ruby-saml, loaded on request
OAuth1 OAuth 1.0a a base for OAuth 1 vendors; needs the oauth gem, loaded on request
Developer a field sign in as anyone; development only, loaded on request

Writing a strategy

A new OAuth 2 vendor is its endpoints and one request to learn who signed in:

class Pipedrive < AnyAuth::Strategies::OAuth2
  def client_options
    { site: "https://api.pipedrive.com", authorize_url: "https://oauth.pipedrive.com/oauth/authorize",
      token_url: "https://oauth.pipedrive.com/oauth/token" }
  end

  private

  def identity(access_token, _session)
    user = get(access_token, "/v1/users/me")["data"]
    { uid: user["id"].to_s, info: AnyAuth::Info.new(name: user["name"], email: user["email"]) }
  end
end

Adding a strategy covers OpenID Connect vendors, fields, PKCE, the steps you can override and how to test it. How a sign-in works says what is kept and checked along the way.

Coming from OmniAuth

OmniAuth AnyAuth
provider :google_oauth2, id, secret config.provider :google, client_id:, client_secret:
/auth/:provider and its callback, by the middleware two routes and actions of your own
request.env["omniauth.auth"] the return value of authenticate_with_provider
omniauth.params, omniauth.origin params:, read back as auth.params
/auth/failure rescue_from AnyAuth::AuthenticationError, or a failure page of your own
a setup proc the arguments you build the strategy with
extra.raw_info auth.raw
  • Keep your provider names: config.provider :google_oauth2, strategy: :google.
  • Compare uids before switching; each strategy page says what it is.
  • auth is an object: auth.uid, not auth["uid"]. credentials has access_token where OmniAuth has token, and no expires flag.

Testing

Stub the vendor's HTTP answers and drive your controllers:

post auth_authorization_path(:google)
sent = Rack::Utils.parse_query(URI(response.location).query)

claims = { sub: "g-1", email: "mette@gmail.com", email_verified: true, iss: "https://accounts.google.com",
           aud: "your-client-id", exp: 1.hour.from_now.to_i, nonce: sent["nonce"] }
stub_request(:post, "https://oauth2.googleapis.com/token").to_return(
  headers: { "Content-Type" => "application/json" },
  body: { access_token: "token", id_token: JWT.encode(claims, nil, "none") }.to_json
)

get auth_callback_path(:google, code: "code", state: sent["state"])

For development, the Developer strategy signs in as any email without a vendor.

Without the helpers

Outside a controller, call request_phase and callback_phase yourself; see How a sign-in works.

Limits

  • A callback that arrives as a cross-site POST (Apple, SAML) needs HTTPS, so the sign-in's cookie can come back with it.
  • Endpoints are written in each strategy; there is no discovery.
  • The ID token is read from the token endpoint over TLS and checked for issuer, audience, expiry and nonce. Its signature is not verified, which OpenID Connect allows for that path only.
  • info is a name, an email, whether it is vouched for, and a picture.