Customizing routes

Tute Costa edited this page Oct 1, 2013 · 4 revisions

Version < 0.5

Doorkeeper versions prior to 0.5 don't provide customization. You're only able to mount all routes into your application routes:

Rails.application.routes.draw do
  mount Doorkeeper::Engine => "/oauth"
  # your routes
end

This will mount the following routes:

GET       /oauth/authorize
POST      /oauth/authorize
DELETE    /oauth/authorize
POST      /oauth/token
resources /oauth/applications

Version >= 0.5

Doorkeeper routes accept a few customization options for generating routes in your application:

The basic generated routes are:

# routes.rb
Rails.application.routes.draw do
  use_doorkeeper
end

generates:

GET       /oauth/authorize
POST      /oauth/authorize
DELETE    /oauth/authorize
POST      /oauth/token
resources /oauth/applications

Changing controllers

You may want to change the controllers to your custom controllers with:


Rails.application.routes.draw do
  use_doorkeeper do
    # it accepts :authorizations, :tokens, :applications and :authorized_applications
    controllers :applications => 'custom_applications'
  end
end

You'll need a CustomApplicationsController class in your app/controllers. If you want to extend the default behaviour, just inherit from Doorkeeper::ApplicationsController. For example:

class CustomApplicationsController < Doorkeeper::ApplicationsController
end

Changing aliases

If can change the alias options with as:

Rails.application.routes.draw do
  use_doorkeeper do
    # it accepts :authorizations, :tokens, :applications and :authorized_applications
    as :authorizations => 'custom_auth', :tokens => 'custom_token'
  end
end

Skipping controllers

If you want to skip some routes and provide your own routes, use skip_controllers option:

Rails.application.routes.draw do
  use_doorkeeper do
    # it accepts :authorizations, :tokens, :applications and :authorized_applications
    skip_controllers :applications, :authorized_applications
  end
end

Namespacing

You can namespace doorkeeper routes just like you do in the usual rails routes:

Rails.application.routes.draw do
  scope 'space' do
    use_doorkeeper
  end
end

This will generate:

GET       /space/oauth/authorize
POST      /space/oauth/authorize
DELETE    /space/oauth/authorize
POST      /space/oauth/token
resources /space/oauth/applications

This also applies to constraints too.

Customizing scope

You can customize doorkeeper default oauth scope:

Rails.application.routes.draw do
  use_doorkeeper :scope => 'oauth2'
end

This will generate:

GET       /oauth2/authorize
POST      /oauth2/authorize
DELETE    /oauth2/authorize
POST      /oauth2/token
resources /oauth2/applications