Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
D

doorkeeper

> 后端框架
Open source

Doorkeeper is an OAuth 2 provider for Ruby on Rails / Grape.

5.5K stars0 likes0 views
WebsiteGitHub

About

Doorkeeper is an OAuth 2 provider for Ruby on Rails / Grape.

Doorkeeper — awesome OAuth 2 provider for your Rails / Grape app.

Doorkeeper is a gem (Rails engine) that makes it easy to introduce OAuth 2 provider functionality to your Ruby on Rails or Grape application.

Supported features:

  • The OAuth 2.0 Authorization Framework
    • Authorization Code Flow
    • Access Token Scopes
    • Refresh token
    • Implicit grant
    • Resource Owner Password Credentials
    • Client Credentials
  • OAuth 2.0 Token Revocation
  • OAuth 2.0 Token Introspection
  • OAuth 2.0 Threat Model and Security Considerations
  • OAuth 2.0 for Native Apps
  • Proof Key for Code Exchange by OAuth Public Clients
  • OAuth 2.0 Authorization Server Issuer Identification — opt-in by setting issuer; adds the iss parameter to authorization redirects returned to the client
  • Resource Indicators for OAuth 2.0

Table of Contents

  • Documentation
  • Installation
    • Ruby on Rails
    • Grape
  • ORMs
  • Extensions
  • Database maintenance
  • Resource Indicators
  • Refresh Token Scopes
  • Custom Grant Flows
  • Custom Client Authentication Methods
  • Example Applications
  • Sponsors
  • Development
  • Contributing
  • Contributors
  • License

Documentation

This documentation is valid for main branch. Please check the documentation for the version of doorkeeper you are using in: https://github.com/doorkeeper-gem/doorkeeper/releases.

Additionally, other resources can be found on:

  • Guides with how-to get started and configuration documentation
  • See the Wiki for articles on how to integrate with other solutions
  • Screencast from railscasts.com: #353 OAuth with Doorkeeper
  • See upgrade guides
  • For general questions, please post on Stack Overflow
  • See SECURITY.md for this project's security disclose policy

Installation

Installation depends on the framework you're using. The first step is to add the following to your Gemfile:

ruby
gem 'doorkeeper'

And run bundle install. After this, check out the guide related to the framework you're using.

Ruby on Rails

Doorkeeper currently supports Ruby on Rails >= 5.0. See the guide here.

Grape

Guide for integration with Grape framework can be found here.

ORMs

Doorkeeper supports Active Record by default, but can be configured to work with the following ORMs:

ORM Support via
Active Record by default
MongoDB doorkeeper-gem/doorkeeper-mongodb
Sequel nbulaj/doorkeeper-sequel
Couchbase acaprojects/doorkeeper-couchbase
RethinkDB aca-labs/doorkeeper-rethinkdb

Extensions

Extensions that are not included by default and can be installed separately.

Link
OpenID Connect extension doorkeeper-gem/doorkeeper-openid_connect
JWT Token support doorkeeper-gem/doorkeeper-jwt
Assertion grant extension doorkeeper-gem/doorkeeper-grants_assertion
I18n translations doorkeeper-gem/doorkeeper-i18n
CIBA - Client Initiated Backchannel Authentication Flow extension doorkeeper-ciba
Device Authorization Grant doorkeeper-device_authorization_grant

Database maintenance

Doorkeeper does not automatically remove expired or revoked tokens and grants. The oauth_access_tokens and oauth_access_grants tables grow indefinitely and can reach millions of rows if left unmanaged.

Prune them periodically with the bundled rake task:

bash
bundle exec rake doorkeeper:db:cleanup

This deletes expired and revoked access tokens and grants. See the Rake tasks guide for details.

Resource Indicators

Doorkeeper supports Resource Indicators for OAuth 2.0 (RFC 8707), allowing clients to signal which protected resource(s) they intend to access. Tokens are then audience-restricted to those resources.

Setup

  1. Run the generator to add the required resource column:
bash
rails generate doorkeeper:resource_indicators
rails db:migrate
  1. Configure a validator in your initializer:
