Skip to content

Commit 313af27

Browse files
authored
Merge pull request #1646 from polleverywhere/confidential-skip-auth
Block public clients automatic authorization skip
2 parents cbaf026 + f202079 commit 313af27

File tree

4 files changed

+73
-2
lines changed

4 files changed

+73
-2
lines changed

app/controllers/doorkeeper/authorizations_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def destroy
3131
private
3232

3333
def render_success
34-
if skip_authorization? || matching_token?
34+
if skip_authorization? || (matching_token? && pre_auth.client.application.confidential?)
3535
redirect_or_render(authorize_response)
3636
elsif Doorkeeper.configuration.api_only
3737
render json: pre_auth

spec/controllers/authorizations_controller_spec.rb

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require "spec_helper"
44

5-
RSpec.describe Doorkeeper::AuthorizationsController do
5+
RSpec.describe Doorkeeper::AuthorizationsController, type: :controller do
66
include AuthorizationRequestHelper
77

88
class ActionDispatch::TestResponse
@@ -736,6 +736,50 @@ def query_params
736736
end
737737
end
738738

739+
describe "GET #new with skip_authorization false" do
740+
let(:params) do
741+
{
742+
client_id: client.uid,
743+
response_type: "token",
744+
redirect_uri: client.redirect_uri,
745+
}
746+
end
747+
748+
before do
749+
allow(Doorkeeper.config.access_token_model).to receive(:matching_token_for).and_return(true)
750+
client.update_attribute :confidential, confidential_client
751+
752+
get :new, params: params
753+
end
754+
755+
context "with matching token and confidential application" do
756+
let(:confidential_client) { true }
757+
758+
it "redirects immediately" do
759+
expect(controller).not_to receive(:render)
760+
expect(response).to be_redirect
761+
expect(response.location).to match(/^#{client.redirect_uri}/)
762+
end
763+
764+
it "issues a token" do
765+
expect(Doorkeeper::AccessToken.count).to be 1
766+
end
767+
end
768+
769+
context "with matching token and non-confidential application" do
770+
let(:confidential_client) { false }
771+
772+
it "renders the new view" do
773+
expect(response).to be_successful
774+
expect(controller).to render_with :new
775+
end
776+
777+
it "doesn't issue a token" do
778+
expect(Doorkeeper::AccessToken.count).to be 0
779+
end
780+
end
781+
end
782+
739783
describe "GET #new in API mode" do
740784
before do
741785
allow(Doorkeeper.config).to receive(:api_only).and_return(true)

spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
Doorkeeper::RSpec.print_configuration_info
3131

3232
require "support/orm/#{DOORKEEPER_ORM}"
33+
require "support/render_with_matcher"
3334

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

spec/support/render_with_matcher.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
# Adds the `render_with` matcher.
4+
# Ex:
5+
# expect(controller).to render_with(template: :show, locals: { alpha: "beta" })
6+
#
7+
module RenderWithMatcher
8+
def self.included(base)
9+
# Setup spying for our "render_with" matcher
10+
base.before do
11+
allow(controller).to receive(:render).and_wrap_original do |original, *args, **kwargs, &block|
12+
original.call(*args, **kwargs, &block)
13+
end
14+
end
15+
end
16+
17+
RSpec::Matchers.define :render_with do |expected|
18+
match do |actual|
19+
have_received(:render).with(expected).matches?(actual)
20+
end
21+
end
22+
end
23+
24+
RSpec.configure do |config|
25+
config.include RenderWithMatcher, type: :controller
26+
end

0 commit comments

Comments
 (0)