doorkeeper
Search…
Getting Started
This guide is relevant for Ruby on Rails with ActiveRecord. It assumes you have a User model using Devise as the authentication framework.
If you want to see how doorkeeper integrates with an existing application, check out the doorkeeper-provider-app repository, which is based on this guide.

Installation

The first step is to add Doorkeeper to your project's dependencies:
1
bundle add doorkeeper
Copied!
After that, you need to generate relevant files with:
1
bundle exec rails generate doorkeeper:install
Copied!
This will introduce three changes:
    1.
    A new initializer in config/initializers/doorkeeper.rb
    2.
    Add doorkeeper's routes to config/routes.rb
    3.
    Locale files in config/locales/doorkeeper.en.yml

Migrations

To generate appropriate tables, run:
1
$ bundle exec rails generate doorkeeper:migration
2
create db/migrate/20190324080634_create_doorkeeper_tables.rb
Copied!
This migration will create all necessary tables for oAuth2 Applications, Access Grants, and Access Tokens. See the database design for more details.
NOTE: If using UUIDs instead of integer IDs, see Using PostgreSQL UUIDs as primary keys with Doorkeeper for changes you will need to make to your migration.

Integrating with existing User Model

Before executing the migration, you may want to add foreign keys to doorkeeper's tables to ensure data integrity. Go to the migration file and uncomment the lines below:
db/migrate/20190324080634_create_doorkeeper_tables.rb
1
# Uncomment below to ensure a valid reference to the resource owner's table
2
add_foreign_key :oauth_access_grants, :users, column: :resource_owner_id
3
add_foreign_key :oauth_access_tokens, :users, column: :resource_owner_id
Copied!
Now you're ready to run the migrations:
1
bundle exec rake db:migrate
Copied!
As the next step, you may want to add associations to your model. If you skip this step, you'll encounter ActiveRecord::InvalidForeignKeyerror when you try to destroy the User that has associated access grants or access tokens.
app/models/user.rb
1
class User < ApplicationRecord
2
has_many :access_grants,
3
class_name: 'Doorkeeper::AccessGrant',
4
foreign_key: :resource_owner_id,
5
dependent: :delete_all # or :destroy if you need callbacks
6
7
has_many :access_tokens,
8
class_name: 'Doorkeeper::AccessToken',
9
foreign_key: :resource_owner_id,
10
dependent: :delete_all # or :destroy if you need callbacks
11
end
Copied!
Last modified 2yr ago