ruby
# config/initializers/doorkeeper.rb
Doorkeeper.configure do
  resource_indicator_validator ->(resource_indicators, client) {
    allowed = %w[https://api.example.com/ https://calendar.example.com/]
    resource_indicators.all? { |r| allowed.include?(r) }
  }
end

The callable receives an array of resource URIs and the OAuth client. Return true to accept or false to reject with invalid_target.

Behavior

  • Resource URIs must be absolute and must not contain a fragment component.
  • Resource indicators are stored on grants and tokens.
  • Token and refresh requests enforce subset restrictions against the original grant.
  • Token introspection responses include aud when resource indicators are present.
  • Grants issued with resource indicators retain their audience restriction even if the validator is later removed from configuration.

Multiple resources

RFC 8707 uses repeated query parameters (?resource=…&resource=…) for multiple values, but Rack collapses repeated keys to the last value. Clients must use the Rails bracket syntax for multiple resource indicators:

?resource[]=https://api.example.com/&resource[]=https://calendar.example.com/

A single resource=… works as-is.

Refresh Token Scopes

A client may narrow the access token it gets back from the refresh token grant by sending a scope parameter. Per RFC 6749 §6 the refresh token itself keeps the scope originally granted by the resource owner: a later refresh that omits scope returns to the granted scope, and one that asks for the granted scope again is accepted.

Doorkeeper tracks the granted scope in the refresh_token_scopes column of oauth_access_tokens. New installs get it from the install migration; existing installs add it with:

bash
rails generate doorkeeper:refresh_token_scopes
rails db:migrate

With the column in place, token introspection of a refresh token reports the granted scope rather than the scope of the access token it was issued with, and reuse_access_token does not hand a grant of a narrower scope an existing token whose refresh token was granted a wider one.

Without the column, a narrowed refresh narrows the refresh token as well, so the chain can never return to the granted scope (the behavior of Doorkeeper before the column existed), and a refresh_token_scopes assigned to an access token is ignored. Rows created before the migration keep that behavior until they are rotated, and so do the ORM extensions (Sequel, MongoDB) until they add the field.

Custom Grant Flows

Besides the built-in OAuth 2 flows, Doorkeeper can recognize and process any custom grant type through its grant flow registry — including grant types whose names are URNs or URIs, such as the SAML 2.0 bearer assertion grant defined by RFC 7522.

A grant flow bundles a matcher for the grant_type parameter with a strategy class that processes the token request. Register it before Doorkeeper.configure and enable it by adding its registered name to grant_flows:

ruby
# config/initializers/doorkeeper.rb
Doorkeeper::GrantFlow.register(
  :saml2_bearer,
  grant_type_matches: "urn:ietf:params:oauth:grant-type:saml2-bearer",
  grant_type_strategy: SamlBearer::Strategy,
)

Doorkeeper.configure do
  grant_flows %w[authorization_code saml2_bearer]
  # ...
end

Note that grant_flows lists the registered flow name (saml2_bearer), while grant_type_matches — a String or a Regexp — is what the request's grant_type parameter is matched against.

The strategy class receives the authorization server as server and builds the request object handling the grant:

ruby
module SamlBearer
  class Strategy < Doorkeeper::Request::Strategy
    delegate :client, :parameters, to: :server

    def request
      @request ||= TokenRequest.new(Doorkeeper.config, client, parameters)
    end
  end
end

The request object validates the grant and issues the token. Subclassing Doorkeeper::OAuth::BaseRequest provides the response handling, scope calculation and token creation, so only the grant-specific parts remain (per RFC 7522 §2.1 the assertion parameter carries a single SAML assertion, base64url-encoded without padding):

…

The client_supports_grant_flow validation keeps the custom grant subject to the allow_grant_flow_for_client configuration option (per-client grant restrictions), just like the built-in flows.

Flows can also handle custom response_type values on the authorization endpoint via the response_type_matches / response_type_strategy options — see the built-in registrations in lib/doorkeeper/grant_flow.rb for reference. An extension can also group several flows under one configuration name with Doorkeeper::GrantFlow.register_alias (e.g. the OpenID Connect extension registers implicit_oidc to expand to multiple response types).

Custom Client Authentication Methods

Doorkeeper authenticates clients (RFC 6749 §2.3) through a registry of named methods. client_secret_basic, client_secret_post and none are built in, and an application or extension can register additional ones — for instance to keep accepting credentials that a partner integration sends in its own headers.

A method is any object that responds to matches_request? and authenticate. Register it before Doorkeeper.configure and enable it by listing its registered name in client_authentication:

ruby
# config/initializers/doorkeeper.rb
Doorkeeper::ClientAuthentication.register(
  :partner_headers,
  PartnerHeaders::Authentication,
)

Doorkeeper.configure do
  client_authentication %i[client_secret_basic client_secret_post partner_headers none]
  # ...
end

The or

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubyauthenticationauthorizationdoorkeepergrape

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架