Skip to content

Commit

Permalink
Merge pull request #1597 from starsolutions/feature/port-forward-brea…
Browse files Browse the repository at this point in the history
…king-option

Add optional support to use the url path for the native authorization code flow. Ports forward [#1143] from 4.4.3
  • Loading branch information
nbulaj authored Nov 20, 2022
2 parents 2533ab9 + 4dbec61 commit bea4d2e
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ User-visible changes worth mentioning.
## main

- [#1593] Add support for Trilogy ActiveRecord adapter.
- [#1597] Add optional support to use the url path for the native authorization code flow. Ports forward [#1143] from 4.4.3
- [#ID] Add your PR description here.

## 5.6.0
Expand Down
14 changes: 14 additions & 0 deletions lib/doorkeeper/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,15 @@ def reuse_access_token
@config.instance_variable_set(:@reuse_access_token, true)
end

# Choose to use the url path for native autorization codes
# Enabling this flag sets the authorization code response route for
# native redirect uris to oauth/authorize/<code>. The default is
# oauth/authorize/native?code=<code>.
# Rationale: https://github.com/doorkeeper-gem/doorkeeper/issues/1143
def use_url_path_for_native_authorization
@config.instance_variable_set(:@use_url_path_for_native_authorization, true)
end

# TODO: maybe make it more generic for other flows too?
# Only allow one valid access token obtained via client credentials
# per client. If a new access token is obtained before the old one
Expand Down Expand Up @@ -623,6 +632,11 @@ def token_grant_types
def deprecated_token_grant_types_resolver
@deprecated_token_grant_types ||= calculate_token_grant_types
end

def native_authorization_code_route
@use_url_path_for_native_authorization = false unless defined?(@use_url_path_for_native_authorization)
@use_url_path_for_native_authorization ? '/:code' : '/native'
end

# [NOTE]: deprecated and will be removed soon
def deprecated_authorization_flows
Expand Down
8 changes: 6 additions & 2 deletions lib/doorkeeper/rails/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def authorization_routes(mapping)
as: mapping[:as],
controller: mapping[:controllers],
) do
routes.get "/native", action: :show, on: :member
routes.get "/", action: :new, on: :member
routes.get native_authorization_code_route, action: :show, on: :member
routes.get '/', action: :new, on: :member
end
end

Expand Down Expand Up @@ -96,6 +96,10 @@ def authorized_applications_routes(mapping)
only: %i[index destroy],
controller: mapping[:controllers]
end

def native_authorization_code_route
Doorkeeper.configuration.native_authorization_code_route
end
end
end
end
32 changes: 32 additions & 0 deletions spec/controllers/authorizations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,38 @@ def query_params
it "does not issue a token" do
expect(Doorkeeper::AccessToken.count).to be 0
end

context 'with use_url_path_for_native_authorization' do
around(:each) do |example|
Doorkeeper.configure do
orm DOORKEEPER_ORM
use_url_path_for_native_authorization
end

Rails.application.reload_routes!

example.run

Doorkeeper.configure do
orm DOORKEEPER_ORM
end

Rails.application.reload_routes!
end

it 'should redirect immediately' do
expect(response).to be_redirect
expect(response.location).to match(/oauth\/authorize\/#{Doorkeeper::AccessGrant.first.token}/)
end

it 'should issue a grant' do
expect(Doorkeeper::AccessGrant.count).to be 1
end

it 'should not issue a token' do
expect(Doorkeeper::AccessToken.count).to be 0
end
end
end

describe "GET #new with skip_authorization true" do
Expand Down
5 changes: 5 additions & 0 deletions spec/dummy/config/initializers/doorkeeper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
#
# enforce_configured_scopes

# Use the url path for the native authorization code flow. Enabling this flag sets the authorization
# code response route for native redirect uris to oauth/authorize/<code>. The default is oauth/authorize/native?code=<code>.
# Rationale: https://github.com/doorkeeper-gem/doorkeeper/issues/1143
# use_url_path_for_native_authorization

# Provide support for an owner to be assigned to each registered application (disabled by default)
# Optional parameter confirmation: true (default false) if you want to enforce ownership of
# a registered application
Expand Down
25 changes: 25 additions & 0 deletions spec/lib/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,31 @@
end
end

describe 'use_url_path_for_native_authorization' do
around(:each) do |example|
Doorkeeper.configure do
orm DOORKEEPER_ORM
use_url_path_for_native_authorization
end

Rails.application.reload_routes!

subject { Doorkeeper.configuration }

example.run

Doorkeeper.configure do
orm DOORKEEPER_ORM
end

Rails.application.reload_routes!
end

it 'sets the native authorization code route /:code' do
expect(subject.native_authorization_code_route).to eq('/:code')
end
end

describe "client_credentials" do
it "has defaults order" do
expect(config.client_credentials_methods)
Expand Down

0 comments on commit bea4d2e

Please sign in to comment.