Skip to content

Commit a90d4c1

Browse files
authored
Merge pull request #1639 from doorkeeper-gem/fix-auth-destroy
Add grant type vlaidation to avoid Internal Server Error for DELETE /oauth/authorize endpoint
2 parents 0dcb083 + a2a39f9 commit a90d4c1

File tree

4 files changed

+39
-30
lines changed

4 files changed

+39
-30
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ User-visible changes worth mentioning.
1010
- [#ID] Add your PR description here.
1111
- [#1602] Allow custom data to be stored inside access grants/tokens.
1212
- [#1634] Code refactoring for custom token attributes.
13+
- [#1639] Add grant type validation to avoid Internal Server Error for DELETE /oauth/authorize endpoint.
1314

1415
# 5.6.4
1516

app/controllers/doorkeeper/authorizations_controller.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,19 @@ def new
1313
end
1414

1515
def create
16-
redirect_or_render authorize_response
16+
redirect_or_render(authorize_response)
1717
end
1818

1919
def destroy
20-
redirect_or_render authorization.deny
20+
redirect_or_render(authorization.deny)
21+
rescue Doorkeeper::Errors::InvalidTokenStrategy => e
22+
error_response = get_error_response_from_exception(e)
23+
24+
if Doorkeeper.configuration.api_only
25+
render json: error_response.body, status: :bad_request
26+
else
27+
render :error, locals: { error_response: error_response }
28+
end
2129
end
2230

2331
private
@@ -37,7 +45,7 @@ def render_error
3745
render json: pre_auth.error_response.body,
3846
status: :bad_request
3947
else
40-
render :error
48+
render :error, locals: { error_response: pre_auth.error_response }
4149
end
4250
end
4351

app/views/doorkeeper/authorizations/error.html.erb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
</div>
44

55
<main role="main">
6-
<pre><%= @pre_auth.error_response.body[:error_description] %></pre>
6+
<pre>
7+
<%= (respond_to?(:error_response) ? error_response : @pre_auth.error_response).body[:error_description] %>
8+
</pre>
79
</main>

spec/controllers/authorizations_controller_spec.rb

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ def query_params
2525
scopes: "default"
2626
end
2727

28+
let(:response_json_body) { JSON.parse(response.body) }
29+
2830
before do
2931
Doorkeeper.configure do
3032
orm DOORKEEPER_ORM
@@ -111,7 +113,6 @@ def query_params
111113
post :create, params: { client_id: client.uid, response_type: "token", redirect_uri: client.redirect_uri }
112114
end
113115

114-
let(:response_json_body) { JSON.parse(response.body) }
115116
let(:redirect_uri) { response_json_body["redirect_uri"] }
116117

117118
it "renders success after authorization" do
@@ -154,8 +155,6 @@ def query_params
154155
}
155156
end
156157

157-
let(:response_json_body) { JSON.parse(response.body) }
158-
159158
it "renders success after authorization" do
160159
expect(response).to be_successful
161160
end
@@ -200,8 +199,6 @@ def query_params
200199
}
201200
end
202201

203-
let(:response_json_body) { JSON.parse(response.body) }
204-
205202
it "renders 400 error" do
206203
expect(response.status).to eq 400
207204
end
@@ -231,8 +228,6 @@ def query_params
231228
}
232229
end
233230

234-
let(:response_json_body) { JSON.parse(response.body) }
235-
236231
it "renders 401 error" do
237232
expect(response.status).to eq 401
238233
end
@@ -262,8 +257,6 @@ def query_params
262257
}
263258
end
264259

265-
let(:response_json_body) { JSON.parse(response.body) }
266-
267260
it "renders 401 error" do
268261
expect(response.status).to eq 401
269262
end
@@ -355,8 +348,6 @@ def query_params
355348
}
356349
end
357350

358-
let(:response_json_body) { JSON.parse(response.body) }
359-
360351
it "renders 400 error" do
361352
expect(response.status).to eq 400
362353
end
@@ -386,8 +377,6 @@ def query_params
386377
}
387378
end
388379

389-
let(:response_json_body) { JSON.parse(response.body) }
390-
391380
it "renders 401 error" do
392381
expect(response.status).to eq 401
393382
end
@@ -418,8 +407,6 @@ def query_params
418407
}
419408
end
420409

421-
let(:response_json_body) { JSON.parse(response.body) }
422-
423410
it "renders 401 error" do
424411
expect(response.status).to eq 401
425412
end
@@ -451,7 +438,6 @@ def query_params
451438
}
452439
end
453440

454-
let(:response_json_body) { JSON.parse(response.body) }
455441
let(:redirect_uri) { response_json_body["redirect_uri"] }
456442

457443
it "renders 400 error" do
@@ -492,8 +478,6 @@ def query_params
492478
}
493479
end
494480

495-
let(:response_json_body) { JSON.parse(response.body) }
496-
497481
it "renders 400 error" do
498482
expect(response.status).to eq 400
499483
end
@@ -838,8 +822,6 @@ def query_params
838822
}
839823
end
840824

841-
let(:response_json_body) { JSON.parse(response.body) }
842-
843825
it "renders success" do
844826
expect(response).to be_successful
845827
end
@@ -928,8 +910,6 @@ def query_params
928910
get :new, params: { an_invalid: "request" }
929911
end
930912

931-
let(:response_json_body) { JSON.parse(response.body) }
932-
933913
it "renders bad request" do
934914
expect(response).to have_http_status(:bad_request)
935915
end
@@ -960,8 +940,6 @@ def query_params
960940
}
961941
end
962942

963-
let(:response_json_body) { JSON.parse(response.body) }
964-
965943
it "renders bad request" do
966944
expect(response).to have_http_status(:bad_request)
967945
end
@@ -992,8 +970,6 @@ def query_params
992970
}
993971
end
994972

995-
let(:response_json_body) { JSON.parse(response.body) }
996-
997973
it "renders 400 error" do
998974
expect(response.status).to eq 400
999975
end
@@ -1106,4 +1082,26 @@ def query_params
11061082
expect(response).to be_successful
11071083
end
11081084
end
1085+
1086+
describe "DELETE #destroy in API mode" do
1087+
context "with invalid params" do
1088+
before do
1089+
allow(Doorkeeper.config).to receive(:api_only).and_return(true)
1090+
delete :destroy, params: {
1091+
client_id: client.uid,
1092+
response_type: "blabla",
1093+
redirect_uri: client.redirect_uri,
1094+
response_mode: "form_post",
1095+
}
1096+
end
1097+
1098+
it "renders bad request" do
1099+
expect(response).to have_http_status(:bad_request)
1100+
end
1101+
1102+
it "includes error in body" do
1103+
expect(response_json_body["error"]).to eq("unsupported_grant_type")
1104+
end
1105+
end
1106+
end
11091107
end

0 commit comments

Comments
 (0)