Skip to content

Commit

Permalink
Merge pull request #1646 from polleverywhere/confidential-skip-auth
Browse files Browse the repository at this point in the history
Block public clients automatic authorization skip
  • Loading branch information
nbulaj committed Mar 20, 2023
2 parents cbaf026 + f202079 commit 313af27
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/controllers/doorkeeper/authorizations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def destroy
private

def render_success
if skip_authorization? || matching_token?
if skip_authorization? || (matching_token? && pre_auth.client.application.confidential?)
redirect_or_render(authorize_response)
elsif Doorkeeper.configuration.api_only
render json: pre_auth
Expand Down
46 changes: 45 additions & 1 deletion spec/controllers/authorizations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "spec_helper"

RSpec.describe Doorkeeper::AuthorizationsController do
RSpec.describe Doorkeeper::AuthorizationsController, type: :controller do
include AuthorizationRequestHelper

class ActionDispatch::TestResponse
Expand Down Expand Up @@ -736,6 +736,50 @@ def query_params
end
end

describe "GET #new with skip_authorization false" do
let(:params) do
{
client_id: client.uid,
response_type: "token",
redirect_uri: client.redirect_uri,
}
end

before do
allow(Doorkeeper.config.access_token_model).to receive(:matching_token_for).and_return(true)
client.update_attribute :confidential, confidential_client

get :new, params: params
end

context "with matching token and confidential application" do
let(:confidential_client) { true }

it "redirects immediately" do
expect(controller).not_to receive(:render)
expect(response).to be_redirect
expect(response.location).to match(/^#{client.redirect_uri}/)
end

it "issues a token" do
expect(Doorkeeper::AccessToken.count).to be 1
end
end

context "with matching token and non-confidential application" do
let(:confidential_client) { false }

it "renders the new view" do
expect(response).to be_successful
expect(controller).to render_with :new
end

it "doesn't issue a token" do
expect(Doorkeeper::AccessToken.count).to be 0
end
end
end

describe "GET #new in API mode" do
before do
allow(Doorkeeper.config).to receive(:api_only).and_return(true)
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
Doorkeeper::RSpec.print_configuration_info

require "support/orm/#{DOORKEEPER_ORM}"
require "support/render_with_matcher"

Dir["#{File.dirname(__FILE__)}/support/{dependencies,helpers,shared}/*.rb"].sort.each { |file| require file }

Expand Down
26 changes: 26 additions & 0 deletions spec/support/render_with_matcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

# Adds the `render_with` matcher.
# Ex:
# expect(controller).to render_with(template: :show, locals: { alpha: "beta" })
#
module RenderWithMatcher
def self.included(base)
# Setup spying for our "render_with" matcher
base.before do
allow(controller).to receive(:render).and_wrap_original do |original, *args, **kwargs, &block|
original.call(*args, **kwargs, &block)
end
end
end

RSpec::Matchers.define :render_with do |expected|
match do |actual|
have_received(:render).with(expected).matches?(actual)
end
end
end

RSpec.configure do |config|
config.include RenderWithMatcher, type: :controller
end

0 comments on commit 313af27

Please sign in to comment.